Skip to contentSkip to conversation

ppc4xx_sdram QOMify and clean ups

PATCH Superseded BALATON Zoltan 2 participants 41 messages Sep 13, 2022 – Sep 18, 2022 Arm/Amlogic Meson SoC support

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover ppc4xx_sdram QOMify and clean ups

This is the end of the QOMify series started by Cédric. This series handles the SDRAM controller models to clean them up, QOMify and unify them and at least partially clean up the mess that has accumulated around these in the past. This includes the not yet merged patches from the last series and new ones that change the DDR2 version used by sam460ex.

v3: Fix patches that got squashed during rebase
v2: address some review comments and try to avoid compile problem with gcc 12.2 (untested)

BALATON Zoltan (20):
ppc440_bamboo: Remove unnecessary memsets
ppc4xx: Introduce Ppc4xxSdramBank struct
ppc4xx_sdram: Get rid of the init RAM hack
ppc4xx: Use Ppc4xxSdramBank in ppc4xx_sdram_banks()
ppc440_bamboo: Add missing 4 MiB valid memory size
ppc4xx_sdram: Move size check to ppc4xx_sdram_init()
ppc4xx_sdram: QOM'ify
ppc4xx_sdram: Drop extra zeros for readability
ppc440_sdram: Split off map/unmap of sdram banks for later reuse
ppc440_sdram: Implement enable bit in the DDR2 SDRAM
ppc440_sdram: Get rid of the init RAM hack
ppc440_sdram: Rename local variable for readibility
ppc4xx_sdram: Rename functions to prevent name clashes
ppc440_sdram: Move RAM size check to ppc440_sdram_init
ppc440_sdram: QOM'ify
ppc4xx_sdram: Move ppc4xx DDR and DDR2 SDRAM controller models
together
ppc4xx_sdram: Use hwaddr for memory bank size
ppc4xx_sdram: Rename local state variable for brevity
ppc4xx_sdram: Generalise bank setup
ppc4xx_sdram: Convert DDR SDRAM controller to new bank handling

 hw/ppc/meson.build      |   3 +-
 hw/ppc/ppc405.h         |   8 +-
 hw/ppc/ppc405_boards.c  |  22 +-
 hw/ppc/ppc405_uc.c      |  33 +-
 hw/ppc/ppc440.h         |   4 -
 hw/ppc/ppc440_bamboo.c  |  29 +-
 hw/ppc/ppc440_uc.c      | 267 +--------------
 hw/ppc/ppc4xx_devs.c    | 413 -----------------------
 hw/ppc/ppc4xx_sdram.c   | 723 ++++++++++++++++++++++++++++++++++++++++
 hw/ppc/sam460ex.c       |  48 +--
 hw/ppc/trace-events     |   1 +
 include/hw/ppc/ppc4xx.h |  66 +++-
 12 files changed, 847 insertions(+), 770 deletions(-)
 create mode 100644 hw/ppc/ppc4xx_sdram.c
Signature · 1 line
2.30.4
#2 lore raw

PATCH v3 2/20 ppc4xx: Introduce Ppc4xxSdramBank struct

Instead of storing sdram bank parameters in unrelated arrays put them in a struct so it's clear they belong to the same bank and simplify the state struct using this bank type.

---
 hw/ppc/ppc440_uc.c      | 49 +++++++++++++++++-----------------
 hw/ppc/ppc4xx_devs.c    | 59 ++++++++++++++++++++---------------------
 include/hw/ppc/ppc4xx.h |  8 ++++++
 3 files changed, 61 insertions(+), 55 deletions(-)
hw/ppc/ppc440_uc.c +24 -25
index 53e981ddf4..db33334e29 100644
@@ -16,7 +16,7 @@
 #include "qemu/module.h"
 #include "hw/irq.h"
 #include "exec/memory.h"
-#include "hw/ppc/ppc.h"
+#include "hw/ppc/ppc4xx.h"
 #include "hw/qdev-properties.h"
 #include "hw/pci/pci.h"
 #include "sysemu/block-backend.h"
@@ -485,11 +485,7 @@ void ppc4xx_sdr_init(CPUPPCState *env)
 typedef struct ppc440_sdram_t {
     uint32_t addr;
     int nbanks;
-    MemoryRegion containers[4]; /* used for clipping */
-    MemoryRegion *ram_memories;
-    hwaddr ram_bases[4];
-    hwaddr ram_sizes[4];
-    uint32_t bcr[4];
+    Ppc4xxSdramBank bank[4];
 } ppc440_sdram_t;
 
 enum {
@@ -570,23 +566,23 @@ static uint64_t sdram_size(uint32_t bcr)
 static void sdram_set_bcr(ppc440_sdram_t *sdram, int i,
                           uint32_t bcr, int enabled)
 {
-    if (sdram->bcr[i] & 1) {
+    if (sdram->bank[i].bcr & 1) {
         /* First unmap RAM if enabled */
         memory_region_del_subregion(get_system_memory(),
-                                    &sdram->containers[i]);
-        memory_region_del_subregion(&sdram->containers[i],
-                                    &sdram->ram_memories[i]);
-        object_unparent(OBJECT(&sdram->containers[i]));
+                                    &sdram->bank[i].container);
+        memory_region_del_subregion(&sdram->bank[i].container,
+                                    &sdram->bank[i].ram);
+        object_unparent(OBJECT(&sdram->bank[i].container));
     }
-    sdram->bcr[i] = bcr & 0xffe0ffc1;
+    sdram->bank[i].bcr = bcr & 0xffe0ffc1;
     if (enabled && (bcr & 1)) {
-        memory_region_init(&sdram->containers[i], NULL, "sdram-containers",
+        memory_region_init(&sdram->bank[i].container, NULL, "sdram-container",
                            sdram_size(bcr));
-        memory_region_add_subregion(&sdram->containers[i], 0,
-                                    &sdram->ram_memories[i]);
+        memory_region_add_subregion(&sdram->bank[i].container, 0,
+                                    &sdram->bank[i].ram);
         memory_region_add_subregion(get_system_memory(),
                                     sdram_base(bcr),
-                                    &sdram->containers[i]);
+                                    &sdram->bank[i].container);
     }
 }
 
@@ -595,9 +591,9 @@ static void sdram_map_bcr(ppc440_sdram_t *sdram)
     int i;
 
     for (i = 0; i < sdram->nbanks; i++) {
-        if (sdram->ram_sizes[i] != 0) {
-            sdram_set_bcr(sdram, i, sdram_bcr(sdram->ram_bases[i],
-                                              sdram->ram_sizes[i]), 1);
+        if (sdram->bank[i].size != 0) {
+            sdram_set_bcr(sdram, i, sdram_bcr(sdram->bank[i].base,
+                                              sdram->bank[i].size), 1);
         } else {
             sdram_set_bcr(sdram, i, 0, 0);
         }
@@ -614,9 +610,9 @@ static uint32_t dcr_read_sdram(void *opaque, int dcrn)
     case SDRAM_R1BAS:
     case SDRAM_R2BAS:
     case SDRAM_R3BAS:
-        if (sdram->ram_sizes[dcrn - SDRAM_R0BAS]) {
-            ret = sdram_bcr(sdram->ram_bases[dcrn - SDRAM_R0BAS],
-                            sdram->ram_sizes[dcrn - SDRAM_R0BAS]);
+        if (sdram->bank[dcrn - SDRAM_R0BAS].size) {
+            ret = sdram_bcr(sdram->bank[dcrn - SDRAM_R0BAS].base,
+                            sdram->bank[dcrn - SDRAM_R0BAS].size);
         }
         break;
     case SDRAM_CONF1HB:
@@ -701,12 +697,15 @@ void ppc440_sdram_init(CPUPPCState *env, int nbanks,
                        int do_init)
 {
     ppc440_sdram_t *sdram;
+    int i;
 
     sdram = g_malloc0(sizeof(*sdram));
     sdram->nbanks = nbanks;
-    sdram->ram_memories = ram_memories;
-    memcpy(sdram->ram_bases, ram_bases, nbanks * sizeof(hwaddr));
-    memcpy(sdram->ram_sizes, ram_sizes, nbanks * sizeof(hwaddr));
+    for (i = 0; i < nbanks; i++) {
+        sdram->bank[i].ram = ram_memories[i];
+        sdram->bank[i].base = ram_bases[i];
+        sdram->bank[i].size = ram_sizes[i];
+    }
     qemu_register_reset(&sdram_reset, sdram);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
hw/ppc/ppc4xx_devs.c +29 -30

120 lines not on this page · raw message

include/hw/ppc/ppc4xx.h +8 -0
index a1781afa8e..2af0d60577 100644
@@ -29,6 +29,14 @@
 #include "exec/memory.h"
 #include "hw/sysbus.h"
 
+typedef struct {
+    MemoryRegion ram;
+    MemoryRegion container; /* used for clipping */
+    hwaddr base;
+    hwaddr size;
+    uint32_t bcr;
+} Ppc4xxSdramBank;
+
 void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                         MemoryRegion ram_memories[],
                         hwaddr ram_bases[], hwaddr ram_sizes[],
Signature · 1 line
2.30.4
#3 lore raw

PATCH v3 3/20 ppc4xx_sdram: Get rid of the init RAM hack

In reply to BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

 hw/ppc/ppc405.h         |  1 -
 hw/ppc/ppc405_boards.c  | 12 ++++++++++--
 hw/ppc/ppc405_uc.c      |  4 +---
 hw/ppc/ppc440_bamboo.c  |  8 +++++++-
 hw/ppc/ppc440_uc.c      |  2 --
 hw/ppc/ppc4xx_devs.c    | 11 +----------
 include/hw/ppc/ppc4xx.h |  8 ++++++--
 7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
     /* Public */
     MemoryRegion ram_banks[2];
     hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
 
     MemoryRegion *dram_mr;
     hwaddr ram_size;
hw/ppc/ppc405_boards.c +10 -2
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
     MachineClass *mc = MACHINE_GET_CLASS(machine);
     const char *kernel_filename = machine->kernel_filename;
     MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
 
     if (machine->ram_size != mc->default_ram_size) {
         char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                              machine->ram_size, &error_fatal);
     object_property_set_link(OBJECT(&ppc405->soc), "dram",
                              OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
     object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                              &error_abort);
     qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);
 
+    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */
+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("Could not enable memory regions");
+        exit(1);
+    }
+
     /* allocate and load BIOS */
     if (machine->firmware) {
         MemoryRegion *bios = g_new(MemoryRegion, 1);
hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                              s->ram_bases[0], s->ram_sizes[0]);
 
     ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
 
     /* External bus controller */
     if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
 static Property ppc405_soc_properties[] = {
     DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                      MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
     DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
hw/ppc/ppc440_bamboo.c +7 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
     ppc4xx_sdram_init(env,
                       qdev_get_gpio_in(uicdev, 14),
                       PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the firmware */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("couldn't enable memory regions");
+        exit(1);
+    }
 
     /* PCI */
     dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
 } ppc440_sdram_t;
 
 enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
     SDRAM_R0BAS = 0x40,
     SDRAM_R1BAS,
     SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -10
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
     qemu_irq irq;
 };
 
-enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
 /*
  * XXX: TOFIX: some patches have made this code become inconsistent:
  *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
 void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                        MemoryRegion *ram_memories,
                        hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
 {
     ppc4xx_sdram_t *sdram;
     int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM0_CFGDATA,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
 }
 
 /*
include/hw/ppc/ppc4xx.h +6 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
     uint32_t bcr;
 } Ppc4xxSdramBank;
 
+enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
 void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                         MemoryRegion ram_memories[],
                         hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
 void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                         MemoryRegion ram_memories[],
                         hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
 
 #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
Signature · 1 line
2.30.4
#1 lore raw

PATCH v3 1/20 ppc440_bamboo: Remove unnecessary memsets

In reply to BALATON Zoltan

In ppc4xx_sdram_init() the struct is allocated with g_new0() so no need to clear its elements. In the bamboo machine init memset can be replaced with array initialiser which is shorter.

---
 hw/ppc/ppc440_bamboo.c | 6 ++----
 hw/ppc/ppc4xx_devs.c   | 8 ++------
 2 files changed, 4 insertions(+), 10 deletions(-)
hw/ppc/ppc440_bamboo.c +2 -4
index ea945a1c99..5ec82fa8c2 100644
@@ -169,8 +169,8 @@ static void bamboo_init(MachineState *machine)
     MemoryRegion *address_space_mem = get_system_memory();
     MemoryRegion *isa = g_new(MemoryRegion, 1);
     MemoryRegion *ram_memories = g_new(MemoryRegion, PPC440EP_SDRAM_NR_BANKS);
-    hwaddr ram_bases[PPC440EP_SDRAM_NR_BANKS];
-    hwaddr ram_sizes[PPC440EP_SDRAM_NR_BANKS];
+    hwaddr ram_bases[PPC440EP_SDRAM_NR_BANKS] = {0};
+    hwaddr ram_sizes[PPC440EP_SDRAM_NR_BANKS] = {0};
     PCIBus *pcibus;
     PowerPCCPU *cpu;
     CPUPPCState *env;
@@ -205,8 +205,6 @@ static void bamboo_init(MachineState *machine)
                        qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
 
     /* SDRAM controller */
-    memset(ram_bases, 0, sizeof(ram_bases));
-    memset(ram_sizes, 0, sizeof(ram_sizes));
     ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_memories,
                        ram_bases, ram_sizes, ppc440ep_sdram_bank_sizes);
     /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
hw/ppc/ppc4xx_devs.c +2 -6
index ce38ae65e6..b4cd10f735 100644
@@ -363,12 +363,8 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
     sdram->irq = irq;
     sdram->nbanks = nbanks;
     sdram->ram_memories = ram_memories;
-    memset(sdram->ram_bases, 0, 4 * sizeof(hwaddr));
-    memcpy(sdram->ram_bases, ram_bases,
-           nbanks * sizeof(hwaddr));
-    memset(sdram->ram_sizes, 0, 4 * sizeof(hwaddr));
-    memcpy(sdram->ram_sizes, ram_sizes,
-           nbanks * sizeof(hwaddr));
+    memcpy(sdram->ram_bases, ram_bases, nbanks * sizeof(hwaddr));
+    memcpy(sdram->ram_sizes, ram_sizes, nbanks * sizeof(hwaddr));
     qemu_register_reset(&sdram_reset, sdram);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
Signature · 1 line
2.30.4
#14 lore raw

PATCH v3 4/20 ppc4xx: Use Ppc4xxSdramBank in ppc4xx_sdram_banks()

In reply to BALATON Zoltan

Change ppc4xx_sdram_banks() to take one Ppc4xxSdramBank array instead of the separate arrays and adjust ppc4xx_sdram_init() and ppc440_sdram_init() accordingly as well as machines using these.

---
v2: Use pointer for ram_banks in the prototype of the init funcs as an array of struct seems to confuse gcc 12.2.1 and provoke a warning

 hw/ppc/ppc405.h         |  4 +---
 hw/ppc/ppc405_uc.c      | 10 +++++-----
 hw/ppc/ppc440.h         |  5 ++---
 hw/ppc/ppc440_bamboo.c  | 15 ++++++---------
 hw/ppc/ppc440_uc.c      |  9 ++++-----
 hw/ppc/ppc4xx_devs.c    | 21 +++++++++------------
 hw/ppc/sam460ex.c       | 15 +++++----------
 include/hw/ppc/ppc4xx.h |  9 +++------
 8 files changed, 35 insertions(+), 53 deletions(-)
hw/ppc/ppc405.h +1 -3
index 756865621b..ca0972b88b 100644
@@ -167,9 +167,7 @@ struct Ppc405SoCState {
     DeviceState parent_obj;
 
     /* Public */
-    MemoryRegion ram_banks[2];
-    hwaddr ram_bases[2], ram_sizes[2];
-
+    Ppc4xxSdramBank ram_banks[2];
     MemoryRegion *dram_mr;
     hwaddr ram_size;
 
hw/ppc/ppc405_uc.c +5 -5
index 1e02347e57..bcbf35bc14 100644
@@ -1074,14 +1074,14 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
 
     /* SDRAM controller */
         /* XXX 405EP has no ECC interrupt */
-    s->ram_bases[0] = 0;
-    s->ram_sizes[0] = s->ram_size;
-    memory_region_init_alias(&s->ram_banks[0], OBJECT(s),
+    s->ram_banks[0].base = 0;
+    s->ram_banks[0].size = s->ram_size;
+    memory_region_init_alias(&s->ram_banks[0].ram, OBJECT(s),
                              "ppc405.sdram0", s->dram_mr,
-                             s->ram_bases[0], s->ram_sizes[0]);
+                             s->ram_banks[0].base, s->ram_banks[0].size);
 
     ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes);
+                      s->ram_banks);
 
     /* External bus controller */
     if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
hw/ppc/ppc440.h +2 -3
index 7cef936125..e6c905b7d6 100644
@@ -11,14 +11,13 @@
 #ifndef PPC440_H
 #define PPC440_H
 
-#include "hw/ppc/ppc.h"
+#include "hw/ppc/ppc4xx.h"
 
 void ppc4xx_l2sram_init(CPUPPCState *env);
 void ppc4xx_cpr_init(CPUPPCState *env);
 void ppc4xx_sdr_init(CPUPPCState *env);
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       MemoryRegion *ram_memories,
-                       hwaddr *ram_bases, hwaddr *ram_sizes,
+                       Ppc4xxSdramBank *ram_banks,
                        int do_init);
 void ppc4xx_ahb_init(CPUPPCState *env);
 void ppc4xx_dma_init(CPUPPCState *env, int dcr_base);
hw/ppc/ppc440_bamboo.c +6 -9
index e3412c4fcd..2aac8a3fe9 100644
@@ -168,9 +168,8 @@ static void bamboo_init(MachineState *machine)
     unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
     MemoryRegion *address_space_mem = get_system_memory();
     MemoryRegion *isa = g_new(MemoryRegion, 1);
-    MemoryRegion *ram_memories = g_new(MemoryRegion, PPC440EP_SDRAM_NR_BANKS);
-    hwaddr ram_bases[PPC440EP_SDRAM_NR_BANKS] = {0};
-    hwaddr ram_sizes[PPC440EP_SDRAM_NR_BANKS] = {0};
+    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank,
+                                        PPC440EP_SDRAM_NR_BANKS);
     PCIBus *pcibus;
     PowerPCCPU *cpu;
     CPUPPCState *env;
@@ -205,13 +204,11 @@ static void bamboo_init(MachineState *machine)
                        qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
 
     /* SDRAM controller */
-    ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                       ram_bases, ram_sizes, ppc440ep_sdram_bank_sizes);
+    ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_banks,
+                       ppc440ep_sdram_bank_sizes);
     /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
-    ppc4xx_sdram_init(env,
-                      qdev_get_gpio_in(uicdev, 14),
-                      PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes);
+    ppc4xx_sdram_init(env, qdev_get_gpio_in(uicdev, 14),
+                      PPC440EP_SDRAM_NR_BANKS, ram_banks);
     /* Enable SDRAM memory regions, this should be done by the firmware */
     if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
         ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
hw/ppc/ppc440_uc.c +4 -5
index 6ab0ad7985..5db59d1190 100644
@@ -690,8 +690,7 @@ static void sdram_reset(void *opaque)
 }
 
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       MemoryRegion *ram_memories,
-                       hwaddr *ram_bases, hwaddr *ram_sizes,
+                       Ppc4xxSdramBank *ram_banks,
                        int do_init)
 {
     ppc440_sdram_t *sdram;
@@ -700,9 +699,9 @@ void ppc440_sdram_init(CPUPPCState *env, int nbanks,
     sdram = g_malloc0(sizeof(*sdram));
     sdram->nbanks = nbanks;
     for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_memories[i];
-        sdram->bank[i].base = ram_bases[i];
-        sdram->bank[i].size = ram_sizes[i];
+        sdram->bank[i].ram = ram_banks[i].ram;
+        sdram->bank[i].base = ram_banks[i].base;
+        sdram->bank[i].size = ram_banks[i].size;
     }
     qemu_register_reset(&sdram_reset, sdram);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
hw/ppc/ppc4xx_devs.c +9 -12
index 936d6f77fe..7bdcbd6fac 100644
@@ -343,9 +343,7 @@ static void sdram_reset(void *opaque)
 }
 
 void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       MemoryRegion *ram_memories,
-                       hwaddr *ram_bases,
-                       hwaddr *ram_sizes)
+                       Ppc4xxSdramBank *ram_banks)
 {
     ppc4xx_sdram_t *sdram;
     int i;
@@ -354,9 +352,9 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
     sdram->irq = irq;
     sdram->nbanks = nbanks;
     for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_memories[i];
-        sdram->bank[i].base = ram_bases[i];
-        sdram->bank[i].size = ram_sizes[i];
+        sdram->bank[i].ram = ram_banks[i].ram;
+        sdram->bank[i].base = ram_banks[i].base;
+        sdram->bank[i].size = ram_banks[i].size;
     }
     qemu_register_reset(&sdram_reset, sdram);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
@@ -376,8 +374,7 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
  * sizes varies by SoC.
  */
 void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
-                        MemoryRegion ram_memories[],
-                        hwaddr ram_bases[], hwaddr ram_sizes[],
+                        Ppc4xxSdramBank ram_banks[],
                         const ram_addr_t sdram_bank_sizes[])
 {
     ram_addr_t size_left = memory_region_size(ram);
@@ -392,13 +389,13 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
             if (bank_size <= size_left) {
                 char name[32];
 
-                ram_bases[i] = base;
-                ram_sizes[i] = bank_size;
+                ram_banks[i].base = base;
+                ram_banks[i].size = bank_size;
                 base += bank_size;
                 size_left -= bank_size;
                 snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
-                memory_region_init_alias(&ram_memories[i], NULL, name, ram,
-                                         ram_bases[i], ram_sizes[i]);
+                memory_region_init_alias(&ram_banks[i].ram, NULL, name, ram,
+                                         ram_banks[i].base, ram_banks[i].size);
                 break;
             }
         }
hw/ppc/sam460ex.c +5 -10
index 850bb3b817..f4c2a693fb 100644
@@ -73,7 +73,6 @@
 #define OPB_FREQ 115000000
 #define EBC_FREQ 115000000
 #define UART_FREQ 11059200
-#define SDRAM_NR_BANKS 4
 
 /* The SoC could also handle 4 GiB but firmware does not work with that. */
 /* Maybe it overflows a signed 32 bit number somewhere? */
@@ -274,9 +273,7 @@ static void sam460ex_init(MachineState *machine)
 {
     MemoryRegion *address_space_mem = get_system_memory();
     MemoryRegion *isa = g_new(MemoryRegion, 1);
-    MemoryRegion *ram_memories = g_new(MemoryRegion, SDRAM_NR_BANKS);
-    hwaddr ram_bases[SDRAM_NR_BANKS] = {0};
-    hwaddr ram_sizes[SDRAM_NR_BANKS] = {0};
+    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank, 1);
     MemoryRegion *l2cache_ram = g_new(MemoryRegion, 1);
     DeviceState *uic[4];
     int i;
@@ -345,20 +342,18 @@ static void sam460ex_init(MachineState *machine)
     /* SDRAM controller */
     /* put all RAM on first bank because board has one slot
      * and firmware only checks that */
-    ppc4xx_sdram_banks(machine->ram, 1, ram_memories, ram_bases, ram_sizes,
-                       ppc460ex_sdram_bank_sizes);
+    ppc4xx_sdram_banks(machine->ram, 1, ram_banks, ppc460ex_sdram_bank_sizes);
 
     /* FIXME: does 460EX have ECC interrupts? */
-    ppc440_sdram_init(env, SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+    ppc440_sdram_init(env, 1, ram_banks, 1);
 
     /* IIC controllers and devices */
     dev = sysbus_create_simple(TYPE_PPC4xx_I2C, 0x4ef600700,
                                qdev_get_gpio_in(uic[0], 2));
     i2c = PPC4xx_I2C(dev)->bus;
     /* SPD EEPROM on RAM module */
-    spd_data = spd_data_generate(ram_sizes[0] < 128 * MiB ? DDR : DDR2,
-                                 ram_sizes[0]);
+    spd_data = spd_data_generate(ram_banks->size < 128 * MiB ? DDR : DDR2,
+                                 ram_banks->size);
     spd_data[20] = 4; /* SO-DIMM module */
     smbus_eeprom_init_one(i2c, 0x50, spd_data);
     /* RTC */
include/hw/ppc/ppc4xx.h +3 -6
index a5e6c185af..5013b8bf3a 100644
@@ -43,14 +43,11 @@ enum {
 };
 
 void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
-                        MemoryRegion ram_memories[],
-                        hwaddr ram_bases[], hwaddr ram_sizes[],
+                        Ppc4xxSdramBank ram_banks[],
                         const ram_addr_t sdram_bank_sizes[]);
 
-void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
-                        MemoryRegion ram_memories[],
-                        hwaddr *ram_bases,
-                        hwaddr *ram_sizes);
+void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
+                       Ppc4xxSdramBank *ram_banks);
 
 #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
Signature · 1 line
2.30.4
#16 lore raw

PATCH v3 5/20 ppc440_bamboo: Add missing 4 MiB valid memory size

In reply to BALATON Zoltan
---
 hw/ppc/ppc440_bamboo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/ppc440_bamboo.c +1 -1
index 2aac8a3fe9..2bd5e41140 100644
@@ -51,7 +51,7 @@
 #define PPC440EP_SDRAM_NR_BANKS 4
 
 static const ram_addr_t ppc440ep_sdram_bank_sizes[] = {
-    256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 0
+    256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
 };
 
 static hwaddr entry;
Signature · 1 line
2.30.4
#26 lore raw

PATCH v3 10/20 ppc440_sdram: Implement enable bit in the DDR2 SDRAM

In reply to BALATON Zoltan

To allow removing the do_init hack we need to improve the DDR2 SDRAM controller model to handle the enable/disable bit that it ignored so far.

---
v2: replace 0x08000000 with BIT(27)

 hw/ppc/ppc440_uc.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
