From patchwork Fri Dec 12 07:43:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gu Zheng X-Patchwork-Id: 420381 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 1E62A14009B for ; Fri, 12 Dec 2014 19:04:35 +1100 (AEDT) Received: from localhost ([::1]:55920 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XzLDF-0002jp-0f for incoming@patchwork.ozlabs.org; Fri, 12 Dec 2014 03:04:33 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42711) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XzL9N-0004Xr-4r for qemu-devel@nongnu.org; Fri, 12 Dec 2014 03:00:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XzL9I-0002qs-7i for qemu-devel@nongnu.org; Fri, 12 Dec 2014 03:00:33 -0500 Received: from [59.151.112.132] (port=36915 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XzL9G-0002lB-UW for qemu-devel@nongnu.org; Fri, 12 Dec 2014 03:00:28 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="44939445" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 12 Dec 2014 15:57:06 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id sBC802HV031877; Fri, 12 Dec 2014 16:00:02 +0800 Received: from localhost.localdomain (10.167.226.100) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Fri, 12 Dec 2014 16:00:25 +0800 From: Gu Zheng To: Date: Fri, 12 Dec 2014 15:43:58 +0800 Message-ID: <1418370238-24386-7-git-send-email-guz.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.7.7 In-Reply-To: <1418370238-24386-1-git-send-email-guz.fnst@cn.fujitsu.com> References: <1418370238-24386-1-git-send-email-guz.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.100] X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 59.151.112.132 Cc: zhugh.fnst@cn.fujitsu.com, qemu-devel@nongnu.org, tangchen@cn.fujitsu.com, isimatu.yasuaki@jp.fujitsu.com, chen.fan.fnst@cn.fujitsu.com, anshul.makkar@profitbricks.com, afaerber@suse.de Subject: [Qemu-devel] [PATCH V2 6/6] cpu: add device_add foo-x86_64-cpu support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Chen Fan Add support to device_add foo-x86_64-cpu, and additional checks of apic id are added into x86_cpuid_set_apic_id() to avoid duplicate. Besides, in order to support "device/device_add foo-x86_64-cpu" which without specified apic id, we assign cpuid_apic_id with a default broadcast value (0xFFFFFFFF) in initfn, and a new function get_free_apic_id() to provide a free apid id to cpuid_apic_id if it still has the default at realize time (e.g. hot add foo-cpu without a specified apic id) to avoid apic id duplicates. Thanks very much for Igor's suggestion. Signed-off-by: Chen Fan Signed-off-by: Gu Zheng Signed-off-by: Zhu Guihua --- v2: use a macro ACPI_ID_NOT_SET to replace the magic number(0xFFFFFFFF). hw/acpi/cpu_hotplug.c | 7 +++++-- hw/i386/pc.c | 6 ------ target-i386/cpu.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- target-i386/topology.h | 18 ++++++++++++++++++ 4 files changed, 67 insertions(+), 11 deletions(-) diff --git a/hw/acpi/cpu_hotplug.c b/hw/acpi/cpu_hotplug.c index b8ebfad..4047294 100644 --- a/hw/acpi/cpu_hotplug.c +++ b/hw/acpi/cpu_hotplug.c @@ -59,8 +59,11 @@ void acpi_cpu_plug_cb(ACPIREGS *ar, qemu_irq irq, return; } - ar->gpe.sts[0] |= ACPI_CPU_HOTPLUG_STATUS; - acpi_update_sci(ar, irq); + /* Only trigger sci if cpu is hotplugged */ + if (dev->hotplugged) { + ar->gpe.sts[0] |= ACPI_CPU_HOTPLUG_STATUS; + acpi_update_sci(ar, irq); + } } void acpi_cpu_hotplug_init(MemoryRegion *parent, Object *owner, diff --git a/hw/i386/pc.c b/hw/i386/pc.c index f31d55e..38e24ed 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1677,13 +1677,7 @@ static void pc_cpu_plug(HotplugHandler *hotplug_dev, Error *local_err = NULL; PCMachineState *pcms = PC_MACHINE(hotplug_dev); - if (!dev->hotplugged) { - goto out; - } - if (!pcms->acpi_dev) { - error_setg(&local_err, - "cpu hotplug is not enabled: missing acpi device"); goto out; } diff --git a/target-i386/cpu.c b/target-i386/cpu.c index 4b4a595..96c6f35 100644 --- a/target-i386/cpu.c +++ b/target-i386/cpu.c @@ -1647,6 +1647,7 @@ static void x86_cpuid_set_apic_id(Object *obj, Visitor *v, void *opaque, const int64_t max = UINT32_MAX; Error *error = NULL; int64_t value; + X86CPUTopoInfo topo; if (dev->realized) { error_setg(errp, "Attempt to set property '%s' on '%s' after " @@ -1666,6 +1667,19 @@ static void x86_cpuid_set_apic_id(Object *obj, Visitor *v, void *opaque, return; } + if (value > x86_cpu_apic_id_from_index(max_cpus - 1)) { + error_setg(errp, "CPU with APIC ID %" PRIi64 + " is more than MAX APIC ID limits", value); + return; + } + + x86_topo_ids_from_apic_id(smp_cores, smp_threads, value, &topo); + if (topo.smt_id >= smp_threads || topo.core_id >= smp_cores) { + error_setg(errp, "CPU with APIC ID %" PRIi64 " does not match " + "topology configuration.", value); + return; + } + if ((value != cpu->env.cpuid_apic_id) && cpu_exists(value)) { error_setg(errp, "CPU with APIC ID %" PRIi64 " exists", value); return; @@ -2120,8 +2134,10 @@ static void x86_cpu_cpudef_class_init(ObjectClass *oc, void *data) { X86CPUDefinition *cpudef = data; X86CPUClass *xcc = X86_CPU_CLASS(oc); + DeviceClass *dc = DEVICE_CLASS(oc); xcc->cpu_def = cpudef; + dc->cannot_instantiate_with_device_add_yet = false; } static void x86_register_cpudef_type(X86CPUDefinition *def) @@ -2130,6 +2146,7 @@ static void x86_register_cpudef_type(X86CPUDefinition *def) TypeInfo ti = { .name = typename, .parent = TYPE_X86_CPU, + .instance_size = sizeof(X86CPU), .class_init = x86_cpu_cpudef_class_init, .class_data = def, }; @@ -2663,12 +2680,29 @@ static void mce_init(X86CPU *cpu) } #ifndef CONFIG_USER_ONLY +static uint32_t get_free_apic_id(void) +{ + int i; + + for (i = 0; i < max_cpus; i++) { + uint32_t id = x86_cpu_apic_id_from_index(i); + + if (!cpu_exists(id)) { + return id; + } + } + + return x86_cpu_apic_id_from_index(max_cpus); +} + +#define APIC_ID_NOT_SET (~0U) + static void x86_cpu_apic_create(X86CPU *cpu, Error **errp) { - CPUX86State *env = &cpu->env; DeviceState *dev = DEVICE(cpu); APICCommonState *apic; const char *apic_type = "apic"; + uint32_t apic_id; if (kvm_irqchip_in_kernel()) { apic_type = "kvm-apic"; @@ -2684,7 +2718,14 @@ static void x86_cpu_apic_create(X86CPU *cpu, Error **errp) object_property_add_child(OBJECT(cpu), "apic", OBJECT(cpu->apic_state), NULL); - qdev_prop_set_uint8(cpu->apic_state, "id", env->cpuid_apic_id); + + apic_id = object_property_get_int(OBJECT(cpu), "apic-id", NULL); + if (apic_id == APIC_ID_NOT_SET) { + apic_id = get_free_apic_id(); + object_property_set_int(OBJECT(cpu), apic_id, "apic-id", errp); + } + + qdev_prop_set_uint8(cpu->apic_state, "id", apic_id); /* TODO: convert to link<> */ apic = APIC_COMMON(cpu->apic_state); apic->cpu = cpu; @@ -2873,7 +2914,7 @@ static void x86_cpu_initfn(Object *obj) NULL, NULL, (void *)cpu->filtered_features, NULL); cpu->hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY; - env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index); + env->cpuid_apic_id = APIC_ID_NOT_SET; x86_cpu_load_def(cpu, xcc->cpu_def, &error_abort); diff --git a/target-i386/topology.h b/target-i386/topology.h index e9ff89c..dcb4988 100644 --- a/target-i386/topology.h +++ b/target-i386/topology.h @@ -132,4 +132,22 @@ static inline apic_id_t x86_apicid_from_cpu_idx(unsigned nr_cores, return apicid_from_topo_ids(nr_cores, nr_threads, &topo); } +/* Calculate CPU topology based on CPU APIC ID. + */ +static inline void x86_topo_ids_from_apic_id(unsigned nr_cores, + unsigned nr_threads, + apic_id_t apic_id, + X86CPUTopoInfo *topo) +{ + unsigned offset_mask; + topo->pkg_id = apic_id >> apicid_pkg_offset(nr_cores, nr_threads); + + offset_mask = (1L << apicid_pkg_offset(nr_cores, nr_threads)) - 1; + topo->core_id = (apic_id & offset_mask) + >> apicid_core_offset(nr_cores, nr_threads); + + offset_mask = (1L << apicid_core_offset(nr_cores, nr_threads)) - 1; + topo->smt_id = apic_id & offset_mask; +} + #endif /* TARGET_I386_TOPOLOGY_H */