From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AAEC5C433F5 for ; Tue, 24 May 2022 20:18:10 +0000 (UTC) Received: from localhost ([::1]:58590 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ntayn-0006CV-JT for qemu-devel@archiver.kernel.org; Tue, 24 May 2022 16:18:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58636) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ntaqg-0006aT-D5 for qemu-devel@nongnu.org; Tue, 24 May 2022 16:09:46 -0400 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]:37878) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ntaqe-0002F8-85 for qemu-devel@nongnu.org; Tue, 24 May 2022 16:09:46 -0400 Received: by mail-ej1-x629.google.com with SMTP id rq11so15444486ejc.4 for ; Tue, 24 May 2022 13:09:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kwet7AqFbAIpfjpoHJp8MnRdkykz0MtFqAh1niMiOCo=; b=Il0qKILEkke7E4pH1AIgjc+KMY/DR6/1GVJHzN2PExrXITX6a2bT+50B9lCgq4zwBA R3TR/cM6/T78pWdS/GHT/iT84YgNGWBpNvYrz1nLLcYrgFeT+xsqMxOuyXYr9N/L9vpX SxGLcJD5hGfLu65VsPlV2kb28hYv5wbby10Jxy/07se7qzLL32M9LeslyKaIEA/OGWvD x9OlN5vd26NK1x2lb9egMv7glpaLitgT0uSdPeC5zXlAwbLdZb892AJadHvoP9wtKvoB EOxayirrtNvlCnxQmEa7GffnS1ApUGrs5Crbo/jFrUErUp8w5fXl5umEDXzMtoZCMf2e NtAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kwet7AqFbAIpfjpoHJp8MnRdkykz0MtFqAh1niMiOCo=; b=3MZ6A6RgxINoaGxGYYX/ZLbxLYaovJpBsdp1+ZgQyuMycrMo9uvgt3yCcrsNyI+9Q3 pyjqLCrXbDQRY732RurEd1HlXM1gg9FqqPuo7y8f8ijciXz39+g+4pTb21NLTit8zpCV T4BkB6waBBOyedroMXmIvNog9XDP77oDwXOX/H2iHTfirxs1DSH2Q06iI+fQmJf6UKp6 kfOL0QMNAV9MKkijfr0nYaHcCbOxvclasR8S9VAnnsbyZSUZHYeQU5AdY8ODmL7zwSxD H0vn1UMBvQEWPgtgzVYSrZuKpTnvtoHE0/C/TUsSI4ucBqeRDU4bY9KM8ls4XbRwYeJI DyCw== X-Gm-Message-State: AOAM532tT088v1SkMCqbrp4kB0FmJWCmR2yiFU6HzjtL8b7UO7rw0cLY aUPHzbUEQ3YOh/LinmMoUrJGdpabr2F1Ef6vimAAKA== X-Google-Smtp-Source: ABdhPJw4fhN+AwozCNai8eal5cMc0MWMV/C5LZa1dJYdSKfRF227JCaGtreEQbAq215eRApEKliiqNO2sg9k2dNnsHg= X-Received: by 2002:a17:907:7ea8:b0:6f5:941:cba9 with SMTP id qb40-20020a1709077ea800b006f50941cba9mr25068736ejc.213.1653422982936; Tue, 24 May 2022 13:09:42 -0700 (PDT) MIME-Version: 1.0 References: <20220223090706.4888-1-damien.hedde@greensocs.com> <20220223090706.4888-13-damien.hedde@greensocs.com> In-Reply-To: <20220223090706.4888-13-damien.hedde@greensocs.com> From: Jim Shu Date: Wed, 25 May 2022 04:09:29 +0800 Message-ID: Subject: Re: [PATCH v4 12/14] add sysbus-mmio-map qapi command To: Damien Hedde Cc: "qemu-devel@nongnu.org Developers" , mark.burton@greensocs.com, edgari@xilinx.com, Eduardo Habkost , =?UTF-8?Q?Daniel_P=2E_Berrang=C3=A9?= , Markus Armbruster , Alistair Francis , Paolo Bonzini , Eric Blake Content-Type: multipart/alternative; boundary="0000000000008fd26a05dfc789b5" Received-SPF: pass client-ip=2a00:1450:4864:20::629; envelope-from=jim.shu@sifive.com; helo=mail-ej1-x629.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --0000000000008fd26a05dfc789b5 Content-Type: text/plain; charset="UTF-8" Tested-by: Jim Shu On Wed, Feb 23, 2022 at 5:37 PM Damien Hedde wrote: > This command allows to map an mmio region of sysbus device onto > the system memory. Its behavior mimics the sysbus_mmio_map() > function apart from the automatic unmap (the C function unmaps > the region if it is already mapped). > For the qapi function we consider it is an error to try to map > an already mapped function. If unmapping is required, it is > probably better to add a sysbus-mmip-unmap command. > > This command is still experimental (hence the 'unstable' feature), > as it is related to the sysbus device creation through qapi commands. > > This command is required to be able to dynamically build a machine > from scratch as there is no qapi-way of doing a memory mapping. > > Signed-off-by: Damien Hedde > --- > Cc: Alistair Francis > > v4: > + integrate priority parameter > + use 'unstable' feature flag instead of 'x-' prefix > + bump version to 7.0 > + dropped Alistair's reviewed-by as a consequence > --- > qapi/qdev.json | 31 ++++++++++++++++++++++++++++++ > hw/core/sysbus.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 80 insertions(+) > > diff --git a/qapi/qdev.json b/qapi/qdev.json > index 2e2de41499..4830e87a90 100644 > --- a/qapi/qdev.json > +++ b/qapi/qdev.json > @@ -160,3 +160,34 @@ > ## > { 'event': 'DEVICE_UNPLUG_GUEST_ERROR', > 'data': { '*device': 'str', 'path': 'str' } } > + > +## > +# @sysbus-mmio-map: > +# > +# Map a sysbus device mmio onto the main system bus. > +# > +# @device: the device's QOM path > +# > +# @mmio: The mmio number to be mapped (defaults to 0). > +# > +# @addr: The base address for the mapping. > +# > +# @priority: The priority of the mapping (defaults to 0). > +# > +# Features: > +# @unstable: Command is meant to map sysbus devices > +# while in preconfig mode. > +# > +# Since: 7.0 > +# > +# Returns: Nothing on success > +# > +## > + > +{ 'command': 'sysbus-mmio-map', > + 'data': { 'device': 'str', > + '*mmio': 'uint8', > + 'addr': 'uint64', > + '*priority': 'int32' }, > + 'features': ['unstable'], > + 'allow-preconfig' : true } > diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c > index 05c1da3d31..df1f1f43a5 100644 > --- a/hw/core/sysbus.c > +++ b/hw/core/sysbus.c > @@ -23,6 +23,7 @@ > #include "hw/sysbus.h" > #include "monitor/monitor.h" > #include "exec/address-spaces.h" > +#include "qapi/qapi-commands-qdev.h" > > static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); > static char *sysbus_get_fw_dev_path(DeviceState *dev); > @@ -154,6 +155,54 @@ static void sysbus_mmio_map_common(SysBusDevice *dev, > int n, hwaddr addr, > } > } > > +void qmp_sysbus_mmio_map(const char *device, > + bool has_mmio, uint8_t mmio, > + uint64_t addr, > + bool has_priority, int32_t priority, > + Error **errp) > +{ > + Object *obj = object_resolve_path_type(device, TYPE_SYS_BUS_DEVICE, > NULL); > + SysBusDevice *dev; > + > + if (phase_get() != PHASE_MACHINE_INITIALIZED) { > + error_setg(errp, "The command is permitted only when " > + "the machine is in initialized phase"); > + return; > + } > + > + if (obj == NULL) { > + error_setg(errp, "Device '%s' not found", device); > + return; > + } > + dev = SYS_BUS_DEVICE(obj); > + > + if (!has_mmio) { > + mmio = 0; > + } > + if (!has_priority) { > + priority = 0; > + } > + > + if (mmio >= dev->num_mmio) { > + error_setg(errp, "MMIO index '%u' does not exist in '%s'", > + mmio, device); > + return; > + } > + > + if (dev->mmio[mmio].addr != (hwaddr)-1) { > + error_setg(errp, "MMIO index '%u' is already mapped", mmio); > + return; > + } > + > + if (!memory_region_try_add_subregion(get_system_memory(), addr, > + dev->mmio[mmio].memory, priority, > + errp)) { > + return; > + } > + > + dev->mmio[mmio].addr = addr; > +} > + > void sysbus_mmio_unmap(SysBusDevice *dev, int n) > { > assert(n >= 0 && n < dev->num_mmio); > -- > 2.35.1 > > > --0000000000008fd26a05dfc789b5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Tested-by: Jim Shu <jim.shu@sifive.com>