hw/ppc/ppc440_uc.c +32 -2
index 01184e717b..3c442eaecc 100644
@@ -23,6 +23,7 @@
 #include "sysemu/reset.h"
 #include "ppc440.h"
 #include "qom/object.h"
+#include "trace.h"
 
 /*****************************************************************************/
 /* L2 Cache as SRAM */
@@ -484,6 +485,7 @@ void ppc4xx_sdr_init(CPUPPCState *env)
 /* SDRAM controller */
 typedef struct ppc440_sdram_t {
     uint32_t addr;
+    uint32_t mcopt2;
     int nbanks;
     Ppc4xxSdramBank bank[4];
 } ppc440_sdram_t;
@@ -581,12 +583,15 @@ static void sdram_set_bcr(ppc440_sdram_t *sdram, int i,
 {
     if (sdram->bank[i].bcr & 1) {
         /* First unmap RAM if enabled */
+        trace_ppc4xx_sdram_unmap(sdram_base(sdram->bank[i].bcr),
+                                 sdram_size(sdram->bank[i].bcr));
         sdram_bank_unmap(&sdram->bank[i]);
     }
     sdram->bank[i].bcr = bcr & 0xffe0ffc1;
     sdram->bank[i].base = sdram_base(bcr);
     sdram->bank[i].size = sdram_size(bcr);
     if (enabled && (bcr & 1)) {
+        trace_ppc4xx_sdram_map(sdram_base(bcr), sdram_size(bcr));
         sdram_bank_map(&sdram->bank[i]);
     }
 }
@@ -596,7 +601,7 @@ static void sdram_map_bcr(ppc440_sdram_t *sdram)
     int i;
 
     for (i = 0; i < sdram->nbanks; i++) {
-        if (sdram->bank[i].size != 0) {
+        if (sdram->bank[i].size) {
             sdram_set_bcr(sdram, i, sdram_bcr(sdram->bank[i].base,
                                               sdram->bank[i].size), 1);
         } else {
@@ -605,6 +610,17 @@ static void sdram_map_bcr(ppc440_sdram_t *sdram)
     }
 }
 
+static void sdram_unmap_bcr(ppc440_sdram_t *sdram)
+{
+    int i;
+
+    for (i = 0; i < sdram->nbanks; i++) {
+        if (sdram->bank[i].size) {
+            sdram_set_bcr(sdram, i, sdram->bank[i].bcr & ~1, 0);
+        }
+    }
+}
+
 static uint32_t dcr_read_sdram(void *opaque, int dcrn)
 {
     ppc440_sdram_t *sdram = opaque;
@@ -636,7 +652,7 @@ static uint32_t dcr_read_sdram(void *opaque, int dcrn)
             ret = 0x80000000;
             break;
         case 0x21: /* SDRAM_MCOPT2 */
-            ret = 0x08000000;
+            ret = sdram->mcopt2;
             break;
         case 0x40: /* SDRAM_MB0CF */
             ret = 0x00008001;
@@ -680,6 +696,19 @@ static void dcr_write_sdram(void *opaque, int dcrn, uint32_t val)
         switch (sdram->addr) {
         case 0x00: /* B0CR */
             break;
+        case 0x21: /* SDRAM_MCOPT2 */
+            if (!(sdram->mcopt2 & BIT(27)) && (val & BIT(27))) {
+                trace_ppc4xx_sdram_enable("enable");
+                /* validate all RAM mappings */
+                sdram_map_bcr(sdram);
+                sdram->mcopt2 |= BIT(27);
+            } else if ((sdram->mcopt2 & BIT(27)) && !(val & BIT(27))) {
+                trace_ppc4xx_sdram_enable("disable");
+                /* invalidate all RAM mappings */
+                sdram_unmap_bcr(sdram);
+                sdram->mcopt2 &= ~BIT(27);
+            }
+            break;
         default:
             break;
         }
@@ -694,6 +723,7 @@ static void sdram_reset(void *opaque)
     ppc440_sdram_t *sdram = opaque;
 
     sdram->addr = 0;
+    sdram->mcopt2 = BIT(27);
 }
 
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
Signature · 1 line
2.30.4
#20 lore raw

PATCH v3 7/20 ppc4xx_sdram: QOM'ify

In reply to BALATON Zoltan

Change the ppc4xx_sdram model to a QOM class derived from the PPC4xx-dcr-device and name it ppc4xx-sdram-ddr. This is mostly modelling the DDR SDRAM controller found in the 440EP (used on the bamboo board) but also backward compatible with the older DDR controllers on some 405 SoCs so we also use it for those now. This likely does not cause problems for guests we run as the new features are just not accessed but to model 405 SoC accurately some features may have to be disabled or the model split between 440 and older.

Newer SoCs (regardless of their PPC core, e.g. 405EX) may have an updated DDR2 SDRAM controller implemented by the ppc440_sdram model (only partially, enough for the 460EX on the sam460ex) that is not yet QOM'ified in this patch. That is intended to become ppc4xx-sdram-ddr2 when QOM'ified later.

---
 hw/ppc/ppc405.h         |  3 +-
 hw/ppc/ppc405_uc.c      | 22 +++++----
 hw/ppc/ppc440_bamboo.c  | 10 +++--
 hw/ppc/ppc4xx_devs.c    | 99 ++++++++++++++++++++++-------------------
 include/hw/ppc/ppc4xx.h | 27 +++++++++--
 5 files changed, 98 insertions(+), 63 deletions(-)
hw/ppc/ppc405.h +1 -2
index ad54dff542..9a4312691e 100644
@@ -167,8 +167,6 @@ struct Ppc405SoCState {
     DeviceState parent_obj;
 
     /* Public */
-    MemoryRegion *dram_mr;
-
     PowerPCCPU cpu;
     PPCUIC uic;
     Ppc405CpcState cpc;
@@ -182,6 +180,7 @@ struct Ppc405SoCState {
     Ppc405PobState pob;
     Ppc4xxPlbState plb;
     Ppc4xxMalState mal;
+    Ppc4xxSdramDdrState sdram;
 };
 
 #endif /* PPC405_H */
hw/ppc/ppc405_uc.c +13 -9
index e1c7188e61..c973cfb04e 100644
@@ -1016,6 +1016,9 @@ static void ppc405_soc_instance_init(Object *obj)
     object_initialize_child(obj, "plb", &s->plb, TYPE_PPC4xx_PLB);
 
     object_initialize_child(obj, "mal", &s->mal, TYPE_PPC4xx_MAL);
+
+    object_initialize_child(obj, "sdram", &s->sdram, TYPE_PPC4xx_SDRAM_DDR);
+    object_property_add_alias(obj, "dram", OBJECT(&s->sdram), "dram");
 }
 
 static void ppc405_reset(void *opaque)
@@ -1073,9 +1076,17 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                        qdev_get_gpio_in(DEVICE(&s->cpu), PPC40x_INPUT_CINT));
 
     /* SDRAM controller */
+    /*
+     * We use the 440 DDR SDRAM controller which has more regs and features
+     * but it's compatible enough for now
+     */
+    object_property_set_int(OBJECT(&s->sdram), "nbanks", 2, &error_abort);
+    if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->sdram), &s->cpu, errp)) {
+        return;
+    }
     /* XXX 405EP has no ECC interrupt */
-    ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->dram_mr);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdram), 0,
+                       qdev_get_gpio_in(DEVICE(&s->uic), 17));
 
     /* External bus controller */
     if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1150,12 +1161,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
     /* Uses UIC IRQs 9, 15, 17 */
 }
 
-static Property ppc405_soc_properties[] = {
-    DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
-                     MemoryRegion *),
-    DEFINE_PROP_END_OF_LIST(),
-};
-
 static void ppc405_soc_class_init(ObjectClass *oc, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(oc);
@@ -1163,7 +1168,6 @@ static void ppc405_soc_class_init(ObjectClass *oc, void *data)
     dc->realize = ppc405_soc_realize;
     /* Reason: only works as part of a ppc405 board/machine */
     dc->user_creatable = false;
-    device_class_set_props(dc, ppc405_soc_properties);
 }
 
 static const TypeInfo ppc405_types[] = {
hw/ppc/ppc440_bamboo.c +6 -4
index 9b456f1819..6052d3a2e0 100644
@@ -48,8 +48,6 @@
 #define PPC440EP_PCI_IO         0xe8000000
 #define PPC440EP_PCI_IOLEN      0x00010000
 
-#define PPC440EP_SDRAM_NR_BANKS 4
-
 static hwaddr entry;
 
 static int bamboo_load_device_tree(hwaddr addr,
@@ -198,9 +196,13 @@ static void bamboo_init(MachineState *machine)
                        qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
 
     /* SDRAM controller */
+    dev = qdev_new(TYPE_PPC4xx_SDRAM_DDR);
+    object_property_set_link(OBJECT(dev), "dram", OBJECT(machine->ram),
+                             &error_abort);
+    ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(dev), cpu, &error_fatal);
+    object_unref(OBJECT(dev));
     /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
-    ppc4xx_sdram_init(env, qdev_get_gpio_in(uicdev, 14),
-                      PPC440EP_SDRAM_NR_BANKS, machine->ram);
+    sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(uicdev, 14));
     /* Enable SDRAM memory regions, this should be done by the firmware */
     if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
         ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
hw/ppc/ppc4xx_devs.c +54 -45

175 lines not on this page · raw message

include/hw/ppc/ppc4xx.h +24 -3
index 6007a8dd04..20d0cdde8a 100644
@@ -46,9 +46,6 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                         Ppc4xxSdramBank ram_banks[],
                         const ram_addr_t sdram_bank_sizes[]);
 
-void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       MemoryRegion *ram);
-
 #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
 /*
@@ -118,4 +115,28 @@ struct Ppc4xxEbcState {
     uint32_t cfg;
 };
 
+/* SDRAM DDR controller */
+#define TYPE_PPC4xx_SDRAM_DDR "ppc4xx-sdram-ddr"
+OBJECT_DECLARE_SIMPLE_TYPE(Ppc4xxSdramDdrState, PPC4xx_SDRAM_DDR);
+struct Ppc4xxSdramDdrState {
+    Ppc4xxDcrDeviceState parent_obj;
+
+    MemoryRegion *dram_mr;
+    uint32_t nbanks; /* Banks to use from 4, e.g. when board has less slots */
+    Ppc4xxSdramBank bank[4];
+    qemu_irq irq;
+
+    uint32_t addr;
+    uint32_t besr0;
+    uint32_t besr1;
+    uint32_t bear;
+    uint32_t cfg;
+    uint32_t status;
+    uint32_t rtr;
+    uint32_t pmit;
+    uint32_t tr;
+    uint32_t ecccfg;
+    uint32_t eccesr;
+};
+
 #endif /* PPC4XX_H */
Signature · 1 line
2.30.4
#17 lore raw

PATCH v3 6/20 ppc4xx_sdram: Move size check to ppc4xx_sdram_init()

In reply to BALATON Zoltan

Instead of checking if memory size is valid in board code move this check to ppc4xx_sdram_init() as this is a restriction imposed by the SDRAM controller.

---
 hw/ppc/ppc405.h         |  2 --
 hw/ppc/ppc405_boards.c  | 10 ----------
 hw/ppc/ppc405_uc.c      | 11 ++---------
 hw/ppc/ppc440_bamboo.c  | 10 +---------
 hw/ppc/ppc4xx_devs.c    | 14 ++++++--------
 include/hw/ppc/ppc4xx.h |  2 +-
 6 files changed, 10 insertions(+), 39 deletions(-)
hw/ppc/ppc405.h +0 -2
index ca0972b88b..ad54dff542 100644
@@ -167,9 +167,7 @@ struct Ppc405SoCState {
     DeviceState parent_obj;
 
     /* Public */
-    Ppc4xxSdramBank ram_banks[2];
     MemoryRegion *dram_mr;
-    hwaddr ram_size;
 
     PowerPCCPU cpu;
     PPCUIC uic;
hw/ppc/ppc405_boards.c +0 -10
index bf02a71c6d..cdd4e0cb4c 100644
@@ -271,22 +271,12 @@ static void boot_from_kernel(MachineState *machine, PowerPCCPU *cpu)
 static void ppc405_init(MachineState *machine)
 {
     Ppc405MachineState *ppc405 = PPC405_MACHINE(machine);
-    MachineClass *mc = MACHINE_GET_CLASS(machine);
     const char *kernel_filename = machine->kernel_filename;
     MemoryRegion *sysmem = get_system_memory();
     CPUPPCState *env;
 
-    if (machine->ram_size != mc->default_ram_size) {
-        char *sz = size_to_str(mc->default_ram_size);
-        error_report("Invalid RAM size, should be %s", sz);
-        g_free(sz);
-        exit(EXIT_FAILURE);
-    }
-
     object_initialize_child(OBJECT(machine), "soc", &ppc405->soc,
                             TYPE_PPC405_SOC);
-    object_property_set_uint(OBJECT(&ppc405->soc), "ram-size",
-                             machine->ram_size, &error_fatal);
     object_property_set_link(OBJECT(&ppc405->soc), "dram",
                              OBJECT(machine->ram), &error_abort);
     object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
hw/ppc/ppc405_uc.c +2 -9
index bcbf35bc14..e1c7188e61 100644
@@ -1073,15 +1073,9 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                        qdev_get_gpio_in(DEVICE(&s->cpu), PPC40x_INPUT_CINT));
 
     /* SDRAM controller */
-        /* XXX 405EP has no ECC interrupt */
-    s->ram_banks[0].base = 0;
-    s->ram_banks[0].size = s->ram_size;
-    memory_region_init_alias(&s->ram_banks[0].ram, OBJECT(s),
-                             "ppc405.sdram0", s->dram_mr,
-                             s->ram_banks[0].base, s->ram_banks[0].size);
-
+    /* XXX 405EP has no ECC interrupt */
     ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks);
+                      s->dram_mr);
 
     /* External bus controller */
     if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1159,7 +1153,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
 static Property ppc405_soc_properties[] = {
     DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                      MemoryRegion *),
-    DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/ppc/ppc440_bamboo.c +1 -9
index 2bd5e41140..9b456f1819 100644
@@ -50,10 +50,6 @@
 
 #define PPC440EP_SDRAM_NR_BANKS 4
 
-static const ram_addr_t ppc440ep_sdram_bank_sizes[] = {
-    256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
-};
-
 static hwaddr entry;
 
 static int bamboo_load_device_tree(hwaddr addr,
@@ -168,8 +164,6 @@ static void bamboo_init(MachineState *machine)
     unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
     MemoryRegion *address_space_mem = get_system_memory();
     MemoryRegion *isa = g_new(MemoryRegion, 1);
-    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank,
-                                        PPC440EP_SDRAM_NR_BANKS);
     PCIBus *pcibus;
     PowerPCCPU *cpu;
     CPUPPCState *env;
@@ -204,11 +198,9 @@ static void bamboo_init(MachineState *machine)
                        qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
 
     /* SDRAM controller */
-    ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_banks,
-                       ppc440ep_sdram_bank_sizes);
     /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
     ppc4xx_sdram_init(env, qdev_get_gpio_in(uicdev, 14),
-                      PPC440EP_SDRAM_NR_BANKS, ram_banks);
+                      PPC440EP_SDRAM_NR_BANKS, machine->ram);
     /* Enable SDRAM memory regions, this should be done by the firmware */
     if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
         ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
hw/ppc/ppc4xx_devs.c +6 -8
index 7bdcbd6fac..eb3aa97b16 100644
@@ -41,7 +41,7 @@
 typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
 struct ppc4xx_sdram_t {
     uint32_t addr;
-    int nbanks;
+    int nbanks; /* Banks to use from the 4, e.g. when board has less slots */
     Ppc4xxSdramBank bank[4];
     uint32_t besr0;
     uint32_t besr1;
@@ -343,19 +343,17 @@ static void sdram_reset(void *opaque)
 }
 
 void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       Ppc4xxSdramBank *ram_banks)
+                       MemoryRegion *ram)
 {
     ppc4xx_sdram_t *sdram;
-    int i;
+    const ram_addr_t valid_bank_sizes[] = {
+        256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
+    };
 
     sdram = g_new0(ppc4xx_sdram_t, 1);
     sdram->irq = irq;
     sdram->nbanks = nbanks;
-    for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_banks[i].ram;
-        sdram->bank[i].base = ram_banks[i].base;
-        sdram->bank[i].size = ram_banks[i].size;
-    }
+    ppc4xx_sdram_banks(ram, sdram->nbanks, sdram->bank, valid_bank_sizes);
     qemu_register_reset(&sdram_reset, sdram);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
include/hw/ppc/ppc4xx.h +1 -1
index 5013b8bf3a..6007a8dd04 100644
@@ -47,7 +47,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                         const ram_addr_t sdram_bank_sizes[]);
 
 void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       Ppc4xxSdramBank *ram_banks);
+                       MemoryRegion *ram);
 
 #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
Signature · 1 line
2.30.4
#22 lore raw

PATCH v3 8/20 ppc4xx_sdram: Drop extra zeros for readability

In reply to BALATON Zoltan

Constants that are written zero padded for no good reason are hard to read, it's easier to see what is meant if it's just 0 or 1 instead.

---
 hw/ppc/ppc4xx_devs.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
hw/ppc/ppc4xx_devs.c +20 -20
index 375834a52b..bfe7b2d3a6 100644
@@ -49,31 +49,31 @@ static uint32_t sdram_ddr_bcr(hwaddr ram_base, hwaddr ram_size)
 
     switch (ram_size) {
     case 4 * MiB:
-        bcr = 0x00000000;
+        bcr = 0;
         break;
     case 8 * MiB:
-        bcr = 0x00020000;
+        bcr = 0x20000;
         break;
     case 16 * MiB:
-        bcr = 0x00040000;
+        bcr = 0x40000;
         break;
     case 32 * MiB:
-        bcr = 0x00060000;
+        bcr = 0x60000;
         break;
     case 64 * MiB:
-        bcr = 0x00080000;
+        bcr = 0x80000;
         break;
     case 128 * MiB:
-        bcr = 0x000A0000;
+        bcr = 0xA0000;
         break;
     case 256 * MiB:
-        bcr = 0x000C0000;
+        bcr = 0xC0000;
         break;
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: invalid RAM size 0x%" HWADDR_PRIx "\n", __func__,
                       ram_size);
-        return 0x00000000;
+        return 0;
     }
     bcr |= ram_base & 0xFF800000;
     bcr |= 1;
@@ -104,7 +104,7 @@ static target_ulong sdram_size(uint32_t bcr)
 static void sdram_set_bcr(Ppc4xxSdramDdrState *sdram, int i,
                           uint32_t bcr, int enabled)
 {
-    if (sdram->bank[i].bcr & 0x00000001) {
+    if (sdram->bank[i].bcr & 1) {
         /* Unmap RAM */
         trace_ppc4xx_sdram_unmap(sdram_base(sdram->bank[i].bcr),
                                  sdram_size(sdram->bank[i].bcr));
@@ -115,7 +115,7 @@ static void sdram_set_bcr(Ppc4xxSdramDdrState *sdram, int i,
         object_unparent(OBJECT(&sdram->bank[i].container));
     }
     sdram->bank[i].bcr = bcr & 0xFFDEE001;
-    if (enabled && (bcr & 0x00000001)) {
+    if (enabled && (bcr & 1)) {
         trace_ppc4xx_sdram_map(sdram_base(bcr), sdram_size(bcr));
         memory_region_init(&sdram->bank[i].container, NULL, "sdram-container",
                            sdram_size(bcr));
@@ -136,7 +136,7 @@ static void sdram_map_bcr(Ppc4xxSdramDdrState *sdram)
             sdram_set_bcr(sdram, i, sdram_ddr_bcr(sdram->bank[i].base,
                                                   sdram->bank[i].size), 1);
         } else {
-            sdram_set_bcr(sdram, i, 0x00000000, 0);
+            sdram_set_bcr(sdram, i, 0, 0);
         }
     }
 }
@@ -213,7 +213,7 @@ static uint32_t sdram_ddr_dcr_read(void *opaque, int dcrn)
         break;
     default:
         /* Avoid gcc warning */
-        ret = 0x00000000;
+        ret = 0;
         break;
     }
 
@@ -306,18 +306,18 @@ static void ppc4xx_sdram_ddr_reset(DeviceState *dev)
 {
     Ppc4xxSdramDdrState *sdram = PPC4xx_SDRAM_DDR(dev);
 
-    sdram->addr = 0x00000000;
-    sdram->bear = 0x00000000;
-    sdram->besr0 = 0x00000000; /* No error */
-    sdram->besr1 = 0x00000000; /* No error */
-    sdram->cfg = 0x00000000;
-    sdram->ecccfg = 0x00000000; /* No ECC */
-    sdram->eccesr = 0x00000000; /* No error */
+    sdram->addr = 0;
+    sdram->bear = 0;
+    sdram->besr0 = 0; /* No error */
+    sdram->besr1 = 0; /* No error */
+    sdram->cfg = 0;
+    sdram->ecccfg = 0; /* No ECC */
+    sdram->eccesr = 0; /* No error */
     sdram->pmit = 0x07C00000;
     sdram->rtr = 0x05F00000;
     sdram->tr = 0x00854009;
     /* We pre-initialize RAM banks */
-    sdram->status = 0x00000000;
+    sdram->status = 0;
     sdram->cfg = 0x00800000;
 }
 
Signature · 1 line
2.30.4
#29 lore raw

PATCH v3 12/20 ppc440_sdram: Rename local variable for readibility

In reply to BALATON Zoltan

Rename local sdram variable in ppc440_sdram_init to s for readibility.

---
 hw/ppc/ppc440_uc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
hw/ppc/ppc440_uc.c +18 -18
index b3f56c49b5..d8a7947196 100644
@@ -729,40 +729,40 @@ static void sdram_reset(void *opaque)
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
                        Ppc4xxSdramBank *ram_banks)
 {
-    ppc440_sdram_t *sdram;
+    ppc440_sdram_t *s;
     int i;
 
-    sdram = g_malloc0(sizeof(*sdram));
-    sdram->nbanks = nbanks;
+    s = g_malloc0(sizeof(*s));
+    s->nbanks = nbanks;
     for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_banks[i].ram;
-        sdram->bank[i].base = ram_banks[i].base;
-        sdram->bank[i].size = ram_banks[i].size;
+        s->bank[i].ram = ram_banks[i].ram;
+        s->bank[i].base = ram_banks[i].base;
+        s->bank[i].size = ram_banks[i].size;
     }
-    qemu_register_reset(&sdram_reset, sdram);
+    qemu_register_reset(&sdram_reset, s);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM0_CFGDATA,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
 
     ppc_dcr_register(env, SDRAM_R0BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_R1BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_R2BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_R3BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_CONF1HB,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_PLBADDULL,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_CONF1LL,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_CONFPATHB,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM_PLBADDUHB,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
 }
 
 /*****************************************************************************/
Signature · 1 line
2.30.4
#28 lore raw

PATCH v3 11/20 ppc440_sdram: Get rid of the init RAM hack

In reply to BALATON Zoltan

Remove the do_init parameter of ppc440_sdram_init and enable SDRAM controller from the board via DCR access instead. Firmware does this so it may not be needed when booting firmware only with -kernel but we enable it unconditionally to preserve previous behaviour.

---
v2: replace 0x08000000 with BIT(27)

 hw/ppc/ppc440.h    | 3 +--
 hw/ppc/ppc440_uc.c | 8 ++------
 hw/ppc/sam460ex.c  | 8 +++++++-
 3 files changed, 10 insertions(+), 9 deletions(-)
hw/ppc/ppc440.h +1 -2
index e6c905b7d6..01d76b8000 100644
@@ -17,8 +17,7 @@ void ppc4xx_l2sram_init(CPUPPCState *env);
 void ppc4xx_cpr_init(CPUPPCState *env);
 void ppc4xx_sdr_init(CPUPPCState *env);
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       Ppc4xxSdramBank *ram_banks,
-                       int do_init);
+                       Ppc4xxSdramBank *ram_banks);
 void ppc4xx_ahb_init(CPUPPCState *env);
 void ppc4xx_dma_init(CPUPPCState *env, int dcr_base);
 void ppc460ex_pcie_init(CPUPPCState *env);
hw/ppc/ppc440_uc.c +2 -6
index 3c442eaecc..b3f56c49b5 100644
@@ -723,12 +723,11 @@ static void sdram_reset(void *opaque)
     ppc440_sdram_t *sdram = opaque;
 
     sdram->addr = 0;
