From nobody Tue Feb 10 15:45:53 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.128.48 as permitted sender) client-ip=209.85.128.48; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wm1-f48.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1616718458; cv=none; d=zohomail.com; s=zohoarc; b=Lwb77xCn1kMk7+LKtcJZf0tkZgZydAnPRES7b2j47Efrp1CKffuxe6DY7AIeCRMsNigXn1Mr1TvwtrYJPMhq3lV/jFF53Rdbhy+I8jlFdL+NahbQHy+pmH1npm0bzrXThxRJNYrUgojr4A12y7B8Xc4FPoGZzgEkdi0RzZx8ErY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616718458; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Sender:Subject:To; bh=saTn6NarVlEELQgHGdwR/wrWO06QjsMUeOIlGAoNQ9M=; b=ShjhDcVDxzOmSxtCbtcJ8RdhbO7u+XO8O1xCkU1zICxVR2GcaWGK+6U4Iq9h3GCAuy9yD4DaOF0/EuqhhhLxl4Ggmwwa3uMxii7+Z0NeOlLzsMUcPVZZgCVbyI8Tma1KnvyCbSl1uTQh08azTk/mcxmpc1YeiLsnW5z+incZCiE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by mx.zohomail.com with SMTPS id 1616718458582746.3276208448418; Thu, 25 Mar 2021 17:27:38 -0700 (PDT) Received: by mail-wm1-f48.google.com with SMTP id f22-20020a7bc8d60000b029010c024a1407so4034451wml.2 for ; Thu, 25 Mar 2021 17:27:37 -0700 (PDT) Return-Path: Return-Path: Received: from localhost.localdomain (17.red-88-21-201.staticip.rima-tde.net. [88.21.201.17]) by smtp.gmail.com with ESMTPSA id q207sm8402159wme.36.2021.03.25.17.27.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 17:27:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=saTn6NarVlEELQgHGdwR/wrWO06QjsMUeOIlGAoNQ9M=; b=hqQ6yMllaA4CLNt9rLG19D+p6ABbiimsAVYZ6gof5/26P4m3A+TrjSLrjhb8E5CpaD k1bjspKsRk4KD+NAw6s2oR1SS849pXkypeM6PRHYeNai8tvs4RdA6nbYVHx3FnhoaWka TnEktTPPI2LDuHuK2WPh+fta7zi84IFZ42Sf7JDhDKXLZxYiDc8BdIEVuqNKBQVcCYwJ 2/IIB6cji/VJHYEabwdj6KE6oZVEEhJ473jpjNzIs5k1/UOxrXyZUXEjd0SYCv1bBGnG OPmAN+++431XhIx16jo99xp2O/rVdQiDHxgsXObnKA9Y4iMar5rXbBoOqRKG9MCPgAPm w3vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=saTn6NarVlEELQgHGdwR/wrWO06QjsMUeOIlGAoNQ9M=; b=at+iF8g+U60VW0tDCLk9bC5Nu2k2ijQdIuSV4rjdKfQTgp+7qogCt0Tv6YXeK4rgiR /lBtjr6Bz7KNTMFlD1z0grscSBs/dIujIWIkyT1fYGDP/74Kt6l0SnLap34IvTKVuz78 QSqhnrTTBHpw00jtoYkByF8aWgWUNWtqs5SjK8JBnkC8hPkBzXEGHHSvTY+jiBnDaR8z STQ0AqwomX3hoepTox64XgCKF8XCOCXgrIIUU6QiUo0J42MW2xblrEFcuuJ3XnBJiJJ+ oHM74/dSdhMwS2sP1obgBD3+ri/+nGitOP6ciGZKs3pV9Ip/NKT60KUNkssonmo73LuO cNLg== X-Gm-Message-State: AOAM533YywQ9M7zLI+6pEW6hEIeIAALj5rb1+Dsux9pCKDuqxysQf1ff biwtoKZ3yEKi87OSTflfWTk= X-Google-Smtp-Source: ABdhPJxdvBd4hUR6euoVap15xJPgJm/JSU6i+IUtGx9IiifYu3ssymwlxnQzlB8aMbmzOqublIVEng== X-Received: by 2002:a1c:bc82:: with SMTP id m124mr10383440wmf.118.1616718456724; Thu, 25 Mar 2021 17:27:36 -0700 (PDT) Sender: =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Laurent Vivier , Mark Cave-Ayland , Peter Maydell , Richard Henderson , Alistair Francis , "Edgar E. Iglesias" , qemu-ppc@nongnu.org, qemu-arm@nongnu.org, David Gibson Subject: [RFC PATCH-for-6.1 01/10] hw/misc: Add device to help managing aliased memory regions Date: Fri, 26 Mar 2021 01:27:19 +0100 Message-Id: <20210326002728.1069834-2-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210326002728.1069834-1-f4bug@amsat.org> References: <20210326002728.1069834-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) // TODO explain here how buses work? when some address lines are // not bound we get memory aliasing, high addresses are masked. // etc... Add a helper to manage this use case easily. For example a having @span_size =3D @region_size / 4 we get such mapping: ^-----------^ | | | | | +-------+ | +---------+ <--+ | | +---------+ | | | | | | | | +-----------> | alias#3 | | | | | | | | | | | +---------+ | | | | +---------+ | | | | | | | | | | +-------> | alias#2 | | | | | | | | |region | container | | | +---------+ | size | | | | +---------+ | | | | | | | | | | | | +----> | alias#1 | | | | | | | | | | | | | | | +---------+ <--+ | | | +-+---+--+--+ +---------+ | | | | | | | | |span | | | | subregion +-> | alias#0 | |size | offset | | | | | | | | +----> | +-------+ | +-----------+ +---------+ <--+ <--+ | | | | | | | | | | | | | | | | ^-----------^ Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- include/hw/misc/aliased_region.h | 87 ++++++++++++++++++++ hw/misc/aliased_region.c | 133 +++++++++++++++++++++++++++++++ MAINTAINERS | 6 ++ hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + 5 files changed, 230 insertions(+) create mode 100644 include/hw/misc/aliased_region.h create mode 100644 hw/misc/aliased_region.c diff --git a/include/hw/misc/aliased_region.h b/include/hw/misc/aliased_reg= ion.h new file mode 100644 index 00000000000..0ce0d5d1cef --- /dev/null +++ b/include/hw/misc/aliased_region.h @@ -0,0 +1,87 @@ +/* + * Aliased memory regions + * + * Copyright (c) 2018 Philippe Mathieu-Daud=C3=A9 + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_MISC_ALIASED_REGION_H +#define HW_MISC_ALIASED_REGION_H + +#include "exec/memory.h" +#include "hw/sysbus.h" + +#define TYPE_ALIASED_REGION "aliased-memory-region" +OBJECT_DECLARE_SIMPLE_TYPE(AliasedRegionState, ALIASED_REGION) + +struct AliasedRegionState { + /*< private >*/ + SysBusDevice parent_obj; + + /*< public >*/ + MemoryRegion container; + uint64_t region_size; + uint64_t span_size; + MemoryRegion *mr; + + struct { + size_t count; + MemoryRegion *alias; + } mem; +}; + +/** + * memory_region_add_subregion_aliased: + * @container: the #MemoryRegion to contain the aliased subregions. + * @offset: the offset relative to @container where the aliased subregion + * are added. + * @region_size: size of the region containing the aliased subregions. + * @subregion: the subregion to be aliased. + * @span_size: size between each aliased subregion + * + * This utility function creates and maps an instance of aliased-memory-re= gion, + * which is a dummy device of a single region which simply contains multip= le + * aliases of the provided @subregion, spanned over the @region_size every + * @span_size. The device is mapped at @offset within @container. + * + * For example a having @span_size =3D @region_size / 4 we get such mappin= g: + * + * +-----------+ + * | | + * | | + * | +-------+ | +---------+ <--+ + * | | +---------+ | + * | | | | | + * | | +-----------> | alias#3 | | + * | | | | | | + * | | | +---------+ | + * | | | +---------+ | + * | | | | | | + * | | | +-------> | alias#2 | | + * | | | | | | |re= gion + * | container | | | +---------+ | s= ize + * | | | | +---------+ | + * | | | | | | | + * | | | | +----> | alias#1 | | + * | | | | | | | | + * | | | | | +---------+ <--+ | + * | | +-+---+--+--+ +---------+ | | + * | | | | | | |span | + * | | | subregion +-> | alias#0 | |size | + * offset | | | | | | | | + * +----> | +-------+ | +-----------+ +---------+ <--+ <--+ + * | | | + * | | | + * | | | + * | | | + * | | | + * + +-----------+ + */ +void memory_region_add_subregion_aliased(MemoryRegion *container, + hwaddr offset, + uint64_t region_size, + MemoryRegion *subregion, + uint64_t span_size); + +#endif diff --git a/hw/misc/aliased_region.c b/hw/misc/aliased_region.c new file mode 100644 index 00000000000..8fcc63f2648 --- /dev/null +++ b/hw/misc/aliased_region.c @@ -0,0 +1,133 @@ +/* + * Aliased memory regions + * + * Copyright (c) 2018 Philippe Mathieu-Daud=C3=A9 + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/cutils.h" +#include "qapi/error.h" +#include "hw/hw.h" +#include "hw/sysbus.h" +#include "hw/misc/aliased_region.h" +#include "hw/qdev-properties.h" + +static void aliased_mem_realize(AliasedRegionState *s, const char *mr_name) +{ + uint64_t subregion_size; + int subregion_bits; + + memory_region_init(&s->container, OBJECT(s), mr_name, s->region_size); + + subregion_bits =3D 64 - clz64(s->span_size - 1); + s->mem.count =3D s->region_size >> subregion_bits; + assert(s->mem.count > 1); + subregion_size =3D 1ULL << subregion_bits; + + s->mem.alias =3D g_new(MemoryRegion, s->mem.count); + for (size_t i =3D 0; i < s->mem.count; i++) { + g_autofree char *name =3D g_strdup_printf("%s [#%zu/%zu]", + memory_region_name(s->mr), + i, s->mem.count); + memory_region_init_alias(&s->mem.alias[i], OBJECT(s), name, + s->mr, 0, s->span_size); + memory_region_add_subregion(&s->container, i * subregion_size, + &s->mem.alias[i]); + } +} + +static void aliased_mr_realize(DeviceState *dev, Error **errp) +{ + AliasedRegionState *s =3D ALIASED_REGION(dev); + g_autofree char *name =3D NULL, *span =3D NULL; + + if (s->region_size =3D=3D 0) { + error_setg(errp, "property 'region-size' not specified or zero"); + return; + } + + if (s->mr =3D=3D NULL) { + error_setg(errp, "property 'iomem' not specified"); + return; + } + + if (!s->span_size) { + s->span_size =3D pow2ceil(memory_region_size(s->mr)); + } else if (!is_power_of_2(s->span_size)) { + error_setg(errp, "property 'span-size' must be a power of 2"); + return; + } + + span =3D size_to_str(s->span_size); + name =3D g_strdup_printf("masked %s [span of %s]", + memory_region_name(s->mr), span); + aliased_mem_realize(s, name); + sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->container); +} + +static void aliased_mr_unrealize(DeviceState *dev) +{ + AliasedRegionState *s =3D ALIASED_REGION(dev); + + g_free(s->mem.alias); +} + +static Property aliased_mr_properties[] =3D { + DEFINE_PROP_UINT64("region-size", AliasedRegionState, region_size, 0), + DEFINE_PROP_UINT64("span-size", AliasedRegionState, span_size, 0), + DEFINE_PROP_LINK("iomem", AliasedRegionState, mr, + TYPE_MEMORY_REGION, MemoryRegion *), + DEFINE_PROP_END_OF_LIST(), +}; + +static void aliased_mr_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->realize =3D aliased_mr_realize; + dc->unrealize =3D aliased_mr_unrealize; + /* Reason: needs to be wired up to work */ + dc->user_creatable =3D false; + device_class_set_props(dc, aliased_mr_properties); +} + +static const TypeInfo aliased_mr_info =3D { + .name =3D TYPE_ALIASED_REGION, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(AliasedRegionState), + .class_init =3D aliased_mr_class_init, +}; + +static void aliased_mr_register_types(void) +{ + type_register_static(&aliased_mr_info); +} + +type_init(aliased_mr_register_types) + +void memory_region_add_subregion_aliased(MemoryRegion *container, + hwaddr offset, + uint64_t region_size, + MemoryRegion *subregion, + uint64_t span_size) +{ + DeviceState *dev; + + if (!region_size) { + region_size =3D pow2ceil(memory_region_size(container)); + } else { + assert(region_size <=3D memory_region_size(container)); + } + + dev =3D qdev_new(TYPE_ALIASED_REGION); + qdev_prop_set_uint64(dev, "region-size", region_size); + qdev_prop_set_uint64(dev, "span-size", span_size); + object_property_set_link(OBJECT(dev), "iomem", OBJECT(subregion), + &error_abort); + sysbus_realize(SYS_BUS_DEVICE(dev), &error_abort); + + memory_region_add_subregion(container, offset, + sysbus_mmio_get_region(SYS_BUS_DEVICE(dev)= , 0)); +} diff --git a/MAINTAINERS b/MAINTAINERS index 554be84b321..f82ffd50a91 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2097,6 +2097,12 @@ S: Maintained F: include/hw/misc/empty_slot.h F: hw/misc/empty_slot.c =20 +Aliased memory region +M: Philippe Mathieu-Daud=C3=A9 +S: Maintained +F: include/hw/misc/aliased_region.h +F: hw/misc/aliased_region.c + Standard VGA M: Gerd Hoffmann S: Maintained diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index c71ed258204..ca51b99989e 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -151,6 +151,9 @@ config AUX config UNIMP bool =20 +config ALIASED_REGION + bool + config LED bool =20 diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 21034dc60a8..e65541b835f 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -12,6 +12,7 @@ softmmu_ss.add(when: 'CONFIG_EMC141X', if_true: files('emc141x.c')) softmmu_ss.add(when: 'CONFIG_UNIMP', if_true: files('unimp.c')) softmmu_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c')) +softmmu_ss.add(when: 'CONFIG_ALIASED_REGION', if_true: files('aliased_regi= on.c')) softmmu_ss.add(when: 'CONFIG_LED', if_true: files('led.c')) softmmu_ss.add(when: 'CONFIG_PVPANIC_COMMON', if_true: files('pvpanic.c')) =20 --=20 2.26.2