On Wed, Feb 23, 2022 at = 5:37 PM Damien Hedde <damien.hedde@greensocs.com> wrote:
This command allows to map an mmio = region of sysbus device onto
the system memory. Its behavior mimics the sysbus_mmio_map()
function apart from the automatic unmap (the C function unmaps
the region if it is already mapped).
For the qapi function we consider it is an error to try to map
an already mapped function. If unmapping is required, it is
probably better to add a sysbus-mmip-unmap command.

This command is still experimental (hence the 'unstable' feature),<= br> as it is related to the sysbus device creation through qapi commands.

This command is required to be able to dynamically build a machine
from scratch as there is no qapi-way of doing a memory mapping.

Signed-off-by: Damien Hedde <damien.hedde@greensocs.com>
---
Cc: Alistair Francis <alistair.francis@wdc.com>

v4:
=C2=A0+ integrate priority parameter
=C2=A0+ use 'unstable' feature flag instead of 'x-' prefix<= br> =C2=A0+ bump version to 7.0
=C2=A0+ dropped Alistair's reviewed-by as a consequence
---
=C2=A0qapi/qdev.json=C2=A0 =C2=A0| 31 ++++++++++++++++++++++++++++++
=C2=A0hw/core/sysbus.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++= +
=C2=A02 files changed, 80 insertions(+)