-    sdram->mcopt2 = BIT(27);
+    sdram->mcopt2 = 0;
 }
 
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       Ppc4xxSdramBank *ram_banks,
-                       int do_init)
+                       Ppc4xxSdramBank *ram_banks)
 {
     ppc440_sdram_t *sdram;
     int i;
@@ -745,9 +744,6 @@ void ppc440_sdram_init(CPUPPCState *env, int nbanks,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
     ppc_dcr_register(env, SDRAM0_CFGDATA,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
 
     ppc_dcr_register(env, SDRAM_R0BAS,
                      sdram, &dcr_read_sdram, &dcr_write_sdram);
hw/ppc/sam460ex.c +7 -1
index f4c2a693fb..dac329d482 100644
@@ -345,7 +345,13 @@ static void sam460ex_init(MachineState *machine)
     ppc4xx_sdram_banks(machine->ram, 1, ram_banks, ppc460ex_sdram_bank_sizes);
 
     /* FIXME: does 460EX have ECC interrupts? */
-    ppc440_sdram_init(env, 1, ram_banks, 1);
+    ppc440_sdram_init(env, 1, ram_banks);
+    /* Enable SDRAM memory regions as we may boot without firmware */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x21) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x08000000)) {
+        error_report("Couldn't enable memory regions");
+        exit(1);
+    }
 
     /* IIC controllers and devices */
     dev = sysbus_create_simple(TYPE_PPC4xx_I2C, 0x4ef600700,
Signature · 1 line
2.30.4
#36 lore raw

PATCH v3 18/20 ppc4xx_sdram: Rename local state variable for brevity

In reply to BALATON Zoltan

Rename the sdram local state variable to s in dcr read/write functions and reset methods for better readability and to match realize methods. Other places not converted will be changed or removed in subsequent patches.

---
 hw/ppc/ppc4xx_sdram.c | 158 +++++++++++++++++++++---------------------
 1 file changed, 79 insertions(+), 79 deletions(-)
hw/ppc/ppc4xx_sdram.c +79 -79
index 242e2f4c6e..e36898a906 100644
@@ -233,56 +233,56 @@ static void sdram_ddr_unmap_bcr(Ppc4xxSdramDdrState *sdram)
 
 static uint32_t sdram_ddr_dcr_read(void *opaque, int dcrn)
 {
-    Ppc4xxSdramDdrState *sdram = opaque;
+    Ppc4xxSdramDdrState *s = opaque;
     uint32_t ret;
 
     switch (dcrn) {
     case SDRAM0_CFGADDR:
-        ret = sdram->addr;
+        ret = s->addr;
         break;
     case SDRAM0_CFGDATA:
-        switch (sdram->addr) {
+        switch (s->addr) {
         case 0x00: /* SDRAM_BESR0 */
-            ret = sdram->besr0;
+            ret = s->besr0;
             break;
         case 0x08: /* SDRAM_BESR1 */
-            ret = sdram->besr1;
+            ret = s->besr1;
             break;
         case 0x10: /* SDRAM_BEAR */
-            ret = sdram->bear;
+            ret = s->bear;
             break;
         case 0x20: /* SDRAM_CFG */
-            ret = sdram->cfg;
+            ret = s->cfg;
             break;
         case 0x24: /* SDRAM_STATUS */
-            ret = sdram->status;
+            ret = s->status;
             break;
         case 0x30: /* SDRAM_RTR */
-            ret = sdram->rtr;
+            ret = s->rtr;
             break;
         case 0x34: /* SDRAM_PMIT */
-            ret = sdram->pmit;
+            ret = s->pmit;
             break;
         case 0x40: /* SDRAM_B0CR */
-            ret = sdram->bank[0].bcr;
+            ret = s->bank[0].bcr;
             break;
         case 0x44: /* SDRAM_B1CR */
-            ret = sdram->bank[1].bcr;
+            ret = s->bank[1].bcr;
             break;
         case 0x48: /* SDRAM_B2CR */
-            ret = sdram->bank[2].bcr;
+            ret = s->bank[2].bcr;
             break;
         case 0x4C: /* SDRAM_B3CR */
-            ret = sdram->bank[3].bcr;
+            ret = s->bank[3].bcr;
             break;
         case 0x80: /* SDRAM_TR */
             ret = -1; /* ? */
             break;
         case 0x94: /* SDRAM_ECCCFG */
-            ret = sdram->ecccfg;
+            ret = s->ecccfg;
             break;
         case 0x98: /* SDRAM_ECCESR */
-            ret = sdram->eccesr;
+            ret = s->eccesr;
             break;
         default: /* Error */
             ret = -1;
@@ -300,78 +300,78 @@ static uint32_t sdram_ddr_dcr_read(void *opaque, int dcrn)
 
 static void sdram_ddr_dcr_write(void *opaque, int dcrn, uint32_t val)
 {
-    Ppc4xxSdramDdrState *sdram = opaque;
+    Ppc4xxSdramDdrState *s = opaque;
 
     switch (dcrn) {
     case SDRAM0_CFGADDR:
-        sdram->addr = val;
+        s->addr = val;
         break;
     case SDRAM0_CFGDATA:
-        switch (sdram->addr) {
+        switch (s->addr) {
         case 0x00: /* SDRAM_BESR0 */
-            sdram->besr0 &= ~val;
+            s->besr0 &= ~val;
             break;
         case 0x08: /* SDRAM_BESR1 */
-            sdram->besr1 &= ~val;
+            s->besr1 &= ~val;
             break;
         case 0x10: /* SDRAM_BEAR */
-            sdram->bear = val;
+            s->bear = val;
             break;
         case 0x20: /* SDRAM_CFG */
             val &= 0xFFE00000;
-            if (!(sdram->cfg & 0x80000000) && (val & 0x80000000)) {
+            if (!(s->cfg & 0x80000000) && (val & 0x80000000)) {
                 trace_ppc4xx_sdram_enable("enable");
                 /* validate all RAM mappings */
-                sdram_ddr_map_bcr(sdram);
-                sdram->status &= ~0x80000000;
-            } else if ((sdram->cfg & 0x80000000) && !(val & 0x80000000)) {
+                sdram_ddr_map_bcr(s);
+                s->status &= ~0x80000000;
+            } else if ((s->cfg & 0x80000000) && !(val & 0x80000000)) {
                 trace_ppc4xx_sdram_enable("disable");
                 /* invalidate all RAM mappings */
-                sdram_ddr_unmap_bcr(sdram);
-                sdram->status |= 0x80000000;
+                sdram_ddr_unmap_bcr(s);
+                s->status |= 0x80000000;
             }
-            if (!(sdram->cfg & 0x40000000) && (val & 0x40000000)) {
-                sdram->status |= 0x40000000;
-            } else if ((sdram->cfg & 0x40000000) && !(val & 0x40000000)) {
-                sdram->status &= ~0x40000000;
+            if (!(s->cfg & 0x40000000) && (val & 0x40000000)) {
+                s->status |= 0x40000000;
+            } else if ((s->cfg & 0x40000000) && !(val & 0x40000000)) {
+                s->status &= ~0x40000000;
             }
-            sdram->cfg = val;
+            s->cfg = val;
             break;
         case 0x24: /* SDRAM_STATUS */
             /* Read-only register */
             break;
         case 0x30: /* SDRAM_RTR */
-            sdram->rtr = val & 0x3FF80000;
+            s->rtr = val & 0x3FF80000;
             break;
         case 0x34: /* SDRAM_PMIT */
-            sdram->pmit = (val & 0xF8000000) | 0x07C00000;
+            s->pmit = (val & 0xF8000000) | 0x07C00000;
             break;
         case 0x40: /* SDRAM_B0CR */
-            sdram_ddr_set_bcr(sdram, 0, val, sdram->cfg & 0x80000000);
+            sdram_ddr_set_bcr(s, 0, val, s->cfg & 0x80000000);
             break;
         case 0x44: /* SDRAM_B1CR */
-            sdram_ddr_set_bcr(sdram, 1, val, sdram->cfg & 0x80000000);
+            sdram_ddr_set_bcr(s, 1, val, s->cfg & 0x80000000);
             break;
         case 0x48: /* SDRAM_B2CR */
-            sdram_ddr_set_bcr(sdram, 2, val, sdram->cfg & 0x80000000);
+            sdram_ddr_set_bcr(s, 2, val, s->cfg & 0x80000000);
             break;
         case 0x4C: /* SDRAM_B3CR */
-            sdram_ddr_set_bcr(sdram, 3, val, sdram->cfg & 0x80000000);
+            sdram_ddr_set_bcr(s, 3, val, s->cfg & 0x80000000);
             break;
         case 0x80: /* SDRAM_TR */
-            sdram->tr = val & 0x018FC01F;
+            s->tr = val & 0x018FC01F;
             break;
         case 0x94: /* SDRAM_ECCCFG */
-            sdram->ecccfg = val & 0x00F00000;
+            s->ecccfg = val & 0x00F00000;
             break;
         case 0x98: /* SDRAM_ECCESR */
             val &= 0xFFF0F000;
-            if (sdram->eccesr == 0 && val != 0) {
-                qemu_irq_raise(sdram->irq);
-            } else if (sdram->eccesr != 0 && val == 0) {
-                qemu_irq_lower(sdram->irq);
+            if (s->eccesr == 0 && val != 0) {
+                qemu_irq_raise(s->irq);
+            } else if (s->eccesr != 0 && val == 0) {
+                qemu_irq_lower(s->irq);
             }
-            sdram->eccesr = val;
+            s->eccesr = val;
             break;
         default: /* Error */
             break;
@@ -382,21 +382,21 @@ static void sdram_ddr_dcr_write(void *opaque, int dcrn, uint32_t val)
 
 static void ppc4xx_sdram_ddr_reset(DeviceState *dev)
 {
-    Ppc4xxSdramDdrState *sdram = PPC4xx_SDRAM_DDR(dev);
-
-    sdram->addr = 0;
-    sdram->bear = 0;
-    sdram->besr0 = 0; /* No error */
-    sdram->besr1 = 0; /* No error */
-    sdram->cfg = 0;
-    sdram->ecccfg = 0; /* No ECC */
-    sdram->eccesr = 0; /* No error */
-    sdram->pmit = 0x07C00000;
-    sdram->rtr = 0x05F00000;
-    sdram->tr = 0x00854009;
+    Ppc4xxSdramDdrState *s = PPC4xx_SDRAM_DDR(dev);
+
+    s->addr = 0;
+    s->bear = 0;
+    s->besr0 = 0; /* No error */
+    s->besr1 = 0; /* No error */
+    s->cfg = 0;
+    s->ecccfg = 0; /* No ECC */
+    s->eccesr = 0; /* No error */
+    s->pmit = 0x07C00000;
+    s->rtr = 0x05F00000;
+    s->tr = 0x00854009;
     /* We pre-initialize RAM banks */
-    sdram->status = 0;
-    sdram->cfg = 0x00800000;
+    s->status = 0;
+    s->cfg = 0x00800000;
 }
 
 static void ppc4xx_sdram_ddr_realize(DeviceState *dev, Error **errp)
@@ -564,7 +564,7 @@ static void sdram_ddr2_unmap_bcr(Ppc4xxSdramDdr2State *sdram)
 
 static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
 {
-    Ppc4xxSdramDdr2State *sdram = opaque;
+    Ppc4xxSdramDdr2State *s = opaque;
     uint32_t ret = 0;
 
     switch (dcrn) {
@@ -572,9 +572,9 @@ static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
     case SDRAM_R1BAS:
     case SDRAM_R2BAS:
     case SDRAM_R3BAS:
-        if (sdram->bank[dcrn - SDRAM_R0BAS].size) {
-            ret = sdram_ddr2_bcr(sdram->bank[dcrn - SDRAM_R0BAS].base,
-                                 sdram->bank[dcrn - SDRAM_R0BAS].size);
+        if (s->bank[dcrn - SDRAM_R0BAS].size) {
+            ret = sdram_ddr2_bcr(s->bank[dcrn - SDRAM_R0BAS].base,
+                                 s->bank[dcrn - SDRAM_R0BAS].size);
         }
         break;
     case SDRAM_CONF1HB:
@@ -584,16 +584,16 @@ static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
     case SDRAM_PLBADDUHB:
         break;
     case SDRAM0_CFGADDR:
-        ret = sdram->addr;
+        ret = s->addr;
         break;
     case SDRAM0_CFGDATA:
-        switch (sdram->addr) {
+        switch (s->addr) {
         case 0x14: /* SDRAM_MCSTAT (405EX) */
         case 0x1F:
             ret = 0x80000000;
             break;
         case 0x21: /* SDRAM_MCOPT2 */
-            ret = sdram->mcopt2;
+            ret = s->mcopt2;
             break;
         case 0x40: /* SDRAM_MB0CF */
             ret = 0x00008001;
@@ -617,7 +617,7 @@ static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
 
 static void sdram_ddr2_dcr_write(void *opaque, int dcrn, uint32_t val)
 {
-    Ppc4xxSdramDdr2State *sdram = opaque;
+    Ppc4xxSdramDdr2State *s = opaque;
 
     switch (dcrn) {
     case SDRAM_R0BAS:
@@ -631,23 +631,23 @@ static void sdram_ddr2_dcr_write(void *opaque, int dcrn, uint32_t val)
     case SDRAM_PLBADDUHB:
         break;
     case SDRAM0_CFGADDR:
-        sdram->addr = val;
+        s->addr = val;
         break;
     case SDRAM0_CFGDATA:
-        switch (sdram->addr) {
+        switch (s->addr) {
         case 0x00: /* B0CR */
             break;
         case 0x21: /* SDRAM_MCOPT2 */
-            if (!(sdram->mcopt2 & BIT(27)) && (val & BIT(27))) {
+            if (!(s->mcopt2 & BIT(27)) && (val & BIT(27))) {
                 trace_ppc4xx_sdram_enable("enable");
                 /* validate all RAM mappings */
-                sdram_ddr2_map_bcr(sdram);
-                sdram->mcopt2 |= BIT(27);
-            } else if ((sdram->mcopt2 & BIT(27)) && !(val & BIT(27))) {
+                sdram_ddr2_map_bcr(s);
+                s->mcopt2 |= BIT(27);
+            } else if ((s->mcopt2 & BIT(27)) && !(val & BIT(27))) {
                 trace_ppc4xx_sdram_enable("disable");
                 /* invalidate all RAM mappings */
-                sdram_ddr2_unmap_bcr(sdram);
-                sdram->mcopt2 &= ~BIT(27);
+                sdram_ddr2_unmap_bcr(s);
+                s->mcopt2 &= ~BIT(27);
             }
             break;
         default:
@@ -661,10 +661,10 @@ static void sdram_ddr2_dcr_write(void *opaque, int dcrn, uint32_t val)
 
 static void ppc4xx_sdram_ddr2_reset(DeviceState *dev)
 {
-    Ppc4xxSdramDdr2State *sdram = PPC4xx_SDRAM_DDR2(dev);
+    Ppc4xxSdramDdr2State *s = PPC4xx_SDRAM_DDR2(dev);
 
-    sdram->addr = 0;
-    sdram->mcopt2 = 0;
+    s->addr = 0;
+    s->mcopt2 = 0;
 }
 
 static void ppc4xx_sdram_ddr2_realize(DeviceState *dev, Error **errp)
Signature · 1 line
2.30.4
#23 lore raw

PATCH v3 9/20 ppc440_sdram: Split off map/unmap of sdram banks for later reuse

In reply to BALATON Zoltan
---
 hw/ppc/ppc440_uc.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)
hw/ppc/ppc440_uc.c +19 -12
index 5db59d1190..01184e717b 100644
@@ -561,26 +561,33 @@ static uint64_t sdram_size(uint32_t bcr)
     return size;
 }
 
+static void sdram_bank_map(Ppc4xxSdramBank *bank)
+{
+    memory_region_init(&bank->container, NULL, "sdram-container", bank->size);
+    memory_region_add_subregion(&bank->container, 0, &bank->ram);
+    memory_region_add_subregion(get_system_memory(), bank->base,
+                                &bank->container);
+}
+
+static void sdram_bank_unmap(Ppc4xxSdramBank *bank)
+{
+    memory_region_del_subregion(get_system_memory(), &bank->container);
+    memory_region_del_subregion(&bank->container, &bank->ram);
+    object_unparent(OBJECT(&bank->container));
+}
+
 static void sdram_set_bcr(ppc440_sdram_t *sdram, int i,
                           uint32_t bcr, int enabled)
 {
     if (sdram->bank[i].bcr & 1) {
         /* First unmap RAM if enabled */
-        memory_region_del_subregion(get_system_memory(),
-                                    &sdram->bank[i].container);
-        memory_region_del_subregion(&sdram->bank[i].container,
-                                    &sdram->bank[i].ram);
-        object_unparent(OBJECT(&sdram->bank[i].container));
+        sdram_bank_unmap(&sdram->bank[i]);
     }
     sdram->bank[i].bcr = bcr & 0xffe0ffc1;
+    sdram->bank[i].base = sdram_base(bcr);
+    sdram->bank[i].size = sdram_size(bcr);
     if (enabled && (bcr & 1)) {
-        memory_region_init(&sdram->bank[i].container, NULL, "sdram-container",
-                           sdram_size(bcr));
-        memory_region_add_subregion(&sdram->bank[i].container, 0,
-                                    &sdram->bank[i].ram);
-        memory_region_add_subregion(get_system_memory(),
-                                    sdram_base(bcr),
-                                    &sdram->bank[i].container);
+        sdram_bank_map(&sdram->bank[i]);
     }
 }
 
Signature · 1 line
2.30.4
#31 lore raw

PATCH v3 13/20 ppc4xx_sdram: Rename functions to prevent name clashes

In reply to BALATON Zoltan

Rename functions to avoid name clashes when moving the DDR2 controller model currently called ppc440_sdram to ppc4xx_devs. This also more clearly shows which function belongs to which model.

---
 hw/ppc/ppc440_uc.c   | 69 ++++++++++++++++++++++----------------------
 hw/ppc/ppc4xx_devs.c | 44 ++++++++++++++--------------
 2 files changed, 57 insertions(+), 56 deletions(-)
hw/ppc/ppc440_uc.c +35 -34
index d8a7947196..565bfffc22 100644
@@ -502,7 +502,7 @@ enum {
     SDRAM_PLBADDUHB = 0x50,
 };
 
-static uint32_t sdram_bcr(hwaddr ram_base, hwaddr ram_size)
+static uint32_t sdram_ddr2_bcr(hwaddr ram_base, hwaddr ram_size)
 {
     uint32_t bcr;
 
@@ -547,12 +547,12 @@ static uint32_t sdram_bcr(hwaddr ram_base, hwaddr ram_size)
     return bcr;
 }
 
-static inline hwaddr sdram_base(uint32_t bcr)
+static inline hwaddr sdram_ddr2_base(uint32_t bcr)
 {
     return (bcr & 0xffe00000) << 2;
 }
 
-static uint64_t sdram_size(uint32_t bcr)
+static uint64_t sdram_ddr2_size(uint32_t bcr)
 {
     uint64_t size;
     int sh;
@@ -578,50 +578,51 @@ static void sdram_bank_unmap(Ppc4xxSdramBank *bank)
     object_unparent(OBJECT(&bank->container));
 }
 
-static void sdram_set_bcr(ppc440_sdram_t *sdram, int i,
-                          uint32_t bcr, int enabled)
+static void sdram_ddr2_set_bcr(ppc440_sdram_t *sdram, int i,
+                               uint32_t bcr, int enabled)
 {
     if (sdram->bank[i].bcr & 1) {
         /* First unmap RAM if enabled */
-        trace_ppc4xx_sdram_unmap(sdram_base(sdram->bank[i].bcr),
-                                 sdram_size(sdram->bank[i].bcr));
+        trace_ppc4xx_sdram_unmap(sdram_ddr2_base(sdram->bank[i].bcr),
+                                 sdram_ddr2_size(sdram->bank[i].bcr));
         sdram_bank_unmap(&sdram->bank[i]);
     }
     sdram->bank[i].bcr = bcr & 0xffe0ffc1;
-    sdram->bank[i].base = sdram_base(bcr);
-    sdram->bank[i].size = sdram_size(bcr);
+    sdram->bank[i].base = sdram_ddr2_base(bcr);
+    sdram->bank[i].size = sdram_ddr2_size(bcr);
     if (enabled && (bcr & 1)) {
-        trace_ppc4xx_sdram_map(sdram_base(bcr), sdram_size(bcr));
+        trace_ppc4xx_sdram_map(sdram_ddr2_base(bcr), sdram_ddr2_size(bcr));
         sdram_bank_map(&sdram->bank[i]);
     }
 }
 
-static void sdram_map_bcr(ppc440_sdram_t *sdram)
+static void sdram_ddr2_map_bcr(ppc440_sdram_t *sdram)
 {
     int i;
 
     for (i = 0; i < sdram->nbanks; i++) {
         if (sdram->bank[i].size) {
-            sdram_set_bcr(sdram, i, sdram_bcr(sdram->bank[i].base,
+            sdram_ddr2_set_bcr(sdram, i,
+                               sdram_ddr2_bcr(sdram->bank[i].base,
                                               sdram->bank[i].size), 1);
         } else {
-            sdram_set_bcr(sdram, i, 0, 0);
+            sdram_ddr2_set_bcr(sdram, i, 0, 0);
         }
     }
 }
 
-static void sdram_unmap_bcr(ppc440_sdram_t *sdram)
+static void sdram_ddr2_unmap_bcr(ppc440_sdram_t *sdram)
 {
     int i;
 
     for (i = 0; i < sdram->nbanks; i++) {
         if (sdram->bank[i].size) {
-            sdram_set_bcr(sdram, i, sdram->bank[i].bcr & ~1, 0);
+            sdram_ddr2_set_bcr(sdram, i, sdram->bank[i].bcr & ~1, 0);
         }
     }
 }
 
-static uint32_t dcr_read_sdram(void *opaque, int dcrn)
+static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
 {
     ppc440_sdram_t *sdram = opaque;
     uint32_t ret = 0;
@@ -632,8 +633,8 @@ static uint32_t dcr_read_sdram(void *opaque, int dcrn)
     case SDRAM_R2BAS:
     case SDRAM_R3BAS:
         if (sdram->bank[dcrn - SDRAM_R0BAS].size) {
-            ret = sdram_bcr(sdram->bank[dcrn - SDRAM_R0BAS].base,
-                            sdram->bank[dcrn - SDRAM_R0BAS].size);
+            ret = sdram_ddr2_bcr(sdram->bank[dcrn - SDRAM_R0BAS].base,
+                                 sdram->bank[dcrn - SDRAM_R0BAS].size);
         }
         break;
     case SDRAM_CONF1HB:
@@ -674,7 +675,7 @@ static uint32_t dcr_read_sdram(void *opaque, int dcrn)
     return ret;
 }
 
-static void dcr_write_sdram(void *opaque, int dcrn, uint32_t val)
+static void sdram_ddr2_dcr_write(void *opaque, int dcrn, uint32_t val)
 {
     ppc440_sdram_t *sdram = opaque;
 
@@ -700,12 +701,12 @@ static void dcr_write_sdram(void *opaque, int dcrn, uint32_t val)
             if (!(sdram->mcopt2 & BIT(27)) && (val & BIT(27))) {
                 trace_ppc4xx_sdram_enable("enable");
                 /* validate all RAM mappings */
-                sdram_map_bcr(sdram);
+                sdram_ddr2_map_bcr(sdram);
                 sdram->mcopt2 |= BIT(27);
             } else if ((sdram->mcopt2 & BIT(27)) && !(val & BIT(27))) {
                 trace_ppc4xx_sdram_enable("disable");
                 /* invalidate all RAM mappings */
-                sdram_unmap_bcr(sdram);
+                sdram_ddr2_unmap_bcr(sdram);
                 sdram->mcopt2 &= ~BIT(27);
             }
             break;
@@ -718,7 +719,7 @@ static void dcr_write_sdram(void *opaque, int dcrn, uint32_t val)
     }
 }
 
-static void sdram_reset(void *opaque)
+static void sdram_ddr2_reset(void *opaque)
 {
     ppc440_sdram_t *sdram = opaque;
 
@@ -739,30 +740,30 @@ void ppc440_sdram_init(CPUPPCState *env, int nbanks,
         s->bank[i].base = ram_banks[i].base;
         s->bank[i].size = ram_banks[i].size;
     }
-    qemu_register_reset(&sdram_reset, s);
+    qemu_register_reset(&sdram_ddr2_reset, s);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM0_CFGDATA,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
 
     ppc_dcr_register(env, SDRAM_R0BAS,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_R1BAS,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_R2BAS,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_R3BAS,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_CONF1HB,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_PLBADDULL,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_CONF1LL,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_CONFPATHB,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
     ppc_dcr_register(env, SDRAM_PLBADDUHB,
-                     s, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
 }
 
 /*****************************************************************************/
hw/ppc/ppc4xx_devs.c +22 -22

113 lines not on this page · raw message

Signature · 1 line
2.30.4
#33 lore raw

PATCH v3 15/20 ppc440_sdram: QOM'ify

In reply to BALATON Zoltan

Change the ppc440_sdram model to a QOM class derived from the PPC4xx-dcr-device and name it ppc4xx-sdram-ddr2. This is mostly modelling the DDR2 SDRAM controller found in the 460EX (used on the sam460ex board). Newer SoCs (regardless of their PPC core, e.g. 405EX) may have this controller but we only emulate enough of it for the sam460ex u-boot firmware.

---
 hw/ppc/ppc440.h         |   2 -
 hw/ppc/ppc440_uc.c      | 115 +++++++++++++++++++++++++---------------
 hw/ppc/sam460ex.c       |   7 ++-
 include/hw/ppc/ppc4xx.h |  14 +++++
 4 files changed, 91 insertions(+), 47 deletions(-)
hw/ppc/ppc440.h +0 -2
index 29f6f14ed7..7c24db8504 100644
@@ -16,8 +16,6 @@
 void ppc4xx_l2sram_init(CPUPPCState *env);
 void ppc4xx_cpr_init(CPUPPCState *env);
 void ppc4xx_sdr_init(CPUPPCState *env);
-void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       MemoryRegion *ram);
 void ppc4xx_ahb_init(CPUPPCState *env);
 void ppc4xx_dma_init(CPUPPCState *env, int dcr_base);
 void ppc460ex_pcie_init(CPUPPCState *env);
hw/ppc/ppc440_uc.c +71 -44

171 lines not on this page · raw message

hw/ppc/sam460ex.c +6 -1
index 9b850808a3..ea06b099b2 100644
@@ -342,11 +342,16 @@ static void sam460ex_init(MachineState *machine)
         error_report("Memory below 64 MiB is not supported");
         exit(1);
     }
+    dev = qdev_new(TYPE_PPC4xx_SDRAM_DDR2);
+    object_property_set_link(OBJECT(dev), "dram", OBJECT(machine->ram),
+                             &error_abort);
     /*
      * Put all RAM on first bank because board has one slot
      * and firmware only checks that
      */
-    ppc440_sdram_init(env, 1, machine->ram);
+    object_property_set_int(OBJECT(dev), "nbanks", 1, &error_abort);
+    ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(dev), cpu, &error_fatal);
+    object_unref(OBJECT(dev));
     /* FIXME: does 460EX have ECC interrupts? */
     /* Enable SDRAM memory regions as we may boot without firmware */
     if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x21) ||
include/hw/ppc/ppc4xx.h +14 -0
index 20d0cdde8a..7d3cfa7ad6 100644
@@ -139,4 +139,18 @@ struct Ppc4xxSdramDdrState {
     uint32_t eccesr;
 };
 
+/* SDRAM DDR2 controller */
+#define TYPE_PPC4xx_SDRAM_DDR2 "ppc4xx-sdram-ddr2"
+OBJECT_DECLARE_SIMPLE_TYPE(Ppc4xxSdramDdr2State, PPC4xx_SDRAM_DDR2);
+struct Ppc4xxSdramDdr2State {
+    Ppc4xxDcrDeviceState parent_obj;
+
+    MemoryRegion *dram_mr;
+    uint32_t nbanks; /* Banks to use from 4, e.g. when board has less slots */
+    Ppc4xxSdramBank bank[4];
+
+    uint32_t addr;
+    uint32_t mcopt2;
+};
+
 #endif /* PPC4XX_H */
Signature · 1 line
2.30.4
#32 lore raw

PATCH v3 14/20 ppc440_sdram: Move RAM size check to ppc440_sdram_init

In reply to BALATON Zoltan

Move the check for valid memory sizes from board to sdram controller init. Board now only checks for additional restrictions imposed by firmware then sdram init checks for valid sizes for SoC.

---
 hw/ppc/ppc440.h    |  4 ++--
 hw/ppc/ppc440_uc.c | 15 +++++++--------
 hw/ppc/sam460ex.c  | 32 +++++++++++++++++---------------
 3 files changed, 26 insertions(+), 25 deletions(-)
hw/ppc/ppc440.h +2 -2
index 01d76b8000..29f6f14ed7 100644
@@ -11,13 +11,13 @@
 #ifndef PPC440_H
 #define PPC440_H
 
-#include "hw/ppc/ppc4xx.h"
+#include "hw/ppc/ppc.h"
 
 void ppc4xx_l2sram_init(CPUPPCState *env);
 void ppc4xx_cpr_init(CPUPPCState *env);
 void ppc4xx_sdr_init(CPUPPCState *env);
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       Ppc4xxSdramBank *ram_banks);
+                       MemoryRegion *ram);
 void ppc4xx_ahb_init(CPUPPCState *env);
 void ppc4xx_dma_init(CPUPPCState *env, int dcr_base);
 void ppc460ex_pcie_init(CPUPPCState *env);
hw/ppc/ppc440_uc.c +7 -8
index 565bfffc22..f48eba215a 100644
@@ -486,7 +486,7 @@ void ppc4xx_sdr_init(CPUPPCState *env)
 typedef struct ppc440_sdram_t {
     uint32_t addr;
     uint32_t mcopt2;
-    int nbanks;
+    int nbanks; /* Banks to use from the 4, e.g. when board has less slots */
     Ppc4xxSdramBank bank[4];
 } ppc440_sdram_t;
 
@@ -728,18 +728,17 @@ static void sdram_ddr2_reset(void *opaque)
 }
 
 void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       Ppc4xxSdramBank *ram_banks)
+                       MemoryRegion *ram)
 {
     ppc440_sdram_t *s;
-    int i;
+    const ram_addr_t valid_bank_sizes[] = {
+        4 * GiB, 2 * GiB, 1 * GiB, 512 * MiB, 256 * MiB, 128 * MiB, 64 * MiB,
+        32 * MiB, 16 * MiB, 8 * MiB, 0
+    };
 
     s = g_malloc0(sizeof(*s));
     s->nbanks = nbanks;
-    for (i = 0; i < nbanks; i++) {
-        s->bank[i].ram = ram_banks[i].ram;
-        s->bank[i].base = ram_banks[i].base;
-        s->bank[i].size = ram_banks[i].size;
-    }
+    ppc4xx_sdram_banks(ram, s->nbanks, s->bank, valid_bank_sizes);
     qemu_register_reset(&sdram_ddr2_reset, s);
     ppc_dcr_register(env, SDRAM0_CFGADDR,
                      s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
hw/ppc/sam460ex.c +17 -15
index dac329d482..9b850808a3 100644
@@ -74,13 +74,6 @@
 #define EBC_FREQ 115000000
 #define UART_FREQ 11059200
 
-/* The SoC could also handle 4 GiB but firmware does not work with that. */
-/* Maybe it overflows a signed 32 bit number somewhere? */
-static const ram_addr_t ppc460ex_sdram_bank_sizes[] = {
-    2 * GiB, 1 * GiB, 512 * MiB, 256 * MiB, 128 * MiB, 64 * MiB,
-    32 * MiB, 0
-};
-
 struct boot_info {
     uint32_t dt_base;
     uint32_t dt_size;
@@ -273,7 +266,6 @@ static void sam460ex_init(MachineState *machine)
 {
     MemoryRegion *address_space_mem = get_system_memory();
     MemoryRegion *isa = g_new(MemoryRegion, 1);
-    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank, 1);
     MemoryRegion *l2cache_ram = g_new(MemoryRegion, 1);
     DeviceState *uic[4];
     int i;
@@ -340,12 +332,22 @@ static void sam460ex_init(MachineState *machine)
     }
 
     /* SDRAM controller */
-    /* put all RAM on first bank because board has one slot
-     * and firmware only checks that */
-    ppc4xx_sdram_banks(machine->ram, 1, ram_banks, ppc460ex_sdram_bank_sizes);
-
+    /* The SoC could also handle 4 GiB but firmware does not work with that. */
+    if (machine->ram_size > 2 * GiB) {
+        error_report("Memory over 2 GiB is not supported");
+        exit(1);
+    }
+    /* Firmware needs at least 64 MiB */
+    if (machine->ram_size < 64 * MiB) {
+        error_report("Memory below 64 MiB is not supported");
+        exit(1);
+    }
+    /*
+     * Put all RAM on first bank because board has one slot
+     * and firmware only checks that
+     */
+    ppc440_sdram_init(env, 1, machine->ram);
     /* FIXME: does 460EX have ECC interrupts? */
-    ppc440_sdram_init(env, 1, ram_banks);
     /* Enable SDRAM memory regions as we may boot without firmware */
     if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x21) ||
         ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x08000000)) {
@@ -358,8 +360,8 @@ static void sam460ex_init(MachineState *machine)
                                qdev_get_gpio_in(uic[0], 2));
     i2c = PPC4xx_I2C(dev)->bus;
     /* SPD EEPROM on RAM module */
-    spd_data = spd_data_generate(ram_banks->size < 128 * MiB ? DDR : DDR2,
-                                 ram_banks->size);
+    spd_data = spd_data_generate(machine->ram_size < 128 * MiB ? DDR : DDR2,
+                                 machine->ram_size);
     spd_data[20] = 4; /* SO-DIMM module */
     smbus_eeprom_init_one(i2c, 0x50, spd_data);
     /* RTC */
Signature · 1 line
2.30.4
#35 lore raw

PATCH v3 17/20 ppc4xx_sdram: Use hwaddr for memory bank size

In reply to BALATON Zoltan

This resolves the target_ulong dependency that's clearly wrong and was also noted in a fixme comment.

---
 hw/ppc/ppc4xx_sdram.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
hw/ppc/ppc4xx_sdram.c +4 -10
index bc28d69a26..242e2f4c6e 100644
@@ -34,7 +34,6 @@
 #include "qapi/error.h"
 #include "qemu/log.h"
 #include "exec/address-spaces.h" /* get_system_memory() */
-#include "exec/cpu-defs.h" /* target_ulong */
 #include "hw/irq.h"
 #include "hw/qdev-properties.h"
 #include "qapi/error.h"
@@ -122,11 +121,6 @@ static void sdram_bank_unmap(Ppc4xxSdramBank *bank)
 
 /*****************************************************************************/
 /* DDR SDRAM controller */
-/*
- * XXX: TOFIX: some patches have made this code become inconsistent:
- *      there are type inconsistencies, mixing hwaddr, target_ulong
- *      and uint32_t
- */
 static uint32_t sdram_ddr_bcr(hwaddr ram_base, hwaddr ram_size)
 {
     uint32_t bcr;
@@ -170,9 +164,9 @@ static inline hwaddr sdram_ddr_base(uint32_t bcr)
     return bcr & 0xFF800000;
 }
 
-static target_ulong sdram_ddr_size(uint32_t bcr)
+static hwaddr sdram_ddr_size(uint32_t bcr)
 {
-    target_ulong size;
+    hwaddr size;
     int sh;
 
     sh = (bcr >> 17) & 0x7;
@@ -513,9 +507,9 @@ static inline hwaddr sdram_ddr2_base(uint32_t bcr)
     return (bcr & 0xffe00000) << 2;
 }
 
-static uint64_t sdram_ddr2_size(uint32_t bcr)
+static hwaddr sdram_ddr2_size(uint32_t bcr)
 {
-    uint64_t size;
+    hwaddr size;
     int sh;
 
     sh = 1024 - ((bcr >> 6) & 0x3ff);
Signature · 1 line
2.30.4
#34 lore raw

PATCH v3 16/20 ppc4xx_sdram: Move ppc4xx DDR and DDR2 SDRAM controller models together

In reply to BALATON Zoltan

Move the PPC4xx DDR and DDR2 SDRAM contrller models into a new file called ppc4xx_sdram to separate from other device models and put them in one place allowing sharing some code between them.

---
 hw/ppc/meson.build      |   3 +-
 hw/ppc/ppc440_uc.c      | 321 -----------------
 hw/ppc/ppc4xx_devs.c    | 403 ---------------------
 hw/ppc/ppc4xx_sdram.c   | 752 ++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/ppc4xx.h |  34 +-
 5 files changed, 771 insertions(+), 742 deletions(-)
 create mode 100644 hw/ppc/ppc4xx_sdram.c
hw/ppc/meson.build +2 -1
index 62801923f3..74720dd1e1 100644
@@ -59,8 +59,9 @@ ppc_ss.add(when: 'CONFIG_PPC440', if_true: files(
   'ppc440_bamboo.c',
   'ppc440_pcix.c', 'ppc440_uc.c'))
 ppc_ss.add(when: 'CONFIG_PPC4XX', if_true: files(
+  'ppc4xx_devs.c',
   'ppc4xx_pci.c',
-  'ppc4xx_devs.c'))
+  'ppc4xx_sdram.c'))
 ppc_ss.add(when: 'CONFIG_SAM460EX', if_true: files('sam460ex.c'))
 # PReP
 ppc_ss.add(when: 'CONFIG_PREP', if_true: files('prep.c'))
hw/ppc/ppc440_uc.c +0 -321

347 lines not on this page · raw message

hw/ppc/ppc4xx_devs.c +0 -403

419 lines not on this page · raw message

hw/ppc/ppc4xx_sdram.c new file +752 -0

752 lines not on this page · raw message

include/hw/ppc/ppc4xx.h +17 -17
index 7d3cfa7ad6..7216d62882 100644
@@ -29,23 +29,6 @@
 #include "exec/memory.h"
 #include "hw/sysbus.h"
 
-typedef struct {
-    MemoryRegion ram;
-    MemoryRegion container; /* used for clipping */
-    hwaddr base;
-    hwaddr size;
-    uint32_t bcr;
-} Ppc4xxSdramBank;
-
-enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
-void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
-                        Ppc4xxSdramBank ram_banks[],
-                        const ram_addr_t sdram_bank_sizes[]);
-
 #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
 /*
@@ -116,6 +99,23 @@ struct Ppc4xxEbcState {
 };
 
 /* SDRAM DDR controller */
+typedef struct {
+    MemoryRegion ram;
+    MemoryRegion container; /* used for clipping */
+    hwaddr base;
+    hwaddr size;
+    uint32_t bcr;
+} Ppc4xxSdramBank;
+
+enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
+#define SDR0_DDR0_DDRM_ENCODE(n)  ((((unsigned long)(n)) & 0x03) << 29)
+#define SDR0_DDR0_DDRM_DDR1       0x20000000
+#define SDR0_DDR0_DDRM_DDR2       0x40000000
+
 #define TYPE_PPC4xx_SDRAM_DDR "ppc4xx-sdram-ddr"
 OBJECT_DECLARE_SIMPLE_TYPE(Ppc4xxSdramDdrState, PPC4xx_SDRAM_DDR);
 struct Ppc4xxSdramDdrState {
Signature · 1 line
2.30.4
#38 lore raw

PATCH v3 20/20 ppc4xx_sdram: Convert DDR SDRAM controller to new bank handling

In reply to BALATON Zoltan

Use the generic bank handling introduced in previous patch in the DDR SDRAM controller too. This also fixes previously broken region unmap due to sdram_ddr_unmap_bcr() ignoring container region so it crashed with an assert when the guest tried to disable the controller.

---
 hw/ppc/ppc4xx_sdram.c | 98 ++++++++++++++++---------------------------
 1 file changed, 37 insertions(+), 61 deletions(-)
hw/ppc/ppc4xx_sdram.c +37 -61
index 79a9efce4b..c731012940 100644
@@ -137,6 +137,8 @@ static void sdram_bank_set_bcr(Ppc4xxSdramBank *bank, uint32_t bcr,
 
 /*****************************************************************************/
 /* DDR SDRAM controller */
+#define SDRAM_DDR_BCR_MASK 0xFFDEE001
+
 static uint32_t sdram_ddr_bcr(hwaddr ram_base, hwaddr ram_size)
 {
     uint32_t bcr;
@@ -195,58 +197,6 @@ static hwaddr sdram_ddr_size(uint32_t bcr)
     return size;
 }
 
-static void sdram_ddr_set_bcr(Ppc4xxSdramDdrState *sdram, int i,
-                              uint32_t bcr, int enabled)
-{
-    if (sdram->bank[i].bcr & 1) {
-        /* Unmap RAM */
-        trace_ppc4xx_sdram_unmap(sdram_ddr_base(sdram->bank[i].bcr),
-                                 sdram_ddr_size(sdram->bank[i].bcr));
-        memory_region_del_subregion(get_system_memory(),
-                                    &sdram->bank[i].container);
-        memory_region_del_subregion(&sdram->bank[i].container,
-                                    &sdram->bank[i].ram);
-        object_unparent(OBJECT(&sdram->bank[i].container));
-    }
-    sdram->bank[i].bcr = bcr & 0xFFDEE001;
-    if (enabled && (bcr & 1)) {
-        trace_ppc4xx_sdram_map(sdram_ddr_base(bcr), sdram_ddr_size(bcr));
-        memory_region_init(&sdram->bank[i].container, NULL, "sdram-container",
-                           sdram_ddr_size(bcr));
-        memory_region_add_subregion(&sdram->bank[i].container, 0,
-                                    &sdram->bank[i].ram);
-        memory_region_add_subregion(get_system_memory(),
-                                    sdram_ddr_base(bcr),
-                                    &sdram->bank[i].container);
-    }
-}
-
-static void sdram_ddr_map_bcr(Ppc4xxSdramDdrState *sdram)
-{
-    int i;
-
-    for (i = 0; i < sdram->nbanks; i++) {
-        if (sdram->bank[i].size != 0) {
-            sdram_ddr_set_bcr(sdram, i, sdram_ddr_bcr(sdram->bank[i].base,
-                                                      sdram->bank[i].size), 1);
-        } else {
-            sdram_ddr_set_bcr(sdram, i, 0, 0);
-        }
-    }
-}
-
-static void sdram_ddr_unmap_bcr(Ppc4xxSdramDdrState *sdram)
-{
-    int i;
-
-    for (i = 0; i < sdram->nbanks; i++) {
-        trace_ppc4xx_sdram_unmap(sdram_ddr_base(sdram->bank[i].bcr),
-                                 sdram_ddr_size(sdram->bank[i].bcr));
-        memory_region_del_subregion(get_system_memory(),
-                                    &sdram->bank[i].ram);
-    }
-}
-
 static uint32_t sdram_ddr_dcr_read(void *opaque, int dcrn)
 {
     Ppc4xxSdramDdrState *s = opaque;
@@ -317,6 +267,7 @@ static uint32_t sdram_ddr_dcr_read(void *opaque, int dcrn)
 static void sdram_ddr_dcr_write(void *opaque, int dcrn, uint32_t val)
 {
     Ppc4xxSdramDdrState *s = opaque;
+    int i;
 
     switch (dcrn) {
     case SDRAM0_CFGADDR:
@@ -338,12 +289,24 @@ static void sdram_ddr_dcr_write(void *opaque, int dcrn, uint32_t val)
             if (!(s->cfg & 0x80000000) && (val & 0x80000000)) {
                 trace_ppc4xx_sdram_enable("enable");
                 /* validate all RAM mappings */
-                sdram_ddr_map_bcr(s);
+                for (i = 0; i < s->nbanks; i++) {
+                    if (s->bank[i].size) {
+                        sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
+                                           s->bank[i].base, s->bank[i].size,
+                                           1);
+                    }
+                }
                 s->status &= ~0x80000000;
             } else if ((s->cfg & 0x80000000) && !(val & 0x80000000)) {
                 trace_ppc4xx_sdram_enable("disable");
                 /* invalidate all RAM mappings */
-                sdram_ddr_unmap_bcr(s);
+                for (i = 0; i < s->nbanks; i++) {
+                    if (s->bank[i].size) {
+                        sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
+                                           s->bank[i].base, s->bank[i].size,
+                                           0);
+                    }
+                }
                 s->status |= 0x80000000;
             }
             if (!(s->cfg & 0x40000000) && (val & 0x40000000)) {
@@ -363,16 +326,16 @@ static void sdram_ddr_dcr_write(void *opaque, int dcrn, uint32_t val)
             s->pmit = (val & 0xF8000000) | 0x07C00000;
             break;
         case 0x40: /* SDRAM_B0CR */
-            sdram_ddr_set_bcr(s, 0, val, s->cfg & 0x80000000);
-            break;
         case 0x44: /* SDRAM_B1CR */
-            sdram_ddr_set_bcr(s, 1, val, s->cfg & 0x80000000);
-            break;
         case 0x48: /* SDRAM_B2CR */
-            sdram_ddr_set_bcr(s, 2, val, s->cfg & 0x80000000);
-            break;
         case 0x4C: /* SDRAM_B3CR */
-            sdram_ddr_set_bcr(s, 3, val, s->cfg & 0x80000000);
+            i = (s->addr - 0x40) / 4;
+            val &= SDRAM_DDR_BCR_MASK;
+            if (s->bank[i].size) {
+                sdram_bank_set_bcr(&s->bank[i], val,
+                                   sdram_ddr_base(val), sdram_ddr_size(val),
+                                   s->cfg & 0x80000000);
+            }
             break;
         case 0x80: /* SDRAM_TR */
             s->tr = val & 0x018FC01F;
@@ -422,6 +385,7 @@ static void ppc4xx_sdram_ddr_realize(DeviceState *dev, Error **errp)
     const ram_addr_t valid_bank_sizes[] = {
         256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
     };
+    int i;
 
     if (s->nbanks < 1 || s->nbanks > 4) {
         error_setg(errp, "Invalid number of RAM banks");
@@ -432,6 +396,18 @@ static void ppc4xx_sdram_ddr_realize(DeviceState *dev, Error **errp)
         return;
     }
     ppc4xx_sdram_banks(s->dram_mr, s->nbanks, s->bank, valid_bank_sizes);
+    for (i = 0; i < s->nbanks; i++) {
+        if (s->bank[i].size) {
+            s->bank[i].bcr = sdram_ddr_bcr(s->bank[i].base, s->bank[i].size);
+            sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
+                               s->bank[i].base, s->bank[i].size, 0);
+        } else {
+            sdram_bank_set_bcr(&s->bank[i], 0, 0, 0, 0);
+        }
+        trace_ppc4xx_sdram_init(sdram_ddr_base(s->bank[i].bcr),
+                                sdram_ddr_size(s->bank[i].bcr),
+                                s->bank[i].bcr);
+    }
 
     sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
 
Signature · 1 line
2.30.4
#37 lore raw

PATCH v3 19/20 ppc4xx_sdram: Generalise bank setup

In reply to BALATON Zoltan

Currently only base and size are set on initial bank creation and bcr value is computed on mapping the region. Set bcr at init so the bcr encoding method becomes local to the controller model and mapping and unmapping can operate on the bank so it can be shared between different controller models. This patch converts the DDR2 controller.

---
 hw/ppc/ppc4xx_sdram.c | 93 ++++++++++++++++++++++---------------------
 hw/ppc/trace-events   |  1 +
 2 files changed, 48 insertions(+), 46 deletions(-)
hw/ppc/ppc4xx_sdram.c +47 -46
index e36898a906..79a9efce4b 100644
@@ -106,6 +106,7 @@ static void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
 
 static void sdram_bank_map(Ppc4xxSdramBank *bank)
 {
+    trace_ppc4xx_sdram_map(bank->base, bank->size);
     memory_region_init(&bank->container, NULL, "sdram-container", bank->size);
     memory_region_add_subregion(&bank->container, 0, &bank->ram);
     memory_region_add_subregion(get_system_memory(), bank->base,
@@ -114,11 +115,26 @@ static void sdram_bank_map(Ppc4xxSdramBank *bank)
 
 static void sdram_bank_unmap(Ppc4xxSdramBank *bank)
 {
+    trace_ppc4xx_sdram_unmap(bank->base, bank->size);
     memory_region_del_subregion(get_system_memory(), &bank->container);
     memory_region_del_subregion(&bank->container, &bank->ram);
     object_unparent(OBJECT(&bank->container));
 }
 
+static void sdram_bank_set_bcr(Ppc4xxSdramBank *bank, uint32_t bcr,
+                               hwaddr base, hwaddr size, int enabled)
+{
+    if (memory_region_is_mapped(&bank->container)) {
+        sdram_bank_unmap(bank);
+    }
+    bank->bcr = bcr;
+    bank->base = base;
+    bank->size = size;
+    if (enabled && (bcr & 1)) {
+        sdram_bank_map(bank);
+    }
+}
+
 /*****************************************************************************/
 /* DDR SDRAM controller */
 static uint32_t sdram_ddr_bcr(hwaddr ram_base, hwaddr ram_size)
@@ -445,6 +461,8 @@ static void ppc4xx_sdram_ddr_class_init(ObjectClass *oc, void *data)
 
 /*****************************************************************************/
 /* DDR2 SDRAM controller */
+#define SDRAM_DDR2_BCR_MASK 0xffe0ffc1
+
 enum {
     SDRAM_R0BAS = 0x40,
     SDRAM_R1BAS,
@@ -518,50 +536,6 @@ static hwaddr sdram_ddr2_size(uint32_t bcr)
     return size;
 }
 
-static void sdram_ddr2_set_bcr(Ppc4xxSdramDdr2State *sdram, int i,
-                               uint32_t bcr, int enabled)
-{
-    if (sdram->bank[i].bcr & 1) {
-        /* First unmap RAM if enabled */
-        trace_ppc4xx_sdram_unmap(sdram_ddr2_base(sdram->bank[i].bcr),
-                                 sdram_ddr2_size(sdram->bank[i].bcr));
-        sdram_bank_unmap(&sdram->bank[i]);
-    }
-    sdram->bank[i].bcr = bcr & 0xffe0ffc1;
-    sdram->bank[i].base = sdram_ddr2_base(bcr);
-    sdram->bank[i].size = sdram_ddr2_size(bcr);
-    if (enabled && (bcr & 1)) {
-        trace_ppc4xx_sdram_map(sdram_ddr2_base(bcr), sdram_ddr2_size(bcr));
-        sdram_bank_map(&sdram->bank[i]);
-    }
-}
-
-static void sdram_ddr2_map_bcr(Ppc4xxSdramDdr2State *sdram)
-{
-    int i;
-
-    for (i = 0; i < sdram->nbanks; i++) {
-        if (sdram->bank[i].size) {
-            sdram_ddr2_set_bcr(sdram, i,
-                               sdram_ddr2_bcr(sdram->bank[i].base,
-                                              sdram->bank[i].size), 1);
-        } else {
-            sdram_ddr2_set_bcr(sdram, i, 0, 0);
-        }
-    }
-}
-
-static void sdram_ddr2_unmap_bcr(Ppc4xxSdramDdr2State *sdram)
-{
-    int i;
-
-    for (i = 0; i < sdram->nbanks; i++) {
-        if (sdram->bank[i].size) {
-            sdram_ddr2_set_bcr(sdram, i, sdram->bank[i].bcr & ~1, 0);
-        }
-    }
-}
-
 static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
 {
     Ppc4xxSdramDdr2State *s = opaque;
@@ -618,6 +592,7 @@ static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
 static void sdram_ddr2_dcr_write(void *opaque, int dcrn, uint32_t val)
 {
     Ppc4xxSdramDdr2State *s = opaque;
+    int i;
 
     switch (dcrn) {
     case SDRAM_R0BAS:
@@ -641,12 +616,24 @@ static void sdram_ddr2_dcr_write(void *opaque, int dcrn, uint32_t val)
             if (!(s->mcopt2 & BIT(27)) && (val & BIT(27))) {
                 trace_ppc4xx_sdram_enable("enable");
                 /* validate all RAM mappings */
-                sdram_ddr2_map_bcr(s);
+                for (i = 0; i < s->nbanks; i++) {
+                    if (s->bank[i].size) {
+                        sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
+                                           s->bank[i].base, s->bank[i].size,
+                                           1);
+                    }
+                }
                 s->mcopt2 |= BIT(27);
             } else if ((s->mcopt2 & BIT(27)) && !(val & BIT(27))) {
                 trace_ppc4xx_sdram_enable("disable");
                 /* invalidate all RAM mappings */
-                sdram_ddr2_unmap_bcr(s);
+                for (i = 0; i < s->nbanks; i++) {
+                    if (s->bank[i].size) {
+                        sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
+                                           s->bank[i].base, s->bank[i].size,
+                                           0);
+                    }
+                }
                 s->mcopt2 &= ~BIT(27);
             }
             break;
@@ -675,6 +662,7 @@ static void ppc4xx_sdram_ddr2_realize(DeviceState *dev, Error **errp)
         4 * GiB, 2 * GiB, 1 * GiB, 512 * MiB, 256 * MiB, 128 * MiB, 64 * MiB,
         32 * MiB, 16 * MiB, 8 * MiB, 0
     };
+    int i;
 
     if (s->nbanks < 1 || s->nbanks > 4) {
         error_setg(errp, "Invalid number of RAM banks");
@@ -685,6 +673,19 @@ static void ppc4xx_sdram_ddr2_realize(DeviceState *dev, Error **errp)
         return;
     }
     ppc4xx_sdram_banks(s->dram_mr, s->nbanks, s->bank, valid_bank_sizes);
+    for (i = 0; i < s->nbanks; i++) {
+        if (s->bank[i].size) {
+            s->bank[i].bcr = sdram_ddr2_bcr(s->bank[i].base, s->bank[i].size);
+            s->bank[i].bcr &= SDRAM_DDR2_BCR_MASK;
+            sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
+                               s->bank[i].base, s->bank[i].size, 0);
+        } else {
+            sdram_bank_set_bcr(&s->bank[i], 0, 0, 0, 0);
+        }
+        trace_ppc4xx_sdram_init(sdram_ddr2_base(s->bank[i].bcr),
+                                sdram_ddr2_size(s->bank[i].bcr),
+                                s->bank[i].bcr);
+    }
 
     ppc4xx_dcr_register(dcr, SDRAM0_CFGADDR,
                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
hw/ppc/trace-events +1 -0
index a07d5aca0f..3b3e4211d4 100644
@@ -179,3 +179,4 @@ ppc405ep_clocks_setup(const char *trace) "%s"
 ppc4xx_sdram_enable(const char *trace) "%s SDRAM controller"
 ppc4xx_sdram_unmap(uint64_t addr, uint64_t size) "Unmap RAM area 0x%" PRIx64 " size 0x%" PRIx64
 ppc4xx_sdram_map(uint64_t addr, uint64_t size) "Map RAM area 0x%" PRIx64 " size 0x%" PRIx64
+ppc4xx_sdram_init(uint64_t base, uint64_t size, uint32_t bcr) "Init RAM area 0x%" PRIx64 " size 0x%" PRIx64 " bcr 0x%x"
Signature · 1 line
2.30.4
#4 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

In reply to BALATON Zoltan
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Quoted 42 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--
  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
      /* Public */
      MemoryRegion ram_banks[2];
      hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
 
      MemoryRegion *dram_mr;
      hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -0
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
      MachineClass *mc = MACHINE_GET_CLASS(machine);
      const char *kernel_filename = machine->kernel_filename;
      MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
 
      if (machine->ram_size != mc->default_ram_size) {
          char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                               machine->ram_size, &error_fatal);

hw/ppc/ppc405_boards.c static void ppc405_init(MachineState *machine) · line 297

Quoted 10 lines
hw/ppc/ppc405_boards.c +2 -2
      object_property_set_link(OBJECT(&ppc405->soc), "dram",
                               OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
      object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                               &error_abort);
      qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);
 
+    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

hw/ppc/ppc405_boards.c static void ppc405_init(MachineState *machine) · line 300

Quoted 3 lines
hw/ppc/ppc405_boards.c +3 -0
+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

I am not in favor of these ppc_drc_write calls and this is still a hack.

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

C.

Quoted 123 lines
hw/ppc/ppc405_boards.c +4 -0
+        error_report("Could not enable memory regions");
+        exit(1);
+    }
+
      /* allocate and load BIOS */
      if (machine->firmware) {
          MemoryRegion *bios = g_new(MemoryRegion, 1);
hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                               s->ram_bases[0], s->ram_sizes[0]);
 
      ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
 
      /* External bus controller */
      if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
  static Property ppc405_soc_properties[] = {
      DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                       MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
      DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
      DEFINE_PROP_END_OF_LIST(),
  };
hw/ppc/ppc440_bamboo.c +7 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
      ppc4xx_sdram_init(env,
                        qdev_get_gpio_in(uicdev, 14),
                        PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the firmware */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("couldn't enable memory regions");
+        exit(1);
+    }
 
      /* PCI */
      dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
  } ppc440_sdram_t;
 
  enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
      SDRAM_R0BAS = 0x40,
      SDRAM_R1BAS,
      SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -10
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
      qemu_irq irq;
  };
 
-enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
  /*
   * XXX: TOFIX: some patches have made this code become inconsistent:
   *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
  void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                         MemoryRegion *ram_memories,
                         hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
  {
      ppc4xx_sdram_t *sdram;
      int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                       sdram, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM0_CFGDATA,
                       sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
  }
 
  /*
include/hw/ppc/ppc4xx.h +6 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
      uint32_t bcr;
  } Ppc4xxSdramBank;
 
+enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
  void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                          MemoryRegion ram_memories[],
                          hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
  void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                          MemoryRegion ram_memories[],
                          hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
 
  #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
Reviewed-by on v3 4/20
#15 lore raw

Re: [PATCH v3 04/20] ppc4xx: Use Ppc4xxSdramBank in ppc4xx_sdram_banks()

In reply to BALATON Zoltan
Quoted 5 lines · BALATON Zoltan

Change ppc4xx_sdram_banks() to take one Ppc4xxSdramBank array instead of the separate arrays and adjust ppc4xx_sdram_init() and ppc440_sdram_init() accordingly as well as machines using these.

Thanks,

C.

Quoted 261 lines

---
v2: Use pointer for ram_banks in the prototype of the init funcs as an array of struct seems to confuse gcc 12.2.1 and provoke a warning

  hw/ppc/ppc405.h         |  4 +---
  hw/ppc/ppc405_uc.c      | 10 +++++-----
  hw/ppc/ppc440.h         |  5 ++---
  hw/ppc/ppc440_bamboo.c  | 15 ++++++---------
  hw/ppc/ppc440_uc.c      |  9 ++++-----
  hw/ppc/ppc4xx_devs.c    | 21 +++++++++------------
  hw/ppc/sam460ex.c       | 15 +++++----------
  include/hw/ppc/ppc4xx.h |  9 +++------
  8 files changed, 35 insertions(+), 53 deletions(-)
hw/ppc/ppc405.h +1 -3
index 756865621b..ca0972b88b 100644
@@ -167,9 +167,7 @@ struct Ppc405SoCState {
      DeviceState parent_obj;
 
      /* Public */
-    MemoryRegion ram_banks[2];
-    hwaddr ram_bases[2], ram_sizes[2];
-
+    Ppc4xxSdramBank ram_banks[2];
      MemoryRegion *dram_mr;
      hwaddr ram_size;
 
hw/ppc/ppc405_uc.c +5 -5
index 1e02347e57..bcbf35bc14 100644
@@ -1074,14 +1074,14 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
 
      /* SDRAM controller */
          /* XXX 405EP has no ECC interrupt */
-    s->ram_bases[0] = 0;
-    s->ram_sizes[0] = s->ram_size;
-    memory_region_init_alias(&s->ram_banks[0], OBJECT(s),
+    s->ram_banks[0].base = 0;
+    s->ram_banks[0].size = s->ram_size;
+    memory_region_init_alias(&s->ram_banks[0].ram, OBJECT(s),
                               "ppc405.sdram0", s->dram_mr,
-                             s->ram_bases[0], s->ram_sizes[0]);
+                             s->ram_banks[0].base, s->ram_banks[0].size);
 
      ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes);
+                      s->ram_banks);
 
      /* External bus controller */
      if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
hw/ppc/ppc440.h +2 -3
index 7cef936125..e6c905b7d6 100644
@@ -11,14 +11,13 @@
  #ifndef PPC440_H
  #define PPC440_H
 
-#include "hw/ppc/ppc.h"
+#include "hw/ppc/ppc4xx.h"
 
  void ppc4xx_l2sram_init(CPUPPCState *env);
  void ppc4xx_cpr_init(CPUPPCState *env);
  void ppc4xx_sdr_init(CPUPPCState *env);
  void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       MemoryRegion *ram_memories,
-                       hwaddr *ram_bases, hwaddr *ram_sizes,
+                       Ppc4xxSdramBank *ram_banks,
                         int do_init);
  void ppc4xx_ahb_init(CPUPPCState *env);
  void ppc4xx_dma_init(CPUPPCState *env, int dcr_base);
hw/ppc/ppc440_bamboo.c +6 -9
index e3412c4fcd..2aac8a3fe9 100644
@@ -168,9 +168,8 @@ static void bamboo_init(MachineState *machine)
      unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
      MemoryRegion *address_space_mem = get_system_memory();
      MemoryRegion *isa = g_new(MemoryRegion, 1);
-    MemoryRegion *ram_memories = g_new(MemoryRegion, PPC440EP_SDRAM_NR_BANKS);
-    hwaddr ram_bases[PPC440EP_SDRAM_NR_BANKS] = {0};
-    hwaddr ram_sizes[PPC440EP_SDRAM_NR_BANKS] = {0};
+    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank,
+                                        PPC440EP_SDRAM_NR_BANKS);
      PCIBus *pcibus;
      PowerPCCPU *cpu;
      CPUPPCState *env;
@@ -205,13 +204,11 @@ static void bamboo_init(MachineState *machine)
                         qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
 
      /* SDRAM controller */
-    ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                       ram_bases, ram_sizes, ppc440ep_sdram_bank_sizes);
+    ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_banks,
+                       ppc440ep_sdram_bank_sizes);
      /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
-    ppc4xx_sdram_init(env,
-                      qdev_get_gpio_in(uicdev, 14),
-                      PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes);
+    ppc4xx_sdram_init(env, qdev_get_gpio_in(uicdev, 14),
+                      PPC440EP_SDRAM_NR_BANKS, ram_banks);
      /* Enable SDRAM memory regions, this should be done by the firmware */
      if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
          ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
hw/ppc/ppc440_uc.c +4 -5
index 6ab0ad7985..5db59d1190 100644
@@ -690,8 +690,7 @@ static void sdram_reset(void *opaque)
  }
 
  void ppc440_sdram_init(CPUPPCState *env, int nbanks,
-                       MemoryRegion *ram_memories,
-                       hwaddr *ram_bases, hwaddr *ram_sizes,
+                       Ppc4xxSdramBank *ram_banks,
                         int do_init)
  {
      ppc440_sdram_t *sdram;
@@ -700,9 +699,9 @@ void ppc440_sdram_init(CPUPPCState *env, int nbanks,
      sdram = g_malloc0(sizeof(*sdram));
      sdram->nbanks = nbanks;
      for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_memories[i];
-        sdram->bank[i].base = ram_bases[i];
-        sdram->bank[i].size = ram_sizes[i];
+        sdram->bank[i].ram = ram_banks[i].ram;
+        sdram->bank[i].base = ram_banks[i].base;
+        sdram->bank[i].size = ram_banks[i].size;
      }
      qemu_register_reset(&sdram_reset, sdram);
      ppc_dcr_register(env, SDRAM0_CFGADDR,
hw/ppc/ppc4xx_devs.c +9 -12
index 936d6f77fe..7bdcbd6fac 100644
@@ -343,9 +343,7 @@ static void sdram_reset(void *opaque)
  }
 
  void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       MemoryRegion *ram_memories,
-                       hwaddr *ram_bases,
-                       hwaddr *ram_sizes)
+                       Ppc4xxSdramBank *ram_banks)
  {
      ppc4xx_sdram_t *sdram;
      int i;
@@ -354,9 +352,9 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
      sdram->irq = irq;
      sdram->nbanks = nbanks;
      for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_memories[i];
-        sdram->bank[i].base = ram_bases[i];
-        sdram->bank[i].size = ram_sizes[i];
+        sdram->bank[i].ram = ram_banks[i].ram;
+        sdram->bank[i].base = ram_banks[i].base;
+        sdram->bank[i].size = ram_banks[i].size;
      }
      qemu_register_reset(&sdram_reset, sdram);
      ppc_dcr_register(env, SDRAM0_CFGADDR,
@@ -376,8 +374,7 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
   * sizes varies by SoC.
   */
  void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
-                        MemoryRegion ram_memories[],
-                        hwaddr ram_bases[], hwaddr ram_sizes[],
+                        Ppc4xxSdramBank ram_banks[],
                          const ram_addr_t sdram_bank_sizes[])
  {
      ram_addr_t size_left = memory_region_size(ram);
@@ -392,13 +389,13 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
              if (bank_size <= size_left) {
                  char name[32];
 
-                ram_bases[i] = base;
-                ram_sizes[i] = bank_size;
+                ram_banks[i].base = base;
+                ram_banks[i].size = bank_size;
                  base += bank_size;
                  size_left -= bank_size;
                  snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
-                memory_region_init_alias(&ram_memories[i], NULL, name, ram,
-                                         ram_bases[i], ram_sizes[i]);
+                memory_region_init_alias(&ram_banks[i].ram, NULL, name, ram,
+                                         ram_banks[i].base, ram_banks[i].size);
                  break;
              }
          }
hw/ppc/sam460ex.c +5 -10
index 850bb3b817..f4c2a693fb 100644
@@ -73,7 +73,6 @@
  #define OPB_FREQ 115000000
  #define EBC_FREQ 115000000
  #define UART_FREQ 11059200
-#define SDRAM_NR_BANKS 4
 
  /* The SoC could also handle 4 GiB but firmware does not work with that. */
  /* Maybe it overflows a signed 32 bit number somewhere? */
@@ -274,9 +273,7 @@ static void sam460ex_init(MachineState *machine)
  {
      MemoryRegion *address_space_mem = get_system_memory();
      MemoryRegion *isa = g_new(MemoryRegion, 1);
-    MemoryRegion *ram_memories = g_new(MemoryRegion, SDRAM_NR_BANKS);
-    hwaddr ram_bases[SDRAM_NR_BANKS] = {0};
-    hwaddr ram_sizes[SDRAM_NR_BANKS] = {0};
+    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank, 1);
      MemoryRegion *l2cache_ram = g_new(MemoryRegion, 1);
      DeviceState *uic[4];
      int i;
@@ -345,20 +342,18 @@ static void sam460ex_init(MachineState *machine)
      /* SDRAM controller */
      /* put all RAM on first bank because board has one slot
       * and firmware only checks that */
-    ppc4xx_sdram_banks(machine->ram, 1, ram_memories, ram_bases, ram_sizes,
-                       ppc460ex_sdram_bank_sizes);
+    ppc4xx_sdram_banks(machine->ram, 1, ram_banks, ppc460ex_sdram_bank_sizes);
 
      /* FIXME: does 460EX have ECC interrupts? */
-    ppc440_sdram_init(env, SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+    ppc440_sdram_init(env, 1, ram_banks, 1);
 
      /* IIC controllers and devices */
      dev = sysbus_create_simple(TYPE_PPC4xx_I2C, 0x4ef600700,
                                 qdev_get_gpio_in(uic[0], 2));
      i2c = PPC4xx_I2C(dev)->bus;
      /* SPD EEPROM on RAM module */
-    spd_data = spd_data_generate(ram_sizes[0] < 128 * MiB ? DDR : DDR2,
-                                 ram_sizes[0]);
+    spd_data = spd_data_generate(ram_banks->size < 128 * MiB ? DDR : DDR2,
+                                 ram_banks->size);
      spd_data[20] = 4; /* SO-DIMM module */
      smbus_eeprom_init_one(i2c, 0x50, spd_data);
      /* RTC */
include/hw/ppc/ppc4xx.h +3 -6
index a5e6c185af..5013b8bf3a 100644
@@ -43,14 +43,11 @@ enum {
  };
 
  void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
-                        MemoryRegion ram_memories[],
-                        hwaddr ram_bases[], hwaddr ram_sizes[],
+                        Ppc4xxSdramBank ram_banks[],
                          const ram_addr_t sdram_bank_sizes[]);
 
-void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
-                        MemoryRegion ram_memories[],
-                        hwaddr *ram_bases,
-                        hwaddr *ram_sizes);
+void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
+                       Ppc4xxSdramBank *ram_banks);
 
  #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#18 lore raw

Re: [PATCH v3 06/20] ppc4xx_sdram: Move size check to ppc4xx_sdram_init()

In reply to BALATON Zoltan
Quoted 3 lines · BALATON Zoltan

Instead of checking if memory size is valid in board code move this check to ppc4xx_sdram_init() as this is a restriction imposed by the SDRAM controller.

So, we are relying on ppc4xx_sdram_banks() to check the RAM size and report the error. The problem is the exit.

You also need to change the prototypes of some routine to take an "Error **errp" parameter as realize routines do.

    qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);
        ppc405_soc_realize(DeviceState *dev, Error **errp)
             ppc4xx_sdram_init()
                  ppc4xx_sdram_banks()

at least, ppc4xx_sdram_banks() should be changed. The next patch doing the QOMification takes care of ppc4xx_sdram_init()

Thanks,

C.

Quoted 169 lines
---
  hw/ppc/ppc405.h         |  2 --
  hw/ppc/ppc405_boards.c  | 10 ----------
  hw/ppc/ppc405_uc.c      | 11 ++---------
  hw/ppc/ppc440_bamboo.c  | 10 +---------
  hw/ppc/ppc4xx_devs.c    | 14 ++++++--------
  include/hw/ppc/ppc4xx.h |  2 +-
  6 files changed, 10 insertions(+), 39 deletions(-)
hw/ppc/ppc405.h +0 -2
index ca0972b88b..ad54dff542 100644
@@ -167,9 +167,7 @@ struct Ppc405SoCState {
      DeviceState parent_obj;
 
      /* Public */
-    Ppc4xxSdramBank ram_banks[2];
      MemoryRegion *dram_mr;
-    hwaddr ram_size;
 
      PowerPCCPU cpu;
      PPCUIC uic;
hw/ppc/ppc405_boards.c +0 -10
index bf02a71c6d..cdd4e0cb4c 100644
@@ -271,22 +271,12 @@ static void boot_from_kernel(MachineState *machine, PowerPCCPU *cpu)
  static void ppc405_init(MachineState *machine)
  {
      Ppc405MachineState *ppc405 = PPC405_MACHINE(machine);
-    MachineClass *mc = MACHINE_GET_CLASS(machine);
      const char *kernel_filename = machine->kernel_filename;
      MemoryRegion *sysmem = get_system_memory();
      CPUPPCState *env;
 
-    if (machine->ram_size != mc->default_ram_size) {
-        char *sz = size_to_str(mc->default_ram_size);
-        error_report("Invalid RAM size, should be %s", sz);
-        g_free(sz);
-        exit(EXIT_FAILURE);
-    }
-
      object_initialize_child(OBJECT(machine), "soc", &ppc405->soc,
                              TYPE_PPC405_SOC);
-    object_property_set_uint(OBJECT(&ppc405->soc), "ram-size",
-                             machine->ram_size, &error_fatal);
      object_property_set_link(OBJECT(&ppc405->soc), "dram",
                               OBJECT(machine->ram), &error_abort);
      object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
hw/ppc/ppc405_uc.c +2 -9
index bcbf35bc14..e1c7188e61 100644
@@ -1073,15 +1073,9 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                         qdev_get_gpio_in(DEVICE(&s->cpu), PPC40x_INPUT_CINT));
 
      /* SDRAM controller */
-        /* XXX 405EP has no ECC interrupt */
-    s->ram_banks[0].base = 0;
-    s->ram_banks[0].size = s->ram_size;
-    memory_region_init_alias(&s->ram_banks[0].ram, OBJECT(s),
-                             "ppc405.sdram0", s->dram_mr,
-                             s->ram_banks[0].base, s->ram_banks[0].size);
-
+    /* XXX 405EP has no ECC interrupt */
      ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks);
+                      s->dram_mr);
 
      /* External bus controller */
      if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1159,7 +1153,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
  static Property ppc405_soc_properties[] = {
      DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                       MemoryRegion *),
