From nobody Mon Nov 25 07:34:58 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1716289071; cv=none; d=zohomail.com; s=zohoarc; b=jJqaFjxrCOGE9w7JESH+poK/kCdfk7NaaKbdR9HQomVrQdoHsBz2ysOSRbYE4N9FbqSUFI+zrNdGoTUutyB976LeRh0XriQMVqrMLLEW5ZGUgeIL1o3Y6ZowwpvnRy6LflCVMMkvQ5FUkyrOhr46eMV1vL9Wb8hQmpiMliukDFk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716289071; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Gm9B/Tp/MHNdt2Lqj83O7NhfybIUx9ygELdpEGJ0Fxg=; b=L5p0Sdf1wjDTI3N8Hq5Qe7kQ4c7EDj6h7ayYUpl6Q7j3vYdNkjsCOw3Fx83C5ddD34qx2Twm3nr99hZyD6bIaxVoa/hghT62Anjq+KE6yV0baBnk5i+6hFvM1WItroO67k7sRATL6fGEvUHxdVwupQq7VL6/gbO5BAfhipB8Mec= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 171628907172940.58609425609052; Tue, 21 May 2024 03:57:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9NB1-000094-81; Tue, 21 May 2024 06:57:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9NAv-00006R-9z; Tue, 21 May 2024 06:56:57 -0400 Received: from sin.source.kernel.org ([145.40.73.55]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9NAs-0008In-SM; Tue, 21 May 2024 06:56:57 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 723B1CE0F0B; Tue, 21 May 2024 10:56:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1484DC32786; Tue, 21 May 2024 10:56:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716289010; bh=28t7Fg5B6krxDtsXGakeNIyt+SyVFB/V4qTfbcdgegE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b+kijK/5NwxHq37VHxPk9CnFTZzTKVXAJTF0nlXAHUWo3pDYn0Kv3AzOW4KQ17iCv jLT9X90Lqhy/xg3HMyD2vsE0SepnZWtoMpVs0DPfOeSE/MzsS6k2vtoZELb/hBrJ7p XrPDXqlXTHWNNO21N7e0vlfpz1B7018EVSayHWoViIwa5Jy+TeAENNwb3AA6PPDAcI hOeegx3IY6jK41Bol65ERNckXqyGoVAYSPad88yeKLo2R//4LHrgzRcyc+K2qLSstg tNGTEcfKvPxP0HnKSUt0+EN91iga0y97ycp+pXfwWLXa91Ng00AiKs/7uMfBW5A4TA pf/NJijogxEmA== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, David Hildenbrand , Atish Patra , Atish Patra Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Sunil V L , Santosh Mamila , Chethan Seshadri , Sivakumar Munnangi Subject: [PATCH v2 1/3] hw/riscv/virt: Add memory hotplugging and virtio-md-pci support Date: Tue, 21 May 2024 12:56:33 +0200 Message-Id: <20240521105635.795211-2-bjorn@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240521105635.795211-1-bjorn@kernel.org> References: <20240521105635.795211-1-bjorn@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=145.40.73.55; envelope-from=bjorn@kernel.org; helo=sin.source.kernel.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1716289073149100003 From: Bj=C3=B6rn T=C3=B6pel Virtio-based memory devices (virtio-mem/virtio-pmem) allows for dynamic resizing of virtual machine memory, and requires proper hotplugging (add/remove) support to work. Add device memory support for RISC-V "virt" machine, and enable virtio-md-pci with the corresponding missing hotplugging callbacks. Signed-off-by: Bj=C3=B6rn T=C3=B6pel --- hw/riscv/Kconfig | 2 + hw/riscv/virt.c | 83 +++++++++++++++++++++++++++++++++++++++++- hw/virtio/virtio-mem.c | 5 ++- 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index a2030e3a6ff0..08f82dbb681a 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -56,6 +56,8 @@ config RISCV_VIRT select PLATFORM_BUS select ACPI select ACPI_PCI + select VIRTIO_MEM_SUPPORTED + select VIRTIO_PMEM_SUPPORTED =20 config SHAKTI_C bool diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c index 4fdb66052587..443902f919d2 100644 --- a/hw/riscv/virt.c +++ b/hw/riscv/virt.c @@ -53,6 +53,8 @@ #include "hw/pci-host/gpex.h" #include "hw/display/ramfb.h" #include "hw/acpi/aml-build.h" +#include "hw/mem/memory-device.h" +#include "hw/virtio/virtio-mem-pci.h" #include "qapi/qapi-visit-common.h" #include "hw/virtio/virtio-iommu.h" =20 @@ -1407,6 +1409,7 @@ static void virt_machine_init(MachineState *machine) DeviceState *mmio_irqchip, *virtio_irqchip, *pcie_irqchip; int i, base_hartid, hart_count; int socket_count =3D riscv_socket_count(machine); + hwaddr device_memory_base, device_memory_size; =20 /* Check socket count limit */ if (VIRT_SOCKETS_MAX < socket_count) { @@ -1420,6 +1423,12 @@ static void virt_machine_init(MachineState *machine) exit(1); } =20 + if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) { + error_report("unsupported amount of memory slots: %"PRIu64, + machine->ram_slots); + exit(EXIT_FAILURE); + } + /* Initialize sockets */ mmio_irqchip =3D virtio_irqchip =3D pcie_irqchip =3D NULL; for (i =3D 0; i < socket_count; i++) { @@ -1553,6 +1562,37 @@ static void virt_machine_init(MachineState *machine) memory_region_add_subregion(system_memory, memmap[VIRT_MROM].base, mask_rom); =20 + /* device memory */ + device_memory_base =3D ROUND_UP(s->memmap[VIRT_DRAM].base + machine->r= am_size, + GiB); + device_memory_size =3D machine->maxram_size - machine->ram_size; + if (device_memory_size > 0) { + /* + * Each DIMM is aligned based on the backend's alignment value. + * Assume max 1G hugepage alignment per slot. + */ + device_memory_size +=3D machine->ram_slots * GiB; + + if (riscv_is_32bit(&s->soc[0])) { + hwaddr memtop =3D device_memory_base + ROUND_UP(device_memory_= size, + GiB); + + if (memtop > UINT32_MAX) { + error_report("memory exceeds 32-bit limit by %lu bytes", + memtop - UINT32_MAX); + exit(EXIT_FAILURE); + } + } + + if (device_memory_base + device_memory_size < device_memory_size) { + error_report("unsupported amount of device memory"); + exit(EXIT_FAILURE); + } + + machine_memory_devices_init(machine, device_memory_base, + device_memory_size); + } + /* * Init fw_cfg. Must be done before riscv_load_fdt, otherwise the * device tree cannot be altered and we get FDT_ERR_NOSPACE. @@ -1712,12 +1752,21 @@ static HotplugHandler *virt_machine_get_hotplug_han= dler(MachineState *machine, MachineClass *mc =3D MACHINE_GET_CLASS(machine); =20 if (device_is_dynamic_sysbus(mc, dev) || - object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { + object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) || + object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { return HOTPLUG_HANDLER(machine); } return NULL; } =20 +static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev, + DeviceState *dev, Error **errp) +{ + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { + virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), e= rrp); + } +} + static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) { @@ -1735,6 +1784,35 @@ static void virt_machine_device_plug_cb(HotplugHandl= er *hotplug_dev, if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { create_fdt_virtio_iommu(s, pci_get_bdf(PCI_DEVICE(dev))); } + + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { + virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); + } +} + +static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_= dev, + DeviceState *dev, + Error **errp) +{ + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { + virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_d= ev), + errp); + } else { + error_setg(errp, + "device unplug request for unsupported device type: %s", + object_get_typename(OBJECT(dev))); + } +} + +static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev, + DeviceState *dev, Error **errp) +{ + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { + virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), err= p); + } else { + error_setg(errp, "virt: device unplug for unsupported device" + " type: %s", object_get_typename(OBJECT(dev))); + } } =20 static void virt_machine_class_init(ObjectClass *oc, void *data) @@ -1757,7 +1835,10 @@ static void virt_machine_class_init(ObjectClass *oc,= void *data) assert(!mc->get_hotplug_handler); mc->get_hotplug_handler =3D virt_machine_get_hotplug_handler; =20 + hc->pre_plug =3D virt_machine_device_pre_plug_cb; hc->plug =3D virt_machine_device_plug_cb; + hc->unplug_request =3D virt_machine_device_unplug_request_cb; + hc->unplug =3D virt_machine_device_unplug_cb; =20 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE); #ifdef CONFIG_TPM diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index ffd119ebacb7..6636e5e1089c 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -51,7 +51,8 @@ static uint32_t virtio_mem_default_thp_size(void) { uint32_t default_thp_size =3D VIRTIO_MEM_MIN_BLOCK_SIZE; =20 -#if defined(__x86_64__) || defined(__arm__) || defined(__powerpc64__) +#if defined(__x86_64__) || defined(__arm__) || defined(__powerpc64__) \ + || defined(__riscv__) default_thp_size =3D 2 * MiB; #elif defined(__aarch64__) if (qemu_real_host_page_size() =3D=3D 4 * KiB) { @@ -161,7 +162,7 @@ static bool virtio_mem_has_shared_zeropage(RAMBlock *rb) * necessary (as the section size can change). But it's more likely that t= he * section size will rather get smaller and not bigger over time. */ -#if defined(TARGET_X86_64) || defined(TARGET_I386) +#if defined(TARGET_X86_64) || defined(TARGET_I386) || defined(TARGET_RISC= V) #define VIRTIO_MEM_USABLE_EXTENT (2 * (128 * MiB)) #elif defined(TARGET_ARM) #define VIRTIO_MEM_USABLE_EXTENT (2 * (512 * MiB)) --=20 2.40.1 From nobody Mon Nov 25 07:34:58 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1716289092; cv=none; d=zohomail.com; s=zohoarc; b=IQxJ25PcHJ4MXGqMMpkcyIq7qLnWVkLSFKWjJbDVkrVxBi105NxfEDqJPo+xAIgg86cBRMdaV5H1L5jnMc2y0UT9l+S6BET3/LpcGHu8fkVOJTtRgykz864INSRxNJdgTNXuu0VfHxviM3kKv+WwmoqxcrBgjOdRzxpnim946Xc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716289092; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=LuCtTVpA4TD+B9DoNIqIYGIHxQoP1DpoqYk5GFKUBOc=; b=Vh3SswitTsTsEkK4G5PNDhwLOs3Ry4/VXLVt9WoQUt0N25sEV7HHat8/UmyOub5dXirlKfoTOF7sgmaIvxj7jC4XjPrhb3rmRH2EIDpARXdxAd332mTK2yC9kKVyJYKWwLVOZxIzmBaMPXTD8EMITHsJ+2beX8IphqdMJCPHWoE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716289092848597.127968521819; Tue, 21 May 2024 03:58:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9NB1-00009P-CF; Tue, 21 May 2024 06:57:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9NAw-00006k-N2; Tue, 21 May 2024 06:56:58 -0400 Received: from dfw.source.kernel.org ([139.178.84.217]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9NAv-0008J3-3R; Tue, 21 May 2024 06:56:58 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 2238362153; Tue, 21 May 2024 10:56:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26BF0C2BD11; Tue, 21 May 2024 10:56:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716289014; bh=izDUykFEQkwQc1xzdS3N2VcVYxMhBjpLt/wly4Hm8+A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ptkkf9FNqTWXHMV9wfe3iEC7Qh9Anqly8weuR4s2SCdBoHXrd7L/2lTASBxBX0Y6a Htvk+obeXVQC7Vq943+8Ci2oFjdFCZ0ycs1jcPZl2o/filrrMhi8pswOoGyvAI97L+ +uTBpUyIQM8GMTUCt8JSKCSddpPJJ5jRdGzQOp1/ZicJjbWY4301MAiEZlp4YrU3fW 7oIg+XAXK0amIU4fG7IPPrvm3ZlCV0Xwpn59ViaGDpwaoHoH1D8mvNbB9z0uu9/Dlv Z1IxH7d/jcsknLoeqgH29b1/waqhq73kN0FIY2GwWpcewvLhosaoi5HT6UqeJO4WBy ZpFaZMbaUhHMg== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, David Hildenbrand , Atish Patra , Atish Patra Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Sunil V L , Santosh Mamila , Chethan Seshadri , Sivakumar Munnangi Subject: [PATCH v2 2/3] hw/riscv/virt-acpi-build: Expose device memory in ACPI SRAT Date: Tue, 21 May 2024 12:56:34 +0200 Message-Id: <20240521105635.795211-3-bjorn@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240521105635.795211-1-bjorn@kernel.org> References: <20240521105635.795211-1-bjorn@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=139.178.84.217; envelope-from=bjorn@kernel.org; helo=dfw.source.kernel.org X-Spam_score_int: -70 X-Spam_score: -7.1 X-Spam_bar: ------- X-Spam_report: (-7.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1716289093216100001 From: Bj=C3=B6rn T=C3=B6pel Now that device memory is supported by RISC-V 'virt', expose that region in the ACPI SRAT (System/Static Resource Affinity Table). ACPI SRAT is used by, e.g., the virtio-mem Linux kernel driver [1]. Link: https://virtio-mem.gitlab.io/user-guide/user-guide-linux.html # [1] Signed-off-by: Bj=C3=B6rn T=C3=B6pel --- hw/riscv/virt-acpi-build.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hw/riscv/virt-acpi-build.c b/hw/riscv/virt-acpi-build.c index 0925528160f8..6dc3baa9ec86 100644 --- a/hw/riscv/virt-acpi-build.c +++ b/hw/riscv/virt-acpi-build.c @@ -610,6 +610,13 @@ build_srat(GArray *table_data, BIOSLinker *linker, RIS= CVVirtState *vms) } } =20 + if (ms->device_memory) { + build_srat_memory(table_data, ms->device_memory->base, + memory_region_size(&ms->device_memory->mr), + ms->numa_state->num_nodes - 1, + MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED= ); + } + acpi_table_end(linker, &table); } =20 --=20 2.40.1 From nobody Mon Nov 25 07:34:58 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1716289101; cv=none; d=zohomail.com; s=zohoarc; b=UPjm0lo971Pzvk3zqZbScjyhnr7r+T4VrAizo72svy1he0+nmk4IXUmldibmRVOErEmZ9QWstYkUdTwKkO+d4w5uQ+NQws7RXYEbGMaTHirAqCFB5hGQA7xpRuLbZvXqXZAUJPVn7gm90h2lpVmm1sRyFxhOm1RKFdlQhQeQkUI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716289101; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gfw88E55xE/udfgcOF/gNIQhZLP6+ecZ/RsMDuxb+a4=; b=QOtUpCWCMEF2vxO/nIEO5BqIhJXNhV6Wx5CHD8sl4OA6ywQf/w18GrNdZ2LFxZ+ZY5EQbM1QC3n6mF9VTyj1rC3RId6/okfAhJpILYmGMBL1YMU8ooblpo+f/1aXWZ1r31DRHwosQWWrMSyzJrKcDp6V5UM8ygV2dwO3mK0e4DU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716289101912999.593008713546; Tue, 21 May 2024 03:58:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9NB5-0000BG-HW; Tue, 21 May 2024 06:57:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9NB3-0000Aa-JV; Tue, 21 May 2024 06:57:05 -0400 Received: from sin.source.kernel.org ([2604:1380:40e1:4800::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9NB1-0008JS-4c; Tue, 21 May 2024 06:57:05 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A4EA2CE0EEE; Tue, 21 May 2024 10:56:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43970C2BD11; Tue, 21 May 2024 10:56:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716289018; bh=BF0Re7pZITalSThFSxkXg3hVynp1HCvG+nenL0gPfLs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=POL+lhgiVkPpuT0SkNNNyJ0AiFoxQ+rqMX7on4gd076BPxskkfyrihULaAql1sVAM 1Ah95jMufB+E8z7K0DWZ3N37e7rr2fums6Sn0o4oT4EYA9Gw/D8Kp1pVzuGWXBGpI0 lhOyRibdd/cxIUglnOGGaYT+o039Xhja6UPDCRrZUrDDG0mTIqWmjG3/GUESzeP1Uq lj6TJOB0whFu6xtqw5+NZ0TB8HL3IAG2Lc3typmS4iqcJKlnQ45QE6ovCx9NJ5igOT cGByKgGAnkClr0e1k/gKdCfRLwJxgXB0ptKHi/OBqoZdjB2DO/fek0DhAf/cqaQHCu nYCWEDmMIdFbA== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, David Hildenbrand , Atish Patra , Atish Patra Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Sunil V L , Santosh Mamila , Chethan Seshadri , Sivakumar Munnangi Subject: [PATCH v2 3/3] hw/riscv/virt: Add ACPI GED and PC-DIMM MHP support Date: Tue, 21 May 2024 12:56:35 +0200 Message-Id: <20240521105635.795211-4-bjorn@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240521105635.795211-1-bjorn@kernel.org> References: <20240521105635.795211-1-bjorn@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=2604:1380:40e1:4800::1; envelope-from=bjorn@kernel.org; helo=sin.source.kernel.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1716289103357100003 From: Bj=C3=B6rn T=C3=B6pel Add ACPI GED for the RISC-V "virt" machine, and wire up PC-DIMM memory hotplugging support. Heavily based/copied from hw/arm/virt.c. Signed-off-by: Bj=C3=B6rn T=C3=B6pel --- hw/riscv/Kconfig | 3 ++ hw/riscv/virt-acpi-build.c | 16 ++++++ hw/riscv/virt.c | 104 ++++++++++++++++++++++++++++++++++++- include/hw/riscv/virt.h | 6 ++- 4 files changed, 126 insertions(+), 3 deletions(-) diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index 08f82dbb681a..bebe412f2107 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -56,6 +56,9 @@ config RISCV_VIRT select PLATFORM_BUS select ACPI select ACPI_PCI + select MEM_DEVICE + select DIMM + select ACPI_HW_REDUCED select VIRTIO_MEM_SUPPORTED select VIRTIO_PMEM_SUPPORTED =20 diff --git a/hw/riscv/virt-acpi-build.c b/hw/riscv/virt-acpi-build.c index 6dc3baa9ec86..61813abdef3f 100644 --- a/hw/riscv/virt-acpi-build.c +++ b/hw/riscv/virt-acpi-build.c @@ -27,6 +27,8 @@ #include "hw/acpi/acpi-defs.h" #include "hw/acpi/acpi.h" #include "hw/acpi/aml-build.h" +#include "hw/acpi/memory_hotplug.h" +#include "hw/acpi/generic_event_device.h" #include "hw/acpi/pci.h" #include "hw/acpi/utils.h" #include "hw/intc/riscv_aclint.h" @@ -432,6 +434,20 @@ static void build_dsdt(GArray *table_data, acpi_dsdt_add_gpex_host(scope, PCIE_IRQ + VIRT_IRQCHIP_NUM_SOURCES= * 2); } =20 + if (s->acpi_dev) { + uint32_t event =3D object_property_get_uint(OBJECT(s->acpi_dev), + "ged-event", &error_abor= t); + + build_ged_aml(scope, "\\_SB."GED_DEVICE, HOTPLUG_HANDLER(s->acpi_d= ev), + GED_IRQ, AML_SYSTEM_MEMORY, memmap[VIRT_ACPI_GED].ba= se); + + if (event & ACPI_GED_MEM_HOTPLUG_EVT) { + build_memory_hotplug_aml(scope, ms->ram_slots, "\\_SB", NULL, + AML_SYSTEM_MEMORY, + memmap[VIRT_PCDIMM_ACPI].base); + } + } + aml_append(dsdt, scope); =20 /* copy AML table into ACPI tables blob and patch header there */ diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c index 443902f919d2..2e35890187f2 100644 --- a/hw/riscv/virt.c +++ b/hw/riscv/virt.c @@ -53,10 +53,13 @@ #include "hw/pci-host/gpex.h" #include "hw/display/ramfb.h" #include "hw/acpi/aml-build.h" +#include "hw/acpi/generic_event_device.h" +#include "hw/acpi/memory_hotplug.h" #include "hw/mem/memory-device.h" #include "hw/virtio/virtio-mem-pci.h" #include "qapi/qapi-visit-common.h" #include "hw/virtio/virtio-iommu.h" +#include "hw/mem/pc-dimm.h" =20 /* KVM AIA only supports APLIC MSI. APLIC Wired is always emulated by QEMU= . */ static bool virt_use_kvm_aia(RISCVVirtState *s) @@ -84,6 +87,8 @@ static const MemMapEntry virt_memmap[] =3D { [VIRT_UART0] =3D { 0x10000000, 0x100 }, [VIRT_VIRTIO] =3D { 0x10001000, 0x1000 }, [VIRT_FW_CFG] =3D { 0x10100000, 0x18 }, + [VIRT_PCDIMM_ACPI] =3D { 0x10200000, MEMORY_HOTPLUG_IO_LEN }, + [VIRT_ACPI_GED] =3D { 0x10210000, ACPI_GED_EVT_SEL_LEN }, [VIRT_FLASH] =3D { 0x20000000, 0x4000000 }, [VIRT_IMSIC_M] =3D { 0x24000000, VIRT_IMSIC_MAX_SIZE }, [VIRT_IMSIC_S] =3D { 0x28000000, VIRT_IMSIC_MAX_SIZE }, @@ -1400,6 +1405,28 @@ static void virt_machine_done(Notifier *notifier, vo= id *data) } } =20 +static DeviceState *create_acpi_ged(RISCVVirtState *s) +{ + DeviceState *dev; + MachineState *ms =3D MACHINE(s); + uint32_t event =3D 0; + + if (ms->ram_slots) { + event |=3D ACPI_GED_MEM_HOTPLUG_EVT; + } + + dev =3D qdev_new(TYPE_ACPI_GED); + qdev_prop_set_uint32(dev, "ged-event", event); + sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); + + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, s->memmap[VIRT_ACPI_GED].base); + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, s->memmap[VIRT_PCDIMM_ACPI].ba= se); + sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(s->irqchip= [0], + GED_IRQ)); + + return dev; +} + static void virt_machine_init(MachineState *machine) { const MemMapEntry *memmap =3D virt_memmap; @@ -1612,6 +1639,10 @@ static void virt_machine_init(MachineState *machine) =20 gpex_pcie_init(system_memory, pcie_irqchip, s); =20 + if (virt_is_acpi_enabled(s)) { + s->acpi_dev =3D create_acpi_ged(s); + } + create_platform_bus(s, mmio_irqchip); =20 serial_mm_init(system_memory, memmap[VIRT_UART0].base, @@ -1752,6 +1783,7 @@ static HotplugHandler *virt_machine_get_hotplug_handl= er(MachineState *machine, MachineClass *mc =3D MACHINE_GET_CLASS(machine); =20 if (device_is_dynamic_sysbus(mc, dev) || + object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) || object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) || object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { return HOTPLUG_HANDLER(machine); @@ -1759,14 +1791,42 @@ static HotplugHandler *virt_machine_get_hotplug_han= dler(MachineState *machine, return NULL; } =20 +static void virt_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState = *dev, + Error **errp) +{ + RISCVVirtState *s =3D RISCV_VIRT_MACHINE(hotplug_dev); + + if (!s->acpi_dev) { + error_setg(errp, + "memory hotplug is not enabled: missing acpi-ged device= "); + return; + } + + pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), NULL, errp); +} + static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) { + if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { + virt_memory_pre_plug(hotplug_dev, dev, errp); + } + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), e= rrp); } } =20 +static void virt_memory_plug(HotplugHandler *hotplug_dev, + DeviceState *dev, Error **errp) +{ + RISCVVirtState *s =3D RISCV_VIRT_MACHINE(hotplug_dev); + + pc_dimm_plug(PC_DIMM(dev), MACHINE(s)); + + hotplug_handler_plug(HOTPLUG_HANDLER(s->acpi_dev), dev, &error_abort); +} + static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) { @@ -1785,16 +1845,36 @@ static void virt_machine_device_plug_cb(HotplugHand= ler *hotplug_dev, create_fdt_virtio_iommu(s, pci_get_bdf(PCI_DEVICE(dev))); } =20 + if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { + virt_memory_plug(hotplug_dev, dev, errp); + } + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); } } =20 +static void virt_dimm_unplug_request(HotplugHandler *hotplug_dev, + DeviceState *dev, Error **errp) +{ + RISCVVirtState *s =3D RISCV_VIRT_MACHINE(hotplug_dev); + + if (!s->acpi_dev) { + error_setg(errp, + "memory hotplug is not enabled: missing acpi-ged device= "); + return; + } + + hotplug_handler_unplug_request(HOTPLUG_HANDLER(s->acpi_dev), dev, errp= ); +} + static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_= dev, DeviceState *dev, Error **errp) { - if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { + if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { + virt_dimm_unplug_request(hotplug_dev, dev, errp); + } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_d= ev), errp); } else { @@ -1804,10 +1884,30 @@ static void virt_machine_device_unplug_request_cb(H= otplugHandler *hotplug_dev, } } =20 +static void virt_dimm_unplug(HotplugHandler *hotplug_dev, + DeviceState *dev, Error **errp) +{ + RISCVVirtState *s =3D RISCV_VIRT_MACHINE(hotplug_dev); + Error *local_err =3D NULL; + + hotplug_handler_unplug(HOTPLUG_HANDLER(s->acpi_dev), dev, &local_err); + if (local_err) { + goto out; + } + + pc_dimm_unplug(PC_DIMM(dev), MACHINE(s)); + qdev_unrealize(dev); + +out: + error_propagate(errp, local_err); +} + static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) { - if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { + if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { + virt_dimm_unplug(hotplug_dev, dev, errp); + } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), err= p); } else { error_setg(errp, "virt: device unplug for unsupported device" diff --git a/include/hw/riscv/virt.h b/include/hw/riscv/virt.h index 3db839160f95..adf460a4021e 100644 --- a/include/hw/riscv/virt.h +++ b/include/hw/riscv/virt.h @@ -62,6 +62,7 @@ struct RISCVVirtState { OnOffAuto acpi; const MemMapEntry *memmap; struct GPEXHost *gpex_host; + DeviceState *acpi_dev; }; =20 enum { @@ -84,12 +85,15 @@ enum { VIRT_PCIE_MMIO, VIRT_PCIE_PIO, VIRT_PLATFORM_BUS, - VIRT_PCIE_ECAM + VIRT_PCIE_ECAM, + VIRT_PCDIMM_ACPI, + VIRT_ACPI_GED, }; =20 enum { UART0_IRQ =3D 10, RTC_IRQ =3D 11, + GED_IRQ =3D 12, VIRTIO_IRQ =3D 1, /* 1 to 8 */ VIRTIO_COUNT =3D 8, PCIE_IRQ =3D 0x20, /* 32 to 35 */ --=20 2.40.1