Skip to contentSkip to conversation

ppc/prep: add IBM RS/6000 43p machine

PATCH In review Hervé Poussineau 6 participants 24 messages May 2, 2013 – May 9, 2013

lore
Share & tools
lore
#0 lore raw

cover ppc/prep: add IBM RS/6000 43p machine

Hi,

This patchset adds an emulation of the IBM RS/6000 43p, also known as 7248. I've fixed comments of Blue Swirl in this series, so I'm sending it as PATCH instead of RFC.

Andreas did some preliminary patches for OpenBIOS to support PReP machines, available at
git://repo.or.cz/openbios/afaerber.git branch prep.
43p emulation has been tested with this firmware, and also with the official firmware, named P93H1904.IMG.

Command line may look like
qemu-system-ppc -M 43p -readconfig ibm_43p.cfg

Changes RFC v2 -> PATCH v1:
- rebased on master
- add a Nvram interface on m48t59
- kept ISA io_base in patch 3
- renamed QOM parent objects as parent_obj

Changes RFC v1 -> RFC v2:
- rebased on master
- works with OpenBIOS
- rework on m48t59 patches
- remove rs6000 debug port emulation (not strictly required)
- checkpatch.pl doesn't complain anymore

Known bugs/missing parts:
- incomplete MPC105 (PCI host bridge) emulation (memory controller)
- no video card (should be a S3)
- QEMU can't change endianness at runtime

OpenBIOS status:
- detects CPU and memory, then freezes with a "Dictionary space overflow"

Official firmware status:
- can boot from a IDE hard disk containing a PReP boot partition
- can't boot from IDE cdroms (overlapping commands not supported by QEMU IDE emulation)
- can't boot from floppies (READ commands are issued, but it seems completion is ignored)
- can't boot from network (detects the netcard, but doesn't use it)
- needs a hack to m48t59 device to work

Regards,

Hervé

Hervé Poussineau (7):
pci: add MPC105 PCI host bridge emulation
qom: handle registration of new types when initializing the first
ones
m48t59: move ISA ports/memory regions registration to QOM constructor
m48t59: register a QOM type for each nvram type we support
m48t59: add a Nvram interface
prep: add IBM RS/6000 7248 (43p) machine emulation
prep: QOM'ify System I/O

 default-configs/ppc-softmmu.mak |    1 +
 docs/ibm_43p.cfg                |   43 ++++
 hw/pci-host/Makefile.objs       |    1 +
 hw/pci-host/mpc105.c            |  488 +++++++++++++++++++++++++++++++++++++++
 hw/ppc/Makefile.objs            |    1 +
 hw/ppc/prep.c                   |  213 +++++++++++++++++
 hw/ppc/prep_systemio.c          |  298 ++++++++++++++++++++++++
 hw/timer/m48t59.c               |  304 ++++++++++++++++++------
 include/hw/pci/pci_ids.h        |    1 +
 include/hw/timer/m48t59.h       |   24 ++
 qom/object.c                    |   45 +++-
 trace-events                    |   11 +
 12 files changed, 1349 insertions(+), 81 deletions(-)
 create mode 100644 docs/ibm_43p.cfg
 create mode 100644 hw/pci-host/mpc105.c
 create mode 100644 hw/ppc/prep_systemio.c
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 1/7 pci: add MPC105 PCI host bridge emulation

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

---
 default-configs/ppc-softmmu.mak |    1 +
 hw/pci-host/Makefile.objs       |    1 +
 hw/pci-host/mpc105.c            |  488 +++++++++++++++++++++++++++++++++++++++
 include/hw/pci/pci_ids.h        |    1 +
 trace-events                    |    7 +
 5 files changed, 498 insertions(+)
 create mode 100644 hw/pci-host/mpc105.c
default-configs/ppc-softmmu.mak +1 -0
index cc3587f..f79b058 100644
@@ -28,6 +28,7 @@ CONFIG_MAC_NVRAM=y
 CONFIG_MAC_DBDMA=y
 CONFIG_HEATHROW_PIC=y
 CONFIG_GRACKLE_PCI=y
+CONFIG_MPC105_PCI=y
 CONFIG_UNIN_PCI=y
 CONFIG_DEC_PCI=y
 CONFIG_PPCE500_PCI=y
hw/pci-host/Makefile.objs +1 -0
index 909e702..ec4427b 100644
@@ -3,6 +3,7 @@ common-obj-y += pam.o
 # PPC devices
 common-obj-$(CONFIG_PREP_PCI) += prep.o
 common-obj-$(CONFIG_GRACKLE_PCI) += grackle.o
+common-obj-$(CONFIG_MPC105_PCI) += mpc105.o
 # NewWorld PowerMac
 common-obj-$(CONFIG_UNIN_PCI) += uninorth.o
 common-obj-$(CONFIG_DEC_PCI) += dec.o
hw/pci-host/mpc105.c new file +488 -0

488 lines not on this page · raw message

include/hw/pci/pci_ids.h +1 -0
index d8dc2f1..933b987 100644
@@ -69,6 +69,7 @@
 #define PCI_VENDOR_ID_TI                 0x104c
 
 #define PCI_VENDOR_ID_MOTOROLA           0x1057
+#define PCI_DEVICE_ID_MOTOROLA_MPC105    0x0001
 #define PCI_DEVICE_ID_MOTOROLA_MPC106    0x0002
 #define PCI_DEVICE_ID_MOTOROLA_RAVEN     0x4801
 
trace-events +7 -0
index 55e80be..1970b5c 100644
@@ -766,6 +766,13 @@ pc87312_info_ide(uint32_t base) "base 0x%x"
 pc87312_info_parallel(uint32_t base, uint32_t irq) "base 0x%x, irq %u"
 pc87312_info_serial(int n, uint32_t base, uint32_t irq) "id=%d, base 0x%x, irq %u"
 
+# hw/pci-host/mpc105.c
+mpc105_unassigned_mem_read(uint64_t addr) "Unassigned mem read %" PRIx64
+mpc105_unassigned_mem_write(uint64_t addr, uint64_t val) "Unassigned mem write %" PRIx64 " = 0x%" PRIx64
+mpc105_simm_enable(int id, uint32_t start, uint32_t end) "SIMM #%d 0x%08x-0x%08x"
+mpc105_simm_disable(int id) "SIMM #%d disabled"
+mpc105_simm_size(int id, uint32_t size) "SIMM #%d is %u MB"
+
 # hw/scsi/vmw_pvscsi.c
 pvscsi_ring_init_data(uint32_t txr_len_log2, uint32_t rxr_len_log2) "TX/RX rings logarithms set to %d/%d"
 pvscsi_ring_init_msg(uint32_t len_log2) "MSG ring logarithm set to %d"
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau <[email protected]>:

Quoted 9 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Quoted 54 lines

---

default-configs/ppc-softmmu.mak |    1 +
hw/pci-host/Makefile.objs       |    1 +
hw/pci-host/mpc105.c            |  488 +++++++++++++++++++++++++++++++++++++++
include/hw/pci/pci_ids.h        |    1 +
trace-events                    |    7 +
5 files changed, 498 insertions(+)

create mode 100644 hw/pci-host/mpc105.c

default-configs/ppc-softmmu.mak +0 -0
index cc3587f..f79b058 100644
@@ -28,6 +28,7 @@ CONFIG_MAC_NVRAM=y

CONFIG_MAC_DBDMA=y
CONFIG_HEATHROW_PIC=y
CONFIG_GRACKLE_PCI=y
+CONFIG_MPC105_PCI=y
CONFIG_UNIN_PCI=y
CONFIG_DEC_PCI=y
CONFIG_PPCE500_PCI=y

hw/pci-host/Makefile.objs +0 -0
index 909e702..ec4427b 100644
@@ -3,6 +3,7 @@ common-obj-y += pam.o
 # PPC devices

common-obj-$(CONFIG_PREP_PCI) += prep.o
common-obj-$(CONFIG_GRACKLE_PCI) += grackle.o
+common-obj-$(CONFIG_MPC105_PCI) += mpc105.o
# NewWorld PowerMac
common-obj-$(CONFIG_UNIN_PCI) += uninorth.o
common-obj-$(CONFIG_DEC_PCI) += dec.o