-    DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
      DEFINE_PROP_END_OF_LIST(),
  };
 
hw/ppc/ppc440_bamboo.c +1 -9
index 2bd5e41140..9b456f1819 100644
@@ -50,10 +50,6 @@
 
  #define PPC440EP_SDRAM_NR_BANKS 4
 
-static const ram_addr_t ppc440ep_sdram_bank_sizes[] = {
-    256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
-};
-
  static hwaddr entry;
 
  static int bamboo_load_device_tree(hwaddr addr,
@@ -168,8 +164,6 @@ static void bamboo_init(MachineState *machine)
      unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
      MemoryRegion *address_space_mem = get_system_memory();
      MemoryRegion *isa = g_new(MemoryRegion, 1);
-    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank,
-                                        PPC440EP_SDRAM_NR_BANKS);
      PCIBus *pcibus;
      PowerPCCPU *cpu;
      CPUPPCState *env;
@@ -204,11 +198,9 @@ static void bamboo_init(MachineState *machine)
                         qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
 
      /* SDRAM controller */
-    ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_banks,
-                       ppc440ep_sdram_bank_sizes);
      /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
      ppc4xx_sdram_init(env, qdev_get_gpio_in(uicdev, 14),
-                      PPC440EP_SDRAM_NR_BANKS, ram_banks);
+                      PPC440EP_SDRAM_NR_BANKS, machine->ram);
      /* Enable SDRAM memory regions, this should be done by the firmware */
      if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
          ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
