From nobody Wed Apr 24 22:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581579479541643.7414071842368; Wed, 12 Feb 2020 23:37:59 -0800 (PST) Received: from localhost ([::1]:48362 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j294P-0003T9-Dv for importer@patchew.org; Thu, 13 Feb 2020 02:37:57 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:56515) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j293L-0001nT-Nh for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j293K-000583-Jt for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:51 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:2764 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j293F-00054P-8V; Thu, 13 Feb 2020 02:36:45 -0500 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 54832667FA2C7C9AF8E6; Thu, 13 Feb 2020 15:36:39 +0800 (CST) Received: from localhost (10.133.205.53) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.439.0; Thu, 13 Feb 2020 15:36:32 +0800 From: Ying Fang To: , Subject: [PATCH v2 1/4] acpi: Add aml_generic_register Date: Thu, 13 Feb 2020 15:36:27 +0800 Message-ID: <20200213073630.2125-2-fangying1@huawei.com> X-Mailer: git-send-email 2.22.0.windows.1 In-Reply-To: <20200213073630.2125-1-fangying1@huawei.com> References: <20200213073630.2125-1-fangying1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.133.205.53] X-CFilter-Loop: Reflected Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 45.249.212.191 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, zhang.zhanghailiang@huawei.com, mst@redhat.com, i.mitsyanko@gmail.com, shannon.zhaosl@gmail.com, guoheyi@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The generic register descriptor describes the localtion of a fixed width register within any of the ACPI-defined address space. This is needed to declare the ACPI CPPC registers. Signed-off-by: Heyi Guo Signed-off-by: Ying Fang --- hw/acpi/aml-build.c | 22 ++++++++++++++++++++++ include/hw/acpi/aml-build.h | 3 +++ 2 files changed, 25 insertions(+) diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 2c3702b882..79b1431f07 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -1370,6 +1370,28 @@ Aml *aml_sleep(uint64_t msec) return var; } =20 +/* ACPI 5.0b: 6.4.3.7 Generic Register Descriptor */ +Aml *aml_generic_register(AmlRegionSpace rs, uint8_t reg_width, + uint8_t reg_offset, AmlAccessType type, uint64_t= addr) +{ + int i; + Aml *var =3D aml_alloc(); + build_append_byte(var->buf, 0x82); /* Generic Register Descriptor */ + build_append_byte(var->buf, 0x0C); /* Length, bits[7:0] value =3D 0x0C= */ + build_append_byte(var->buf, 0); /* Length, bits[15:8] value =3D 0 */ + build_append_byte(var->buf, rs); /* Address Space ID */ + build_append_byte(var->buf, reg_width); /* Register Bit Width */ + build_append_byte(var->buf, reg_offset); /* Register Bit Offset */ + build_append_byte(var->buf, type); /* Access Size */ + + /* Register address */ + for (i =3D 0; i < 8; i++) { + build_append_byte(var->buf, extract64(addr, i * 8, 8)); + } + + return var; +} + static uint8_t Hex2Byte(const char *src) { int hi, lo; diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index de4a406568..37a047b156 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -364,6 +364,9 @@ Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fi= xed, Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz, uint8_t channel); Aml *aml_sleep(uint64_t msec); +Aml *aml_generic_register(AmlRegionSpace rs, uint8_t reg_width, + uint8_t reg_offset, AmlAccessType type, + uint64_t addr); Aml *aml_i2c_serial_bus_device(uint16_t address, const char *resource_sour= ce); =20 /* Block AML object primitives */ --=20 2.19.1 From nobody Wed Apr 24 22:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581579668533574.6821772980218; Wed, 12 Feb 2020 23:41:08 -0800 (PST) Received: from localhost ([::1]:48408 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j297T-0007Kc-Cb for importer@patchew.org; Thu, 13 Feb 2020 02:41:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:56553) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j293N-0001qT-Di for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j293M-0005BG-3y for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:53 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:51406 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j293J-00055Z-1D; Thu, 13 Feb 2020 02:36:49 -0500 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 2A32D118DFD8799ED6B0; Thu, 13 Feb 2020 15:36:43 +0800 (CST) Received: from localhost (10.133.205.53) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.439.0; Thu, 13 Feb 2020 15:36:33 +0800 From: Ying Fang To: , Subject: [PATCH v2 2/4] acpi/cppc: Add ACPI CPPC registers Date: Thu, 13 Feb 2020 15:36:28 +0800 Message-ID: <20200213073630.2125-3-fangying1@huawei.com> X-Mailer: git-send-email 2.22.0.windows.1 In-Reply-To: <20200213073630.2125-1-fangying1@huawei.com> References: <20200213073630.2125-1-fangying1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.133.205.53] X-CFilter-Loop: Reflected Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 45.249.212.32 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, zhang.zhanghailiang@huawei.com, mst@redhat.com, i.mitsyanko@gmail.com, shannon.zhaosl@gmail.com, guoheyi@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The Continuous Performance Control Package is used to describe the ACPI CPPC registers. Signed-off-by: Heyi Guo Signed-off-by: Ying Fang --- hw/arm/virt-acpi-build.c | 74 ++++++++++++++++++++++++++++++++++++- hw/arm/virt.c | 1 + include/hw/acpi/acpi-defs.h | 32 ++++++++++++++++ include/hw/arm/virt.h | 1 + 4 files changed, 106 insertions(+), 2 deletions(-) diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c index bd5f771e9b..1f1a0ed324 100644 --- a/hw/arm/virt-acpi-build.c +++ b/hw/arm/virt-acpi-build.c @@ -41,6 +41,7 @@ #include "hw/acpi/pci.h" #include "hw/acpi/memory_hotplug.h" #include "hw/acpi/generic_event_device.h" +#include "hw/acpi/acpi-defs.h" #include "hw/pci/pcie_host.h" #include "hw/pci/pci.h" #include "hw/arm/virt.h" @@ -51,7 +52,70 @@ =20 #define ARM_SPI_BASE 32 =20 -static void acpi_dsdt_add_cpus(Aml *scope, int smp_cpus) + +static void acpi_dsdt_add_psd(Aml *dev, int cpus) +{ + Aml *pkg; + Aml *sub; + + sub =3D aml_package(5); + aml_append(sub, aml_int(5)); + aml_append(sub, aml_int(0)); + /* Assume all vCPUs belong to the same domain */ + aml_append(sub, aml_int(0)); + /* SW_ANY: OSPM coordinate, initiate on any processor */ + aml_append(sub, aml_int(0xFD)); + aml_append(sub, aml_int(cpus)); + + pkg =3D aml_package(1); + aml_append(pkg, sub); + + aml_append(dev, aml_name_decl("_PSD", pkg)); +} + +static void acpi_dsdt_add_cppc(Aml *dev, uint64_t cpu_base) +{ + Aml *cpc; + int i; + + /* ACPI 6.3 8.4.7.1, version 3 of the CPPC table is used */ + cpc =3D aml_package(23); + aml_append(cpc, aml_int(23)); + aml_append(cpc, aml_int(3)); + + for (i =3D 0; i < CPPC_REG_COUNT; i++) { + Aml *res; + uint8_t reg_width; + uint8_t acc_type; + uint64_t addr; + /* Only some necessary registers are emulated */ + if ((i >=3D MIN_PERF && i < REFERENCE_CTR) || + (i >=3D ENABLE && i < LOWEST_FREQ)) { + reg_width =3D 0; + acc_type =3D AML_ANY_ACC; + addr =3D 0; + } else { + addr =3D cpu_base + i * 4; + if (i =3D=3D REFERENCE_CTR || i =3D=3D DELIVERED_CTR) { + reg_width =3D 64; + acc_type =3D AML_QWORD_ACC; + } else { + reg_width =3D 32; + acc_type =3D AML_DWORD_ACC; + } + } + + res =3D aml_resource_template(); + aml_append(res, aml_generic_register(AML_SYSTEM_MEMORY, reg_width,= 0, + acc_type, addr)); + aml_append(cpc, res); + } + + aml_append(dev, aml_name_decl("_CPC", cpc)); +} + +static void acpi_dsdt_add_cpus(Aml *scope, int smp_cpus, + const MemMapEntry *cppc_memmap) { uint16_t i; =20 @@ -59,6 +123,12 @@ static void acpi_dsdt_add_cpus(Aml *scope, int smp_cpus) Aml *dev =3D aml_device("C%.03X", i); aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007"))); aml_append(dev, aml_name_decl("_UID", aml_int(i))); + /* + * Append _CPC and _PSD to show CPU frequency + */ + acpi_dsdt_add_cppc(dev, + cppc_memmap->base + i * CPPC_REG_PER_CPU_STRIDE= ); + acpi_dsdt_add_psd(dev, smp_cpus); aml_append(scope, dev); } } @@ -736,7 +806,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, Virt= MachineState *vms) * the RTC ACPI device at all when using UEFI. */ scope =3D aml_scope("\\_SB"); - acpi_dsdt_add_cpus(scope, vms->smp_cpus); + acpi_dsdt_add_cpus(scope, vms->smp_cpus, &memmap[VIRT_CPUFREQ]); acpi_dsdt_add_uart(scope, &memmap[VIRT_UART], (irqmap[VIRT_UART] + ARM_SPI_BASE)); acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]); diff --git a/hw/arm/virt.c b/hw/arm/virt.c index f788fe27d6..ed9dc38b60 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -144,6 +144,7 @@ static const MemMapEntry base_memmap[] =3D { [VIRT_PCDIMM_ACPI] =3D { 0x09070000, MEMORY_HOTPLUG_IO_LEN }, [VIRT_ACPI_GED] =3D { 0x09080000, ACPI_GED_EVT_SEL_LEN }, [VIRT_MMIO] =3D { 0x0a000000, 0x00000200 }, + [VIRT_CPUFREQ] =3D { 0x0b000000, 0x00010000 }, /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that siz= e */ [VIRT_PLATFORM_BUS] =3D { 0x0c000000, 0x02000000 }, [VIRT_SECURE_MEM] =3D { 0x0e000000, 0x01000000 }, diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h index 57a3f58b0c..3a33f7220d 100644 --- a/include/hw/acpi/acpi-defs.h +++ b/include/hw/acpi/acpi-defs.h @@ -634,4 +634,36 @@ struct AcpiIortRC { } QEMU_PACKED; typedef struct AcpiIortRC AcpiIortRC; =20 +/* + * CPPC register definition from kernel header + * include/acpi/cppc_acpi.h + * The last element is newly added for easy use + */ +enum cppc_regs { + HIGHEST_PERF, + NOMINAL_PERF, + LOW_NON_LINEAR_PERF, + LOWEST_PERF, + GUARANTEED_PERF, + DESIRED_PERF, + MIN_PERF, + MAX_PERF, + PERF_REDUC_TOLERANCE, + TIME_WINDOW, + CTR_WRAP_TIME, + REFERENCE_CTR, + DELIVERED_CTR, + PERF_LIMITED, + ENABLE, + AUTO_SEL_ENABLE, + AUTO_ACT_WINDOW, + ENERGY_PERF, + REFERENCE_PERF, + LOWEST_FREQ, + NOMINAL_FREQ, + CPPC_REG_COUNT, +}; + +#define CPPC_REG_PER_CPU_STRIDE 0x40 + #endif diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h index 71508bf40c..f371713728 100644 --- a/include/hw/arm/virt.h +++ b/include/hw/arm/virt.h @@ -67,6 +67,7 @@ enum { VIRT_SMMU, VIRT_UART, VIRT_MMIO, + VIRT_CPUFREQ, VIRT_RTC, VIRT_FW_CFG, VIRT_PCIE, --=20 2.19.1 From nobody Wed Apr 24 22:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581579602482530.6965629766455; Wed, 12 Feb 2020 23:40:02 -0800 (PST) Received: from localhost ([::1]:48386 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j296P-00064Z-8j for importer@patchew.org; Thu, 13 Feb 2020 02:40:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:56544) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j293M-0001pQ-RO for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j293L-00058w-2L for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:52 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2705 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j293F-000557-AL; Thu, 13 Feb 2020 02:36:46 -0500 Received: from DGGEMS409-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id C84ED23787A711AEBACA; Thu, 13 Feb 2020 15:36:41 +0800 (CST) Received: from localhost (10.133.205.53) by DGGEMS409-HUB.china.huawei.com (10.3.19.209) with Microsoft SMTP Server id 14.3.439.0; Thu, 13 Feb 2020 15:36:34 +0800 From: Ying Fang To: , Subject: [PATCH v2 3/4] arm: Add the cpufreq device model Date: Thu, 13 Feb 2020 15:36:29 +0800 Message-ID: <20200213073630.2125-4-fangying1@huawei.com> X-Mailer: git-send-email 2.22.0.windows.1 In-Reply-To: <20200213073630.2125-1-fangying1@huawei.com> References: <20200213073630.2125-1-fangying1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.133.205.53] X-CFilter-Loop: Reflected Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 45.249.212.190 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, zhang.zhanghailiang@huawei.com, mst@redhat.com, i.mitsyanko@gmail.com, shannon.zhaosl@gmail.com, guoheyi@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" On ARM64 platform, CPU frequency is retrieved by ACPI CPPC, so here we create the virtual cpufreq device to present the CPPC registers and ACPI _CPC objects. The default frequency is set host CPU nominal frequency, which is obtained from the host CPPC sysfs. Other performance data are set to the same value, since we don't support guest performance scaling. Performance counters are also not emulated and they simply return 1 if readed, and guest should fallback to use the desired performance value as the current performance. Signed-off-by: Heyi Guo Signed-off-by: Ying Fang --- hw/acpi/Makefile.objs | 1 + hw/acpi/cpufreq.c | 249 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 250 insertions(+) create mode 100644 hw/acpi/cpufreq.c diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs index 777da07f4d..61530675d4 100644 --- a/hw/acpi/Makefile.objs +++ b/hw/acpi/Makefile.objs @@ -16,6 +16,7 @@ common-obj-y +=3D bios-linker-loader.o common-obj-y +=3D aml-build.o utils.o common-obj-$(CONFIG_ACPI_PCI) +=3D pci.o common-obj-$(CONFIG_TPM) +=3D tpm.o +common-obj-$(CONFIG_CPUFREQ) +=3D cpufreq.o =20 common-obj-$(CONFIG_IPMI) +=3D ipmi.o common-obj-$(call lnot,$(CONFIG_IPMI)) +=3D ipmi-stub.o diff --git a/hw/acpi/cpufreq.c b/hw/acpi/cpufreq.c new file mode 100644 index 0000000000..57fa35a29e --- /dev/null +++ b/hw/acpi/cpufreq.c @@ -0,0 +1,249 @@ +/* + * ACPI CPPC register device + * + * Support for showing CPU frequency in guest OS. + * + * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO.,LTD. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include "qemu/osdep.h" +#include "hw/sysbus.h" +#include "chardev/char.h" +#include "qemu/log.h" +#include "trace.h" +#include "qemu/option.h" +#include "sysemu/sysemu.h" +#include "hw/acpi/acpi-defs.h" +#include "qemu/cutils.h" +#include "qemu/error-report.h" +#include "hw/boards.h" + +#define TYPE_CPUFREQ "cpufreq" +#define CPUFREQ(obj) OBJECT_CHECK(CpufreqState, (obj), TYPE_CPUFREQ) +#define NOMINAL_FREQ_FILE "/sys/devices/system/cpu/cpu0/acpi_cppc/nominal_= freq" +#define CPU_MAX_FREQ_FILE "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_ma= x_freq" +#define HZ_MAX_LENGTH 1024 +#define MAX_SUPPORT_SPACE 0x10000 + +typedef struct CpufreqState { + SysBusDevice parent_obj; + + MemoryRegion iomem; + uint32_t HighestPerformance; + uint32_t NominalPerformance; + uint32_t LowestNonlinearPerformance; + uint32_t LowestPerformance; + uint32_t GuaranteedPerformance; + uint32_t DesiredPerformance; + uint64_t ReferencePerformanceCounter; + uint64_t DeliveredPerformanceCounter; + uint32_t PerformanceLimited; + uint32_t LowestFreq; + uint32_t NominalFreq; + uint32_t reg_size; +} CpufreqState; + + +static uint64_t cpufreq_read(void *opaque, hwaddr offset, + unsigned size) +{ + CpufreqState *s =3D (CpufreqState *)opaque; + uint64_t r; + uint64_t n; + + MachineState *ms =3D MACHINE(qdev_get_machine()); + unsigned int smp_cpus =3D ms->smp.cpus; + + if (offset >=3D smp_cpus * CPPC_REG_PER_CPU_STRIDE) { + warn_report("cpufreq_read: offset 0x%" HWADDR_PRIx " out of range", + offset); + return 0; + } + + n =3D offset % CPPC_REG_PER_CPU_STRIDE; + switch (n) { + case 0: + r =3D s->HighestPerformance; + break; + case 4: + r =3D s->NominalPerformance; + break; + case 8: + r =3D s->LowestNonlinearPerformance; + break; + case 12: + r =3D s->LowestPerformance; + break; + case 16: + r =3D s->GuaranteedPerformance; + break; + case 20: + r =3D s->DesiredPerformance; + break; + /* + * We don't have real counters and it is hard to emulate, so always se= t the + * counter value to 1 to rely on Linux to use the DesiredPerformance v= alue + * directly. + */ + case 24: + r =3D s->ReferencePerformanceCounter; + break; + /* + * Guest may still access the register by 32bit; add the process to + * eliminate unnecessary warnings + */ + case 28: + r =3D s->ReferencePerformanceCounter >> 32; + break; + case 32: + r =3D s->DeliveredPerformanceCounter; + break; + case 36: + r =3D s->DeliveredPerformanceCounter >> 32; + break; + + case 40: + r =3D s->PerformanceLimited; + break; + case 44: + r =3D s->LowestFreq; + break; + case 48: + r =3D s->NominalFreq; + break; + default: + error_printf("cpufreq_read: Bad offset 0x%" HWADDR_PRIx "\n", offs= et); + r =3D 0; + break; + } + return r; +} + +static void cpufreq_write(void *opaque, hwaddr offset, + uint64_t value, unsigned size) +{ + uint64_t n; + + MachineState *ms =3D MACHINE(qdev_get_machine()); + unsigned int smp_cpus =3D ms->smp.cpus; + + if (offset >=3D smp_cpus * CPPC_REG_PER_CPU_STRIDE) { + error_printf("cpufreq_write: offset 0x%" HWADDR_PRIx " out of rang= e", + offset); + return; + } + + n =3D offset % CPPC_REG_PER_CPU_STRIDE; + + switch (n) { + case 20: + break; + default: + error_printf("cpufreq_write: Bad offset 0x%" HWADDR_PRIx "\n", off= set); + } +} + +static uint32_t CPPC_Read(const char *hostpath) +{ + int fd; + char buffer[HZ_MAX_LENGTH] =3D { 0 }; + unsigned long hz; + const char *endptr =3D NULL; + int len; + int ret; + + fd =3D qemu_open(hostpath, O_RDONLY); + if (fd < 0) { + return 0; + } + + len =3D read(fd, buffer, HZ_MAX_LENGTH); + qemu_close(fd); + if (len <=3D 0) { + return 0; + } + ret =3D qemu_strtoul(buffer, &endptr, 0, &hz); + if (ret < 0) { + return 0; + } + return (uint32_t)hz; +} + +static const MemoryRegionOps cpufreq_ops =3D { + .read =3D cpufreq_read, + .write =3D cpufreq_write, + .endianness =3D DEVICE_NATIVE_ENDIAN, +}; + +static void hz_init(CpufreqState *s) +{ + uint32_t hz; + + hz =3D CPPC_Read(NOMINAL_FREQ_FILE); + if (hz =3D=3D 0) { + hz =3D CPPC_Read(CPU_MAX_FREQ_FILE); + /* Value in CpuMaxFrequency is in KHz unit; convert to MHz */ + hz =3D hz / 1000; + } + + s->HighestPerformance =3D hz; + s->NominalPerformance =3D hz; + s->LowestNonlinearPerformance =3D hz; + s->LowestPerformance =3D hz; + s->GuaranteedPerformance =3D hz; + s->DesiredPerformance =3D hz; + s->ReferencePerformanceCounter =3D 1; + s->DeliveredPerformanceCounter =3D 1; + s->PerformanceLimited =3D 0; + s->LowestFreq =3D hz; + s->NominalFreq =3D hz; +} + +static void cpufreq_init(Object *obj) +{ + SysBusDevice *sbd =3D SYS_BUS_DEVICE(obj); + CpufreqState *s =3D CPUFREQ(obj); + + MachineState *ms =3D MACHINE(qdev_get_machine()); + unsigned int smp_cpus =3D ms->smp.cpus; + + s->reg_size =3D smp_cpus * CPPC_REG_PER_CPU_STRIDE; + if (s->reg_size > MAX_SUPPORT_SPACE) { + error_report("Required space 0x%x excesses the maximun size 0x%x", + s->reg_size, MAX_SUPPORT_SPACE); + abort(); + } + + memory_region_init_io(&s->iomem, OBJECT(s), &cpufreq_ops, s, "cpufreq", + s->reg_size); + sysbus_init_mmio(sbd, &s->iomem); + hz_init(s); + return; +} + +static const TypeInfo cpufreq_info =3D { + .name =3D TYPE_CPUFREQ, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(CpufreqState), + .instance_init =3D cpufreq_init, +}; + +static void cpufreq_register_types(void) +{ + type_register_static(&cpufreq_info); +} + +type_init(cpufreq_register_types) --=20 2.19.1 From nobody Wed Apr 24 22:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581579551441563.4940016113507; Wed, 12 Feb 2020 23:39:11 -0800 (PST) Received: from localhost ([::1]:48380 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j295a-0005Bx-9x for importer@patchew.org; Thu, 13 Feb 2020 02:39:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:56541) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j293M-0001pC-Nv for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j293L-0005AZ-Ni for qemu-devel@nongnu.org; Thu, 13 Feb 2020 02:36:52 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:2765 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j293J-00055t-1d; Thu, 13 Feb 2020 02:36:49 -0500 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 77AA469E7198E136C052; Thu, 13 Feb 2020 15:36:45 +0800 (CST) Received: from localhost (10.133.205.53) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.439.0; Thu, 13 Feb 2020 15:36:34 +0800 From: Ying Fang To: , Subject: [PATCH v2 4/4] arm: Create the cpufreq device Date: Thu, 13 Feb 2020 15:36:30 +0800 Message-ID: <20200213073630.2125-5-fangying1@huawei.com> X-Mailer: git-send-email 2.22.0.windows.1 In-Reply-To: <20200213073630.2125-1-fangying1@huawei.com> References: <20200213073630.2125-1-fangying1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.133.205.53] X-CFilter-Loop: Reflected Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 45.249.212.191 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, zhang.zhanghailiang@huawei.com, mst@redhat.com, i.mitsyanko@gmail.com, shannon.zhaosl@gmail.com, guoheyi@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Add the cpufreq device to arm64 virt machine Signed-off-by: Heyi Guo Signed-off-by: Ying Fang --- default-configs/aarch64-softmmu.mak | 1 + hw/acpi/Kconfig | 4 ++++ hw/arm/virt.c | 13 +++++++++++++ 3 files changed, 18 insertions(+) diff --git a/default-configs/aarch64-softmmu.mak b/default-configs/aarch64-= softmmu.mak index 958b1e08e4..0a030e853f 100644 --- a/default-configs/aarch64-softmmu.mak +++ b/default-configs/aarch64-softmmu.mak @@ -6,3 +6,4 @@ include arm-softmmu.mak CONFIG_XLNX_ZYNQMP_ARM=3Dy CONFIG_XLNX_VERSAL=3Dy CONFIG_SBSA_REF=3Dy +CONFIG_CPUFREQ=3Dy diff --git a/hw/acpi/Kconfig b/hw/acpi/Kconfig index 54209c6f2f..7d8aa58492 100644 --- a/hw/acpi/Kconfig +++ b/hw/acpi/Kconfig @@ -38,3 +38,7 @@ config ACPI_VMGENID depends on PC =20 config ACPI_HW_REDUCED + +config CPUFREQ + bool + default y diff --git a/hw/arm/virt.c b/hw/arm/virt.c index ed9dc38b60..53638f9557 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -764,6 +764,17 @@ static void create_uart(const VirtMachineState *vms, i= nt uart, g_free(nodename); } =20 +static void create_cpufreq(const VirtMachineState *vms, MemoryRegion *mem) +{ + hwaddr base =3D vms->memmap[VIRT_CPUFREQ].base; + DeviceState *dev =3D qdev_create(NULL, "cpufreq"); + SysBusDevice *s =3D SYS_BUS_DEVICE(dev); + + qdev_init_nofail(dev); + memory_region_add_subregion(mem, base, sysbus_mmio_get_region(s, 0)); +} + + static void create_rtc(const VirtMachineState *vms) { char *nodename; @@ -1723,6 +1734,8 @@ static void machvirt_init(MachineState *machine) =20 create_uart(vms, VIRT_UART, sysmem, serial_hd(0)); =20 + create_cpufreq(vms, sysmem); + if (vms->secure) { create_secure_ram(vms, secure_sysmem); create_uart(vms, VIRT_SECURE_UART, secure_sysmem, serial_hd(1)); --=20 2.19.1