hw/pci-host/mpc105.c new file +13 -0
new file mode 100644
index 0000000..8e4cc95
@@ -0,0 +1,488 @@
+/*
+ * QEMU MPC-105 Eagle PCI host
+ *
+ * Copyright (c) 2013 Hervé Poussineau
+ *
+ * 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) version 3 or 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

hw/pci-host/mpc105.c line 23

Quoted 10 lines
hw/pci-host/mpc105.c new file +10 -0
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/pci/pci_host.h"
+#include "hw/i386/pc.h"

That include sounds odd :).

Quoted 287 lines
hw/pci-host/mpc105.c new file +287 -0
+#include "hw/loader.h"
+#include "exec/address-spaces.h"
+#include "elf.h"
+#include "trace.h"
+
+#define TYPE_MPC105_PCI_HOST_BRIDGE "mpc105-pcihost"
+#define MPC105_PCI_HOST_BRIDGE(obj) \
+    OBJECT_CHECK(Mpc105HostState, (obj), TYPE_MPC105_PCI_HOST_BRIDGE)
+
+#define TYPE_MPC105 "mpc105"
+#define MPC105(obj) \
+    OBJECT_CHECK(Mpc105State, (obj), TYPE_MPC105)
+
+#define MEM_STA_03     0x0080
+#define MEM_STA_47     0x0084
+#define EXT_MEM_STA_03 0x0088
+#define EXT_MEM_STA_47 0x008c
+#define MEM_END_03     0x0090
+#define MEM_END_47     0x0094
+#define EXT_MEM_END_03 0x0098
+#define EXT_MEM_END_47 0x009c
+#define MEM_BANK_EN    0x00a0
+#define PROC_CFG_A8    0x00a8
+#define PROC_CFG_AC    0x00ac
+#define ALT_OSV_1      0x00ba
+#define ERR_EN_REG1    0x00c0
+#define ERR_DR1        0x00c1
+#define ERR_EN_REG2    0x00c4
+#define MEM_CFG_1      0x00f0
+#define MEM_CFG_2      0x00f4
+#define MEM_CFG_4      0x00fc
+
+#define MEM_CFG_1_MEMGO (1 << 19)
+
+#define BIOS_SIZE (1024 * 1024)
+
+typedef struct Mpc105State {
+    PCIDevice parent_obj;
+    uint32_t ram_size;
+    uint32_t elf_machine;
+    uint32_t x_auto_conf;
+    char *bios_name;
+    MemoryRegion bios;
+    MemoryRegion simm[8];
+    bool use_sizer[8];
+    /* use a sizer to allow access to only part of a simm */
+    MemoryRegion sizer[8];
+} Mpc105State;
+
+static uint64_t mpc105_unassigned_read(void *opaque, hwaddr addr,
+                                       unsigned int size)
+{
+    trace_mpc105_unassigned_mem_read(addr);
+    return 0;
+}
+
+static void mpc105_unassigned_write(void *opaque, hwaddr addr, uint64_t data,
+                                    unsigned int size)
+{
+    trace_mpc105_unassigned_mem_write(addr, data);
+}
+
+static const MemoryRegionOps mpc105_unassigned_ops = {
+    .read = mpc105_unassigned_read,
+    .write = mpc105_unassigned_write,
+};
+
+static void mpc105_update_memory_mappings(Mpc105State *s)
+{
+    uint32_t start_address, end_address;
+    uint32_t start, ext_start, end, ext_end;
+    uint32_t cfg1;
+    uint64_t simm_size;
+    uint8_t *pci_conf;
+    uint8_t en;
+    bool enabled;
+    int i;
+
+    pci_conf = PCI_DEVICE(s)->config;
+    cfg1 = pci_get_long(pci_conf + MEM_CFG_1);
+
+    memory_region_transaction_begin();
+    if (cfg1 & MEM_CFG_1_MEMGO) {
+        en = pci_get_byte(pci_conf + MEM_BANK_EN);
+    } else {
+        en = 0;
+    }
+
+    for (i = 0; i < 8; i++) {
+        enabled = (en & (1 << i));
+
+        start = pci_get_byte(pci_conf + MEM_STA_03 + i);
+        ext_start = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & 0x3;
+        end = pci_get_byte(pci_conf + MEM_END_03 + i);
+        ext_end = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & 0x3;
+        start_address = (ext_start << 28) | (start << 20);
+        end_address = (ext_end << 28) | (end << 20) | 0xfffff;
+
+        enabled &= start_address < end_address;
+
+        if (enabled) {
+            trace_mpc105_simm_enable(i, start_address, end_address + 1);
+        } else {
+            trace_mpc105_simm_disable(i);
+        }
+
+        simm_size = memory_region_size(&s->simm[i]);
+        if (simm_size == 0) {
+            continue;
+        }
+
+        /* Clean links between system memory, sizer and simm */
+        if (s->use_sizer[i]) {
+            memory_region_del_subregion(get_system_memory(), &s->sizer[i]);
+            memory_region_del_subregion(&s->sizer[i], &s->simm[i]);
+            s->use_sizer[i] = false;
+        } else {
+            memory_region_del_subregion(get_system_memory(), &s->simm[i]);
+        }
+
+        /* Recreate links compatible with new memory layout */
+        if (enabled && end_address - start_address + 1 < simm_size) {
+            memory_region_init_io(&s->sizer[i], &mpc105_unassigned_ops,
+                                  s, memory_region_name(&s->sizer[i]),
+                                  end_address - start_address + 1);
+            memory_region_add_subregion(&s->sizer[i], 0, &s->simm[i]);
+            memory_region_add_subregion(get_system_memory(), start_address,
+                                        &s->sizer[i]);
+            s->use_sizer[i] = true;
+        } else {
+            memory_region_add_subregion(get_system_memory(), start_address,
+                                        &s->simm[i]);
+        }
+        memory_region_set_enabled(&s->simm[i], enabled);
+    }
+    memory_region_transaction_commit();
+}
+
+static void mpc105_write_config(PCIDevice *dev, uint32_t addr, uint32_t val,
+                                int l)
+{
+    Mpc105State *s = MPC105(dev);
+
+    pci_default_write_config(dev, addr, val, l);
+    if ((addr >= MEM_STA_03 && addr <= MEM_BANK_EN) || addr == MEM_CFG_1) {
+        mpc105_update_memory_mappings(s);
+    }
+}
+
+static void mpc105_reset(Mpc105State *s)
+{
+    PCIDevice *pci = PCI_DEVICE(s);
+    uint8_t *pci_conf;
+    int id;
+
+    pci_conf = pci->config;
+
+    memset(pci_conf + PCI_CONFIG_HEADER_SIZE, 0,
+           PCI_CONFIG_SPACE_SIZE - PCI_CONFIG_HEADER_SIZE);
+    pci_conf[PCI_COMMAND] = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
+    pci_conf[PCI_STATUS] = PCI_STATUS_FAST_BACK;
+    pci_set_long(pci_conf + PROC_CFG_A8, 0xff000010);
+    pci_set_long(pci_conf + PROC_CFG_AC, 0x000c060c);
+    pci_set_byte(pci_conf + ALT_OSV_1, 0x04);
+    pci_set_byte(pci_conf + ERR_EN_REG1, 0x01);
+    pci_set_long(pci_conf + MEM_CFG_1, 0xff020000);
+    pci_set_long(pci_conf + MEM_CFG_2, 0x00000003);
+    pci_set_long(pci_conf + MEM_CFG_4, 0x00100000);
+
+    memset(pci->wmask + PCI_CONFIG_HEADER_SIZE, 0,
+           MEM_CFG_1 - PCI_CONFIG_HEADER_SIZE);
+    memset(pci->wmask + 0x70, 0xff, 2);
+    memset(pci->wmask + MEM_STA_03, 0xff, MEM_BANK_EN - MEM_STA_03 + 1);
+    memset(pci->wmask + PROC_CFG_A8, 0xff, 8);
+    pci_set_word(pci->wmask + ALT_OSV_1, 0xffff);
+    pci_set_byte(pci->wmask + ERR_EN_REG1, 0xff);
+    pci_set_byte(pci->w1cmask + ERR_DR1, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc3, 0xff);
+    pci_set_byte(pci->wmask + ERR_EN_REG2, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc5, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc7, 0xff);
+
+    for (id = 0; id < 8; ++id) {
+        memory_region_set_enabled(&s->simm[id], false);
+    }
+
+    if (s->x_auto_conf) {
+        /* enable all memory banks, starting from address 0 */
+        uint32_t start_address = 0, end_address = 0;
+        uint8_t ext_start, ext_end, enabled = 0;
+        int i;
+        for (i = 0; i < 8; i++) {
+            if (!memory_region_size(&s->simm[i])) {
+                continue;
+            }
+            end_address += memory_region_size(&s->simm[i]);
+            ext_start = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & ~0x3;
+            ext_end = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & ~0x3;
+            ext_start |= (start_address >> 28) & 0x3;
+            ext_end |= ((end_address - 1) >> 28) & 0x3;
+            pci_set_byte(pci_conf + MEM_STA_03 + i, start_address >> 20);
+            pci_set_byte(pci_conf + EXT_MEM_STA_03 + i, ext_start);
+            pci_set_byte(pci_conf + MEM_END_03 + i, (end_address - 1) >> 20);
+            pci_set_byte(pci_conf + EXT_MEM_END_03 + i, ext_end);
+            start_address = end_address;
+            enabled |= 1 << i;
+        }
+        pci_set_byte(pci_conf + MEM_BANK_EN, enabled);
+        pci_long_test_and_set_mask(pci_conf + MEM_CFG_1, MEM_CFG_1_MEMGO);
+        mpc105_update_memory_mappings(s);
+    }
+}
+
+static void qdev_mpc105_reset(DeviceState *dev)
+{
+    Mpc105State *s = MPC105(dev);
+    mpc105_reset(s);
+}
+
+static int mpc105_post_load(void *opaque, int version_id)
+{
+    Mpc105State *s = opaque;
+    mpc105_update_memory_mappings(s);
+    return 0;
+}
+
+static const VMStateDescription vmstate_mpc105 = {
+    .name = "mpc105",
+    .version_id = 1,
+    .minimum_version_id = 0,
+    .minimum_version_id_old = 1,
+    .post_load = mpc105_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_PCI_DEVICE(parent_obj, Mpc105State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static uint64_t mpc105_intack_read(void *opaque, hwaddr addr,
+                                   unsigned int size)
+{
+    return pic_read_irq(isa_pic);
+}
+
+static const MemoryRegionOps mpc105_intack_ops = {
+    .read = mpc105_intack_read,
+    .valid = {
+        .max_access_size = 1,
+    },
+};
+
+static int mpc105_initfn(PCIDevice *dev)
+{
+    Mpc105State *s = MPC105(dev);
+    char *filename;
+    int bios_size = -1;
+    int i = 0;
+    uint32_t simm_size[8] = { 0 };
+
+    unsigned int ram_size = s->ram_size / (1024 * 1024);
+    while (i < 8) {
+        int idx = qemu_fls(ram_size);
+        if (idx < 5) {
+            /* Need at least 16 Mb for a slot */
+            break;
+        } else if (idx >= 8) {
+            /* Limit to 128 Mb by slot (at max) */
+            idx = 8;
+        }
+        simm_size[i] = 1 << (idx - 1);
+        ram_size -= simm_size[i];
+        i++;
+    }
+
+    for (i = 0; i < 8; i++) {
+        char name[] = "simm.?";
+        name[5] = i + '0';
+        if (simm_size[i]) {
+            trace_mpc105_simm_size(i, simm_size[i]);
+            memory_region_init_ram(&s->simm[i], name,
+                                   simm_size[i] * 1024 * 1024);
+            vmstate_register_ram_global(&s->simm[i]);
+        } else {
+            memory_region_init(&s->simm[i], name, 0);
+        }
+        memory_region_init(&s->sizer[i], "sizer", 0);
+        memory_region_add_subregion_overlap(get_system_memory(), 0,

hw/pci-host/mpc105.c line 320

Quoted 10 lines
hw/pci-host/mpc105.c new file +10 -0
+                                            &s->simm[i], i);
+        memory_region_set_enabled(&s->simm[i], false);
+    }
+
+    memory_region_init_ram(&s->bios, "bios", BIOS_SIZE);
+    memory_region_set_readonly(&s->bios, true);
+    memory_region_add_subregion(get_system_memory(), (uint32_t)(-BIOS_SIZE),
+                                &s->bios);
+    vmstate_register_ram_global(&s->bios);
+    if (s->bios_name) {

Can't you just reuse the normal -bios logic?

hw/pci-host/mpc105.c line 323

Quoted 3 lines
hw/pci-host/mpc105.c new file +3 -0
+        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
+        if (filename) {
+            if (s->elf_machine != EM_NONE) {

Elf machine?

Alex

Quoted 93 lines
hw/pci-host/mpc105.c new file +93 -0
+                bios_size = load_elf(filename, NULL, NULL, NULL,
+                                     NULL, NULL, 1, s->elf_machine, 0);
+            }
+            if (bios_size < 0) {
+                bios_size = get_image_size(filename);
+                if (bios_size > 0 && bios_size <= BIOS_SIZE) {
+                    hwaddr bios_addr;
+                    bios_size = (bios_size + 0xfff) & ~0xfff;
+                    bios_addr = (uint32_t)(-BIOS_SIZE);
+                    bios_size = load_image_targphys(filename, bios_addr,
+                                                    bios_size);
+                }
+            }
+        }
+        if (bios_size < 0 || bios_size > BIOS_SIZE) {
+            hw_error("qemu: could not load bios image '%s'\n", s->bios_name);
+        }
+        if (filename) {
+            g_free(filename);
+        }
+    }
+
+    return 0;
+}
+
+static void mpc105_class_init(ObjectClass *klass, void *data)
+{
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    k->init = mpc105_initfn;
+    k->vendor_id = PCI_VENDOR_ID_MOTOROLA;
+    k->device_id = PCI_DEVICE_ID_MOTOROLA_MPC105;
+    k->class_id = PCI_CLASS_BRIDGE_HOST;
+    k->config_write = mpc105_write_config;
+    dc->desc = "MPC105 PCI bridge/Memory controller";
+    dc->reset = qdev_mpc105_reset;
+    dc->vmsd = &vmstate_mpc105;
+    dc->no_user = 1;
+}
+
+static TypeInfo mpc105_info = {
+    .name          = TYPE_MPC105,
+    .parent        = TYPE_PCI_DEVICE,
+    .instance_size = sizeof(Mpc105State),
+    .class_init    = mpc105_class_init,
+};
+
+typedef struct Mpc105HostState {
+    PCIHostState host_state;
+    uint32_t ram_size;
+    qemu_irq irq[PCI_NUM_PINS];
+    PCIBus pci_bus;
+    MemoryRegion pci_io;
+    MemoryRegion isa_io;
+    MemoryRegion pci_intack;
+    MemoryRegion pci_memory;
+    MemoryRegion rom;
+    Mpc105State pci_dev;
+} Mpc105HostState;
+
+static void mpc105_set_irq(void *opaque, int irq_num, int level)
+{
+    qemu_irq *pic = opaque;
+
+    qemu_set_irq(pic[irq_num] , level);
+}
+
+static int mpc105_map_irq(PCIDevice *pci_dev, int irq_num)
+{
+    return (irq_num + (pci_dev->devfn >> 3)) & 1;
+}
+
+static void mpc105_pcihost_realizefn(DeviceState *d, Error **errp)
+{
+    SysBusDevice *dev = SYS_BUS_DEVICE(d);
+    PCIHostState *h = PCI_HOST_BRIDGE(dev);
+    Mpc105HostState *s = MPC105_PCI_HOST_BRIDGE(dev);
+    int i;
+
+    for (i = 0; i < PCI_NUM_PINS; i++) {
+        sysbus_init_irq(dev, &s->irq[i]);
+    }
+
+    pci_bus_irqs(&s->pci_bus, mpc105_set_irq, mpc105_map_irq, s->irq, 4);
+
+    memory_region_init_io(&h->conf_mem, &pci_host_conf_le_ops, s,
+                          "pci-conf-idx", 1);
+    memory_region_add_subregion(get_system_io(), 0xcf8, &h->conf_mem);
+
+    memory_region_init_io(&h->data_mem, &pci_host_data_le_ops, s,
+                          "pci-conf-data", 4);
+
#3 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

Quoted 9 lines · Alexander Graf

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau <[email protected]>:

Quoted 6 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Quoted 5 lines
Quoted 3 lines

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Me too, but I'm not confident enough in my capabilities to do it.
Autoconfiguration is only in one place of the code, so I think it can be removed easily once OpenBIOS has this logic.

Quoted 58 lines
Quoted 58 lines

---

default-configs/ppc-softmmu.mak |    1 +
hw/pci-host/Makefile.objs       |    1 +
hw/pci-host/mpc105.c            |  488 +++++++++++++++++++++++++++++++++++++++
include/hw/pci/pci_ids.h        |    1 +
trace-events                    |    7 +
5 files changed, 498 insertions(+)

create mode 100644 hw/pci-host/mpc105.c

default-configs/ppc-softmmu.mak +0 -0
index cc3587f..f79b058 100644
@@ -28,6 +28,7 @@ CONFIG_MAC_NVRAM=y

CONFIG_MAC_DBDMA=y
CONFIG_HEATHROW_PIC=y
CONFIG_GRACKLE_PCI=y
+CONFIG_MPC105_PCI=y
CONFIG_UNIN_PCI=y
CONFIG_DEC_PCI=y
CONFIG_PPCE500_PCI=y

hw/pci-host/Makefile.objs +0 -0
index 909e702..ec4427b 100644
@@ -3,6 +3,7 @@ common-obj-y += pam.o
 # PPC devices

common-obj-$(CONFIG_PREP_PCI) += prep.o
common-obj-$(CONFIG_GRACKLE_PCI) += grackle.o
+common-obj-$(CONFIG_MPC105_PCI) += mpc105.o
# NewWorld PowerMac
common-obj-$(CONFIG_UNIN_PCI) += uninorth.o
common-obj-$(CONFIG_DEC_PCI) += dec.o

hw/pci-host/mpc105.c new file +17 -0
new file mode 100644
index 0000000..8e4cc95
@@ -0,0 +1,488 @@
+/*
+ * QEMU MPC-105 Eagle PCI host
+ *
+ * Copyright (c) 2013 Hervé Poussineau
+ *
+ * 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) version 3 or 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 <http://www.gnu.org/licenses/>.
Quoted 8 lines

hw/pci-host/mpc105.c line 23

Quoted 6 lines
hw/pci-host/mpc105.c new file +6 -0
+ */
+
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/pci/pci_host.h"
+#include "hw/i386/pc.h"

That include sounds odd :).

Sure, but you need to access pic_read_irq() :)
hw/pci-host/prep.c also includes it.

Quoted 292 lines
Quoted 292 lines
hw/pci-host/mpc105.c new file +291 -0
+#include "hw/loader.h"
+#include "exec/address-spaces.h"
+#include "elf.h"
+#include "trace.h"
+
+#define TYPE_MPC105_PCI_HOST_BRIDGE "mpc105-pcihost"
+#define MPC105_PCI_HOST_BRIDGE(obj) \
+    OBJECT_CHECK(Mpc105HostState, (obj), TYPE_MPC105_PCI_HOST_BRIDGE)
+
+#define TYPE_MPC105 "mpc105"
+#define MPC105(obj) \
+    OBJECT_CHECK(Mpc105State, (obj), TYPE_MPC105)
+
+#define MEM_STA_03     0x0080
+#define MEM_STA_47     0x0084
+#define EXT_MEM_STA_03 0x0088
+#define EXT_MEM_STA_47 0x008c
+#define MEM_END_03     0x0090
+#define MEM_END_47     0x0094
+#define EXT_MEM_END_03 0x0098
+#define EXT_MEM_END_47 0x009c
+#define MEM_BANK_EN    0x00a0
+#define PROC_CFG_A8    0x00a8
+#define PROC_CFG_AC    0x00ac
+#define ALT_OSV_1      0x00ba
+#define ERR_EN_REG1    0x00c0
+#define ERR_DR1        0x00c1
+#define ERR_EN_REG2    0x00c4
+#define MEM_CFG_1      0x00f0
+#define MEM_CFG_2      0x00f4
+#define MEM_CFG_4      0x00fc
+
+#define MEM_CFG_1_MEMGO (1 << 19)
+
+#define BIOS_SIZE (1024 * 1024)
+
+typedef struct Mpc105State {
+    PCIDevice parent_obj;
+    uint32_t ram_size;
+    uint32_t elf_machine;
+    uint32_t x_auto_conf;
+    char *bios_name;
+    MemoryRegion bios;
+    MemoryRegion simm[8];
+    bool use_sizer[8];
+    /* use a sizer to allow access to only part of a simm */
+    MemoryRegion sizer[8];
+} Mpc105State;
+
+static uint64_t mpc105_unassigned_read(void *opaque, hwaddr addr,
+                                       unsigned int size)
+{
+    trace_mpc105_unassigned_mem_read(addr);
+    return 0;
+}
+
+static void mpc105_unassigned_write(void *opaque, hwaddr addr, uint64_t data,
+                                    unsigned int size)
+{
+    trace_mpc105_unassigned_mem_write(addr, data);
+}
+
+static const MemoryRegionOps mpc105_unassigned_ops = {
+    .read = mpc105_unassigned_read,
+    .write = mpc105_unassigned_write,
+};
+
+static void mpc105_update_memory_mappings(Mpc105State *s)
+{
+    uint32_t start_address, end_address;
+    uint32_t start, ext_start, end, ext_end;
+    uint32_t cfg1;
+    uint64_t simm_size;
+    uint8_t *pci_conf;
+    uint8_t en;
+    bool enabled;
+    int i;
+
+    pci_conf = PCI_DEVICE(s)->config;
+    cfg1 = pci_get_long(pci_conf + MEM_CFG_1);
+
+    memory_region_transaction_begin();
+    if (cfg1 & MEM_CFG_1_MEMGO) {
+        en = pci_get_byte(pci_conf + MEM_BANK_EN);
+    } else {
+        en = 0;
+    }
+
+    for (i = 0; i < 8; i++) {
+        enabled = (en & (1 << i));
+
+        start = pci_get_byte(pci_conf + MEM_STA_03 + i);
+        ext_start = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & 0x3;
+        end = pci_get_byte(pci_conf + MEM_END_03 + i);
+        ext_end = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & 0x3;
+        start_address = (ext_start << 28) | (start << 20);
+        end_address = (ext_end << 28) | (end << 20) | 0xfffff;
+
+        enabled &= start_address < end_address;
+
+        if (enabled) {
+            trace_mpc105_simm_enable(i, start_address, end_address + 1);
+        } else {
+            trace_mpc105_simm_disable(i);
+        }
+
+        simm_size = memory_region_size(&s->simm[i]);
+        if (simm_size == 0) {
+            continue;
+        }
+
+        /* Clean links between system memory, sizer and simm */
+        if (s->use_sizer[i]) {
+            memory_region_del_subregion(get_system_memory(), &s->sizer[i]);
+            memory_region_del_subregion(&s->sizer[i], &s->simm[i]);
+            s->use_sizer[i] = false;
+        } else {
+            memory_region_del_subregion(get_system_memory(), &s->simm[i]);
+        }
+
+        /* Recreate links compatible with new memory layout */
+        if (enabled && end_address - start_address + 1 < simm_size) {
+            memory_region_init_io(&s->sizer[i], &mpc105_unassigned_ops,
+                                  s, memory_region_name(&s->sizer[i]),
+                                  end_address - start_address + 1);
+            memory_region_add_subregion(&s->sizer[i], 0, &s->simm[i]);
+            memory_region_add_subregion(get_system_memory(), start_address,
+                                        &s->sizer[i]);
+            s->use_sizer[i] = true;
+        } else {
+            memory_region_add_subregion(get_system_memory(), start_address,
+                                        &s->simm[i]);
+        }
+        memory_region_set_enabled(&s->simm[i], enabled);
+    }
+    memory_region_transaction_commit();
+}
+
+static void mpc105_write_config(PCIDevice *dev, uint32_t addr, uint32_t val,
+                                int l)
+{
+    Mpc105State *s = MPC105(dev);
+
+    pci_default_write_config(dev, addr, val, l);
+    if ((addr >= MEM_STA_03 && addr <= MEM_BANK_EN) || addr == MEM_CFG_1) {
+        mpc105_update_memory_mappings(s);
+    }
+}
+
+static void mpc105_reset(Mpc105State *s)
+{
+    PCIDevice *pci = PCI_DEVICE(s);
+    uint8_t *pci_conf;
+    int id;
+
+    pci_conf = pci->config;
+
+    memset(pci_conf + PCI_CONFIG_HEADER_SIZE, 0,
+           PCI_CONFIG_SPACE_SIZE - PCI_CONFIG_HEADER_SIZE);
+    pci_conf[PCI_COMMAND] = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
+    pci_conf[PCI_STATUS] = PCI_STATUS_FAST_BACK;
+    pci_set_long(pci_conf + PROC_CFG_A8, 0xff000010);
+    pci_set_long(pci_conf + PROC_CFG_AC, 0x000c060c);
+    pci_set_byte(pci_conf + ALT_OSV_1, 0x04);
+    pci_set_byte(pci_conf + ERR_EN_REG1, 0x01);
+    pci_set_long(pci_conf + MEM_CFG_1, 0xff020000);
+    pci_set_long(pci_conf + MEM_CFG_2, 0x00000003);
+    pci_set_long(pci_conf + MEM_CFG_4, 0x00100000);
+
+    memset(pci->wmask + PCI_CONFIG_HEADER_SIZE, 0,
+           MEM_CFG_1 - PCI_CONFIG_HEADER_SIZE);
+    memset(pci->wmask + 0x70, 0xff, 2);
+    memset(pci->wmask + MEM_STA_03, 0xff, MEM_BANK_EN - MEM_STA_03 + 1);
+    memset(pci->wmask + PROC_CFG_A8, 0xff, 8);
+    pci_set_word(pci->wmask + ALT_OSV_1, 0xffff);
+    pci_set_byte(pci->wmask + ERR_EN_REG1, 0xff);
+    pci_set_byte(pci->w1cmask + ERR_DR1, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc3, 0xff);
+    pci_set_byte(pci->wmask + ERR_EN_REG2, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc5, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc7, 0xff);
+
+    for (id = 0; id < 8; ++id) {
+        memory_region_set_enabled(&s->simm[id], false);
+    }
+
+    if (s->x_auto_conf) {
+        /* enable all memory banks, starting from address 0 */
+        uint32_t start_address = 0, end_address = 0;
+        uint8_t ext_start, ext_end, enabled = 0;
+        int i;
+        for (i = 0; i < 8; i++) {
+            if (!memory_region_size(&s->simm[i])) {
+                continue;
+            }
+            end_address += memory_region_size(&s->simm[i]);
+            ext_start = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & ~0x3;
+            ext_end = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & ~0x3;
+            ext_start |= (start_address >> 28) & 0x3;
+            ext_end |= ((end_address - 1) >> 28) & 0x3;
+            pci_set_byte(pci_conf + MEM_STA_03 + i, start_address >> 20);
+            pci_set_byte(pci_conf + EXT_MEM_STA_03 + i, ext_start);
+            pci_set_byte(pci_conf + MEM_END_03 + i, (end_address - 1) >> 20);
+            pci_set_byte(pci_conf + EXT_MEM_END_03 + i, ext_end);
+            start_address = end_address;
+            enabled |= 1 << i;
+        }
+        pci_set_byte(pci_conf + MEM_BANK_EN, enabled);
+        pci_long_test_and_set_mask(pci_conf + MEM_CFG_1, MEM_CFG_1_MEMGO);
+        mpc105_update_memory_mappings(s);
+    }
+}
+
+static void qdev_mpc105_reset(DeviceState *dev)
+{
+    Mpc105State *s = MPC105(dev);
+    mpc105_reset(s);
+}
+
+static int mpc105_post_load(void *opaque, int version_id)
+{
+    Mpc105State *s = opaque;
+    mpc105_update_memory_mappings(s);
+    return 0;
+}
+
+static const VMStateDescription vmstate_mpc105 = {
+    .name = "mpc105",
+    .version_id = 1,
+    .minimum_version_id = 0,
+    .minimum_version_id_old = 1,
+    .post_load = mpc105_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_PCI_DEVICE(parent_obj, Mpc105State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static uint64_t mpc105_intack_read(void *opaque, hwaddr addr,
+                                   unsigned int size)
+{
+    return pic_read_irq(isa_pic);
+}
+
+static const MemoryRegionOps mpc105_intack_ops = {
+    .read = mpc105_intack_read,
+    .valid = {
+        .max_access_size = 1,
+    },
+};
+
+static int mpc105_initfn(PCIDevice *dev)
+{
+    Mpc105State *s = MPC105(dev);
+    char *filename;
+    int bios_size = -1;
+    int i = 0;
+    uint32_t simm_size[8] = { 0 };
+
+    unsigned int ram_size = s->ram_size / (1024 * 1024);
+    while (i < 8) {
+        int idx = qemu_fls(ram_size);
+        if (idx < 5) {
+            /* Need at least 16 Mb for a slot */
+            break;
+        } else if (idx >= 8) {
+            /* Limit to 128 Mb by slot (at max) */
+            idx = 8;
+        }
+        simm_size[i] = 1 << (idx - 1);
+        ram_size -= simm_size[i];
+        i++;
+    }
+
+    for (i = 0; i < 8; i++) {
+        char name[] = "simm.?";
+        name[5] = i + '0';
+        if (simm_size[i]) {
+            trace_mpc105_simm_size(i, simm_size[i]);
+            memory_region_init_ram(&s->simm[i], name,
+                                   simm_size[i] * 1024 * 1024);
+            vmstate_register_ram_global(&s->simm[i]);
+        } else {
+            memory_region_init(&s->simm[i], name, 0);
+        }
+        memory_region_init(&s->sizer[i], "sizer", 0);
+        memory_region_add_subregion_overlap(get_system_memory(), 0,
+                                            &s->simm[i], i);
+        memory_region_set_enabled(&s->simm[i], false);
+    }
+
Quoted 8 lines

hw/pci-host/mpc105.c line 320

Quoted 6 lines
hw/pci-host/mpc105.c new file +6 -0
+    memory_region_init_ram(&s->bios, "bios", BIOS_SIZE);
+    memory_region_set_readonly(&s->bios, true);
+    memory_region_add_subregion(get_system_memory(), (uint32_t)(-BIOS_SIZE),
+                                &s->bios);
+    vmstate_register_ram_global(&s->bios);
+    if (s->bios_name) {

Can't you just reuse the normal -bios logic?

bios property is filled with -bios name in the machine init function.
mpc105 datasheet explicity says where the bios/firmware i loaded, and I wanted to keep the machine init function as small as possible.

Quoted 6 lines

hw/pci-host/mpc105.c line 323

Quoted 4 lines
hw/pci-host/mpc105.c new file +3 -0
+        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
+        if (filename) {
+            if (s->elf_machine != EM_NONE) {

Elf machine?

Yes, like EM_PPC, required by function load_elf below.

Quoted 86 lines
Quoted 86 lines
hw/pci-host/mpc105.c new file +85 -0
+                bios_size = load_elf(filename, NULL, NULL, NULL,
+                                     NULL, NULL, 1, s->elf_machine, 0);
+            }
+            if (bios_size < 0) {
+                bios_size = get_image_size(filename);
+                if (bios_size > 0 && bios_size <= BIOS_SIZE) {
+                    hwaddr bios_addr;
+                    bios_size = (bios_size + 0xfff) & ~0xfff;
+                    bios_addr = (uint32_t)(-BIOS_SIZE);
+                    bios_size = load_image_targphys(filename, bios_addr,
+                                                    bios_size);
+                }
+            }
+        }
+        if (bios_size < 0 || bios_size > BIOS_SIZE) {
+            hw_error("qemu: could not load bios image '%s'\n", s->bios_name);
+        }
+        if (filename) {
+            g_free(filename);
+        }
+    }
+
+    return 0;
+}
+
+static void mpc105_class_init(ObjectClass *klass, void *data)
+{
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    k->init = mpc105_initfn;
+    k->vendor_id = PCI_VENDOR_ID_MOTOROLA;
+    k->device_id = PCI_DEVICE_ID_MOTOROLA_MPC105;
+    k->class_id = PCI_CLASS_BRIDGE_HOST;
+    k->config_write = mpc105_write_config;
+    dc->desc = "MPC105 PCI bridge/Memory controller";
+    dc->reset = qdev_mpc105_reset;
+    dc->vmsd = &vmstate_mpc105;
+    dc->no_user = 1;
+}
+
+static TypeInfo mpc105_info = {
+    .name          = TYPE_MPC105,
+    .parent        = TYPE_PCI_DEVICE,
+    .instance_size = sizeof(Mpc105State),
+    .class_init    = mpc105_class_init,
+};
+
+typedef struct Mpc105HostState {
+    PCIHostState host_state;
+    uint32_t ram_size;
+    qemu_irq irq[PCI_NUM_PINS];
+    PCIBus pci_bus;
+    MemoryRegion pci_io;
+    MemoryRegion isa_io;
+    MemoryRegion pci_intack;
+    MemoryRegion pci_memory;
+    MemoryRegion rom;
+    Mpc105State pci_dev;
+} Mpc105HostState;
+
+static void mpc105_set_irq(void *opaque, int irq_num, int level)
+{
+    qemu_irq *pic = opaque;
+
+    qemu_set_irq(pic[irq_num] , level);
+}
+
+static int mpc105_map_irq(PCIDevice *pci_dev, int irq_num)
+{
+    return (irq_num + (pci_dev->devfn >> 3)) & 1;
+}
+
+static void mpc105_pcihost_realizefn(DeviceState *d, Error **errp)
+{
+    SysBusDevice *dev = SYS_BUS_DEVICE(d);
+    PCIHostState *h = PCI_HOST_BRIDGE(dev);
+    Mpc105HostState *s = MPC105_PCI_HOST_BRIDGE(dev);
+    int i;
+
+    for (i = 0; i < PCI_NUM_PINS; i++) {
+        sysbus_init_irq(dev, &s->irq[i]);
+    }
+
+    pci_bus_irqs(&s->pci_bus, mpc105_set_irq, mpc105_map_irq, s->irq, 4);
Quoted 9 lines

hw/pci-host/mpc105.c line 417

Quoted 8 lines
hw/pci-host/mpc105.c new file +8 -0
+
+    memory_region_init_io(&h->conf_mem, &pci_host_conf_le_ops, s,
+                          "pci-conf-idx", 1);
+    memory_region_add_subregion(get_system_io(), 0xcf8, &h->conf_mem);
+
+    memory_region_init_io(&h->data_mem, &pci_host_data_le_ops, s,
+                          "pci-conf-data", 4);
+

Hervé

#4 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

Quoted 10 lines · Hervé Poussineau
Quoted 9 lines · Alexander Graf

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau <[email protected]>:

Quoted 6 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Me too, but I'm not confident enough in my capabilities to do it.

Huh? Why not? Most of the device initialization code in OpenBIOS happens in C, so you don't even have to touch Forth code :).

Quoted 1 line

Autoconfiguration is only in one place of the code, so I think it can be removed easily once OpenBIOS has this logic.

I'd prefer if we could come up with a clean model from the start. It really shouldn't be hard at all.

Quoted 72 lines
Quoted 63 lines
Quoted 63 lines

---

default-configs/ppc-softmmu.mak |    1 +
hw/pci-host/Makefile.objs       |    1 +
hw/pci-host/mpc105.c            |  488 
+++++++++++++++++++++++++++++++++++++++
include/hw/pci/pci_ids.h        |    1 +
trace-events                    |    7 +
5 files changed, 498 insertions(+)

create mode 100644 hw/pci-host/mpc105.c

default-configs/ppc-softmmu.mak +0 -0
index cc3587f..f79b058 100644
@@ -28,6 +28,7 @@ CONFIG_MAC_NVRAM=y

CONFIG_MAC_DBDMA=y
CONFIG_HEATHROW_PIC=y
CONFIG_GRACKLE_PCI=y
+CONFIG_MPC105_PCI=y
CONFIG_UNIN_PCI=y
CONFIG_DEC_PCI=y
CONFIG_PPCE500_PCI=y

hw/pci-host/Makefile.objs +0 -0
index 909e702..ec4427b 100644
@@ -3,6 +3,7 @@ common-obj-y += pam.o
 # PPC devices

common-obj-$(CONFIG_PREP_PCI) += prep.o
common-obj-$(CONFIG_GRACKLE_PCI) += grackle.o
+common-obj-$(CONFIG_MPC105_PCI) += mpc105.o
# NewWorld PowerMac
common-obj-$(CONFIG_UNIN_PCI) += uninorth.o
common-obj-$(CONFIG_DEC_PCI) += dec.o

hw/pci-host/mpc105.c new file +6 -0
new file mode 100644
index 0000000..8e4cc95
@@ -0,0 +1,488 @@
+/*
+ * QEMU MPC-105 Eagle PCI host
+ *
+ * Copyright (c) 2013 Hervé Poussineau
+ *
+ * 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) version 3 or 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
<http://www.gnu.org/licenses/>.

Quoted 8 lines

hw/pci-host/mpc105.c line 23

Quoted 6 lines

+ */

hw/pci-host/mpc105.c new file +5 -0
+
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/pci/pci_host.h"
+#include "hw/i386/pc.h"

That include sounds odd :).

Quoted 2 lines

Sure, but you need to access pic_read_irq() :)
hw/pci-host/prep.c also includes it.

Phew. Would be a good thing to pull out of there. But it's out of the scope for this set.

Quoted 318 lines
Quoted 309 lines
Quoted 309 lines
hw/pci-host/mpc105.c new file +57 -0
+#include "hw/loader.h"
+#include "exec/address-spaces.h"
+#include "elf.h"
+#include "trace.h"
+
+#define TYPE_MPC105_PCI_HOST_BRIDGE "mpc105-pcihost"
+#define MPC105_PCI_HOST_BRIDGE(obj) \
+    OBJECT_CHECK(Mpc105HostState, (obj), TYPE_MPC105_PCI_HOST_BRIDGE)
+
+#define TYPE_MPC105 "mpc105"
+#define MPC105(obj) \
+    OBJECT_CHECK(Mpc105State, (obj), TYPE_MPC105)
+
+#define MEM_STA_03     0x0080
+#define MEM_STA_47     0x0084
+#define EXT_MEM_STA_03 0x0088
+#define EXT_MEM_STA_47 0x008c
+#define MEM_END_03     0x0090
+#define MEM_END_47     0x0094
+#define EXT_MEM_END_03 0x0098
+#define EXT_MEM_END_47 0x009c
+#define MEM_BANK_EN    0x00a0
+#define PROC_CFG_A8    0x00a8
+#define PROC_CFG_AC    0x00ac
+#define ALT_OSV_1      0x00ba
+#define ERR_EN_REG1    0x00c0
+#define ERR_DR1        0x00c1
+#define ERR_EN_REG2    0x00c4
+#define MEM_CFG_1      0x00f0
+#define MEM_CFG_2      0x00f4
+#define MEM_CFG_4      0x00fc
+
+#define MEM_CFG_1_MEMGO (1 << 19)
+
+#define BIOS_SIZE (1024 * 1024)
+
+typedef struct Mpc105State {
+    PCIDevice parent_obj;
+    uint32_t ram_size;
+    uint32_t elf_machine;
+    uint32_t x_auto_conf;
+    char *bios_name;
+    MemoryRegion bios;
+    MemoryRegion simm[8];
+    bool use_sizer[8];
+    /* use a sizer to allow access to only part of a simm */
+    MemoryRegion sizer[8];
+} Mpc105State;
+
+static uint64_t mpc105_unassigned_read(void *opaque, hwaddr addr,
+                                       unsigned int size)
+{
+    trace_mpc105_unassigned_mem_read(addr);
+    return 0;
+}
+
+static void mpc105_unassigned_write(void *opaque, hwaddr addr, 

uint64_t data,

hw/pci-host/mpc105.c new file +58 -0
+                                    unsigned int size)
+{
+    trace_mpc105_unassigned_mem_write(addr, data);
+}
+
+static const MemoryRegionOps mpc105_unassigned_ops = {
+    .read = mpc105_unassigned_read,
+    .write = mpc105_unassigned_write,
+};
+
+static void mpc105_update_memory_mappings(Mpc105State *s)
+{
+    uint32_t start_address, end_address;
+    uint32_t start, ext_start, end, ext_end;
+    uint32_t cfg1;
+    uint64_t simm_size;
+    uint8_t *pci_conf;
+    uint8_t en;
+    bool enabled;
+    int i;
+
+    pci_conf = PCI_DEVICE(s)->config;
+    cfg1 = pci_get_long(pci_conf + MEM_CFG_1);
+
+    memory_region_transaction_begin();
+    if (cfg1 & MEM_CFG_1_MEMGO) {
+        en = pci_get_byte(pci_conf + MEM_BANK_EN);
+    } else {
+        en = 0;
+    }
+
+    for (i = 0; i < 8; i++) {
+        enabled = (en & (1 << i));
+
+        start = pci_get_byte(pci_conf + MEM_STA_03 + i);
+        ext_start = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & 0x3;
+        end = pci_get_byte(pci_conf + MEM_END_03 + i);
+        ext_end = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) & 0x3;
+        start_address = (ext_start << 28) | (start << 20);
+        end_address = (ext_end << 28) | (end << 20) | 0xfffff;
+
+        enabled &= start_address < end_address;
+
+        if (enabled) {
+            trace_mpc105_simm_enable(i, start_address, end_address 
+ 1);
+        } else {
+            trace_mpc105_simm_disable(i);
+        }
+
+        simm_size = memory_region_size(&s->simm[i]);
+        if (simm_size == 0) {
+            continue;
+        }
+
+        /* Clean links between system memory, sizer and simm */
+        if (s->use_sizer[i]) {
+            memory_region_del_subregion(get_system_memory(), 

&s->sizer[i]);

hw/pci-host/mpc105.c new file +4 -0
+            memory_region_del_subregion(&s->sizer[i], &s->simm[i]);
+            s->use_sizer[i] = false;
+        } else {
+            memory_region_del_subregion(get_system_memory(), 

&s->simm[i]);

hw/pci-host/mpc105.c new file +5 -0
+        }
+
+        /* Recreate links compatible with new memory layout */
+        if (enabled && end_address - start_address + 1 < simm_size) {
+            memory_region_init_io(&s->sizer[i], 

&mpc105_unassigned_ops,

hw/pci-host/mpc105.c new file +4 -0
+                                  s, memory_region_name(&s->sizer[i]),
+                                  end_address - start_address + 1);
+            memory_region_add_subregion(&s->sizer[i], 0, &s->simm[i]);
+            memory_region_add_subregion(get_system_memory(), 

start_address,
+ &s->sizer[i]);

hw/pci-host/mpc105.c new file +3 -0
+            s->use_sizer[i] = true;
+        } else {
+            memory_region_add_subregion(get_system_memory(), 

start_address,
+ &s->simm[i]);

hw/pci-host/mpc105.c new file +7 -0
+        }
+        memory_region_set_enabled(&s->simm[i], enabled);
+    }
+    memory_region_transaction_commit();
+}
+
+static void mpc105_write_config(PCIDevice *dev, uint32_t addr, 

uint32_t val,

hw/pci-host/mpc105.c new file +6 -0
+                                int l)
+{
+    Mpc105State *s = MPC105(dev);
+
+    pci_default_write_config(dev, addr, val, l);
+    if ((addr >= MEM_STA_03 && addr <= MEM_BANK_EN) || addr == 

MEM_CFG_1) {

hw/pci-host/mpc105.c new file +53 -0
+        mpc105_update_memory_mappings(s);
+    }
+}
+
+static void mpc105_reset(Mpc105State *s)
+{
+    PCIDevice *pci = PCI_DEVICE(s);
+    uint8_t *pci_conf;
+    int id;
+
+    pci_conf = pci->config;
+
+    memset(pci_conf + PCI_CONFIG_HEADER_SIZE, 0,
+           PCI_CONFIG_SPACE_SIZE - PCI_CONFIG_HEADER_SIZE);
+    pci_conf[PCI_COMMAND] = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
+    pci_conf[PCI_STATUS] = PCI_STATUS_FAST_BACK;
+    pci_set_long(pci_conf + PROC_CFG_A8, 0xff000010);
+    pci_set_long(pci_conf + PROC_CFG_AC, 0x000c060c);
+    pci_set_byte(pci_conf + ALT_OSV_1, 0x04);
+    pci_set_byte(pci_conf + ERR_EN_REG1, 0x01);
+    pci_set_long(pci_conf + MEM_CFG_1, 0xff020000);
+    pci_set_long(pci_conf + MEM_CFG_2, 0x00000003);
+    pci_set_long(pci_conf + MEM_CFG_4, 0x00100000);
+
+    memset(pci->wmask + PCI_CONFIG_HEADER_SIZE, 0,
+           MEM_CFG_1 - PCI_CONFIG_HEADER_SIZE);
+    memset(pci->wmask + 0x70, 0xff, 2);
+    memset(pci->wmask + MEM_STA_03, 0xff, MEM_BANK_EN - MEM_STA_03 
+ 1);
+    memset(pci->wmask + PROC_CFG_A8, 0xff, 8);
+    pci_set_word(pci->wmask + ALT_OSV_1, 0xffff);
+    pci_set_byte(pci->wmask + ERR_EN_REG1, 0xff);
+    pci_set_byte(pci->w1cmask + ERR_DR1, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc3, 0xff);
+    pci_set_byte(pci->wmask + ERR_EN_REG2, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc5, 0xff);
+    pci_set_byte(pci->w1cmask + 0xc7, 0xff);
+
+    for (id = 0; id < 8; ++id) {
+        memory_region_set_enabled(&s->simm[id], false);
+    }
+
+    if (s->x_auto_conf) {
+        /* enable all memory banks, starting from address 0 */
+        uint32_t start_address = 0, end_address = 0;
+        uint8_t ext_start, ext_end, enabled = 0;
+        int i;
+        for (i = 0; i < 8; i++) {
+            if (!memory_region_size(&s->simm[i])) {
+                continue;
+            }
+            end_address += memory_region_size(&s->simm[i]);
+            ext_start = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) 

& ~0x3;
+ ext_end = pci_get_byte(pci_conf + EXT_MEM_STA_03 + i) &
~0x3;

hw/pci-host/mpc105.c new file +3 -0
+            ext_start |= (start_address >> 28) & 0x3;
+            ext_end |= ((end_address - 1) >> 28) & 0x3;
+            pci_set_byte(pci_conf + MEM_STA_03 + i, start_address 
Quoted 1 line
Quoted 1 line

20);

hw/pci-host/mpc105.c new file +2 -0
+            pci_set_byte(pci_conf + EXT_MEM_STA_03 + i, ext_start);
+            pci_set_byte(pci_conf + MEM_END_03 + i, (end_address - 

1) >> 20);

hw/pci-host/mpc105.c new file +6 -0
+            pci_set_byte(pci_conf + EXT_MEM_END_03 + i, ext_end);
+            start_address = end_address;
+            enabled |= 1 << i;
+        }
+        pci_set_byte(pci_conf + MEM_BANK_EN, enabled);
+        pci_long_test_and_set_mask(pci_conf + MEM_CFG_1, 

MEM_CFG_1_MEMGO);

hw/pci-host/mpc105.c new file +83 -0
+        mpc105_update_memory_mappings(s);
+    }
+}
+
+static void qdev_mpc105_reset(DeviceState *dev)
+{
+    Mpc105State *s = MPC105(dev);
+    mpc105_reset(s);
+}
+
+static int mpc105_post_load(void *opaque, int version_id)
+{
+    Mpc105State *s = opaque;
+    mpc105_update_memory_mappings(s);
+    return 0;
+}
+
+static const VMStateDescription vmstate_mpc105 = {
+    .name = "mpc105",
+    .version_id = 1,
+    .minimum_version_id = 0,
+    .minimum_version_id_old = 1,
+    .post_load = mpc105_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_PCI_DEVICE(parent_obj, Mpc105State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static uint64_t mpc105_intack_read(void *opaque, hwaddr addr,
+                                   unsigned int size)
+{
+    return pic_read_irq(isa_pic);
+}
+
+static const MemoryRegionOps mpc105_intack_ops = {
+    .read = mpc105_intack_read,
+    .valid = {
+        .max_access_size = 1,
+    },
+};
+
+static int mpc105_initfn(PCIDevice *dev)
+{
+    Mpc105State *s = MPC105(dev);
+    char *filename;
+    int bios_size = -1;
+    int i = 0;
+    uint32_t simm_size[8] = { 0 };
+
+    unsigned int ram_size = s->ram_size / (1024 * 1024);
+    while (i < 8) {
+        int idx = qemu_fls(ram_size);
+        if (idx < 5) {
+            /* Need at least 16 Mb for a slot */
+            break;
+        } else if (idx >= 8) {
+            /* Limit to 128 Mb by slot (at max) */
+            idx = 8;
+        }
+        simm_size[i] = 1 << (idx - 1);
+        ram_size -= simm_size[i];
+        i++;
+    }
+
+    for (i = 0; i < 8; i++) {
+        char name[] = "simm.?";
+        name[5] = i + '0';
+        if (simm_size[i]) {
+            trace_mpc105_simm_size(i, simm_size[i]);
+            memory_region_init_ram(&s->simm[i], name,
+                                   simm_size[i] * 1024 * 1024);
+            vmstate_register_ram_global(&s->simm[i]);
+        } else {
+            memory_region_init(&s->simm[i], name, 0);
+        }
+        memory_region_init(&s->sizer[i], "sizer", 0);
+        memory_region_add_subregion_overlap(get_system_memory(), 0,
+ &s->simm[i], i);
+        memory_region_set_enabled(&s->simm[i], false);
+    }
+
+    memory_region_init_ram(&s->bios, "bios", BIOS_SIZE);
Quoted 8 lines

hw/pci-host/mpc105.c line 320

Quoted 6 lines
hw/pci-host/mpc105.c new file +2 -0
+    memory_region_set_readonly(&s->bios, true);
+    memory_region_add_subregion(get_system_memory(), 

(uint32_t)(-BIOS_SIZE),
+ &s->bios);

hw/pci-host/mpc105.c new file +2 -0
+    vmstate_register_ram_global(&s->bios);
+    if (s->bios_name) {

Can't you just reuse the normal -bios logic?

Quoted 2 lines

bios property is filled with -bios name in the machine init function.
mpc105 datasheet explicity says where the bios/firmware i loaded, and I wanted to keep the machine init function as small as possible.

I see, you just pass it from the machine model to the device. That's fine.

Quoted 9 lines
Quoted 7 lines

hw/pci-host/mpc105.c line 323

Quoted 5 lines
hw/pci-host/mpc105.c new file +3 -0
+        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
+        if (filename) {
+            if (s->elf_machine != EM_NONE) {

Elf machine?

Yes, like EM_PPC, required by function load_elf below.

to make the device target agnostic. Works for me :).

Alex

#5 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

Quoted 17 lines · Alexander Graf
Quoted 16 lines · Hervé Poussineau
Quoted 14 lines · Alexander Graf

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau <[email protected]>:

Quoted 9 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Quoted 8 lines
Quoted 1 line

Me too, but I'm not confident enough in my capabilities to do it.

Huh? Why not? Most of the device initialization code in OpenBIOS happens in C, so you don't even have to touch Forth code :).

Quoted 1 line

Autoconfiguration is only in one place of the code, so I think it can be removed easily once OpenBIOS has this logic.

I'd prefer if we could come up with a clean model from the start. It really shouldn't be hard at all.

I thought that for all other usages of OpenBIOS in QEMU, RAM was supposed to be available as soon as machine was powered on.

However, I checked OpenBIOS code:
One of the first things done in arch/ppc/qemu/start.S is to copy the exception vectors. So, I should add code before it to detect memory controller, detect ram size and configure memory controller? It seems quite a bit of code.
Do you have an example of how to do it for another memory controller, so I can adapt the code?

Regards,

Hervé

#6 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

Quoted 19 lines · Hervé Poussineau
Quoted 18 lines · Alexander Graf
Quoted 17 lines · Hervé Poussineau
Quoted 9 lines · Alexander Graf

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau <[email protected]>:

Quoted 6 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Quoted 5 lines
Quoted 3 lines

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Me too, but I'm not confident enough in my capabilities to do it.

Quoted 8 lines
Quoted 3 lines

Huh? Why not? Most of the device initialization code in OpenBIOS happens in C, so you don't even have to touch Forth code :).

Quoted 1 line

Autoconfiguration is only in one place of the code, so I think it can be removed easily once OpenBIOS has this logic.

I'd prefer if we could come up with a clean model from the start. It really shouldn't be hard at all.

I thought that for all other usages of OpenBIOS in QEMU, RAM was supposed to be available as soon as machine was powered on.

However, I checked OpenBIOS code:
One of the first things done in arch/ppc/qemu/start.S is to copy the exception vectors. So, I should add code before it to detect memory controller, detect ram size and configure memory controller?

You could just try to poke fw_cfg and ask it for the machine model. Based on that you can do all the initialization hardcoded.

Quoted 2 lines

It seems quite a bit of code.
Do you have an example of how to do it for another memory controller, so I can adapt the code?

Unfortunately not, no.

Alex

#7 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

In reply to Hervé Poussineau

Am 06.05.2013 22:57, schrieb Hervé Poussineau:

Quoted 30 lines
Quoted 19 lines · Alexander Graf
Quoted 10 lines · Hervé Poussineau
Quoted 9 lines · Alexander Graf

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau <[email protected]>:

Quoted 6 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Me too, but I'm not confident enough in my capabilities to do it.

Quoted 9 lines

Huh? Why not? Most of the device initialization code in OpenBIOS happens in C, so you don't even have to touch Forth code :).

Quoted 2 lines

Autoconfiguration is only in one place of the code, so I think it can be removed easily once OpenBIOS has this logic.

I'd prefer if we could come up with a clean model from the start. It really shouldn't be hard at all.

Quoted 7 lines

I thought that for all other usages of OpenBIOS in QEMU, RAM was supposed to be available as soon as machine was powered on.

However, I checked OpenBIOS code:
One of the first things done in arch/ppc/qemu/start.S is to copy the exception vectors. So, I should add code before it to detect memory controller, detect ram size and configure memory controller?

No. Why? QEMU does not depend on the memory controller being initialized, only the OS might expect some registers to be filled in. So you should look at or add the MPC105 PHB initialization hook in OpenBIOS' PCI code, long after the memory is set up.

Quoted 3 lines

It seems quite a bit of code.
Do you have an example of how to do it for another memory controller, so I can adapt the code?

Not sure if there's a memory controller specifically, but the Mac PHBs are being initialized and on my PReP OpenBIOS branch I may have touched that code...

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#8 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

Quoted 33 lines · Andreas Färber

Am 06.05.2013 22:57, schrieb Hervé Poussineau:

Quoted 24 lines
Quoted 14 lines · Alexander Graf
Quoted 13 lines · Hervé Poussineau
Quoted 12 lines · Alexander Graf

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau <[email protected]>:

Quoted 9 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Quoted 9 lines
Quoted 1 line

Me too, but I'm not confident enough in my capabilities to do it.

Huh? Why not? Most of the device initialization code in OpenBIOS happens in C, so you don't even have to touch Forth code :).

Quoted 2 lines

Autoconfiguration is only in one place of the code, so I think it can be removed easily once OpenBIOS has this logic.

I'd prefer if we could come up with a clean model from the start. It really shouldn't be hard at all.

Quoted 7 lines

I thought that for all other usages of OpenBIOS in QEMU, RAM was supposed to be available as soon as machine was powered on.

However, I checked OpenBIOS code:
One of the first things done in arch/ppc/qemu/start.S is to copy the exception vectors. So, I should add code before it to detect memory controller, detect ram size and configure memory controller?

Quoted 4 lines

No. Why? QEMU does not depend on the memory controller being initialized, only the OS might expect some registers to be filled in. So you should look at or add the MPC105 PHB initialization hook in OpenBIOS' PCI code, long after the memory is set up.

OpenBIOS depends of memory being available (at least the first KB/MB) even at its very startup, in arch/ppc/qemu/start.S. PCI initialization code comes much later. At boot, MPC105 datasheet says that memory controller is not configured, ie you have to not use RAM in OpenBIOS before PCI initialization.

For other PPC targets (mac99, g3beige) using OpenBIOS, RAM is accessible at startup, so that's not a problem for OpenBIOS.

So, no, QEMU does not depend of the memory controller being initialized, but OpenBIOS depends of the RAM being accessible ways before PCI initialization. I don't speak of the OS which might (or might not) expect some registers to be filled in.
x-auto-conf property correctly sets some registers, so that memory is available at startup (like on mac99, g3beige emulations).

Hervé

#9 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 1/7] pci: add MPC105 PCI host bridge emulation

Quoted 51 lines · Hervé Poussineau
Quoted 43 lines · Andreas Färber

Am 06.05.2013 22:57, schrieb Hervé Poussineau:

Quoted 33 lines
Quoted 22 lines · Alexander Graf
Quoted 12 lines · Hervé Poussineau
Quoted 10 lines · Alexander Graf

Am 02.05.2013 um 22:08 schrieb Hervé Poussineau
<[email protected]>:

Quoted 6 lines

Non-contiguous I/O is not implemented.

There is also somewhere a bug in the memory controller, which means that some real firmwares may not detect the correct amount of memory. This can be bypassed by adding '-m 1G' on the command line.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Add x-auto-conf property, to automatically configure the memory controller at startup. This will be required by OpenBIOS, which doesn't know how to do it.

Why not teach it? I'd prefer to see that logic in firmware.

Me too, but I'm not confident enough in my capabilities to do it.

Quoted 9 lines

Huh? Why not? Most of the device initialization code in OpenBIOS happens in C, so you don't even have to touch Forth code :).

Quoted 2 lines

Autoconfiguration is only in one place of the code, so I think it can be removed easily once OpenBIOS has this logic.

I'd prefer if we could come up with a clean model from the start. It really shouldn't be hard at all.

Quoted 7 lines

I thought that for all other usages of OpenBIOS in QEMU, RAM was supposed to be available as soon as machine was powered on.

However, I checked OpenBIOS code:
One of the first things done in arch/ppc/qemu/start.S is to copy the exception vectors. So, I should add code before it to detect memory controller, detect ram size and configure memory controller?

Quoted 4 lines

No. Why? QEMU does not depend on the memory controller being initialized, only the OS might expect some registers to be filled in. So you should look at or add the MPC105 PHB initialization hook in OpenBIOS' PCI code, long after the memory is set up.

Quoted 3 lines

OpenBIOS depends of memory being available (at least the first KB/MB) even at its very startup, in arch/ppc/qemu/start.S. PCI initialization code comes much later.

OpenBIOS for Sparc32 and Sparc64 does not use RAM before RAM size has been read from fw_cfg. A check for QEMU signature is done and machine ID is also read before that.
arch/sparc32/entry.S
arch/sparc64/entry.S

It should be easy to change PPC to check the machine ID before accessing RAM.

Quoted 2 lines

At boot, MPC105 datasheet says that memory controller is not configured, ie you have to not use RAM in OpenBIOS before PCI initialization.

The memory controller should be initialized much earlier than PCI.

Quoted 13 lines

For other PPC targets (mac99, g3beige) using OpenBIOS, RAM is accessible at startup, so that's not a problem for OpenBIOS.

So, no, QEMU does not depend of the memory controller being initialized, but OpenBIOS depends of the RAM being accessible ways before PCI initialization. I don't speak of the OS which might (or might not) expect some registers to be filled in.
x-auto-conf property correctly sets some registers, so that memory is available at startup (like on mac99, g3beige emulations).

Hervé

#10 lore raw

PATCH 2/7 qom: handle registration of new types when initializing the first ones

In reply to Hervé Poussineau

When initializing all types in object_class_foreach, called by object_class_get_list, some new types may be registered. Those will change the type internal hashtable which is currently enumerated, and may crash QEMU.

Fix it, by adding a second hash table which contains all the non-initialized types, merged to the main one before each round of initializations.

Bug has been detected when registering dynamic types containing an interface.

---
 qom/object.c |   45 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 8 deletions(-)
qom/object.c +37 -8
index 75e6aac..e0a24dc 100644
@@ -65,25 +65,39 @@ struct TypeImpl
 
 static Type type_interface;
 
+static GHashTable *type_table_to_initialize;
+static GHashTable *type_table_initialized;
+
 static GHashTable *type_table_get(void)
 {
-    static GHashTable *type_table;
-
-    if (type_table == NULL) {
-        type_table = g_hash_table_new(g_str_hash, g_str_equal);
+    if (!type_table_initialized) {
+        type_table_initialized = g_hash_table_new(g_str_hash, g_str_equal);
     }
 
-    return type_table;
+    return type_table_initialized;
 }
 
 static void type_table_add(TypeImpl *ti)
 {
-    g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
+    GHashTable **type_table;
+    if (ti->class) {
+        type_table = &type_table_initialized;
+    } else {
+        type_table = &type_table_to_initialize;
+    }
+    if (!*type_table) {
+        *type_table = g_hash_table_new(g_str_hash, g_str_equal);
+    }
+    g_hash_table_insert(*type_table, (void *)ti->name, ti);
 }
 
 static TypeImpl *type_table_lookup(const char *name)
 {
-    return g_hash_table_lookup(type_table_get(), name);
+    TypeImpl *ret = g_hash_table_lookup(type_table_get(), name);
+    if (!ret && type_table_to_initialize) {
+        ret = g_hash_table_lookup(type_table_to_initialize, name);
+    }
+    return ret;
 }
 
 static TypeImpl *type_register_internal(const TypeInfo *info)
@@ -573,13 +587,28 @@ static void object_class_foreach_tramp(gpointer key, gpointer value,
     data->fn(k, data->opaque);
 }
 
+static void object_class_merge(gpointer key, gpointer value,
+                               gpointer opaque)
+{
+    g_hash_table_insert(type_table_get(), key, value);
+}
+
 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
                           const char *implements_type, bool include_abstract,
                           void *opaque)
 {
     OCFData data = { fn, implements_type, include_abstract, opaque };
 
-    g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
+    while (type_table_to_initialize &&
+           g_hash_table_size(type_table_to_initialize) > 0) {
+        g_hash_table_foreach(type_table_to_initialize, object_class_merge,
+                             NULL);
+        g_hash_table_destroy(type_table_to_initialize);
+        type_table_to_initialize = NULL;
+
+        g_hash_table_foreach(type_table_get(), object_class_foreach_tramp,
+                             &data);
+    }
 }
 
 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
Signature · 1 line
1.7.10.4
#11 lore raw

Re: [Qemu-devel] [PATCH 2/7] qom: handle registration of new types when initializing the first ones

Am 02.05.2013 22:08, schrieb Hervé Poussineau:

Quoted 7 lines

When initializing all types in object_class_foreach, called by object_class_get_list, some new types may be registered. Those will change the type internal hashtable which is currently enumerated, and may crash QEMU.

Fix it, by adding a second hash table which contains all the non-initialized types, merged to the main one before each round of initializations.

Quoted 6 lines

Bug has been detected when registering dynamic types containing an interface.

---
 qom/object.c |   45 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 8 deletions(-)

Could you be more specific about how to reproduce the problem? Is it a generic issue or specific to some later patch in this series? I find neither object_class_get_list() nor object_class_for_each() being used in this series. And registering types during object_class_for_each() doesn't sound right... CC'ing Anthony and Paolo.

Andreas

Quoted 82 lines
qom/object.c +37 -8
index 75e6aac..e0a24dc 100644
@@ -65,25 +65,39 @@ struct TypeImpl
 
 static Type type_interface;
 
+static GHashTable *type_table_to_initialize;
+static GHashTable *type_table_initialized;
+
 static GHashTable *type_table_get(void)
 {
-    static GHashTable *type_table;
-
-    if (type_table == NULL) {
-        type_table = g_hash_table_new(g_str_hash, g_str_equal);
+    if (!type_table_initialized) {
+        type_table_initialized = g_hash_table_new(g_str_hash, g_str_equal);
     }
 
-    return type_table;
+    return type_table_initialized;
 }
 
 static void type_table_add(TypeImpl *ti)
 {
-    g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
+    GHashTable **type_table;
+    if (ti->class) {
+        type_table = &type_table_initialized;
+    } else {
+        type_table = &type_table_to_initialize;
+    }
+    if (!*type_table) {
+        *type_table = g_hash_table_new(g_str_hash, g_str_equal);
+    }
+    g_hash_table_insert(*type_table, (void *)ti->name, ti);
 }
 
 static TypeImpl *type_table_lookup(const char *name)
 {
-    return g_hash_table_lookup(type_table_get(), name);
+    TypeImpl *ret = g_hash_table_lookup(type_table_get(), name);
+    if (!ret && type_table_to_initialize) {
+        ret = g_hash_table_lookup(type_table_to_initialize, name);
+    }
+    return ret;
 }
 
 static TypeImpl *type_register_internal(const TypeInfo *info)
@@ -573,13 +587,28 @@ static void object_class_foreach_tramp(gpointer key, gpointer value,
     data->fn(k, data->opaque);
 }
 
+static void object_class_merge(gpointer key, gpointer value,
+                               gpointer opaque)
+{
+    g_hash_table_insert(type_table_get(), key, value);
+}
+
 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
                           const char *implements_type, bool include_abstract,
                           void *opaque)
 {
     OCFData data = { fn, implements_type, include_abstract, opaque };
 
-    g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
+    while (type_table_to_initialize &&
+           g_hash_table_size(type_table_to_initialize) > 0) {
+        g_hash_table_foreach(type_table_to_initialize, object_class_merge,
+                             NULL);
+        g_hash_table_destroy(type_table_to_initialize);
+        type_table_to_initialize = NULL;
+
+        g_hash_table_foreach(type_table_get(), object_class_foreach_tramp,
+                             &data);
+    }
 }
 
 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#12 lore raw

Re: [Qemu-devel] [PATCH 2/7] qom: handle registration of new types when initializing the first ones

Quoted 15 lines · Andreas Färber

Am 02.05.2013 22:08, schrieb Hervé Poussineau:

Quoted 7 lines

When initializing all types in object_class_foreach, called by object_class_get_list, some new types may be registered. Those will change the type internal hashtable which is currently enumerated, and may crash QEMU.

Fix it, by adding a second hash table which contains all the non-initialized types, merged to the main one before each round of initializations.

Quoted 6 lines

Bug has been detected when registering dynamic types containing an interface.

---
 qom/object.c |   45 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 8 deletions(-)
Quoted 5 lines

Could you be more specific about how to reproduce the problem? Is it a generic issue or specific to some later patch in this series? I find neither object_class_get_list() nor object_class_for_each() being used in this series. And registering types during object_class_for_each() doesn't sound right... CC'ing Anthony and Paolo.

Try the attached patch, and run with qemu-system-ppc (no arguments)
I added a dummy interface to a random device, but the problem should be exposed by whatever interface on whatever device. I saw the problem in patch 5/7 ("add a Nvram interface"). However, the problem doesn't seem to appear on other system emulations like i386.

With attached patch, you'll get an assert:
qemu-system-ppc: qom/object.c:82: type_table_add: Assertion `!enumerating' failed.

Program received signal SIGABRT, Aborted. 0xb7fe1430 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fe1430 in __kernel_vsyscall ()
#1 0xb6f27941 in *__GI_raise (sig=6) at
../nptl/sysdeps/unix/sysv/linux/raise.c:64
#2 0xb6f2ad72 in *__GI_abort () at abort.c:92 #3 0xb6f20b58 in *__GI___assert_fail
(assertion=assertion@entry=0x803809f8 "!enumerating",

     file=file@entry=0x80380adc "qom/object.c", line=line@entry=82,
     function=function@entry=0x80380c6c "type_table_add") at assert.c:81
 #4  0x80197513 in type_table_add (ti=0x80b67bd0) at qom/object.c:82
 #5  type_register_internal (info=0xbfffef0c) at qom/object.c:124
 #6  0x8019764c in type_initialize_interface (parent=0x80b3ec18 

"interface",
ti=<error reading variable: Unhandled dwarf expression opcode 0xfa>, ti=<error reading variable: Unhandled dwarf expression opcode
0xfa>) at qom/object.c:218

 #7  0x801978fe in type_initialize (ti=<optimized out>) at qom/object.c:271
 #8  type_initialize (ti=0x80b3eb30) at qom/object.c:229
 #9  0x80197dfa in object_class_foreach_tramp (key=0x80b3ebf0, 
value=0x80b3eb30, opaque=0xbffff03c)
     at qom/object.c:563
 #10 0xb7ef35e2 in g_hash_table_foreach () from 
/lib/i386-linux-gnu/libglib-2.0.so.0
 #11 0x801980b1 in object_class_foreach (fn=fn@entry=0x80197180 
<object_class_get_list_tramp>,
     implements_type=implements_type@entry=0x8039b834 "powerpc-cpu", 
include_abstract=include_abstract@entry=false,
     opaque=opaque@entry=0xbffff078) at qom/object.c:585

#12 0x801981ba in object_class_get_list
(implements_type=implements_type@entry=0x8039b834 "powerpc-cpu",
include_abstract=include_abstract@entry=false) at qom/object.c:618
#13 0x80328d4e in ppc_cpu_class_by_name (name=name@entry=0x8039dc69 "G3")

     at target-ppc/translate_init.c:8003
 #14 0x80328f7a in cpu_ppc_init (cpu_model=cpu_model@entry=0x8039dc69 "G3")
     at target-ppc/translate_init.c:8020
 #15 0x80216724 in ppc_heathrow_init (args=0xbffff2a8) at 

hw/ppc/mac_oldworld.c:109
#16 0x80040b81 in main (argc=1, argv=0xbffff4b4, envp=0xbffff4bc) at
vl.c:4304

Quoted 81 lines
Quoted 81 lines
qom/object.c +37 -8
index 75e6aac..e0a24dc 100644
@@ -65,25 +65,39 @@ struct TypeImpl
 
 static Type type_interface;
 
+static GHashTable *type_table_to_initialize;
+static GHashTable *type_table_initialized;
+
 static GHashTable *type_table_get(void)
 {
-    static GHashTable *type_table;
-
-    if (type_table == NULL) {
-        type_table = g_hash_table_new(g_str_hash, g_str_equal);
+    if (!type_table_initialized) {
+        type_table_initialized = g_hash_table_new(g_str_hash, g_str_equal);
     }
 
-    return type_table;
+    return type_table_initialized;
 }
 
 static void type_table_add(TypeImpl *ti)
 {
-    g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
+    GHashTable **type_table;
+    if (ti->class) {
+        type_table = &type_table_initialized;
+    } else {
+        type_table = &type_table_to_initialize;
+    }
+    if (!*type_table) {
+        *type_table = g_hash_table_new(g_str_hash, g_str_equal);
+    }
+    g_hash_table_insert(*type_table, (void *)ti->name, ti);
 }
 
 static TypeImpl *type_table_lookup(const char *name)
 {
-    return g_hash_table_lookup(type_table_get(), name);
+    TypeImpl *ret = g_hash_table_lookup(type_table_get(), name);
+    if (!ret && type_table_to_initialize) {
+        ret = g_hash_table_lookup(type_table_to_initialize, name);
+    }
+    return ret;
 }
 
 static TypeImpl *type_register_internal(const TypeInfo *info)
@@ -573,13 +587,28 @@ static void object_class_foreach_tramp(gpointer key, gpointer value,
     data->fn(k, data->opaque);
 }
 
+static void object_class_merge(gpointer key, gpointer value,
+                               gpointer opaque)
+{
+    g_hash_table_insert(type_table_get(), key, value);
+}
+
 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
                           const char *implements_type, bool include_abstract,
                           void *opaque)
 {
     OCFData data = { fn, implements_type, include_abstract, opaque };
 
-    g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
+    while (type_table_to_initialize &&
+           g_hash_table_size(type_table_to_initialize) > 0) {
+        g_hash_table_foreach(type_table_to_initialize, object_class_merge,
+                             NULL);
+        g_hash_table_destroy(type_table_to_initialize);
+        type_table_to_initialize = NULL;
+
+        g_hash_table_foreach(type_table_get(), object_class_foreach_tramp,
+                             &data);
+    }
 }
 
Quoted 4 lines

qom/object.c static void object_class_foreach_tramp(gpointer key, gpointer value, · line 614

Quoted 2 lines
qom/object.c +0 -0
 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),

From 4da22be31d5fc8df887b8c76c609b9844bebe9f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20Poussineau?= <[email protected]>
Date: Sun, 5 May 2013 10:31:24 +0200
Subject: [PATCH] usb-ehci: add an empty interface to expose a problem in QOM

Run with qemu-system-ppc (no arguments)

qemu-system-ppc: qom/object.c:82: type_table_add: Assertion `!enumerating' failed.

Program received signal SIGABRT, Aborted.
0xb7fe1430 in __kernel_vsyscall ()
(gdb) bt

 #0  0xb7fe1430 in __kernel_vsyscall ()
 #1  0xb6f27941 in *__GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
 #2  0xb6f2ad72 in *__GI_abort () at abort.c:92
 #3  0xb6f20b58 in *__GI___assert_fail (assertion=assertion@entry=0x803809f8 "!enumerating",
     file=file@entry=0x80380adc "qom/object.c", line=line@entry=82,
     function=function@entry=0x80380c6c "type_table_add") at assert.c:81
 #4  0x80197513 in type_table_add (ti=0x80b67bd0) at qom/object.c:82
 #5  type_register_internal (info=0xbfffef0c) at qom/object.c:124
 #6  0x8019764c in type_initialize_interface (parent=0x80b3ec18 "interface",
     ti=<error reading variable: Unhandled dwarf expression opcode 0xfa>,
     ti=<error reading variable: Unhandled dwarf expression opcode 0xfa>) at qom/object.c:218
 #7  0x801978fe in type_initialize (ti=<optimized out>) at qom/object.c:271
 #8  type_initialize (ti=0x80b3eb30) at qom/object.c:229
 #9  0x80197dfa in object_class_foreach_tramp (key=0x80b3ebf0, value=0x80b3eb30, opaque=0xbffff03c)
     at qom/object.c:563
 #10 0xb7ef35e2 in g_hash_table_foreach () from /lib/i386-linux-gnu/libglib-2.0.so.0
 #11 0x801980b1 in object_class_foreach (fn=fn@entry=0x80197180 <object_class_get_list_tramp>,
     implements_type=implements_type@entry=0x8039b834 "powerpc-cpu", include_abstract=include_abstract@entry=false,
     opaque=opaque@entry=0xbffff078) at qom/object.c:585
 #12 0x801981ba in object_class_get_list (implements_type=implements_type@entry=0x8039b834 "powerpc-cpu",
     include_abstract=include_abstract@entry=false) at qom/object.c:618
 #13 0x80328d4e in ppc_cpu_class_by_name (name=name@entry=0x8039dc69 "G3")
     at target-ppc/translate_init.c:8003
 #14 0x80328f7a in cpu_ppc_init (cpu_model=cpu_model@entry=0x8039dc69 "G3")
     at target-ppc/translate_init.c:8020
 #15 0x80216724 in ppc_heathrow_init (args=0xbffff2a8) at hw/ppc/mac_oldworld.c:109
 #16 0x80040b81 in main (argc=1, argv=0xbffff4b4, envp=0xbffff4bc) at vl.c:4304
---
 hw/usb/hcd-uhci.c |    4 ++++
 qom/object.c      |    4 ++++
 2 files changed, 8 insertions(+)
hw/usb/hcd-uhci.c +4 -0
index f8c4286..a95ca30 100644
@@ -1386,6 +1386,10 @@ static void uhci_register_types(void)
         .instance_size = sizeof(UHCIState),
         .class_size    = sizeof(UHCIPCIDeviceClass),
         .class_init    = uhci_class_init,
+        .interfaces = (InterfaceInfo[]) {
+            { TYPE_INTERFACE },
+            { }
+        }
     };
     int i;
 