hw/ppc/ppc4xx_devs.c +6 -8
index 7bdcbd6fac..eb3aa97b16 100644
@@ -41,7 +41,7 @@
  typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
  struct ppc4xx_sdram_t {
      uint32_t addr;
-    int nbanks;
+    int nbanks; /* Banks to use from the 4, e.g. when board has less slots */
      Ppc4xxSdramBank bank[4];
      uint32_t besr0;
      uint32_t besr1;
@@ -343,19 +343,17 @@ static void sdram_reset(void *opaque)
  }
 
  void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       Ppc4xxSdramBank *ram_banks)
+                       MemoryRegion *ram)
  {
      ppc4xx_sdram_t *sdram;
-    int i;
+    const ram_addr_t valid_bank_sizes[] = {
+        256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
+    };
 
      sdram = g_new0(ppc4xx_sdram_t, 1);
      sdram->irq = irq;
      sdram->nbanks = nbanks;
-    for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_banks[i].ram;
-        sdram->bank[i].base = ram_banks[i].base;
-        sdram->bank[i].size = ram_banks[i].size;
-    }
+    ppc4xx_sdram_banks(ram, sdram->nbanks, sdram->bank, valid_bank_sizes);
      qemu_register_reset(&sdram_reset, sdram);
      ppc_dcr_register(env, SDRAM0_CFGADDR,
                       sdram, &dcr_read_sdram, &dcr_write_sdram);
include/hw/ppc/ppc4xx.h +1 -1
index 5013b8bf3a..6007a8dd04 100644
@@ -47,7 +47,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                          const ram_addr_t sdram_bank_sizes[]);
 
  void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       Ppc4xxSdramBank *ram_banks);
+                       MemoryRegion *ram);
 
  #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#21 lore raw

Re: [PATCH v3 07/20] ppc4xx_sdram: QOM'ify

In reply to BALATON Zoltan
Quoted 267 lines · BALATON Zoltan

Change the ppc4xx_sdram model to a QOM class derived from the PPC4xx-dcr-device and name it ppc4xx-sdram-ddr. This is mostly modelling the DDR SDRAM controller found in the 440EP (used on the bamboo board) but also backward compatible with the older DDR controllers on some 405 SoCs so we also use it for those now. This likely does not cause problems for guests we run as the new features are just not accessed but to model 405 SoC accurately some features may have to be disabled or the model split between 440 and older.

Newer SoCs (regardless of their PPC core, e.g. 405EX) may have an updated DDR2 SDRAM controller implemented by the ppc440_sdram model (only partially, enough for the 460EX on the sam460ex) that is not yet QOM'ified in this patch. That is intended to become ppc4xx-sdram-ddr2 when QOM'ified later.

---
  hw/ppc/ppc405.h         |  3 +-
  hw/ppc/ppc405_uc.c      | 22 +++++----
  hw/ppc/ppc440_bamboo.c  | 10 +++--
  hw/ppc/ppc4xx_devs.c    | 99 ++++++++++++++++++++++-------------------
  include/hw/ppc/ppc4xx.h | 27 +++++++++--
  5 files changed, 98 insertions(+), 63 deletions(-)
hw/ppc/ppc405.h +1 -2
index ad54dff542..9a4312691e 100644
@@ -167,8 +167,6 @@ struct Ppc405SoCState {
      DeviceState parent_obj;
 
      /* Public */
-    MemoryRegion *dram_mr;
-
      PowerPCCPU cpu;
      PPCUIC uic;
      Ppc405CpcState cpc;
@@ -182,6 +180,7 @@ struct Ppc405SoCState {
      Ppc405PobState pob;
      Ppc4xxPlbState plb;
      Ppc4xxMalState mal;
+    Ppc4xxSdramDdrState sdram;
  };
 
  #endif /* PPC405_H */
hw/ppc/ppc405_uc.c +13 -9
index e1c7188e61..c973cfb04e 100644
@@ -1016,6 +1016,9 @@ static void ppc405_soc_instance_init(Object *obj)
      object_initialize_child(obj, "plb", &s->plb, TYPE_PPC4xx_PLB);
 
      object_initialize_child(obj, "mal", &s->mal, TYPE_PPC4xx_MAL);
+
+    object_initialize_child(obj, "sdram", &s->sdram, TYPE_PPC4xx_SDRAM_DDR);
+    object_property_add_alias(obj, "dram", OBJECT(&s->sdram), "dram");
  }
 
  static void ppc405_reset(void *opaque)
@@ -1073,9 +1076,17 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                         qdev_get_gpio_in(DEVICE(&s->cpu), PPC40x_INPUT_CINT));
 
      /* SDRAM controller */
+    /*
+     * We use the 440 DDR SDRAM controller which has more regs and features
+     * but it's compatible enough for now
+     */
+    object_property_set_int(OBJECT(&s->sdram), "nbanks", 2, &error_abort);
+    if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->sdram), &s->cpu, errp)) {
+        return;
+    }
      /* XXX 405EP has no ECC interrupt */
-    ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->dram_mr);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdram), 0,
+                       qdev_get_gpio_in(DEVICE(&s->uic), 17));
 
      /* External bus controller */
      if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1150,12 +1161,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
      /* Uses UIC IRQs 9, 15, 17 */
  }
 