diff --git a/qapi/qdev.json b/qapi/qdev.json
index 2e2de41499..4830e87a90 100644
--- a/qapi/qdev.json
+++ b/qapi/qdev.json
@@ -160,3 +160,34 @@
=C2=A0##
=C2=A0{ 'event': 'DEVICE_UNPLUG_GUEST_ERROR',
=C2=A0 =C2=A0'data': { '*device': 'str', 'path&= #39;: 'str' } }
+
+##
+# @sysbus-mmio-map:
+#
+# Map a sysbus device mmio onto the main system bus.
+#
+# @device: the device's QOM path
+#
+# @mmio: The mmio number to be mapped (defaults to 0).
+#
+# @addr: The base address for the mapping.
+#
+# @priority: The priority of the mapping (defaults to 0).
+#
+# Features:
+# @unstable: Command is meant to map sysbus devices
+#=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 while in preconfig mode.
+#
+# Since: 7.0
+#
+# Returns: Nothing on success
+#
+##
+
+{ 'command': 'sysbus-mmio-map',
+=C2=A0 'data': { 'device': 'str',
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 '*mmio': 'uint8'= ,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 'addr': 'uint64'= ,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 '*priority': 'int32&= #39; },
+=C2=A0 'features': ['unstable'],
+=C2=A0 'allow-preconfig' : true }
diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index 05c1da3d31..df1f1f43a5 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -23,6 +23,7 @@
=C2=A0#include "hw/sysbus.h"
=C2=A0#include "monitor/monitor.h"
=C2=A0#include "exec/address-spaces.h"
+#include "qapi/qapi-commands-qdev.h"

=C2=A0static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int inde= nt);
=C2=A0static char *sysbus_get_fw_dev_path(DeviceState *dev);
@@ -154,6 +155,54 @@ static void sysbus_mmio_map_common(SysBusDevice *dev, = int n, hwaddr addr,
=C2=A0 =C2=A0 =C2=A0}
=C2=A0}

+void qmp_sysbus_mmio_map(const char *device,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0bool has_mmio, uint8_t mmio,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0uint64_t addr,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0bool has_priority, int32_t priority,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0Error **errp)
+{
+=C2=A0 =C2=A0 Object *obj =3D object_resolve_path_type(device, TYPE_SYS_BU= S_DEVICE, NULL);
+=C2=A0 =C2=A0 SysBusDevice *dev;
+
+=C2=A0 =C2=A0 if (phase_get() !=3D PHASE_MACHINE_INITIALIZED) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "The command is permitte= d only when "
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0"the machine is in initialized phase");
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 if (obj =3D=3D NULL) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "Device '%s' not= found", device);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 dev =3D SYS_BUS_DEVICE(obj);
+
+=C2=A0 =C2=A0 if (!has_mmio) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 mmio =3D 0;
+=C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 if (!has_priority) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 priority =3D 0;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 if (mmio >=3D dev->num_mmio) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "MMIO index '%u'= does not exist in '%s'",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0mmio,= device);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 if (dev->mmio[mmio].addr !=3D (hwaddr)-1) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "MMIO index '%u'= is already mapped", mmio);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 if (!memory_region_try_add_subregion(get_system_memory(), ad= dr,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0de= v->mmio[mmio].memory, priority,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0er= rp)) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 dev->mmio[mmio].addr =3D addr;
+}
+
=C2=A0void sysbus_mmio_unmap(SysBusDevice *dev, int n)
=C2=A0{
=C2=A0 =C2=A0 =C2=A0assert(n >=3D 0 && n < dev->num_mmio);=
--
2.35.1


--0000000000008fd26a05dfc789b5--