qom/object.c +4 -0
index 75e6aac..b8e9f4f 100644
@@ -76,8 +76,10 @@ static GHashTable *type_table_get(void)
     return type_table;
 }
 
+static bool enumerating = false;
 static void type_table_add(TypeImpl *ti)
 {
+    assert(!enumerating);
     g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
 }
 
@@ -579,7 +581,9 @@ void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
 {
     OCFData data = { fn, implements_type, include_abstract, opaque };
 
+    enumerating = true;
     g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
+    enumerating = false;
 }
 
 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
Signature · 1 line
1.7.10.4
#13 lore raw

PATCH 3/7 m48t59: move ISA ports/memory regions registration to QOM constructor

In reply to Hervé Poussineau

-device m48t59_isa can now be used to create a fully functional nvram.

---
 hw/timer/m48t59.c |   15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
hw/timer/m48t59.c +8 -7
index 45753d8..fb78d20 100644
@@ -655,9 +655,9 @@ M48t59State *m48t59_init(qemu_irq IRQ, hwaddr mem_base,
     d = FROM_SYSBUS(M48t59SysBusState, s);
     state = &d->state;
     sysbus_connect_irq(s, 0, IRQ);
-    memory_region_init_io(&d->io, &m48t59_io_ops, state, "m48t59", 4);
     if (io_base != 0) {
-        memory_region_add_subregion(get_system_io(), io_base, &d->io);
+        memory_region_add_subregion(get_system_io(), io_base,
+                                    sysbus_mmio_get_region(dev, 1));
     }
     if (mem_base != 0) {
         sysbus_mmio_map(s, 0, mem_base);
@@ -683,11 +683,6 @@ M48t59State *m48t59_init_isa(ISABus *bus, uint32_t io_base, uint16_t size,
     d = ISA_M48T59(isadev);
     s = &d->state;
 
-    memory_region_init_io(&d->io, &m48t59_io_ops, s, "m48t59", 4);
-    if (io_base != 0) {
-        isa_register_ioport(isadev, &d->io, io_base);
-    }
-
     return s;
 }
 
@@ -710,6 +705,10 @@ static int m48t59_init_isa1(ISADevice *dev)
 
     isa_init_irq(dev, &s->IRQ, 8);
     m48t59_init_common(s);
+    memory_region_init_io(&d->io, &m48t59_io_ops, s, "m48t59", 4);
+    if (s->io_base != 0) {
+        isa_register_ioport(dev, &d->io, s->io_base);
+    }
 
     return 0;
 }
@@ -723,6 +722,8 @@ static int m48t59_init1(SysBusDevice *dev)
 
     memory_region_init_io(&s->iomem, &nvram_ops, s, "m48t59.nvram", s->size);
     sysbus_init_mmio(dev, &s->iomem);
+    memory_region_init_io(&d->io, &m48t59_io_ops, s, "m48t59", 4);
+    sysbus_init_mmio(dev, &d->io);
     m48t59_init_common(s);
 
     return 0;
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH 4/7 m48t59: register a QOM type for each nvram type we support

In reply to Hervé Poussineau

As m48t59 devices can only be created with m48t59_init() or m48t59_init_isa(), we know exactly which nvram types are required. Register only those three types.
Remove .model and .size properties as they can be infered from nvram name.

Rename type to 'isa-*' (and 'sysbus-*') to do like other devices ISA devices (isa-ide, isa-parallel, isa-serial...)

---
 hw/timer/m48t59.c |  248 +++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 172 insertions(+), 76 deletions(-)
hw/timer/m48t59.c +172 -76
index fb78d20..23a6ab3 100644
@@ -2,6 +2,7 @@
  * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
  *
  * Copyright (c) 2003-2005, 2007 Jocelyn Mayer
+ * Copyright (c) 2013 Hervé Poussineau
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -37,12 +38,31 @@
 #define NVRAM_PRINTF(fmt, ...) do { } while (0)
 #endif
 
+#define TYPE_M48TXX_SYS_BUS "m48txx-sysbus"
+#define M48TXX_SYS_BUS_CLASS(klass) \
+    OBJECT_CLASS_CHECK(M48txxSysBusDeviceClass, (klass), TYPE_M48TXX_SYS_BUS)
+#define M48TXX_SYS_BUS(obj) \
+    OBJECT_CHECK(M48txxSysBusState, (obj), TYPE_M48TXX_SYS_BUS)
+
+#define TYPE_M48TXX_ISA "m48txx-isa"
+#define M48TXX_ISA_CLASS(klass) \
+    OBJECT_CLASS_CHECK(M48txxISADeviceClass, (klass), TYPE_M48TXX_ISA)
+#define M48TXX_ISA(obj) \
+    OBJECT_CHECK(M48txxISAState, (obj), TYPE_M48TXX_ISA)
+
 /*
  * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
  * alarm and a watchdog timer and related control registers. In the
  * PPC platform there is also a nvram lock function.
  */
 
+typedef struct M48txxInfo {
+    const char *isa_name;
+    const char *sysbus_name;
+    uint32_t model; /* 2 = m48t02, 8 = m48t08, 59 = m48t59 */
+    uint32_t size;
+} M48txxInfo;
+
 /*
  * Chipset docs:
  * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
@@ -54,7 +74,6 @@ struct M48t59State {
     /* Hardware parameters */
     qemu_irq IRQ;
     MemoryRegion iomem;
-    uint32_t io_base;
     uint32_t size;
     /* RTC management */
     time_t   time_offset;
@@ -72,22 +91,45 @@ struct M48t59State {
     uint8_t  lock;
 };
 
-#define TYPE_ISA_M48T59 "m48t59_isa"
-#define ISA_M48T59(obj) \
-    OBJECT_CHECK(M48t59ISAState, (obj), TYPE_ISA_M48T59)
-
-typedef struct M48t59ISAState {
+typedef struct M48txxISAState {
     ISADevice parent_obj;
-
     M48t59State state;
+    uint32_t io_base;
     MemoryRegion io;
-} M48t59ISAState;
+} M48txxISAState;
+
+typedef struct M48txxISADeviceClass {
+    ISADeviceClass parent_class;
+    M48txxInfo info;
+} M48txxISADeviceClass;
 
-typedef struct M48t59SysBusState {
-    SysBusDevice busdev;
+typedef struct M48txxSysBusState {
+    SysBusDevice parent_obj;
     M48t59State state;
     MemoryRegion io;
-} M48t59SysBusState;
+} M48txxSysBusState;
+
+typedef struct M48txxSysBusDeviceClass {
+    SysBusDeviceClass parent_class;
+    M48txxInfo info;
+} M48txxSysBusDeviceClass;
+
+static M48txxInfo m48txx_info[] = {
+    {
+        .sysbus_name = "sysbus-m48t02",
+        .model = 2,
+        .size = 0x800,
+    },{
+        .sysbus_name = "sysbus-m48t08",
+        .model = 8,
+        .size = 0x2000,
+    },{
+        .isa_name = "isa-m48t59",
+        .model = 59,
+        .size = 0x2000,
+    }
+};
+
 
 /* Fake timer functions */
 
@@ -613,7 +655,7 @@ static void m48t59_reset_common(M48t59State *NVRAM)
 
 static void m48t59_reset_isa(DeviceState *d)
 {
-    M48t59ISAState *isa = ISA_M48T59(d);
+    M48txxISAState *isa = M48TXX_ISA(d);
     M48t59State *NVRAM = &isa->state;
 
     m48t59_reset_common(NVRAM);
@@ -621,7 +663,7 @@ static void m48t59_reset_isa(DeviceState *d)
 
 static void m48t59_reset_sysbus(DeviceState *d)
 {
-    M48t59SysBusState *sys = container_of(d, M48t59SysBusState, busdev.qdev);
+    M48txxSysBusState *sys = M48TXX_SYS_BUS(d);
     M48t59State *NVRAM = &sys->state;
 
     m48t59_reset_common(NVRAM);
@@ -643,47 +685,62 @@ M48t59State *m48t59_init(qemu_irq IRQ, hwaddr mem_base,
 {
     DeviceState *dev;
     SysBusDevice *s;
-    M48t59SysBusState *d;
+    M48txxSysBusState *d;
     M48t59State *state;
+    int i;
 
-    dev = qdev_create(NULL, "m48t59");
-    qdev_prop_set_uint32(dev, "model", model);
-    qdev_prop_set_uint32(dev, "size", size);
-    qdev_prop_set_uint32(dev, "io_base", io_base);
-    qdev_init_nofail(dev);
-    s = SYS_BUS_DEVICE(dev);
-    d = FROM_SYSBUS(M48t59SysBusState, s);
-    state = &d->state;
-    sysbus_connect_irq(s, 0, IRQ);
-    if (io_base != 0) {
-        memory_region_add_subregion(get_system_io(), io_base,
-                                    sysbus_mmio_get_region(dev, 1));
-    }
-    if (mem_base != 0) {
-        sysbus_mmio_map(s, 0, mem_base);
+    for (i = 0; i < ARRAY_SIZE(m48txx_info); i++) {
+        if (!m48txx_info[i].sysbus_name ||
+            m48txx_info[i].size != size ||
+            m48txx_info[i].model != model) {
+            continue;
+        }
+
+        dev = qdev_create(NULL, m48txx_info[i].sysbus_name);
+        qdev_init_nofail(dev);
+        s = SYS_BUS_DEVICE(dev);
+        d = M48TXX_SYS_BUS(s);
+        state = &d->state;
+        sysbus_connect_irq(s, 0, IRQ);
+        memory_region_init_io(&d->io, &m48t59_io_ops, state, "m48t59", 4);
+        if (io_base != 0) {
+            memory_region_add_subregion(get_system_io(), io_base,
+                                        sysbus_mmio_get_region(s, 1));
+        }
+        if (mem_base != 0) {
+            sysbus_mmio_map(s, 0, mem_base);
+        }
+
+        return state;
     }
 
-    return state;
+    assert(false);
+    return NULL;
 }
 
 M48t59State *m48t59_init_isa(ISABus *bus, uint32_t io_base, uint16_t size,
                              int model)
 {
-    M48t59ISAState *d;
-    ISADevice *isadev;
+    M48txxISAState *d;
     DeviceState *dev;
-    M48t59State *s;
-
-    isadev = isa_create(bus, TYPE_ISA_M48T59);
-    dev = DEVICE(isadev);
-    qdev_prop_set_uint32(dev, "model", model);
-    qdev_prop_set_uint32(dev, "size", size);
-    qdev_prop_set_uint32(dev, "io_base", io_base);
-    qdev_init_nofail(dev);
-    d = ISA_M48T59(isadev);
-    s = &d->state;
-
-    return s;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(m48txx_info); i++) {
+        if (!m48txx_info[i].isa_name ||
+            m48txx_info[i].size != size ||
+            m48txx_info[i].model != model) {
+            continue;
+        }
+
+        dev = DEVICE(isa_create(bus, m48txx_info[i].isa_name));
+        qdev_prop_set_uint32(dev, "iobase", io_base);
+        qdev_init_nofail(dev);
+        d = container_of(ISA_DEVICE(dev), M48txxISAState, parent_obj);
+        return &d->state;
+    }
+
+    assert(false);
+    return NULL;
 }
 
 static void m48t59_init_common(M48t59State *s)
@@ -700,14 +757,19 @@ static void m48t59_init_common(M48t59State *s)
 
 static int m48t59_init_isa1(ISADevice *dev)
 {
-    M48t59ISAState *d = ISA_M48T59(dev);
+    ISADeviceClass *ic = ISA_DEVICE_GET_CLASS(dev);
+    M48txxISADeviceClass *u = container_of(ic, M48txxISADeviceClass,
+                                           parent_class);
+    M48txxISAState *d = container_of(dev, M48txxISAState, parent_obj);
     M48t59State *s = &d->state;
 
+    s->model = u->info.model;
+    s->size = u->info.size;
     isa_init_irq(dev, &s->IRQ, 8);
     m48t59_init_common(s);
     memory_region_init_io(&d->io, &m48t59_io_ops, s, "m48t59", 4);
-    if (s->io_base != 0) {
-        isa_register_ioport(dev, &d->io, s->io_base);
+    if (d->io_base != 0) {
+        isa_register_ioport(dev, &d->io, d->io_base);
     }
 
     return 0;
@@ -715,9 +777,14 @@ static int m48t59_init_isa1(ISADevice *dev)
 
 static int m48t59_init1(SysBusDevice *dev)
 {
-    M48t59SysBusState *d = FROM_SYSBUS(M48t59SysBusState, dev);
+    SysBusDeviceClass *k = SYS_BUS_DEVICE_GET_CLASS(dev);
+    M48txxSysBusDeviceClass *u = container_of(k, M48txxSysBusDeviceClass,
+                                              parent_class);
+    M48txxSysBusState *d = container_of(dev, M48txxSysBusState, parent_obj);
     M48t59State *s = &d->state;
 
+    s->model = u->info.model;
+    s->size = u->info.size;
     sysbus_init_irq(dev, &s->IRQ);
 
     memory_region_init_io(&s->iomem, &nvram_ops, s, "m48t59.nvram", s->size);
@@ -730,57 +797,86 @@ static int m48t59_init1(SysBusDevice *dev)
 }
 
 static Property m48t59_isa_properties[] = {
-    DEFINE_PROP_UINT32("size",    M48t59ISAState, state.size,    -1),
-    DEFINE_PROP_UINT32("model",   M48t59ISAState, state.model,   -1),
-    DEFINE_PROP_HEX32( "io_base", M48t59ISAState, state.io_base,  0),
+    DEFINE_PROP_HEX32("iobase", M48txxISAState, io_base, 0x74),
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static void m48t59_isa_class_init(ObjectClass *klass, void *data)
+static void m48txx_isa_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
+    M48txxISADeviceClass *u = M48TXX_ISA_CLASS(klass);
+    M48txxInfo *info = data;
+
     ic->init = m48t59_init_isa1;
     dc->no_user = 1;
     dc->reset = m48t59_reset_isa;
-    dc->props = m48t59_isa_properties;
+    if (info) {
+        dc->props = m48t59_isa_properties;
+        u->info = *info;
+    }
 }
 
-static const TypeInfo m48t59_isa_info = {
-    .name          = TYPE_ISA_M48T59,
-    .parent        = TYPE_ISA_DEVICE,
-    .instance_size = sizeof(M48t59ISAState),
-    .class_init    = m48t59_isa_class_init,
-};
-
-static Property m48t59_properties[] = {
-    DEFINE_PROP_UINT32("size",    M48t59SysBusState, state.size,    -1),
-    DEFINE_PROP_UINT32("model",   M48t59SysBusState, state.model,   -1),
-    DEFINE_PROP_HEX32( "io_base", M48t59SysBusState, state.io_base,  0),
-    DEFINE_PROP_END_OF_LIST(),
-};
-
-static void m48t59_class_init(ObjectClass *klass, void *data)
+static void m48txx_sysbus_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
+    M48txxSysBusDeviceClass *u = M48TXX_SYS_BUS_CLASS(klass);
+    M48txxInfo *info = data;
 
     k->init = m48t59_init1;
     dc->reset = m48t59_reset_sysbus;
-    dc->props = m48t59_properties;
+    if (info) {
+        u->info = *info;
+    }
 }
 
-static const TypeInfo m48t59_info = {
-    .name          = "m48t59",
-    .parent        = TYPE_SYS_BUS_DEVICE,
-    .instance_size = sizeof(M48t59SysBusState),
-    .class_init    = m48t59_class_init,
+static const TypeInfo m48txx_sysbus_type_info = {
+    .name = TYPE_M48TXX_SYS_BUS,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(M48txxSysBusState),
+    .abstract = true,
+    .class_init = m48txx_sysbus_class_init,
+};
+
+static const TypeInfo m48txx_isa_type_info = {
+    .name = TYPE_M48TXX_ISA,
+    .parent = TYPE_ISA_DEVICE,
+    .instance_size = sizeof(M48txxISAState),
+    .abstract = true,
+    .class_init = m48txx_isa_class_init,
 };
 
 static void m48t59_register_types(void)
 {
-    type_register_static(&m48t59_info);
-    type_register_static(&m48t59_isa_info);
+    TypeInfo sysbus_type_info = {
+        .parent = TYPE_M48TXX_SYS_BUS,
+        .class_size = sizeof(M48txxSysBusDeviceClass),
+        .class_init = m48txx_sysbus_class_init,
+    };
+    TypeInfo isa_type_info = {
+        .parent = TYPE_M48TXX_ISA,
+        .class_size = sizeof(M48txxISADeviceClass),
+        .class_init = m48txx_isa_class_init,
+    };
+    int i;
+
+    type_register_static(&m48txx_sysbus_type_info);
+    type_register_static(&m48txx_isa_type_info);
+
+    for (i = 0; i < ARRAY_SIZE(m48txx_info); i++) {
+        if (m48txx_info[i].sysbus_name) {
+            sysbus_type_info.name = m48txx_info[i].sysbus_name;
+            sysbus_type_info.class_data = &m48txx_info[i];
+            type_register(&sysbus_type_info);
+        }
+
+        if (m48txx_info[i].isa_name) {
+            isa_type_info.name = m48txx_info[i].isa_name;
+            isa_type_info.class_data = &m48txx_info[i];
+            type_register(&isa_type_info);
+        }
+    }
 }
 
 type_init(m48t59_register_types)
Signature · 1 line
1.7.10.4
#15 lore raw

Re: [Qemu-devel] [PATCH 4/7] m48t59: register a QOM type for each nvram type we support

Quoted 3 lines · Hervé Poussineau

As m48t59 devices can only be created with m48t59_init() or m48t59_init_isa(), we know exactly which nvram types are required. Register only those three types.

Wasn't this patch NACKed by Blue?

Quoted 385 lines

Remove .model and .size properties as they can be infered from nvram name.

Rename type to 'isa-*' (and 'sysbus-*') to do like other devices ISA devices (isa-ide, isa-parallel, isa-serial...)

---
 hw/timer/m48t59.c |  248 +++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 172 insertions(+), 76 deletions(-)
hw/timer/m48t59.c +172 -77
index fb78d20..23a6ab3 100644
@@ -2,6 +2,7 @@
  * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
  *
  * Copyright (c) 2003-2005, 2007 Jocelyn Mayer
+ * Copyright (c) 2013 Hervé Poussineau
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -37,12 +38,31 @@
 #define NVRAM_PRINTF(fmt, ...) do { } while (0)
 #endif
 
+#define TYPE_M48TXX_SYS_BUS "m48txx-sysbus"
+#define M48TXX_SYS_BUS_CLASS(klass) \
+    OBJECT_CLASS_CHECK(M48txxSysBusDeviceClass, (klass), TYPE_M48TXX_SYS_BUS)
+#define M48TXX_SYS_BUS(obj) \
+    OBJECT_CHECK(M48txxSysBusState, (obj), TYPE_M48TXX_SYS_BUS)
+
+#define TYPE_M48TXX_ISA "m48txx-isa"
+#define M48TXX_ISA_CLASS(klass) \
+    OBJECT_CLASS_CHECK(M48txxISADeviceClass, (klass), TYPE_M48TXX_ISA)
+#define M48TXX_ISA(obj) \
+    OBJECT_CHECK(M48txxISAState, (obj), TYPE_M48TXX_ISA)
+
 /*
  * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
  * alarm and a watchdog timer and related control registers. In the
  * PPC platform there is also a nvram lock function.
  */
 
+typedef struct M48txxInfo {
+    const char *isa_name;
+    const char *sysbus_name;
+    uint32_t model; /* 2 = m48t02, 8 = m48t08, 59 = m48t59 */
+    uint32_t size;
+} M48txxInfo;
+
 /*
  * Chipset docs:
  * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
@@ -54,7 +74,6 @@ struct M48t59State {
     /* Hardware parameters */
     qemu_irq IRQ;
     MemoryRegion iomem;
-    uint32_t io_base;
     uint32_t size;
     /* RTC management */
     time_t   time_offset;
@@ -72,22 +91,45 @@ struct M48t59State {
     uint8_t  lock;
 };
 
-#define TYPE_ISA_M48T59 "m48t59_isa"
-#define ISA_M48T59(obj) \
-    OBJECT_CHECK(M48t59ISAState, (obj), TYPE_ISA_M48T59)
-
-typedef struct M48t59ISAState {
+typedef struct M48txxISAState {
     ISADevice parent_obj;
-
     M48t59State state;
+    uint32_t io_base;
     MemoryRegion io;
-} M48t59ISAState;
+} M48txxISAState;
+
+typedef struct M48txxISADeviceClass {
+    ISADeviceClass parent_class;
+    M48txxInfo info;
+} M48txxISADeviceClass;
 
-typedef struct M48t59SysBusState {
-    SysBusDevice busdev;
+typedef struct M48txxSysBusState {
+    SysBusDevice parent_obj;
     M48t59State state;
     MemoryRegion io;
-} M48t59SysBusState;
+} M48txxSysBusState;
+
+typedef struct M48txxSysBusDeviceClass {
+    SysBusDeviceClass parent_class;
+    M48txxInfo info;
+} M48txxSysBusDeviceClass;
+
+static M48txxInfo m48txx_info[] = {
+    {
+        .sysbus_name = "sysbus-m48t02",
+        .model = 2,
+        .size = 0x800,
+    },{
+        .sysbus_name = "sysbus-m48t08",
+        .model = 8,
+        .size = 0x2000,
+    },{
+        .isa_name = "isa-m48t59",
+        .model = 59,
+        .size = 0x2000,
+    }
+};
+
 
 /* Fake timer functions */
 
@@ -613,7 +655,7 @@ static void m48t59_reset_common(M48t59State *NVRAM)
 
 static void m48t59_reset_isa(DeviceState *d)
 {
-    M48t59ISAState *isa = ISA_M48T59(d);
+    M48txxISAState *isa = M48TXX_ISA(d);
     M48t59State *NVRAM = &isa->state;
 
     m48t59_reset_common(NVRAM);
@@ -621,7 +663,7 @@ static void m48t59_reset_isa(DeviceState *d)
 
 static void m48t59_reset_sysbus(DeviceState *d)
 {
-    M48t59SysBusState *sys = container_of(d, M48t59SysBusState, busdev.qdev);
+    M48txxSysBusState *sys = M48TXX_SYS_BUS(d);
     M48t59State *NVRAM = &sys->state;
 
     m48t59_reset_common(NVRAM);
@@ -643,47 +685,62 @@ M48t59State *m48t59_init(qemu_irq IRQ, hwaddr mem_base,
 {
     DeviceState *dev;
     SysBusDevice *s;
-    M48t59SysBusState *d;
+    M48txxSysBusState *d;
     M48t59State *state;
+    int i;
 
-    dev = qdev_create(NULL, "m48t59");
-    qdev_prop_set_uint32(dev, "model", model);
-    qdev_prop_set_uint32(dev, "size", size);
-    qdev_prop_set_uint32(dev, "io_base", io_base);
-    qdev_init_nofail(dev);
-    s = SYS_BUS_DEVICE(dev);
-    d = FROM_SYSBUS(M48t59SysBusState, s);
-    state = &d->state;
-    sysbus_connect_irq(s, 0, IRQ);
-    if (io_base != 0) {
-        memory_region_add_subregion(get_system_io(), io_base,
-                                    sysbus_mmio_get_region(dev, 1));
-    }
-    if (mem_base != 0) {
-        sysbus_mmio_map(s, 0, mem_base);
+    for (i = 0; i < ARRAY_SIZE(m48txx_info); i++) {
+        if (!m48txx_info[i].sysbus_name ||
+            m48txx_info[i].size != size ||
+            m48txx_info[i].model != model) {
+            continue;
+        }
+
+        dev = qdev_create(NULL, m48txx_info[i].sysbus_name);
+        qdev_init_nofail(dev);
+        s = SYS_BUS_DEVICE(dev);
+        d = M48TXX_SYS_BUS(s);
+        state = &d->state;
+        sysbus_connect_irq(s, 0, IRQ);
+        memory_region_init_io(&d->io, &m48t59_io_ops, state, "m48t59", 4);
+        if (io_base != 0) {
+            memory_region_add_subregion(get_system_io(), io_base,
+                                        sysbus_mmio_get_region(s, 1));
+        }
+        if (mem_base != 0) {
+            sysbus_mmio_map(s, 0, mem_base);
+        }
+
+        return state;
     }
 
-    return state;
+    assert(false);
+    return NULL;
 }
 
 M48t59State *m48t59_init_isa(ISABus *bus, uint32_t io_base, uint16_t size,
                              int model)
 {
-    M48t59ISAState *d;
-    ISADevice *isadev;
+    M48txxISAState *d;
     DeviceState *dev;
-    M48t59State *s;
-
-    isadev = isa_create(bus, TYPE_ISA_M48T59);
-    dev = DEVICE(isadev);
-    qdev_prop_set_uint32(dev, "model", model);
-    qdev_prop_set_uint32(dev, "size", size);
-    qdev_prop_set_uint32(dev, "io_base", io_base);
-    qdev_init_nofail(dev);
-    d = ISA_M48T59(isadev);
-    s = &d->state;
-
-    return s;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(m48txx_info); i++) {
+        if (!m48txx_info[i].isa_name ||
+            m48txx_info[i].size != size ||
+            m48txx_info[i].model != model) {
+            continue;
+        }
+
+        dev = DEVICE(isa_create(bus, m48txx_info[i].isa_name));
+        qdev_prop_set_uint32(dev, "iobase", io_base);
+        qdev_init_nofail(dev);
+        d = container_of(ISA_DEVICE(dev), M48txxISAState, parent_obj);
+        return &d->state;
+    }
+
+    assert(false);
+    return NULL;
 }
 
 static void m48t59_init_common(M48t59State *s)
@@ -700,14 +757,19 @@ static void m48t59_init_common(M48t59State *s)
 
 static int m48t59_init_isa1(ISADevice *dev)
 {
-    M48t59ISAState *d = ISA_M48T59(dev);
+    ISADeviceClass *ic = ISA_DEVICE_GET_CLASS(dev);
+    M48txxISADeviceClass *u = container_of(ic, M48txxISADeviceClass,
+                                           parent_class);
+    M48txxISAState *d = container_of(dev, M48txxISAState, parent_obj);
     M48t59State *s = &d->state;
 
+    s->model = u->info.model;
+    s->size = u->info.size;
     isa_init_irq(dev, &s->IRQ, 8);
     m48t59_init_common(s);
     memory_region_init_io(&d->io, &m48t59_io_ops, s, "m48t59", 4);
-    if (s->io_base != 0) {
-        isa_register_ioport(dev, &d->io, s->io_base);
+    if (d->io_base != 0) {
+        isa_register_ioport(dev, &d->io, d->io_base);
     }
 
     return 0;
@@ -715,9 +777,14 @@ static int m48t59_init_isa1(ISADevice *dev)
 
 static int m48t59_init1(SysBusDevice *dev)
 {
-    M48t59SysBusState *d = FROM_SYSBUS(M48t59SysBusState, dev);
+    SysBusDeviceClass *k = SYS_BUS_DEVICE_GET_CLASS(dev);
+    M48txxSysBusDeviceClass *u = container_of(k, M48txxSysBusDeviceClass,
+                                              parent_class);
+    M48txxSysBusState *d = container_of(dev, M48txxSysBusState, parent_obj);
     M48t59State *s = &d->state;
 
+    s->model = u->info.model;
+    s->size = u->info.size;
     sysbus_init_irq(dev, &s->IRQ);
 
     memory_region_init_io(&s->iomem, &nvram_ops, s, "m48t59.nvram", s->size);
@@ -730,57 +797,86 @@ static int m48t59_init1(SysBusDevice *dev)
 }
 
 static Property m48t59_isa_properties[] = {
-    DEFINE_PROP_UINT32("size",    M48t59ISAState, state.size,    -1),
-    DEFINE_PROP_UINT32("model",   M48t59ISAState, state.model,   -1),
-    DEFINE_PROP_HEX32( "io_base", M48t59ISAState, state.io_base,  0),
+    DEFINE_PROP_HEX32("iobase", M48txxISAState, io_base, 0x74),
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static void m48t59_isa_class_init(ObjectClass *klass, void *data)
+static void m48txx_isa_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
+    M48txxISADeviceClass *u = M48TXX_ISA_CLASS(klass);
+    M48txxInfo *info = data;
+
     ic->init = m48t59_init_isa1;
     dc->no_user = 1;
     dc->reset = m48t59_reset_isa;
-    dc->props = m48t59_isa_properties;
+    if (info) {
+        dc->props = m48t59_isa_properties;
+        u->info = *info;
+    }
 }
 
-static const TypeInfo m48t59_isa_info = {
-    .name          = TYPE_ISA_M48T59,
-    .parent        = TYPE_ISA_DEVICE,
-    .instance_size = sizeof(M48t59ISAState),
-    .class_init    = m48t59_isa_class_init,
-};
-
-static Property m48t59_properties[] = {
-    DEFINE_PROP_UINT32("size",    M48t59SysBusState, state.size,    -1),
-    DEFINE_PROP_UINT32("model",   M48t59SysBusState, state.model,   -1),
-    DEFINE_PROP_HEX32( "io_base", M48t59SysBusState, state.io_base,  0),
-    DEFINE_PROP_END_OF_LIST(),
-};
-
-static void m48t59_class_init(ObjectClass *klass, void *data)
+static void m48txx_sysbus_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
+    M48txxSysBusDeviceClass *u = M48TXX_SYS_BUS_CLASS(klass);
+    M48txxInfo *info = data;
 
     k->init = m48t59_init1;
     dc->reset = m48t59_reset_sysbus;
-    dc->props = m48t59_properties;
+    if (info) {
+        u->info = *info;
+    }
 }
 
-static const TypeInfo m48t59_info = {
-    .name          = "m48t59",
-    .parent        = TYPE_SYS_BUS_DEVICE,
-    .instance_size = sizeof(M48t59SysBusState),
-    .class_init    = m48t59_class_init,
+static const TypeInfo m48txx_sysbus_type_info = {
+    .name = TYPE_M48TXX_SYS_BUS,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(M48txxSysBusState),
+    .abstract = true,
+    .class_init = m48txx_sysbus_class_init,
+};
+
+static const TypeInfo m48txx_isa_type_info = {
+    .name = TYPE_M48TXX_ISA,
+    .parent = TYPE_ISA_DEVICE,
+    .instance_size = sizeof(M48txxISAState),
+    .abstract = true,
+    .class_init = m48txx_isa_class_init,
 };
 
 static void m48t59_register_types(void)
 {
-    type_register_static(&m48t59_info);
-    type_register_static(&m48t59_isa_info);
+    TypeInfo sysbus_type_info = {
+        .parent = TYPE_M48TXX_SYS_BUS,
+        .class_size = sizeof(M48txxSysBusDeviceClass),
+        .class_init = m48txx_sysbus_class_init,
+    };
+    TypeInfo isa_type_info = {
+        .parent = TYPE_M48TXX_ISA,
+        .class_size = sizeof(M48txxISADeviceClass),
+        .class_init = m48txx_isa_class_init,
+    };
+    int i;
+
+    type_register_static(&m48txx_sysbus_type_info);
+    type_register_static(&m48txx_isa_type_info);
+
+    for (i = 0; i < ARRAY_SIZE(m48txx_info); i++) {
+        if (m48txx_info[i].sysbus_name) {
+            sysbus_type_info.name = m48txx_info[i].sysbus_name;
+            sysbus_type_info.class_data = &m48txx_info[i];
+            type_register(&sysbus_type_info);
+        }
+
+        if (m48txx_info[i].isa_name) {
+            isa_type_info.name = m48txx_info[i].isa_name;
+            isa_type_info.class_data = &m48txx_info[i];
+            type_register(&isa_type_info);
+        }
+    }
 }
 
 type_init(m48t59_register_types)
--
Quoted 3 lines

1.7.10.4

--
Regards,
Artyom Tarasenko

linux/sparc and solaris/sparc under qemu blog:
http://tyom.blogspot.com/search/label/qemu

#16 lore raw

Re: [Qemu-devel] [PATCH 4/7] m48t59: register a QOM type for each nvram type we support

Quoted 6 lines · Artyom Tarasenko
Quoted 3 lines · Hervé Poussineau

As m48t59 devices can only be created with m48t59_init() or m48t59_init_isa(), we know exactly which nvram types are required. Register only those three types.

Wasn't this patch NACKed by Blue?

I think I changed what's requested, ie I kept the io-base property of the ISA variant, so the sun4u emulation can be changed later to the right address. However, fixing sun4u emulation is not part of this patchset.

Otherwise, if I didn't understood, please elaborate.

Regards,

Hervé

#17 lore raw

Re: [Qemu-devel] [PATCH 4/7] m48t59: register a QOM type for each nvram type we support

Quoted 11 lines · Hervé Poussineau
Quoted 9 lines · Artyom Tarasenko
Quoted 5 lines · Hervé Poussineau

As m48t59 devices can only be created with m48t59_init() or m48t59_init_isa(),
we know exactly which nvram types are required. Register only those three types.

Quoted 6 lines
Quoted 1 line

Wasn't this patch NACKed by Blue?

I think I changed what's requested, ie I kept the io-base property of the ISA variant, so the sun4u emulation can be changed later to the right address. However, fixing sun4u emulation is not part of this patchset.

Sorry, I misread the patch. The value 0x74 is now just a default value for the isa port, right? How would I create an isa m48t59 card on some other port?

Artyom

--
Regards,
Artyom Tarasenko

linux/sparc and solaris/sparc under qemu blog:
http://tyom.blogspot.com/search/label/qemu

#18 lore raw

Re: [Qemu-devel] [PATCH 4/7] m48t59: register a QOM type for each nvram type we support

Quoted 10 lines · Artyom Tarasenko
Quoted 9 lines · Hervé Poussineau
Quoted 7 lines · Artyom Tarasenko
Quoted 4 lines · Hervé Poussineau

As m48t59 devices can only be created with m48t59_init() or m48t59_init_isa(),
we know exactly which nvram types are required. Register only those three types.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Wasn't this patch NACKed by Blue?

I think I changed what's requested, ie I kept the io-base property of the ISA variant, so the sun4u emulation can be changed later to the right address. However, fixing sun4u emulation is not part of this patchset.

Sorry, I misread the patch. The value 0x74 is now just a default value for the isa port, right? How would I create an isa m48t59 card on some other port?

Yes, 0x74 is now the default value.

You can create an isa m48t59 device on port 0x100
- with command line: -device isa-m4859,iobase=0x100
- in code using the helper: m48t59_init_isa(isa_bus, 0x100, 0x2000, 59);

- with QOM: DeviceState *dev;
            dev = DEVICE(isa_create(isa_bus, "isa-m48t59"));
            qdev_prop_set_uint16(dev, "iobase", 0x100);
            qdev_init_nofail(dev);

Hervé

#19 lore raw

PATCH 5/7 m48t59: add a Nvram interface

In reply to Hervé Poussineau
---
 hw/timer/m48t59.c         |   61 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/timer/m48t59.h |   24 ++++++++++++++++++
 2 files changed, 85 insertions(+)
hw/timer/m48t59.c +61 -0
index 23a6ab3..3ecb14e 100644
@@ -796,6 +796,24 @@ static int m48t59_init1(SysBusDevice *dev)
     return 0;
 }
 
+static uint32_t m48txx_isa_read(Nvram *obj, uint32_t addr)
+{
+    M48txxISAState *d = M48TXX_ISA(obj);
+    return m48t59_read(&d->state, addr);
+}
+
+static void m48txx_isa_write(Nvram *obj, uint32_t addr, uint32_t val)
+{
+    M48txxISAState *d = M48TXX_ISA(obj);
+    m48t59_write(&d->state, addr, val);
+}
+
+static void m48txx_isa_toggle_lock(Nvram *obj, int lock)
+{
+    M48txxISAState *d = M48TXX_ISA(obj);
+    m48t59_toggle_lock(&d->state, lock);
+}
+
 static Property m48t59_isa_properties[] = {
     DEFINE_PROP_HEX32("iobase", M48txxISAState, io_base, 0x74),
     DEFINE_PROP_END_OF_LIST(),
@@ -805,6 +823,7 @@ static void m48txx_isa_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
+    NvramClass *nc = NVRAM_CLASS(klass);
     M48txxISADeviceClass *u = M48TXX_ISA_CLASS(klass);
     M48txxInfo *info = data;
 
@@ -814,13 +833,36 @@ static void m48txx_isa_class_init(ObjectClass *klass, void *data)
     if (info) {
         dc->props = m48t59_isa_properties;
         u->info = *info;
+    } else {
+        nc->read = m48txx_isa_read;
+        nc->write = m48txx_isa_write;
+        nc->toggle_lock = m48txx_isa_toggle_lock;
     }
 }
 
+static uint32_t m48txx_sysbus_read(Nvram *obj, uint32_t addr)
+{
+    M48txxSysBusState *d = M48TXX_SYS_BUS(obj);
+    return m48t59_read(&d->state, addr);
+}
+
+static void m48txx_sysbus_write(Nvram *obj, uint32_t addr, uint32_t val)
+{
+    M48txxSysBusState *d = M48TXX_SYS_BUS(obj);
+    m48t59_write(&d->state, addr, val);
+}
+
+static void m48txx_sysbus_toggle_lock(Nvram *obj, int lock)
+{
+    M48txxSysBusState *d = M48TXX_SYS_BUS(obj);
+    m48t59_toggle_lock(&d->state, lock);
+}
+
 static void m48txx_sysbus_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
+    NvramClass *nc = NVRAM_CLASS(klass);
     M48txxSysBusDeviceClass *u = M48TXX_SYS_BUS_CLASS(klass);
     M48txxInfo *info = data;
 
@@ -828,15 +870,29 @@ static void m48txx_sysbus_class_init(ObjectClass *klass, void *data)
     dc->reset = m48t59_reset_sysbus;
     if (info) {
         u->info = *info;
+    } else {
+        nc->read = m48txx_sysbus_read;
+        nc->write = m48txx_sysbus_write;
+        nc->toggle_lock = m48txx_sysbus_toggle_lock;
     }
 }
 
+static const TypeInfo nvram_info = {
+    .name = TYPE_NVRAM,
+    .parent = TYPE_INTERFACE,
+    .class_size = sizeof(NvramClass),
+};
+
 static const TypeInfo m48txx_sysbus_type_info = {
     .name = TYPE_M48TXX_SYS_BUS,
     .parent = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(M48txxSysBusState),
     .abstract = true,
     .class_init = m48txx_sysbus_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_NVRAM },
+        { }
+    }
 };
 
 static const TypeInfo m48txx_isa_type_info = {
@@ -845,6 +901,10 @@ static const TypeInfo m48txx_isa_type_info = {
     .instance_size = sizeof(M48txxISAState),
     .abstract = true,
     .class_init = m48txx_isa_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_NVRAM },
+        { }
+    }
 };
 
 static void m48t59_register_types(void)
@@ -861,6 +921,7 @@ static void m48t59_register_types(void)
     };
     int i;
 
+    type_register_static(&nvram_info);
     type_register_static(&m48txx_sysbus_type_info);
     type_register_static(&m48txx_isa_type_info);
 
include/hw/timer/m48t59.h +24 -0
index 59337fa..72b7ac1 100644
@@ -1,6 +1,9 @@
 #ifndef NVRAM_H
 #define NVRAM_H
 
+#include "qemu-common.h"
+#include "qom/object.h"
+
 /* NVRAM helpers */
 typedef uint32_t (*nvram_read_t)(void *private, uint32_t addr);
 typedef void (*nvram_write_t)(void *private, uint32_t addr, uint32_t val);
@@ -31,4 +34,25 @@ M48t59State *m48t59_init_isa(ISABus *bus, uint32_t io_base, uint16_t size,
 M48t59State *m48t59_init(qemu_irq IRQ, hwaddr mem_base,
                          uint32_t io_base, uint16_t size, int type);
 
+#define TYPE_NVRAM "nvram"
+
+#define NVRAM_CLASS(klass) \
+    OBJECT_CLASS_CHECK(NvramClass, (klass), TYPE_NVRAM)
+#define NVRAM_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(NvramClass, (obj), TYPE_NVRAM)
+#define NVRAM(obj) \
+    INTERFACE_CHECK(Nvram, (obj), TYPE_NVRAM)
+
+typedef struct Nvram {
+    Object parent;
+} Nvram;
+
+typedef struct NvramClass {
+    InterfaceClass parent;
+
+    uint32_t (*read)(Nvram *obj, uint32_t addr);
+    void (*write)(Nvram *obj, uint32_t addr, uint32_t val);
+    void (*toggle_lock)(Nvram *obj, int lock);
+} NvramClass;
+
 #endif /* !NVRAM_H */
Signature · 1 line
1.7.10.4
#20 lore raw

PATCH 6/7 prep: add IBM RS/6000 7248 (43p) machine emulation

In reply to Hervé Poussineau

Machine is very simple (only one PCI host bridge and an ISA bridge). Provide a ibm_43p.cfg file to add more devices to this machine.

Syntax is:
qemu-system-ppc -M 43p -readconfig ibm_43p.cfg

---
 docs/ibm_43p.cfg |   38 ++++++++++
 hw/ppc/prep.c    |  213 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 251 insertions(+)
 create mode 100644 docs/ibm_43p.cfg
docs/ibm_43p.cfg new file +38 -0
new file mode 100644
index 0000000..70bbfdb
@@ -0,0 +1,38 @@
+############################################################################
+#
+# qemu-system-ppc -M 43p creates a bare machine with just the very essential
+# chipset devices being present:
+#
+#     00.0 - Host bridge
+#     0b.0 - ISA bridge
+#
+# This config file documents the other devices and how they are
+# created.  You can simply use "-readconfig $thisfile" to create
+# them all.
+
+[device]
+  driver = "i8042"
+
+[device]
+  driver = "pc87312"
+  config = "12"
+
+[device]
+  driver = "pcnet"
+  addr = "12.0"
+
+[device]
+  driver = "isa-m48t59"
+  iobase = "0x74"
+
+[device]
+  driver = "isa-ide"
+  iobase = "0x1f0"
+  iobase2 = "0x3f6"
+  irq = "14"
+
+[device]
+  driver = "isa-ide"
+  iobase = "0x170"
+  iobase2 = "0x376"
+  irq = "15"
hw/ppc/prep.c +213 -0

252 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#21 lore raw

PATCH 7/7 prep: QOM'ify System I/O

In reply to Hervé Poussineau

Most of the functionality is extracted from hw/ppc/prep.c. Also add support for board identification/equipment registers.

Document it for the IBM 43p emulation.

---
 docs/ibm_43p.cfg       |    5 +
 hw/ppc/Makefile.objs   |    1 +
 hw/ppc/prep_systemio.c |  298 ++++++++++++++++++++++++++++++++++++++++++++++++
 trace-events           |    4 +
 4 files changed, 308 insertions(+)
 create mode 100644 hw/ppc/prep_systemio.c
docs/ibm_43p.cfg +5 -0
index 70bbfdb..653d50b 100644
@@ -36,3 +36,8 @@
   iobase = "0x170"
   iobase2 = "0x376"
   irq = "15"
+
+[device]
+  driver = "prep-systemio"
+  ibm-planar-id = "0xc0"
+  equipment = "0xff"
hw/ppc/Makefile.objs +1 -0
index be00d1d..cd1eb1b 100644
@@ -9,6 +9,7 @@ obj-y += ppc405_boards.o ppc4xx_devs.o ppc405_uc.o ppc440_bamboo.o
 obj-y += ppc4xx_pci.o
 # PReP
 obj-y += prep.o
+obj-y += prep_systemio.o
 # OldWorld PowerMac
 obj-y += mac_oldworld.o
 # NewWorld PowerMac
hw/ppc/prep_systemio.c new file +298 -0

298 lines not on this page · raw message

trace-events +4 -0
index 1970b5c..be35eed 100644
@@ -773,6 +773,10 @@ mpc105_simm_enable(int id, uint32_t start, uint32_t end) "SIMM #%d 0x%08x-0x%08x
 mpc105_simm_disable(int id) "SIMM #%d disabled"
 mpc105_simm_size(int id, uint32_t size) "SIMM #%d is %u MB"
 
+# hw/ppc/prep_systemio.c
+prep_systemio_read(uint32_t addr, uint32_t val) "read addr=%x val=%x"
+prep_systemio_write(uint32_t addr, uint32_t val) "write addr=%x val=%x"
+
 # hw/scsi/vmw_pvscsi.c
 pvscsi_ring_init_data(uint32_t txr_len_log2, uint32_t rxr_len_log2) "TX/RX rings logarithms set to %d/%d"
 pvscsi_ring_init_msg(uint32_t len_log2) "MSG ring logarithm set to %d"
Signature · 1 line
1.7.10.4
#22 lore raw

Re: [Qemu-devel] [PATCH 7/7] prep: QOM'ify System I/O

Am 02.05.2013 22:09, schrieb Hervé Poussineau:

Quoted 7 lines

Most of the functionality is extracted from hw/ppc/prep.c. Also add support for board identification/equipment registers.

Document it for the IBM 43p emulation.

Quoted 7 lines
---
 docs/ibm_43p.cfg       |    5 +
 hw/ppc/Makefile.objs   |    1 +
 hw/ppc/prep_systemio.c |  298 ++++++++++++++++++++++++++++++++++++++++++++++++
 trace-events           |    4 +
 4 files changed, 308 insertions(+)
 create mode 100644 hw/ppc/prep_systemio.c

Haven't reviewed the full patch yet, but since this is not modifying hw/ppc/prep.c, it is duplicating code rather than QOM'ifying the existing code.

Have you looked into Julio's patch whom you CC? I'm still not sure how to solve things for 1.5 (and this series a consider -next).

Regards,
Andreas

#23 lore raw

Re: [Qemu-devel] [PATCH 7/7] prep: QOM'ify System I/O

Quoted 16 lines · Andreas Färber

Am 02.05.2013 22:09, schrieb Hervé Poussineau:

Quoted 7 lines

Most of the functionality is extracted from hw/ppc/prep.c. Also add support for board identification/equipment registers.

Document it for the IBM 43p emulation.

Quoted 7 lines
---
 docs/ibm_43p.cfg       |    5 +
 hw/ppc/Makefile.objs   |    1 +
 hw/ppc/prep_systemio.c |  298 ++++++++++++++++++++++++++++++++++++++++++++++++
 trace-events           |    4 +
 4 files changed, 308 insertions(+)
 create mode 100644 hw/ppc/prep_systemio.c
Quoted 6 lines

Haven't reviewed the full patch yet, but since this is not modifying hw/ppc/prep.c, it is duplicating code rather than QOM'ifying the existing code.

Have you looked into Julio's patch whom you CC? I'm still not sure how to solve things for 1.5 (and this series a consider -next).

Yes, I've partly taken Julio's patch into account, ie port 0x92 is now read/write. However, I didn't change the way the reset is done due to missing agrement of how it should be done: "this is touching on the same soft reset topic that I am awaiting the outcome for x86". [1]

Moreover, registers emulated are not exactly the same as in hw/ppc/prep.c:
Registers not present in hw/ppc/prep_systemio.c:
0x800: Motorola CPU configuration register
0x802: Motorola base module feature register
0x803: Motorola base module status register
0x823: Something related to no L2 cache?
Those seem specific to Motorola, so they probably belong to another device.

New registers added to hw/ppc/prep_systemio.c:
0x818: Key lock (Read Only)
0x852: System Board Identification (Read Only)

I'm not really sure of impacts of changing 'prep' machine to remove some registers and adding new ones, so I prefered to keep it as is. However, if you think that I should use the QOM'ified System I/O device in 'prep' machine (and this will change emulated machine), I'll do it.

Hervé

[1] https://lists.gnu.org/archive/html/qemu-devel/2013-04/msg03359.html