-static Property ppc405_soc_properties[] = {
-    DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
-                     MemoryRegion *),
-    DEFINE_PROP_END_OF_LIST(),
-};
-
  static void ppc405_soc_class_init(ObjectClass *oc, void *data)
  {
      DeviceClass *dc = DEVICE_CLASS(oc);
@@ -1163,7 +1168,6 @@ static void ppc405_soc_class_init(ObjectClass *oc, void *data)
      dc->realize = ppc405_soc_realize;
      /* Reason: only works as part of a ppc405 board/machine */
      dc->user_creatable = false;
-    device_class_set_props(dc, ppc405_soc_properties);
  }
 
  static const TypeInfo ppc405_types[] = {
hw/ppc/ppc440_bamboo.c +6 -4
index 9b456f1819..6052d3a2e0 100644
@@ -48,8 +48,6 @@
  #define PPC440EP_PCI_IO         0xe8000000
  #define PPC440EP_PCI_IOLEN      0x00010000
 
-#define PPC440EP_SDRAM_NR_BANKS 4
-
  static hwaddr entry;
 
  static int bamboo_load_device_tree(hwaddr addr,
@@ -198,9 +196,13 @@ static void bamboo_init(MachineState *machine)
                         qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
 
      /* SDRAM controller */
+    dev = qdev_new(TYPE_PPC4xx_SDRAM_DDR);
+    object_property_set_link(OBJECT(dev), "dram", OBJECT(machine->ram),
+                             &error_abort);
+    ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(dev), cpu, &error_fatal);
+    object_unref(OBJECT(dev));
      /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
-    ppc4xx_sdram_init(env, qdev_get_gpio_in(uicdev, 14),
-                      PPC440EP_SDRAM_NR_BANKS, machine->ram);
+    sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(uicdev, 14));
      /* Enable SDRAM memory regions, this should be done by the firmware */
      if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
          ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
hw/ppc/ppc4xx_devs.c +15 -44
index eb3aa97b16..375834a52b 100644
@@ -38,30 +38,12 @@
 
  /*****************************************************************************/
  /* SDRAM controller */
-typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
-struct ppc4xx_sdram_t {
-    uint32_t addr;
-    int nbanks; /* Banks to use from the 4, e.g. when board has less slots */
-    Ppc4xxSdramBank bank[4];
-    uint32_t besr0;
-    uint32_t besr1;
-    uint32_t bear;
-    uint32_t cfg;
-    uint32_t status;
-    uint32_t rtr;
-    uint32_t pmit;
-    uint32_t tr;
-    uint32_t ecccfg;
-    uint32_t eccesr;
-    qemu_irq irq;
-};
-
  /*
   * XXX: TOFIX: some patches have made this code become inconsistent:
   *      there are type inconsistencies, mixing hwaddr, target_ulong
   *      and uint32_t
   */
-static uint32_t sdram_bcr(hwaddr ram_base, hwaddr ram_size)
+static uint32_t sdram_ddr_bcr(hwaddr ram_base, hwaddr ram_size)
  {
      uint32_t bcr;
 
@@ -119,7 +101,7 @@ static target_ulong sdram_size(uint32_t bcr)
      return size;
  }
 
-static void sdram_set_bcr(ppc4xx_sdram_t *sdram, int i,
+static void sdram_set_bcr(Ppc4xxSdramDdrState *sdram, int i,
                            uint32_t bcr, int enabled)
  {
      if (sdram->bank[i].bcr & 0x00000001) {
@@ -145,21 +127,21 @@ static void sdram_set_bcr(ppc4xx_sdram_t *sdram, int i,
      }
  }
 
-static void sdram_map_bcr(ppc4xx_sdram_t *sdram)
+static void sdram_map_bcr(Ppc4xxSdramDdrState *sdram)
  {
      int i;
 
      for (i = 0; i < sdram->nbanks; i++) {
          if (sdram->bank[i].size != 0) {
-            sdram_set_bcr(sdram, i, sdram_bcr(sdram->bank[i].base,
-                                              sdram->bank[i].size), 1);
+            sdram_set_bcr(sdram, i, sdram_ddr_bcr(sdram->bank[i].base,
+                                                  sdram->bank[i].size), 1);
          } else {
              sdram_set_bcr(sdram, i, 0x00000000, 0);
          }
      }
  }
 
-static void sdram_unmap_bcr(ppc4xx_sdram_t *sdram)
+static void sdram_unmap_bcr(Ppc4xxSdramDdrState *sdram)
  {
      int i;
 
@@ -171,12 +153,11 @@ static void sdram_unmap_bcr(ppc4xx_sdram_t *sdram)
      }
  }
 
-static uint32_t dcr_read_sdram(void *opaque, int dcrn)
+static uint32_t sdram_ddr_dcr_read(void *opaque, int dcrn)
  {
-    ppc4xx_sdram_t *sdram;
+    Ppc4xxSdramDdrState *sdram = opaque;
      uint32_t ret;
 
-    sdram = opaque;
      switch (dcrn) {
      case SDRAM0_CFGADDR:
          ret = sdram->addr;
@@ -239,11 +220,10 @@ static uint32_t dcr_read_sdram(void *opaque, int dcrn)
      return ret;
  }
 
-static void dcr_write_sdram(void *opaque, int dcrn, uint32_t val)
+static void sdram_ddr_dcr_write(void *opaque, int dcrn, uint32_t val)
  {
-    ppc4xx_sdram_t *sdram;
+    Ppc4xxSdramDdrState *sdram = opaque;
 
-    sdram = opaque;
      switch (dcrn) {
      case SDRAM0_CFGADDR:
          sdram->addr = val;
@@ -322,11 +302,10 @@ static void dcr_write_sdram(void *opaque, int dcrn, uint32_t val)
      }
  }
 
-static void sdram_reset(void *opaque)
+static void ppc4xx_sdram_ddr_reset(DeviceState *dev)
  {
-    ppc4xx_sdram_t *sdram;
+    Ppc4xxSdramDdrState *sdram = PPC4xx_SDRAM_DDR(dev);
 
-    sdram = opaque;
      sdram->addr = 0x00000000;
      sdram->bear = 0x00000000;
      sdram->besr0 = 0x00000000; /* No error */
@@ -342,23 +321,48 @@ static void sdram_reset(void *opaque)
      sdram->cfg = 0x00800000;
  }
 
-void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       MemoryRegion *ram)
+static void ppc4xx_sdram_ddr_realize(DeviceState *dev, Error **errp)
  {
-    ppc4xx_sdram_t *sdram;
+    Ppc4xxSdramDdrState *s = PPC4xx_SDRAM_DDR(dev);
+    Ppc4xxDcrDeviceState *dcr = PPC4xx_DCR_DEVICE(dev);
      const ram_addr_t valid_bank_sizes[] = {
          256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
      };
 
-    sdram = g_new0(ppc4xx_sdram_t, 1);
-    sdram->irq = irq;
-    sdram->nbanks = nbanks;
-    ppc4xx_sdram_banks(ram, sdram->nbanks, sdram->bank, valid_bank_sizes);
-    qemu_register_reset(&sdram_reset, sdram);
-    ppc_dcr_register(env, SDRAM0_CFGADDR,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
-    ppc_dcr_register(env, SDRAM0_CFGDATA,

hw/ppc/ppc4xx_devs.c static void sdram_reset(void *opaque) · line 340

Quoted 10 lines
hw/ppc/ppc4xx_devs.c +9 -1
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+    if (s->nbanks < 1 || s->nbanks > 4) {
+        error_setg(errp, "Invalid number of RAM banks");
+        return;
+    }
+    if (!s->dram_mr) {
+        error_setg(errp, "Missing dram memory region");
+        return;
+    }
+    ppc4xx_sdram_banks(s->dram_mr, s->nbanks, s->bank, valid_bank_sizes);

As said previously, ppc4xx_sdram_banks should a take "Error **errp" and propagate the error.

Thanks,

C.

Quoted 83 lines
hw/ppc/ppc4xx_devs.c +30 -0
+
+    sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
+
+    ppc4xx_dcr_register(dcr, SDRAM0_CFGADDR,
+                        s, &sdram_ddr_dcr_read, &sdram_ddr_dcr_write);
+    ppc4xx_dcr_register(dcr, SDRAM0_CFGDATA,
+                        s, &sdram_ddr_dcr_read, &sdram_ddr_dcr_write);
+}
+
+static Property ppc4xx_sdram_ddr_props[] = {
+    DEFINE_PROP_LINK("dram", Ppc4xxSdramDdrState, dram_mr, TYPE_MEMORY_REGION,
+                     MemoryRegion *),
+    DEFINE_PROP_UINT32("nbanks", Ppc4xxSdramDdrState, nbanks, 4),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void ppc4xx_sdram_ddr_class_init(ObjectClass *oc, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+
+    dc->realize = ppc4xx_sdram_ddr_realize;
+    dc->reset = ppc4xx_sdram_ddr_reset;
+    /* Reason: only works as function of a ppc4xx SoC */
+    dc->user_creatable = false;
+    device_class_set_props(dc, ppc4xx_sdram_ddr_props);
  }
 
  /*
@@ -948,6 +952,11 @@ static void ppc4xx_dcr_class_init(ObjectClass *oc, void *data)
 
  static const TypeInfo ppc4xx_types[] = {
      {
+        .name           = TYPE_PPC4xx_SDRAM_DDR,
+        .parent         = TYPE_PPC4xx_DCR_DEVICE,
+        .instance_size  = sizeof(Ppc4xxSdramDdrState),
+        .class_init     = ppc4xx_sdram_ddr_class_init,
+    }, {
          .name           = TYPE_PPC4xx_MAL,
          .parent         = TYPE_PPC4xx_DCR_DEVICE,
          .instance_size  = sizeof(Ppc4xxMalState),
include/hw/ppc/ppc4xx.h +24 -3
index 6007a8dd04..20d0cdde8a 100644
@@ -46,9 +46,6 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                          Ppc4xxSdramBank ram_banks[],
                          const ram_addr_t sdram_bank_sizes[]);
 
-void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       MemoryRegion *ram);
-
  #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
  /*
@@ -118,4 +115,28 @@ struct Ppc4xxEbcState {
      uint32_t cfg;
  };
 
+/* SDRAM DDR controller */
+#define TYPE_PPC4xx_SDRAM_DDR "ppc4xx-sdram-ddr"
+OBJECT_DECLARE_SIMPLE_TYPE(Ppc4xxSdramDdrState, PPC4xx_SDRAM_DDR);
+struct Ppc4xxSdramDdrState {
+    Ppc4xxDcrDeviceState parent_obj;
+
+    MemoryRegion *dram_mr;
+    uint32_t nbanks; /* Banks to use from 4, e.g. when board has less slots */
+    Ppc4xxSdramBank bank[4];
+    qemu_irq irq;
+
+    uint32_t addr;
+    uint32_t besr0;
+    uint32_t besr1;
+    uint32_t bear;
+    uint32_t cfg;
+    uint32_t status;
+    uint32_t rtr;
+    uint32_t pmit;
+    uint32_t tr;
+    uint32_t ecccfg;
+    uint32_t eccesr;
+};
+
  #endif /* PPC4XX_H */
Reviewed-by on v3 12/20
#30 lore raw

Re: [PATCH v3 12/20] ppc440_sdram: Rename local variable for readibility

In reply to BALATON Zoltan
Quoted 3 lines · BALATON Zoltan

Rename local sdram variable in ppc440_sdram_init to s for readibility.

Thanks,

C.

Quoted 67 lines
---
  hw/ppc/ppc440_uc.c | 36 ++++++++++++++++++------------------
  1 file changed, 18 insertions(+), 18 deletions(-)
hw/ppc/ppc440_uc.c +18 -18
index b3f56c49b5..d8a7947196 100644
@@ -729,40 +729,40 @@ static void sdram_reset(void *opaque)
  void ppc440_sdram_init(CPUPPCState *env, int nbanks,
                         Ppc4xxSdramBank *ram_banks)
  {
-    ppc440_sdram_t *sdram;
+    ppc440_sdram_t *s;
      int i;
 
-    sdram = g_malloc0(sizeof(*sdram));
-    sdram->nbanks = nbanks;
+    s = g_malloc0(sizeof(*s));
+    s->nbanks = nbanks;
      for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_banks[i].ram;
-        sdram->bank[i].base = ram_banks[i].base;
-        sdram->bank[i].size = ram_banks[i].size;
+        s->bank[i].ram = ram_banks[i].ram;
+        s->bank[i].base = ram_banks[i].base;
+        s->bank[i].size = ram_banks[i].size;
      }
-    qemu_register_reset(&sdram_reset, sdram);
+    qemu_register_reset(&sdram_reset, s);
      ppc_dcr_register(env, SDRAM0_CFGADDR,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM0_CFGDATA,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
 
      ppc_dcr_register(env, SDRAM_R0BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_R1BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_R2BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_R3BAS,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_CONF1HB,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_PLBADDULL,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_CONF1LL,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_CONFPATHB,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM_PLBADDUHB,
-                     sdram, &dcr_read_sdram, &dcr_write_sdram);
+                     s, &dcr_read_sdram, &dcr_write_sdram);
  }
 
  /*****************************************************************************/
#27 lore raw

Re: [PATCH v3 10/20] ppc440_sdram: Implement enable bit in the DDR2 SDRAM

In reply to BALATON Zoltan
Quoted 5 lines · BALATON Zoltan

To allow removing the do_init hack we need to improve the DDR2 SDRAM controller model to handle the enable/disable bit that it ignored so far.

Please consider adding a define instead.

Thanks,

C.

Quoted 106 lines

---
v2: replace 0x08000000 with BIT(27)

  hw/ppc/ppc440_uc.c | 34 ++++++++++++++++++++++++++++++++--
  1 file changed, 32 insertions(+), 2 deletions(-)
hw/ppc/ppc440_uc.c +32 -2
index 01184e717b..3c442eaecc 100644
@@ -23,6 +23,7 @@
  #include "sysemu/reset.h"
  #include "ppc440.h"
  #include "qom/object.h"
+#include "trace.h"
 
  /*****************************************************************************/
  /* L2 Cache as SRAM */
@@ -484,6 +485,7 @@ void ppc4xx_sdr_init(CPUPPCState *env)
  /* SDRAM controller */
  typedef struct ppc440_sdram_t {
      uint32_t addr;
+    uint32_t mcopt2;
      int nbanks;
      Ppc4xxSdramBank bank[4];
  } ppc440_sdram_t;
@@ -581,12 +583,15 @@ static void sdram_set_bcr(ppc440_sdram_t *sdram, int i,
  {
      if (sdram->bank[i].bcr & 1) {
          /* First unmap RAM if enabled */
+        trace_ppc4xx_sdram_unmap(sdram_base(sdram->bank[i].bcr),
+                                 sdram_size(sdram->bank[i].bcr));
          sdram_bank_unmap(&sdram->bank[i]);
      }
      sdram->bank[i].bcr = bcr & 0xffe0ffc1;
      sdram->bank[i].base = sdram_base(bcr);
      sdram->bank[i].size = sdram_size(bcr);
      if (enabled && (bcr & 1)) {
+        trace_ppc4xx_sdram_map(sdram_base(bcr), sdram_size(bcr));
          sdram_bank_map(&sdram->bank[i]);
      }
  }
@@ -596,7 +601,7 @@ static void sdram_map_bcr(ppc440_sdram_t *sdram)
      int i;
 
      for (i = 0; i < sdram->nbanks; i++) {
-        if (sdram->bank[i].size != 0) {
+        if (sdram->bank[i].size) {
              sdram_set_bcr(sdram, i, sdram_bcr(sdram->bank[i].base,
                                                sdram->bank[i].size), 1);
          } else {
@@ -605,6 +610,17 @@ static void sdram_map_bcr(ppc440_sdram_t *sdram)
      }
  }
 
+static void sdram_unmap_bcr(ppc440_sdram_t *sdram)
+{
+    int i;
+
+    for (i = 0; i < sdram->nbanks; i++) {
+        if (sdram->bank[i].size) {
+            sdram_set_bcr(sdram, i, sdram->bank[i].bcr & ~1, 0);
+        }
+    }
+}
+
  static uint32_t dcr_read_sdram(void *opaque, int dcrn)
  {
      ppc440_sdram_t *sdram = opaque;
@@ -636,7 +652,7 @@ static uint32_t dcr_read_sdram(void *opaque, int dcrn)
              ret = 0x80000000;
              break;
          case 0x21: /* SDRAM_MCOPT2 */
-            ret = 0x08000000;
+            ret = sdram->mcopt2;
              break;
          case 0x40: /* SDRAM_MB0CF */
              ret = 0x00008001;
@@ -680,6 +696,19 @@ static void dcr_write_sdram(void *opaque, int dcrn, uint32_t val)
          switch (sdram->addr) {
          case 0x00: /* B0CR */
              break;
+        case 0x21: /* SDRAM_MCOPT2 */
+            if (!(sdram->mcopt2 & BIT(27)) && (val & BIT(27))) {
+                trace_ppc4xx_sdram_enable("enable");
+                /* validate all RAM mappings */
+                sdram_map_bcr(sdram);
+                sdram->mcopt2 |= BIT(27);
+            } else if ((sdram->mcopt2 & BIT(27)) && !(val & BIT(27))) {
+                trace_ppc4xx_sdram_enable("disable");
+                /* invalidate all RAM mappings */
+                sdram_unmap_bcr(sdram);
+                sdram->mcopt2 &= ~BIT(27);
+            }
+            break;
          default:
              break;
          }
@@ -694,6 +723,7 @@ static void sdram_reset(void *opaque)
      ppc440_sdram_t *sdram = opaque;
 
      sdram->addr = 0;
+    sdram->mcopt2 = BIT(27);
  }
 
  void ppc440_sdram_init(CPUPPCState *env, int nbanks,
#24 lore raw

Re: [PATCH v3 09/20] ppc440_sdram: Split off map/unmap of sdram banks for later reuse

In reply to BALATON Zoltan
Quoted 10 lines · BALATON Zoltan
---
  hw/ppc/ppc440_uc.c | 31 +++++++++++++++++++------------
  1 file changed, 19 insertions(+), 12 deletions(-)
hw/ppc/ppc440_uc.c +0 -0
index 5db59d1190..01184e717b 100644

hw/ppc/ppc440_uc.c static uint64_t sdram_size(uint32_t bcr) · line 566

Quoted 6 lines
hw/ppc/ppc440_uc.c +3 -0
@@ -561,26 +561,33 @@ static uint64_t sdram_size(uint32_t bcr)
      return size;
  }
 
+static void sdram_bank_map(Ppc4xxSdramBank *bank)
+{
+    memory_region_init(&bank->container, NULL, "sdram-container", bank->size);

This init belongs to the realize routine.

It is a oneliner. I think you can do it now without risks.

Thanks,

C.

Quoted 39 lines
hw/ppc/ppc440_uc.c +16 -12
+    memory_region_add_subregion(&bank->container, 0, &bank->ram);
+    memory_region_add_subregion(get_system_memory(), bank->base,
+                                &bank->container);
+}
+
+static void sdram_bank_unmap(Ppc4xxSdramBank *bank)
+{
+    memory_region_del_subregion(get_system_memory(), &bank->container);
+    memory_region_del_subregion(&bank->container, &bank->ram);
+    object_unparent(OBJECT(&bank->container));
+}
+
  static void sdram_set_bcr(ppc440_sdram_t *sdram, int i,
                            uint32_t bcr, int enabled)
  {
      if (sdram->bank[i].bcr & 1) {
          /* First unmap RAM if enabled */
-        memory_region_del_subregion(get_system_memory(),
-                                    &sdram->bank[i].container);
-        memory_region_del_subregion(&sdram->bank[i].container,
-                                    &sdram->bank[i].ram);
-        object_unparent(OBJECT(&sdram->bank[i].container));
+        sdram_bank_unmap(&sdram->bank[i]);
      }
      sdram->bank[i].bcr = bcr & 0xffe0ffc1;
+    sdram->bank[i].base = sdram_base(bcr);
+    sdram->bank[i].size = sdram_size(bcr);
      if (enabled && (bcr & 1)) {
-        memory_region_init(&sdram->bank[i].container, NULL, "sdram-container",
-                           sdram_size(bcr));
-        memory_region_add_subregion(&sdram->bank[i].container, 0,
-                                    &sdram->bank[i].ram);
-        memory_region_add_subregion(get_system_memory(),
-                                    sdram_base(bcr),
-                                    &sdram->bank[i].container);
+        sdram_bank_map(&sdram->bank[i]);
      }
  }
 
#39 lore raw
In reply to BALATON Zoltan
Quoted 6 lines · BALATON Zoltan

This is the end of the QOMify series started by Cédric. This series handles the SDRAM controller models to clean them up, QOMify and unify them and at least partially clean up the mess that has accumulated around these in the past. This includes the not yet merged patches from the last series and new ones that change the DDR2 version used by sam460ex.

I made comments on the first ~10 patches. Let's try to agree on these first. We will see the remaining ones in a second patchset.

Thanks,

C.

Quoted 42 lines

v3: Fix patches that got squashed during rebase
v2: address some review comments and try to avoid compile problem with gcc 12.2 (untested)

BALATON Zoltan (20):
ppc440_bamboo: Remove unnecessary memsets
ppc4xx: Introduce Ppc4xxSdramBank struct
ppc4xx_sdram: Get rid of the init RAM hack
ppc4xx: Use Ppc4xxSdramBank in ppc4xx_sdram_banks()
ppc440_bamboo: Add missing 4 MiB valid memory size
ppc4xx_sdram: Move size check to ppc4xx_sdram_init()
ppc4xx_sdram: QOM'ify
ppc4xx_sdram: Drop extra zeros for readability
ppc440_sdram: Split off map/unmap of sdram banks for later reuse
ppc440_sdram: Implement enable bit in the DDR2 SDRAM
ppc440_sdram: Get rid of the init RAM hack
ppc440_sdram: Rename local variable for readibility
ppc4xx_sdram: Rename functions to prevent name clashes
ppc440_sdram: Move RAM size check to ppc440_sdram_init
ppc440_sdram: QOM'ify
ppc4xx_sdram: Move ppc4xx DDR and DDR2 SDRAM controller models
together
ppc4xx_sdram: Use hwaddr for memory bank size
ppc4xx_sdram: Rename local state variable for brevity
ppc4xx_sdram: Generalise bank setup
ppc4xx_sdram: Convert DDR SDRAM controller to new bank handling

  hw/ppc/meson.build      |   3 +-
  hw/ppc/ppc405.h         |   8 +-
  hw/ppc/ppc405_boards.c  |  22 +-
  hw/ppc/ppc405_uc.c      |  33 +-
  hw/ppc/ppc440.h         |   4 -
  hw/ppc/ppc440_bamboo.c  |  29 +-
  hw/ppc/ppc440_uc.c      | 267 +--------------
  hw/ppc/ppc4xx_devs.c    | 413 -----------------------
  hw/ppc/ppc4xx_sdram.c   | 723 ++++++++++++++++++++++++++++++++++++++++
  hw/ppc/sam460ex.c       |  48 +--
  hw/ppc/trace-events     |   1 +
  include/hw/ppc/ppc4xx.h |  66 +++-
  12 files changed, 847 insertions(+), 770 deletions(-)

create mode 100644 hw/ppc/ppc4xx_sdram.c

#19 lore raw

Re: [PATCH v3 06/20] ppc4xx_sdram: Move size check to ppc4xx_sdram_init()

In reply to Cédric Le Goater
Quoted 12 lines · Cédric Le Goater
Quoted 3 lines · BALATON Zoltan

Instead of checking if memory size is valid in board code move this check to ppc4xx_sdram_init() as this is a restriction imposed by the SDRAM controller.

So, we are relying on ppc4xx_sdram_banks() to check the RAM size and report the error. The problem is the exit.

You also need to change the prototypes of some routine to take an "Error **errp" parameter as realize routines do.

Quoted 7 lines
   qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);
       ppc405_soc_realize(DeviceState *dev, Error **errp)
            ppc4xx_sdram_init()
                 ppc4xx_sdram_banks()

at least, ppc4xx_sdram_banks() should be changed. The next patch doing the QOMification takes care of ppc4xx_sdram_init()

I thought about changing ppc4xx_sdram_banks to use Error but did not do it first because of how the message is formatted. I had to look up docs on how to do this with Error, added a patch in v4 to do this conversion.

Regards,
BALATON Zoltan

Quoted 174 lines

Thanks,

C.

Quoted 164 lines
---
  hw/ppc/ppc405.h         |  2 --
  hw/ppc/ppc405_boards.c  | 10 ----------
  hw/ppc/ppc405_uc.c      | 11 ++---------
  hw/ppc/ppc440_bamboo.c  | 10 +---------
  hw/ppc/ppc4xx_devs.c    | 14 ++++++--------
  include/hw/ppc/ppc4xx.h |  2 +-
  6 files changed, 10 insertions(+), 39 deletions(-)
hw/ppc/ppc405.h +0 -2
index ca0972b88b..ad54dff542 100644
@@ -167,9 +167,7 @@ struct Ppc405SoCState {
      DeviceState parent_obj;
        /* Public */
-    Ppc4xxSdramBank ram_banks[2];
      MemoryRegion *dram_mr;
-    hwaddr ram_size;
        PowerPCCPU cpu;
      PPCUIC uic;
hw/ppc/ppc405_boards.c +0 -9
index bf02a71c6d..cdd4e0cb4c 100644
@@ -271,22 +271,12 @@ static void boot_from_kernel(MachineState *machine, 
 PowerPCCPU *cpu)
  static void ppc405_init(MachineState *machine)
  {
      Ppc405MachineState *ppc405 = PPC405_MACHINE(machine);
-    MachineClass *mc = MACHINE_GET_CLASS(machine);
      const char *kernel_filename = machine->kernel_filename;
      MemoryRegion *sysmem = get_system_memory();
      CPUPPCState *env;
  -    if (machine->ram_size != mc->default_ram_size) {
-        char *sz = size_to_str(mc->default_ram_size);
-        error_report("Invalid RAM size, should be %s", sz);
-        g_free(sz);
-        exit(EXIT_FAILURE);
-    }
-
      object_initialize_child(OBJECT(machine), "soc", &ppc405->soc,
                              TYPE_PPC405_SOC);
-    object_property_set_uint(OBJECT(&ppc405->soc), "ram-size",
-                             machine->ram_size, &error_fatal);
      object_property_set_link(OBJECT(&ppc405->soc), "dram",
                               OBJECT(machine->ram), &error_abort);
      object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
hw/ppc/ppc405_uc.c +2 -9
index bcbf35bc14..e1c7188e61 100644
@@ -1073,15 +1073,9 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
                         qdev_get_gpio_in(DEVICE(&s->cpu), 
 PPC40x_INPUT_CINT));
        /* SDRAM controller */
-        /* XXX 405EP has no ECC interrupt */
-    s->ram_banks[0].base = 0;
-    s->ram_banks[0].size = s->ram_size;
-    memory_region_init_alias(&s->ram_banks[0].ram, OBJECT(s),
-                             "ppc405.sdram0", s->dram_mr,
-                             s->ram_banks[0].base, s->ram_banks[0].size);
-
+    /* XXX 405EP has no ECC interrupt */
      ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks);
+                      s->dram_mr);
        /* External bus controller */
      if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1159,7 +1153,6 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
  static Property ppc405_soc_properties[] = {
      DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                       MemoryRegion *),
-    DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
      DEFINE_PROP_END_OF_LIST(),
  };

diff --git a/hw/ppc/ppc440_bamboo.c b/hw/ppc/ppc440_bamboo.c
index 2bd5e41140..9b456f1819 100644

hw/ppc/ppc440_bamboo.c +0 -1
@@ -50,10 +50,6 @@
    #define PPC440EP_SDRAM_NR_BANKS 4
  -static const ram_addr_t ppc440ep_sdram_bank_sizes[] = {
-    256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 

0

hw/ppc/ppc440_bamboo.c +0 -6
-};
-
  static hwaddr entry;
    static int bamboo_load_device_tree(hwaddr addr,
@@ -168,8 +164,6 @@ static void bamboo_init(MachineState *machine)
      unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
      MemoryRegion *address_space_mem = get_system_memory();
      MemoryRegion *isa = g_new(MemoryRegion, 1);
-    Ppc4xxSdramBank *ram_banks = g_new0(Ppc4xxSdramBank,
-                                        PPC440EP_SDRAM_NR_BANKS);
      PCIBus *pcibus;
      PowerPCCPU *cpu;
      CPUPPCState *env;
@@ -204,11 +198,9 @@ static void bamboo_init(MachineState *machine)
                         qdev_get_gpio_in(DEVICE(cpu), PPC40x_INPUT_CINT));
        /* SDRAM controller */
-    ppc4xx_sdram_banks(machine->ram, PPC440EP_SDRAM_NR_BANKS, ram_banks,
-                       ppc440ep_sdram_bank_sizes);
      /* XXX 440EP's ECC interrupts are on UIC1, but we've only created 

UIC0. */

      ppc4xx_sdram_init(env, qdev_get_gpio_in(uicdev, 14),
hw/ppc/ppc440_bamboo.c +1 -1
-                      PPC440EP_SDRAM_NR_BANKS, ram_banks);
+                      PPC440EP_SDRAM_NR_BANKS, machine->ram);
      /* Enable SDRAM memory regions, this should be done by the firmware 

*/

      if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
          ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
hw/ppc/ppc4xx_devs.c +1 -1
index 7bdcbd6fac..eb3aa97b16 100644
@@ -41,7 +41,7 @@
  typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
  struct ppc4xx_sdram_t {
      uint32_t addr;
-    int nbanks;
+    int nbanks; /* Banks to use from the 4, e.g. when board has less slots 
 */
      Ppc4xxSdramBank bank[4];
      uint32_t besr0;
      uint32_t besr1;
hw/ppc/ppc4xx_devs.c +3 -2
@@ -343,19 +343,17 @@ static void sdram_reset(void *opaque)
  }
    void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       Ppc4xxSdramBank *ram_banks)
+                       MemoryRegion *ram)
  {
      ppc4xx_sdram_t *sdram;
-    int i;
+    const ram_addr_t valid_bank_sizes[] = {
+        256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * 

MiB, 0

hw/ppc/ppc4xx_devs.c +2 -5
+    };
        sdram = g_new0(ppc4xx_sdram_t, 1);
      sdram->irq = irq;
      sdram->nbanks = nbanks;
-    for (i = 0; i < nbanks; i++) {
-        sdram->bank[i].ram = ram_banks[i].ram;
-        sdram->bank[i].base = ram_banks[i].base;
-        sdram->bank[i].size = ram_banks[i].size;
-    }
+    ppc4xx_sdram_banks(ram, sdram->nbanks, sdram->bank, valid_bank_sizes);
      qemu_register_reset(&sdram_reset, sdram);
      ppc_dcr_register(env, SDRAM0_CFGADDR,
                       sdram, &dcr_read_sdram, &dcr_write_sdram);
include/hw/ppc/ppc4xx.h +1 -1
index 5013b8bf3a..6007a8dd04 100644
@@ -47,7 +47,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                          const ram_addr_t sdram_bank_sizes[]);
    void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
-                       Ppc4xxSdramBank *ram_banks);
+                       MemoryRegion *ram);
    #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#5 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

In reply to Cédric Le Goater
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 44 lines
Quoted 44 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--
  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
      /* Public */
      MemoryRegion ram_banks[2];
      hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
        MemoryRegion *dram_mr;
      hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -2
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
      MachineClass *mc = MACHINE_GET_CLASS(machine);
      const char *kernel_filename = machine->kernel_filename;
      MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
        if (machine->ram_size != mc->default_ram_size) {
          char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                               machine->ram_size, &error_fatal);
      object_property_set_link(OBJECT(&ppc405->soc), "dram",
                               OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
Quoted 8 lines

hw/ppc/ppc405_boards.c static void ppc405_init(MachineState *machine)

Quoted 6 lines
hw/ppc/ppc405_boards.c +1 -0
      object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                               &error_abort);
      qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);
  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data 
 */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 6 lines

hw/ppc/ppc405_boards.c static void ppc405_init(MachineState *machine) · line 300

Quoted 3 lines
hw/ppc/ppc405_boards.c +3 -0
+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

I am not in favor of these ppc_drc_write calls and this is still a hack.

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree. Doing it like this patch is cleaner IMO.

Regards,
BALATON Zoltan

Quoted 124 lines

C.

Quoted 118 lines
hw/ppc/ppc405_boards.c +4 -0
+        error_report("Could not enable memory regions");
+        exit(1);
+    }
+
      /* allocate and load BIOS */
      if (machine->firmware) {
          MemoryRegion *bios = g_new(MemoryRegion, 1);
hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
                               s->ram_bases[0], s->ram_sizes[0]);
        ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
        /* External bus controller */
      if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
  static Property ppc405_soc_properties[] = {
      DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                       MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
      DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
      DEFINE_PROP_END_OF_LIST(),
  };
hw/ppc/ppc440_bamboo.c +7 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
      ppc4xx_sdram_init(env,
                        qdev_get_gpio_in(uicdev, 14),
                        PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the firmware */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("couldn't enable memory regions");
+        exit(1);
+    }
        /* PCI */
      dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
  } ppc440_sdram_t;
    enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
      SDRAM_R0BAS = 0x40,
      SDRAM_R1BAS,
      SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -9
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
      qemu_irq irq;
  };
  -enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
  /*
   * XXX: TOFIX: some patches have made this code become inconsistent:
   *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
  void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                         MemoryRegion *ram_memories,
                         hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
  {
      ppc4xx_sdram_t *sdram;
      int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, 
 int nbanks,
                       sdram, &dcr_read_sdram, &dcr_write_sdram);
      ppc_dcr_register(env, SDRAM0_CFGDATA,
                       sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
  }
    /*
include/hw/ppc/ppc4xx.h +5 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
      uint32_t bcr;
  } Ppc4xxSdramBank;
  +enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
  void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                          MemoryRegion ram_memories[],
                          hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
  void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                          MemoryRegion ram_memories[],
                          hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
    #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#25 lore raw

Re: [PATCH v3 09/20] ppc440_sdram: Split off map/unmap of sdram banks for later reuse

In reply to Cédric Le Goater
Quoted 20 lines · Cédric Le Goater
Quoted 10 lines · BALATON Zoltan
---
  hw/ppc/ppc440_uc.c | 31 +++++++++++++++++++------------
  1 file changed, 19 insertions(+), 12 deletions(-)
hw/ppc/ppc440_uc.c +0 -0
index 5db59d1190..01184e717b 100644

hw/ppc/ppc440_uc.c static uint64_t sdram_size(uint32_t bcr) · line 566

Quoted 6 lines
hw/ppc/ppc440_uc.c +2 -0
@@ -561,26 +561,33 @@ static uint64_t sdram_size(uint32_t bcr)
      return size;
  }
  +static void sdram_bank_map(Ppc4xxSdramBank *bank)
+{
+    memory_region_init(&bank->container, NULL, "sdram-container", 
 bank->size);

This init belongs to the realize routine.

Quoted 1 line

It is a oneliner. I think you can do it now without risks.

Not in this patch for sure as this is just moving this out from the write callback here. What you suggest is a separate clean up that could be considered as a followup after this series. I'm not sure which way is best as one could argue that we don't need this container region and could just create the ram region alias with the right size and offset but that may need to store more info or understand the register values better. Since I may not understand it completely I chose to not break it more and just leave it as it is for now. You could think about changing it afterwards separately.

Regards,
BALATON Zoltan

Quoted 47 lines

Thanks,

C.

Quoted 40 lines
hw/ppc/ppc440_uc.c +16 -12
+    memory_region_add_subregion(&bank->container, 0, &bank->ram);
+    memory_region_add_subregion(get_system_memory(), bank->base,
+                                &bank->container);
+}
+
+static void sdram_bank_unmap(Ppc4xxSdramBank *bank)
+{
+    memory_region_del_subregion(get_system_memory(), &bank->container);
+    memory_region_del_subregion(&bank->container, &bank->ram);
+    object_unparent(OBJECT(&bank->container));
+}
+
  static void sdram_set_bcr(ppc440_sdram_t *sdram, int i,
                            uint32_t bcr, int enabled)
  {
      if (sdram->bank[i].bcr & 1) {
          /* First unmap RAM if enabled */
-        memory_region_del_subregion(get_system_memory(),
-                                    &sdram->bank[i].container);
-        memory_region_del_subregion(&sdram->bank[i].container,
-                                    &sdram->bank[i].ram);
-        object_unparent(OBJECT(&sdram->bank[i].container));
+        sdram_bank_unmap(&sdram->bank[i]);
      }
      sdram->bank[i].bcr = bcr & 0xffe0ffc1;
+    sdram->bank[i].base = sdram_base(bcr);
+    sdram->bank[i].size = sdram_size(bcr);
      if (enabled && (bcr & 1)) {
-        memory_region_init(&sdram->bank[i].container, NULL, 
 "sdram-container",
-                           sdram_size(bcr));
-        memory_region_add_subregion(&sdram->bank[i].container, 0,
-                                    &sdram->bank[i].ram);
-        memory_region_add_subregion(get_system_memory(),
-                                    sdram_base(bcr),
-                                    &sdram->bank[i].container);
+        sdram_bank_map(&sdram->bank[i]);
      }
  }
#40 lore raw
In reply to Cédric Le Goater
Quoted 11 lines · Cédric Le Goater
Quoted 6 lines · BALATON Zoltan

This is the end of the QOMify series started by Cédric. This series handles the SDRAM controller models to clean them up, QOMify and unify them and at least partially clean up the mess that has accumulated around these in the past. This includes the not yet merged patches from the last series and new ones that change the DDR2 version used by sam460ex.

I made comments on the first ~10 patches. Let's try to agree on these first. We will see the remaining ones in a second patchset.

Patch 10 does not make much sense without 11 and the final unificatoin of the two controllers is the real goal here so please try to review further patches too.

Regards,
BALATON Zoltan

Quoted 51 lines

Thanks,

C.

Quoted 42 lines

v3: Fix patches that got squashed during rebase
v2: address some review comments and try to avoid compile problem with gcc 12.2 (untested)

BALATON Zoltan (20):
ppc440_bamboo: Remove unnecessary memsets
ppc4xx: Introduce Ppc4xxSdramBank struct
ppc4xx_sdram: Get rid of the init RAM hack
ppc4xx: Use Ppc4xxSdramBank in ppc4xx_sdram_banks()
ppc440_bamboo: Add missing 4 MiB valid memory size
ppc4xx_sdram: Move size check to ppc4xx_sdram_init()
ppc4xx_sdram: QOM'ify
ppc4xx_sdram: Drop extra zeros for readability
ppc440_sdram: Split off map/unmap of sdram banks for later reuse
ppc440_sdram: Implement enable bit in the DDR2 SDRAM
ppc440_sdram: Get rid of the init RAM hack
ppc440_sdram: Rename local variable for readibility
ppc4xx_sdram: Rename functions to prevent name clashes
ppc440_sdram: Move RAM size check to ppc440_sdram_init
ppc440_sdram: QOM'ify
ppc4xx_sdram: Move ppc4xx DDR and DDR2 SDRAM controller models
together
ppc4xx_sdram: Use hwaddr for memory bank size
ppc4xx_sdram: Rename local state variable for brevity
ppc4xx_sdram: Generalise bank setup
ppc4xx_sdram: Convert DDR SDRAM controller to new bank handling

  hw/ppc/meson.build      |   3 +-
  hw/ppc/ppc405.h         |   8 +-
  hw/ppc/ppc405_boards.c  |  22 +-
  hw/ppc/ppc405_uc.c      |  33 +-
  hw/ppc/ppc440.h         |   4 -
  hw/ppc/ppc440_bamboo.c  |  29 +-
  hw/ppc/ppc440_uc.c      | 267 +--------------
  hw/ppc/ppc4xx_devs.c    | 413 -----------------------
  hw/ppc/ppc4xx_sdram.c   | 723 ++++++++++++++++++++++++++++++++++++++++
  hw/ppc/sam460ex.c       |  48 +--
  hw/ppc/trace-events     |   1 +
  include/hw/ppc/ppc4xx.h |  66 +++-
  12 files changed, 847 insertions(+), 770 deletions(-)

create mode 100644 hw/ppc/ppc4xx_sdram.c

#6 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

In reply to BALATON Zoltan
Quoted 65 lines · BALATON Zoltan
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 43 lines
Quoted 43 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -1
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -1
-                             kernel_filename != NULL, &error_abort);
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

I am not in favor of these ppc_drc_write calls and this is still a hack.

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

Quoted 6 lines
Quoted 4 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Thanks,

C.

Quoted 126 lines

Doing it like this patch is cleaner IMO.

Regards,
BALATON Zoltan

Quoted 121 lines

C.

Quoted 115 lines

+        error_report("Could not enable memory regions"); +        exit(1);
+    }
+
      /* allocate and load BIOS */
      if (machine->firmware) {
          MemoryRegion *bios = g_new(MemoryRegion, 1);

hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                                s->ram_bases[0], s->ram_sizes[0]);
         ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
         /* External bus controller */
       if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
   static Property ppc405_soc_properties[] = {
       DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                        MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
       DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
       DEFINE_PROP_END_OF_LIST(),
   };
hw/ppc/ppc440_bamboo.c +7 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
       ppc4xx_sdram_init(env,
                         qdev_get_gpio_in(uicdev, 14),
                         PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the firmware */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("couldn't enable memory regions");
+        exit(1);
+    }
         /* PCI */
       dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
   } ppc440_sdram_t;
     enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
       SDRAM_R0BAS = 0x40,
       SDRAM_R1BAS,
       SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -9
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
       qemu_irq irq;
   };
   -enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
   /*
    * XXX: TOFIX: some patches have made this code become inconsistent:
    *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
   void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                          MemoryRegion *ram_memories,
                          hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
   {
       ppc4xx_sdram_t *sdram;
       int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
       ppc_dcr_register(env, SDRAM0_CFGDATA,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
   }
     /*
include/hw/ppc/ppc4xx.h +5 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
       uint32_t bcr;
   } Ppc4xxSdramBank;
   +enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
   void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                           MemoryRegion ram_memories[],
                           hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
   void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                           MemoryRegion ram_memories[],
                           hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
     #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#7 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

Quoted 74 lines · Cédric Le Goater
Quoted 73 lines · BALATON Zoltan
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 44 lines
Quoted 44 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -2
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -0
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 6 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

I am not in favor of these ppc_drc_write calls and this is still a hack.

Quoted 7 lines
Quoted 3 lines

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the firmware
would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

I don't get this. We're not calling any CPU instructions, ppc_dcr_write just calls the write callback the device has registered for the dcr so it just does the same as the hack did at the end just doing it the same way the firmware should do.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's

In all machines booting with -kernel likely deviates and all machines probably have additinal code in this case to do some things normally done by the firmware. Look at pegasos2_machine_reset() for example. All that is not needed when we boot with firmware as then the firmware will do all that and provide the device tree, etc. bur we need to do these when booting without firmware. In thes case QEMU also emulates the firmware and has to do thinigs like enabling the memory controller.

Quoted 4 lines

the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

In my opinion the SDRAM controller model should model the hardware and if the board uses it differently then it should take care of that and not change the model.

Quoted 8 lines

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Why? You already have the ppc405_set_bootinfo and all it's stuff in the ppc405 board which is also only needed without firmware. If you're opposed to the few lines enabling the memory controller being in ppc405_init I could put it in a function either in ppc405_boards.c or if you think this should be in ppc4xx_sdrem.c then we can export that function via include/hw/ppc/ppc4xx.h and call that from boards but I don't want to add hacks and a property for this in the device model becuase I'm not convinced it belongs there. If the hardware would have such an option then modeling that in is valid but if it's done by the firmare on the real hardware then either use the firmware or do it in board code which is then emulating the firmware too.

Regards,
BALATON Zoltan

Quoted 143 lines

Thanks,

C.

Quoted 131 lines

Doing it like this patch is cleaner IMO.

Regards,
BALATON Zoltan

Quoted 126 lines

C.

Quoted 120 lines

+        error_report("Could not enable memory regions"); +        exit(1);
+    }
+
      /* allocate and load BIOS */
      if (machine->firmware) {
          MemoryRegion *bios = g_new(MemoryRegion, 1);

hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
                                s->ram_bases[0], s->ram_sizes[0]);
         ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
         /* External bus controller */
       if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
   static Property ppc405_soc_properties[] = {
       DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                        MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
       DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
       DEFINE_PROP_END_OF_LIST(),

  };

hw/ppc/ppc440_bamboo.c +7 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
       ppc4xx_sdram_init(env,
                         qdev_get_gpio_in(uicdev, 14),
                         PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the firmware 
 */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("couldn't enable memory regions");
+        exit(1);
+    }
         /* PCI */
       dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
   } ppc440_sdram_t;
     enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
       SDRAM_R0BAS = 0x40,
       SDRAM_R1BAS,
       SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -9
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
       qemu_irq irq;
   };
   -enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
   /*
    * XXX: TOFIX: some patches have made this code become inconsistent:
    *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
   void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                          MemoryRegion *ram_memories,
                          hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
   {
       ppc4xx_sdram_t *sdram;
       int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq 
 irq, int nbanks,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
       ppc_dcr_register(env, SDRAM0_CFGDATA,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
   }
     /*
include/hw/ppc/ppc4xx.h +5 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
       uint32_t bcr;
   } Ppc4xxSdramBank;
   +enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
   void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                           MemoryRegion ram_memories[],
                           hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int 
 nr_banks,
   void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                           MemoryRegion ram_memories[],
                           hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
     #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#8 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

Quoted 94 lines · BALATON Zoltan
Quoted 74 lines · Cédric Le Goater
Quoted 73 lines · BALATON Zoltan
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 44 lines
Quoted 44 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -2
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -0
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 6 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

I am not in favor of these ppc_drc_write calls and this is still a hack.

Quoted 7 lines
Quoted 3 lines

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the
firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

I don't get this. We're not calling any CPU instructions, ppc_dcr_write just calls the write callback the device has registered for the dcr so it just does the same as the hack did at the end just doing it the same way the firmware should do.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's

Quoted 17 lines

In all machines booting with -kernel likely deviates and all machines probably have additinal code in this case to do some things normally done by the firmware. Look at pegasos2_machine_reset() for example. All that is not needed when we boot with firmware as then the firmware will do all that and provide the device tree, etc. bur we need to do these when booting without firmware. In thes case QEMU also emulates the firmware and has to do thinigs like enabling the memory controller.

Quoted 4 lines

the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

In my opinion the SDRAM controller model should model the hardware and if the board uses it differently then it should take care of that and not change the model.

Quoted 8 lines

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Additionally, if you don't like the FIXME comment, it's there because this would really belong at the beginning of boot_from_kernel() function before that calls ppc405_set_bootinfo which is called when booting without firmware but I left it where it was in init for now because you menfioned that firmware boot was also broken when I had it at the end of boot_from_kernel so I suspect the board is not providing the SPD data so the firmware cannot detect the RAM and this why it's not enabling the SDRAM itself (or maybe that part is even compiled out because of that) but then it's a limitation of the board emulation and not the SDRAM controller so it should be handled in the board code.

Regards,
BALATON Zoltan

Quoted 148 lines

Why? You already have the ppc405_set_bootinfo and all it's stuff in the ppc405 board which is also only needed without firmware. If you're opposed to the few lines enabling the memory controller being in ppc405_init I could put it in a function either in ppc405_boards.c or if you think this should be in ppc4xx_sdrem.c then we can export that function via include/hw/ppc/ppc4xx.h and call that from boards but I don't want to add hacks and a property for this in the device model becuase I'm not convinced it belongs there. If the hardware would have such an option then modeling that in is valid but if it's done by the firmare on the real hardware then either use the firmware or do it in board code which is then emulating the firmware too.

Regards,
BALATON Zoltan

Quoted 142 lines

Thanks,

C.

Quoted 131 lines

Doing it like this patch is cleaner IMO.

Regards,
BALATON Zoltan

Quoted 126 lines

C.

Quoted 120 lines

+        error_report("Could not enable memory regions"); +        exit(1);
+    }
+
      /* allocate and load BIOS */
      if (machine->firmware) {
          MemoryRegion *bios = g_new(MemoryRegion, 1);

hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
                                s->ram_bases[0], s->ram_sizes[0]);
         ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
         /* External bus controller */
       if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
   static Property ppc405_soc_properties[] = {
       DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                        MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
       DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
       DEFINE_PROP_END_OF_LIST(),

  };

hw/ppc/ppc440_bamboo.c +7 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
       ppc4xx_sdram_init(env,
                         qdev_get_gpio_in(uicdev, 14),
                         PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the firmware 
 */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("couldn't enable memory regions");
+        exit(1);
+    }
         /* PCI */
       dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
   } ppc440_sdram_t;
     enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
       SDRAM_R0BAS = 0x40,
       SDRAM_R1BAS,
       SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -9
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
       qemu_irq irq;
   };
   -enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
   /*
    * XXX: TOFIX: some patches have made this code become inconsistent:
    *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
   void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                          MemoryRegion *ram_memories,
                          hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
   {
       ppc4xx_sdram_t *sdram;
       int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq 
 irq, int nbanks,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
       ppc_dcr_register(env, SDRAM0_CFGDATA,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
   }
     /*
include/hw/ppc/ppc4xx.h +5 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
       uint32_t bcr;
   } Ppc4xxSdramBank;
   +enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
   void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                           MemoryRegion ram_memories[],
                           hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int 
 nr_banks,
   void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                           MemoryRegion ram_memories[],
                           hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
     #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#9 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

Quoted 107 lines · BALATON Zoltan
Quoted 106 lines · BALATON Zoltan
Quoted 71 lines · Cédric Le Goater
Quoted 70 lines · BALATON Zoltan
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 43 lines
Quoted 43 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -1
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -1
-                             kernel_filename != NULL, &error_abort);
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 5 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I am not in favor of these ppc_drc_write calls and this is still a hack.

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

I don't get this. We're not calling any CPU instructions, ppc_dcr_write just calls the write callback the device has registered for the dcr so it just does the same as the hack did at the end just doing it the same way the firmware should do.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's

In all machines booting with -kernel likely deviates and all machines probably have additinal code in this case to do some things normally done by the firmware. Look at pegasos2_machine_reset() for example. All that is not needed when we boot with firmware as then the firmware will do all that and provide the device tree, etc. bur we need to do these when booting without firmware. In thes case QEMU also emulates the firmware and has to do thinigs like enabling the memory controller.

Quoted 4 lines

the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

In my opinion the SDRAM controller model should model the hardware and if the board uses it differently then it should take care of that and not change the model.

Quoted 6 lines

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Additionally, if you don't like the FIXME comment,

I didn't understand it. That's different.

Quoted 1 line

it's there because this would really belong at the beginning of boot_from_kernel() function before that calls ppc405_set_bootinfo which is called when booting without firmware but I left it where it was in init for now because you menfioned that firmware boot was also broken

hmm ? but It's not anymore. v2 broke it I think.

Quoted 1 line

when I had it at the end of boot_from_kernel so I suspect the board is not providing the SPD data so the firmware cannot detect the RAM and this why it's not enabling the SDRAM itself (or maybe that part is even compiled out because of that) but then it's a limitation of the board emulation and not the SDRAM controller so it should be handled in the board code.

Here is the U-boot tree that can be used :

https://gitlab.com/huth/u-boot/-/tree/taihu

and the SDRAM init hacks :

https://gitlab.com/huth/u-boot/-/commit/296e0479a972fa57390f0f3a912650168dabe851

May be there is a way to fix the model to remove the U-boot hack.

Thomas added a uboot.bin in the tree which is used by :

tests/avocado/ppc_405.py

Thanks,

C.

#12 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

In reply to BALATON Zoltan
Quoted 106 lines · BALATON Zoltan
Quoted 71 lines · Cédric Le Goater
Quoted 70 lines · BALATON Zoltan
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 43 lines
Quoted 43 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -1
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -1
-                             kernel_filename != NULL, &error_abort);
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 5 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I am not in favor of these ppc_drc_write calls and this is still a hack.

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

I don't get this. We're not calling any CPU instructions, ppc_dcr_write just calls the write callback the device has registered for the dcr so it just does the same as the hack did at the end just doing it the same way the firmware should do.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's

In all machines booting with -kernel likely deviates and all machines probably have additinal code in this case to do some things normally done by the firmware. Look at pegasos2_machine_reset() for example. All that is not needed when we boot with firmware as then the firmware will do all that and provide the device tree, etc. bur we need to do these when booting without firmware. In thes case QEMU also emulates the firmware and has to do thinigs like enabling the memory controller.

Quoted 4 lines

the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

In my opinion the SDRAM controller model should model the hardware and if the board uses it differently then it should take care of that and not change the model.

Quoted 6 lines

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Quoted 4 lines
Quoted 2 lines

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Why? You already have the ppc405_set_bootinfo and all it's stuff in the ppc405 board which is also only needed without firmware.

True. ppc405_set_bootinfo() is mimicking u-boot and updating RAM to pretend that FW already ran. It is done from under boot_from_kernel() only.

FYI, The U-boot support was quite a mess :

https://lists.ozlabs.org/pipermail/linuxppc-dev/2009-July/074487.html

Quoted 1 line

If you're opposed to the few lines enabling the memory controller being in ppc405_init

Well, that's what the init property is doing already. I do understand you need to move the code to cleanup the SDRAM model, but there are cleaner ways to do so.

Alternative would be to remove the SDRAM pre-init for now, change the model and re-add it at the end when all is cleanly modified. The -kernel support would be temporarily broken but that's fine.

On that topic, you should probably consider changing the patchset to propose first, a new SDRAM model (without using it in the boards) and then do the model "switch" at the end. That might be easier to review.

Quoted 1 line

I could put it in a function either in ppc405_boards.c or if you think this should be in ppc4xx_sdrem.c then we can export that function via include/hw/ppc/ppc4xx.h and call that from boards

Yes. That would be better.

Please call from under boot_from_kernel(), something like

  sdram_map_bcr(&ppc405->soc.sdram);
Quoted 1 line

but I don't want to add hacks and a property for this in the device model becuase I'm not convinced it belongs there.

That's how the default state of a model is changed and possibly taken into account at reset. A bit like HW strapping would work.

Typically, in that case, you could use properties to change the BCR reg values and do the mapping accordingly at reset.

Quoted 1 line

If the hardware would have such an option then modeling that in is valid

QEMU has all kinds of extensions which makes it much more useful than HW in some places. The frontier is not that well draw.

Thanks,

C.

Quoted 143 lines

but if it's done by the firmare on the real hardware then either use the firmware or do it in board code which is then emulating the firmware too.

Regards,
BALATON Zoltan

Quoted 138 lines

Thanks,

C.

Quoted 126 lines

Doing it like this patch is cleaner IMO.

Regards,
BALATON Zoltan

Quoted 121 lines

C.

Quoted 115 lines

+        error_report("Could not enable memory regions"); +        exit(1);
+    }
+
      /* allocate and load BIOS */
      if (machine->firmware) {
          MemoryRegion *bios = g_new(MemoryRegion, 1);

hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
                                s->ram_bases[0], s->ram_sizes[0]);
         ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
         /* External bus controller */
       if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, Error **errp)
   static Property ppc405_soc_properties[] = {
       DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                        MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
       DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
       DEFINE_PROP_END_OF_LIST(),
   };
hw/ppc/ppc440_bamboo.c +7 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
       ppc4xx_sdram_init(env,
                         qdev_get_gpio_in(uicdev, 14),
                         PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the firmware */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) ||
+        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {
+        error_report("couldn't enable memory regions");
+        exit(1);
+    }
         /* PCI */
       dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
   } ppc440_sdram_t;
     enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
       SDRAM_R0BAS = 0x40,
       SDRAM_R1BAS,
       SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -9
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
       qemu_irq irq;
   };
   -enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
   /*
    * XXX: TOFIX: some patches have made this code become inconsistent:
    *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
   void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                          MemoryRegion *ram_memories,
                          hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
   {
       ppc4xx_sdram_t *sdram;
       int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
       ppc_dcr_register(env, SDRAM0_CFGDATA,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
   }
     /*
include/hw/ppc/ppc4xx.h +5 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
       uint32_t bcr;
   } Ppc4xxSdramBank;
   +enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
   void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                           MemoryRegion ram_memories[],
                           hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
   void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                           MemoryRegion ram_memories[],
                           hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
     #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"
 
#10 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

In reply to Cédric Le Goater
Quoted 114 lines · Cédric Le Goater
Quoted 113 lines · BALATON Zoltan
Quoted 111 lines · BALATON Zoltan
Quoted 74 lines · Cédric Le Goater
Quoted 73 lines · BALATON Zoltan
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 44 lines
Quoted 44 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -2
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -0
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 6 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

I am not in favor of these ppc_drc_write calls and this is still a hack.

Quoted 7 lines
Quoted 3 lines

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the
firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

I don't get this. We're not calling any CPU instructions, ppc_dcr_write just calls the write callback the device has registered for the dcr so it just does the same as the hack did at the end just doing it the same way the firmware should do.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's

In all machines booting with -kernel likely deviates and all machines probably have additinal code in this case to do some things normally done by the firmware. Look at pegasos2_machine_reset() for example. All that is not needed when we boot with firmware as then the firmware will do all that and provide the device tree, etc. bur we need to do these when booting without firmware. In thes case QEMU also emulates the firmware and has to do thinigs like enabling the memory controller.

Quoted 4 lines

the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

In my opinion the SDRAM controller model should model the hardware and if the board uses it differently then it should take care of that and not change the model.

Quoted 8 lines

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Quoted 7 lines
Quoted 1 line

Additionally, if you don't like the FIXME comment,

I didn't understand it. That's different.

Quoted 1 line

it's there because this would really belong at the beginning of boot_from_kernel() function before that calls ppc405_set_bootinfo which is called when booting without firmware but I left it where it was in init for now because you menfioned that firmware boot was also broken

hmm ? but It's not anymore. v2 broke it I think.

First I've put enabling SDRAM controller in boot_from_kernel at the end before qemu_register_reset but this is wrong as the bootinfo is written in RAM so it should be enabled before. So this should be done at the beginning of boot_from_kernel but you said my first version not only broke -kernel but also booting with firmware so that told me firmware also does not enable the SDRAM conroller itself (as now seen below it's disabled) so I moved enabling back to where it was in init. Ideally it should be done by the firmware if using that or QEMU at the beginning of boot_from_kernel when emulating firmwere but that does not work yet so that's why we have the FIXME comment to remind for this.

Quoted 10 lines
Quoted 1 line

when I had it at the end of boot_from_kernel so I suspect the board is not providing the SPD data so the firmware cannot detect the RAM and this why it's not enabling the SDRAM itself (or maybe that part is even compiled out because of that) but then it's a limitation of the board emulation and not the SDRAM controller so it should be handled in the board code.

Here is the U-boot tree that can be used :

https://gitlab.com/huth/u-boot/-/tree/taihu

and the SDRAM init hacks :
https://gitlab.com/huth/u-boot/-/commit/296e0479a972fa57390f0f3a912650168dabe851

May be there is a way to fix the model to remove the U-boot hack.

I don't know how that works for 405. For 440 used by Sam460ex u-boot reads the SPD EEPROMs to detect memmory size and configures the SDRAM DDR2 controller according to that but that needs i2c and SPD data as in sam460ex.c. The code you pointed to seems to try a fixed table of valid RAM sizes in mb0cf[] that is either taken from CONFIG_SYS_SDRAM_TABLE value or use default of 128, 64, 32, 16, 4 MB. Maybe the model can be changed to allow this check to succeed so it does not have to be disabled but I don't know what it really tries to do here. The SoC manual may also have some docs on the register which you could check if you have it. This controller should be the same as in some 440 with DDR SDRAM. I think there are three different memory controllers in these SoCs: SDRAM, DDR and DDR2. The 460EX has DDR2, the 440 in bamboo has DDR and I think this is backwards compatible with SDRAM in 405 but the 440 DDR controller has some more regs for ECC. Therefore in QEMU we mostly only model DDR2 and DDR and use DDR instead of SDRAM in 405 as its common regs are likely the same but I don't have docs to prove it, I've only seen docs on 440 DDR so it's just what I've found while doing this series. Anyway you should keep this in mind when changing the DDR model and also check bamboo firmware which I don't have.

Regards,
BALATON Zoltan

Quoted 10 lines

Thomas added a uboot.bin in the tree which is used by :

tests/avocado/ppc_405.py

Thanks,

C.

#11 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

Quoted 116 lines · BALATON Zoltan
Quoted 115 lines · Cédric Le Goater
Quoted 113 lines · BALATON Zoltan
Quoted 93 lines · BALATON Zoltan
Quoted 73 lines · Cédric Le Goater
Quoted 72 lines · BALATON Zoltan
Quoted 8 lines · Cédric Le Goater
Quoted 5 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel
without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 44 lines
Quoted 44 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -2
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -0
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 5 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I am not in favor of these ppc_drc_write calls and this is still a hack.

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

I don't get this. We're not calling any CPU instructions, ppc_dcr_write just calls the write callback the device has registered for the dcr so it just does the same as the hack did at the end just doing it the same way the firmware should do.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's

Quoted 17 lines

In all machines booting with -kernel likely deviates and all machines probably have additinal code in this case to do some things normally done by the firmware. Look at pegasos2_machine_reset() for example. All that is not needed when we boot with firmware as then the firmware will do all that and provide the device tree, etc. bur we need to do these when booting without firmware. In thes case QEMU also emulates the firmware and has to do thinigs like enabling the memory controller.

Quoted 4 lines

the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

In my opinion the SDRAM controller model should model the hardware and if the board uses it differently then it should take care of that and not change the model.

Quoted 8 lines

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Additionally, if you don't like the FIXME comment,

Quoted 7 lines
Quoted 5 lines

I didn't understand it. That's different.

Quoted 1 line

it's there because this would really belong at the beginning of boot_from_kernel() function before that calls ppc405_set_bootinfo which is called when booting without firmware but I left it where it was in init for now because you menfioned that firmware boot was also broken

hmm ? but It's not anymore. v2 broke it I think.

First I've put enabling SDRAM controller in boot_from_kernel at the end before qemu_register_reset but this is wrong as the bootinfo is written in RAM so it should be enabled before. So this should be done at the beginning of boot_from_kernel but you said my first version not only broke -kernel but also booting with firmware so that told me firmware also does not enable the SDRAM conroller itself (as now seen below it's disabled) so I moved enabling

On second look, enabling the memory controller in line 195 is not #ifdef-ed out so maybe it would work with enable only at the beginning of boot_from_kernel, I'll try to test with the firmware. For removing the hack the register values would need to be checked but that's something I don't want to do. You could look at that later as a follow up.

Regards,
BALATON Zoltan

Quoted 28 lines

back to where it was in init. Ideally it should be done by the firmware if using that or QEMU at the beginning of boot_from_kernel when emulating firmwere but that does not work yet so that's why we have the FIXME comment to remind for this.

Quoted 10 lines
Quoted 1 line

when I had it at the end of boot_from_kernel so I suspect the board is not providing the SPD data so the firmware cannot detect the RAM and this why it's not enabling the SDRAM itself (or maybe that part is even compiled out because of that) but then it's a limitation of the board emulation and not the SDRAM controller so it should be handled in the board code.

Here is the U-boot tree that can be used :

https://gitlab.com/huth/u-boot/-/tree/taihu

and the SDRAM init hacks :
https://gitlab.com/huth/u-boot/-/commit/296e0479a972fa57390f0f3a912650168dabe851

May be there is a way to fix the model to remove the U-boot hack.

I don't know how that works for 405. For 440 used by Sam460ex u-boot reads the SPD EEPROMs to detect memmory size and configures the SDRAM DDR2 controller according to that but that needs i2c and SPD data as in sam460ex.c. The code you pointed to seems to try a fixed table of valid RAM sizes in mb0cf[] that is either taken from CONFIG_SYS_SDRAM_TABLE value or use default of 128, 64, 32, 16, 4 MB. Maybe the model can be changed to allow this check to succeed so it does not have to be disabled but I don't know what it really tries to do here. The SoC manual may also have some docs on the register which you could check if you have it. This controller should be the same as in some 440 with DDR SDRAM. I think there are three different memory controllers in these SoCs: SDRAM, DDR and DDR2. The 460EX has DDR2, the 440 in bamboo has DDR and I think this is backwards compatible with SDRAM in 405 but the 440 DDR controller has some more regs for ECC. Therefore in QEMU we mostly only model DDR2 and DDR and use DDR instead of SDRAM in 405 as its common regs are likely the same but I don't have docs to prove it, I've only seen docs on 440 DDR so it's just what I've found while doing this series. Anyway you should keep this in mind when changing the DDR model and also check bamboo firmware which I don't have.

Regards,
BALATON Zoltan

Quoted 9 lines

Thomas added a uboot.bin in the tree which is used by :

tests/avocado/ppc_405.py

Thanks,

C.

#13 lore raw

Re: [PATCH v3 03/20] ppc4xx_sdram: Get rid of the init RAM hack

In reply to Cédric Le Goater
Quoted 124 lines · Cédric Le Goater
Quoted 109 lines · BALATON Zoltan
Quoted 74 lines · Cédric Le Goater
Quoted 73 lines · BALATON Zoltan
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines · BALATON Zoltan

The do_init parameter of ppc4xx_sdram_init() is used to map memory regions that is normally done by the firmware by programming the SDRAM controller. This is needed when booting a kernel directly from -kernel without a firmware. Do this from board code accesing normal SDRAM

accessing

Fixed, also two ofhers in another patch you haven't noticed.

Quoted 44 lines
Quoted 44 lines

controller registers the same way as firmware would do, so we can get rid of this hack.

---
v2: Fix ref405ep boot with -kernel and U-Boot

  hw/ppc/ppc405.h         |  1 -
  hw/ppc/ppc405_boards.c  | 12 ++++++++++--
  hw/ppc/ppc405_uc.c      |  4 +---
  hw/ppc/ppc440_bamboo.c  |  8 +++++++-
  hw/ppc/ppc440_uc.c      |  2 --
  hw/ppc/ppc4xx_devs.c    | 11 +----------
  include/hw/ppc/ppc4xx.h |  8 ++++++--

  7 files changed, 25 insertions(+), 21 deletions(-)
hw/ppc/ppc405.h +0 -1
index 1e558c7831..756865621b 100644
@@ -169,7 +169,6 @@ struct Ppc405SoCState {
       /* Public */
       MemoryRegion ram_banks[2];
       hwaddr ram_bases[2], ram_sizes[2];
-    bool do_dram_init;
         MemoryRegion *dram_mr;
       hwaddr ram_size;
hw/ppc/ppc405_boards.c +1 -2
index 083f12b23e..bf02a71c6d 100644
@@ -274,6 +274,7 @@ static void ppc405_init(MachineState *machine)
       MachineClass *mc = MACHINE_GET_CLASS(machine);
       const char *kernel_filename = machine->kernel_filename;
       MemoryRegion *sysmem = get_system_memory();
+    CPUPPCState *env;
         if (machine->ram_size != mc->default_ram_size) {
           char *sz = size_to_str(mc->default_ram_size);
@@ -288,12 +289,19 @@ static void ppc405_init(MachineState *machine)
                                machine->ram_size, &error_fatal);
       object_property_set_link(OBJECT(&ppc405->soc), "dram",
                                OBJECT(machine->ram), &error_abort);
-    object_property_set_bool(OBJECT(&ppc405->soc), "dram-init",
-                             kernel_filename != NULL, &error_abort);
Quoted 8 lines
Quoted 6 lines
hw/ppc/ppc405_boards.c +0 -0
       object_property_set_uint(OBJECT(&ppc405->soc), "sys-clk", 33333333,
                                &error_abort);
       qdev_realize(DEVICE(&ppc405->soc), NULL, &error_fatal);

  +    /* Enable SDRAM memory regions */
+    /* FIXME This shouldn't be needed with firmware but we lack SPD data */

what do you mean ?

U-Boot detects the available RAM by reading the SPD info of the RAM modules but that probably also needs i2c emulation. See sam460ex.

Quoted 6 lines
Quoted 3 lines

+    env = &ppc405->soc.cpu.env;
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) {

I am not in favor of these ppc_drc_write calls and this is still a hack.

Quoted 7 lines
Quoted 3 lines

It's not. Normally this is done by firmware to enable memory controller but the board code has to do it if not using firmware (e.g. booting with -kernel) the same way it provides bootinfo or device tree mods the
firmware would normally do or in this case maybe the emulation is incomplete so the part of firmware that configures the SDRAM controller does not run.

Exactly, and what the above proposal does is mimicking execution of CPU instructions before the CPU is even fully initiated. Reset has not been called at that stage.

I don't get this. We're not calling any CPU instructions, ppc_dcr_write just calls the write callback the device has registered for the dcr so it just does the same as the hack did at the end just doing it the same way the firmware should do.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The "dram-init" property is a cleaner solution. It takes care of doing the pre-mapping of RAM banks in the realize routine of the sdram model (when available).

I disagree, the hardware does not have such feature, it proviesd DCRs as the way to configure it. Adding a special property for it deviates from hardware and clutters qtree.

In this machine, running QEMU with -kernel deviates from HW. That's

In all machines booting with -kernel likely deviates and all machines probably have additinal code in this case to do some things normally done by the firmware. Look at pegasos2_machine_reset() for example. All that is not needed when we boot with firmware as then the firmware will do all that and provide the device tree, etc. bur we need to do these when booting without firmware. In thes case QEMU also emulates the firmware and has to do thinigs like enabling the memory controller.

Quoted 4 lines

the whole purpose of this option. It assumes that the SDRAM device is pre-initialized (and much more should be done) by the QEMU machine and the simplest way to acheive this goal is to inform the SDRAM model to take care of the pre-initialization.

In my opinion the SDRAM controller model should model the hardware and if the board uses it differently then it should take care of that and not change the model.

Quoted 6 lines

Another way would be to change the default reset values of the SDRAM registers (in the realize method using some property) and perform some actions (mapping the banks) in the reset handler of the SDRAM device model. That would be a deferred initialization but a property is still needed to change the default behavior of the SDRAM model.

Quoted 4 lines
Quoted 2 lines

Anyhow, this should be isolated under the SDRAM device model and not in the machine init by using the CPU state. That's clearly ugly.

Why? You already have the ppc405_set_bootinfo and all it's stuff in the ppc405 board which is also only needed without firmware.

True. ppc405_set_bootinfo() is mimicking u-boot and updating RAM to pretend that FW already ran. It is done from under boot_from_kernel() only.

FYI, The U-boot support was quite a mess :

https://lists.ozlabs.org/pipermail/linuxppc-dev/2009-July/074487.html

Quoted 1 line

If you're opposed to the few lines enabling the memory controller being in ppc405_init

Quoted 7 lines

Well, that's what the init property is doing already. I do understand you need to move the code to cleanup the SDRAM model, but there are cleaner ways to do so.

Alternative would be to remove the SDRAM pre-init for now, change the model and re-add it at the end when all is cleanly modified. The -kernel support would be temporarily broken but that's fine.

Doing the init in any other way than through dcr write is bad becuause then the register values and mapped regions can get out of sync so I don't like to add any other hack to go around this. The code to enable and disable the contrller is there at one place and everything should go through that.

Quoted 3 lines

On that topic, you should probably consider changing the patchset to propose first, a new SDRAM model (without using it in the boards) and then do the model "switch" at the end. That might be easier to review.

Besides that ir would require me to redo everthing from scratch it would also be hard to bisect as you ended up with one patch changing everything and no way to test the individual steps so I don't think this is a good idea.

Quoted 3 lines
Quoted 1 line

I could put it in a function either in ppc405_boards.c or if you think this should be in ppc4xx_sdrem.c then we can export that function via include/hw/ppc/ppc4xx.h and call that from boards

Yes. That would be better.

I went with this then in v5...

Quoted 3 lines

Please call from under boot_from_kernel(), something like

sdram_map_bcr(&ppc405->soc.sdram);

...but not quite like this becuase of the above and also because this function will be gone by the end of the series.

Quoted 7 lines
Quoted 1 line

but I don't want to add hacks and a property for this in the device model becuase I'm not convinced it belongs there.

That's how the default state of a model is changed and possibly taken into account at reset. A bit like HW strapping would work.

Typically, in that case, you could use properties to change the BCR reg values and do the mapping accordingly at reset.

I'm not a fan of qdev properties that turn a sysbus_create_simple() into 5 lines of unreadable code and also mess up -device help and info qtree output. (At least these devices are not used creatable so -device help is not a problem here.)

Quoted 4 lines
Quoted 1 line

If the hardware would have such an option then modeling that in is valid

QEMU has all kinds of extensions which makes it much more useful than HW in some places. The frontier is not that well draw.

Sure but in this case I think it would not make it more useful and we could just model the device better without such an addition.

Regards,
BALATON Zoltan

Quoted 156 lines

Thanks,

C.

Quoted 148 lines

but if it's done by the firmare on the real hardware then either use the firmware or do it in board code which is then emulating the firmware too.

Regards,
BALATON Zoltan

Quoted 143 lines

Thanks,

C.

Quoted 131 lines

Doing it like this patch is cleaner IMO.

Regards,
BALATON Zoltan

Quoted 126 lines

C.

Quoted 120 lines

+        error_report("Could not enable memory regions"); +        exit(1);
+    }
+
      /* allocate and load BIOS */
      if (machine->firmware) {
          MemoryRegion *bios = g_new(MemoryRegion, 1);

hw/ppc/ppc405_uc.c +1 -3
index 2ca42fdef6..1e02347e57 100644
@@ -1081,8 +1081,7 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
                                s->ram_bases[0], s->ram_sizes[0]);
         ppc4xx_sdram_init(env, qdev_get_gpio_in(DEVICE(&s->uic), 17), 1,
-                      s->ram_banks, s->ram_bases, s->ram_sizes,
-                      s->do_dram_init);
+                      s->ram_banks, s->ram_bases, s->ram_sizes);
         /* External bus controller */
       if (!ppc4xx_dcr_realize(PPC4xx_DCR_DEVICE(&s->ebc), &s->cpu, errp)) {
@@ -1160,7 +1159,6 @@ static void ppc405_soc_realize(DeviceState *dev, 
 Error **errp)
   static Property ppc405_soc_properties[] = {
       DEFINE_PROP_LINK("dram", Ppc405SoCState, dram_mr, TYPE_MEMORY_REGION,
                        MemoryRegion *),
-    DEFINE_PROP_BOOL("dram-init", Ppc405SoCState, do_dram_init, 0),
       DEFINE_PROP_UINT64("ram-size", Ppc405SoCState, ram_size, 0),
       DEFINE_PROP_END_OF_LIST(),

  };

hw/ppc/ppc440_bamboo.c +2 -1
index 5ec82fa8c2..e3412c4fcd 100644
@@ -211,7 +211,13 @@ static void bamboo_init(MachineState *machine)
       ppc4xx_sdram_init(env,
                         qdev_get_gpio_in(uicdev, 14),
                         PPC440EP_SDRAM_NR_BANKS, ram_memories,
-                      ram_bases, ram_sizes, 1);
+                      ram_bases, ram_sizes);
+    /* Enable SDRAM memory regions, this should be done by the 

firmware */
+    if (ppc_dcr_write(env->dcr_env, SDRAM0_CFGADDR, 0x20) || +        ppc_dcr_write(env->dcr_env, SDRAM0_CFGDATA, 0x80000000)) { +        error_report("couldn't enable memory regions"); +        exit(1);
+    }
        /* PCI */
      dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,

hw/ppc/ppc440_uc.c +0 -2
index db33334e29..6ab0ad7985 100644
@@ -489,8 +489,6 @@ typedef struct ppc440_sdram_t {
   } ppc440_sdram_t;
     enum {
-    SDRAM0_CFGADDR = 0x10,
-    SDRAM0_CFGDATA,
       SDRAM_R0BAS = 0x40,
       SDRAM_R1BAS,
       SDRAM_R2BAS,
hw/ppc/ppc4xx_devs.c +1 -9
index 1226ec4aa9..936d6f77fe 100644
@@ -56,11 +56,6 @@ struct ppc4xx_sdram_t {
       qemu_irq irq;
   };
   -enum {
-    SDRAM0_CFGADDR = 0x010,
-    SDRAM0_CFGDATA = 0x011,
-};
-
   /*
    * XXX: TOFIX: some patches have made this code become inconsistent:
    *      there are type inconsistencies, mixing hwaddr, target_ulong
@@ -350,8 +345,7 @@ static void sdram_reset(void *opaque)
   void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq irq, int nbanks,
                          MemoryRegion *ram_memories,
                          hwaddr *ram_bases,
-                       hwaddr *ram_sizes,
-                       int do_init)
+                       hwaddr *ram_sizes)
   {
       ppc4xx_sdram_t *sdram;
       int i;
@@ -369,9 +363,6 @@ void ppc4xx_sdram_init(CPUPPCState *env, qemu_irq 
 irq, int nbanks,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
       ppc_dcr_register(env, SDRAM0_CFGDATA,
                        sdram, &dcr_read_sdram, &dcr_write_sdram);
-    if (do_init) {
-        sdram_map_bcr(sdram);
-    }
   }
     /*
include/hw/ppc/ppc4xx.h +5 -2
index 2af0d60577..a5e6c185af 100644
@@ -37,6 +37,11 @@ typedef struct {
       uint32_t bcr;
   } Ppc4xxSdramBank;
   +enum {
+    SDRAM0_CFGADDR = 0x010,
+    SDRAM0_CFGDATA = 0x011,
+};
+
   void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
                           MemoryRegion ram_memories[],
                           hwaddr ram_bases[], hwaddr ram_sizes[],
@@ -45,8 +50,7 @@ void ppc4xx_sdram_banks(MemoryRegion *ram, int 
 nr_banks,
   void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
                           MemoryRegion ram_memories[],
                           hwaddr *ram_bases,
-                        hwaddr *ram_sizes,
-                        int do_init);
+                        hwaddr *ram_sizes);
     #define TYPE_PPC4xx_PCI_HOST_BRIDGE "ppc4xx-pcihost"