Skip to contentSkip to conversation

Altera NiosII support

PATCH Applied [email protected] 6 participants 31 messages Sep 10, 2012 – Sep 15, 2012 1 message not in the archive Page 2 of 2

Subsystems (2)
lore
Share & tools
lore
#24 lore raw

Re: [Qemu-devel] [PATCH 8/9] MicroBlaze: Add a config that is dynamically set up by a device tree file.

In reply to [email protected]
Quoted 61 lines · On Mon, Sep 10, 2012 at 12:20 AM, <[email protected]> wrote:

From: Chris Wulff <[email protected]>

---
 default-configs/microblaze-softmmu.mak |    1 +
 hw/labx_microblaze_devicetree.c        |  597 ++++++++++++++++++++++++++++++++
 hw/microblaze/Makefile.objs            |    3 +-
 3 files changed, 600 insertions(+), 1 deletion(-)
 create mode 100644 hw/labx_microblaze_devicetree.c
default-configs/microblaze-softmmu.mak +1 -0
index 64c9485..7e4cab8 100644
@@ -5,3 +5,4 @@ CONFIG_PFLASH_CFI01=y
 CONFIG_SERIAL=y
 CONFIG_XILINX=y
 CONFIG_XILINX_AXI=y
+CONFIG_LABX=y
hw/labx_microblaze_devicetree.c new file +36 -0
new file mode 100644
index 0000000..581ad14
@@ -0,0 +1,597 @@
+/*
+ * Flexible model of microblaze designs that use a device-tree to determine
+ * the hardware configuration.
+ *
+ * Copyright (c) 2010 LabX Technologies, LLC
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/lgpl-2.1.html>
+ */
+
+#include "sysbus.h"
+#include "hw.h"
+#include "net.h"
+#include "flash.h"
+#include "sysemu.h"
+#include "devices.h"
+#include "boards.h"
+#include "device_tree.h"
+#include "xilinx.h"
+#include "labx_devices.h"
+#include "loader.h"
+#include "elf.h"
+#include "blockdev.h"
+#include "exec-memory.h"
+#include "microblaze_pic_cpu.h"

hw/labx_microblaze_devicetree.c line 46

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+
+#define LMB_BRAM_SIZE  (128 * 1024)
+
+static struct
+{
+    uint32_t bootstrap_pc;
+    uint32_t cmdline;
+    uint32_t initrd;
+    uint32_t fdt;
+} boot_info;

Please read CODING_STYLE, struct names must use CamelCase.

hw/labx_microblaze_devicetree.c line 49

Quoted 3 lines
hw/labx_microblaze_devicetree.c new file +3 -0
+
+/* Current ethernet device index for multiple network interfaces */
+static int eth_dev_index = -1;

This looks fishy.

Quoted 74 lines
hw/labx_microblaze_devicetree.c new file +74 -0
+
+static void main_cpu_reset(void *opaque)
+{
+    MicroBlazeCPU *cpu = opaque;
+    CPUMBState *env = &cpu->env;
+
+    cpu_reset(CPU(cpu));
+
+    env->regs[5] = boot_info.cmdline;
+    env->regs[6] = boot_info.initrd;
+    env->regs[7] = boot_info.fdt;
+    env->sregs[SR_PC] = boot_info.bootstrap_pc;
+}
+
+#ifndef CONFIG_FDT
+#error "Device-tree support is required for this target to function"
+#endif
+
+static uint32_t fdt_get_int_from_array(void *fdt, int node,
+                                       const char *name, int index)
+{
+    int array_length;
+    const void *array = qemu_devtree_getprop_offset(fdt, node, name,
+                                                    &array_length);
+    if (index >= array_length) {
+        printf("fdt_get_int_from_array: requesting %s from node %d, "
+               "index %d out of range (%d max)\n",
+               name, node, index, array_length);
+        return 0;
+    } else {
+        return qemu_devtree_int_array_index(array, index);
+    }
+}
+
+#define BINARY_DEVICE_TREE_FILE "labx-microblaze.dtb"
+static void *get_device_tree(int *fdt_size)
+{
+    char *path;
+    void *fdt;
+
+    /* Try the local "mb.dtb" override.  */
+    fdt = load_device_tree("mb.dtb", fdt_size);
+    if (!fdt) {
+        path = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
+        if (path) {
+            fdt = load_device_tree(path, fdt_size);
+            g_free(path);
+        }
+    }
+
+    return fdt;
+}
+
+static int labx_microblaze_load_device_tree(target_phys_addr_t addr,
+                                      uint32_t ramsize,
+                                      target_phys_addr_t initrd_base,
+                                      target_phys_addr_t initrd_size,
+                                      const char *kernel_cmdline)
+{
+    int fdt_size;
+    void *fdt;
+    int r;
+
+    fdt = get_device_tree(&fdt_size);
+
+    if (!fdt) {
+        return 0;
+    }
+
+    if (kernel_cmdline && strlen(kernel_cmdline)) {
+        r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
+                                        kernel_cmdline);
+        if (r < 0) {
+            fprintf(stderr, "couldn't set /chosen/bootargs\n");

hw/labx_microblaze_devicetree.c line 133

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+        }
+    }
+    cpu_physical_memory_write(addr, (void *)fdt, fdt_size);
+
+    return fdt_size;
+}
+
+static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
+{
+    return addr - 0x30000000LL;

ULL

Quoted 22 lines
hw/labx_microblaze_devicetree.c new file +22 -0
+}
+
+static ram_addr_t get_dram_base(void *fdt)
+{
+    int root = qemu_devtree_node_offset(fdt, "/");
+    int memory = qemu_devtree_subnode_offset_namelen(fdt, root, "memory", 6);
+    if (memory > 0) {
+        int reglen;
+        const void *reg =
+            qemu_devtree_getprop_offset(fdt, memory, "reg", &reglen);
+
+        if (reglen >= 4) {
+            printf("DRAM base %08X, size %08X\n",
+                   qemu_devtree_int_array_index(reg, 0),
+                   qemu_devtree_int_array_index(reg, 1));
+            return qemu_devtree_int_array_index(reg, 0);
+        }
+    }
+
+    printf("DRAM base not found. Defaulting to 0x88000000\n");
+
+    return 0x88000000; /* Default to something reasonable */

hw/labx_microblaze_devicetree.c line 165

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+}
+
+typedef void (*device_init_func_t)(void *fdt, int node);
+
+typedef struct {
+    device_init_func_t probe;
+    int pass;
+    const char **compat;
+
+} devinfo_t;

Although a common error, _t suffix is reserved.

hw/labx_microblaze_devicetree.c line 172

Quoted 7 lines
hw/labx_microblaze_devicetree.c new file +7 -0
+
+/*
+ * Xilinx interrupt controller device
+ */
+MicroBlazeCPU *g_cpu;
+static qemu_irq irq[32] = {};
+static qemu_irq *cpu_irq;

These should form the device state structure, passed around instead of static state.

hw/labx_microblaze_devicetree.c line 181

Quoted 9 lines
hw/labx_microblaze_devicetree.c new file +9 -0
+
+static void xilinx_interrupt_controller_probe(void *fdt, int node)
+{
+    int i;
+    DeviceState *dev;
+    int reglen;
+    const void *reg = qemu_devtree_getprop_offset(fdt, node, "reg", &reglen);
+    uint32_t irq_addr = qemu_devtree_int_array_index(reg, 0);
+    int nrIrqLen;

CamelCase is not used for other identifiers.

hw/labx_microblaze_devicetree.c line 187

Quoted 6 lines
hw/labx_microblaze_devicetree.c new file +6 -0
+    const void *nrIrq =
+        qemu_devtree_getprop_offset(fdt, node, "xlnx,num-intr-inputs",
+                                    &nrIrqLen);
+    uint32_t nrIrqs = qemu_devtree_int_array_index(nrIrq, 0);
+
+    printf("  IRQ BASE %08X NIRQS %d\n", irq_addr, nrIrqs);

Leftover debugging?

hw/labx_microblaze_devicetree.c line 196

Quoted 9 lines
hw/labx_microblaze_devicetree.c new file +9 -0
+
+    cpu_irq = microblaze_pic_init_cpu(&g_cpu->env);
+    dev = xilinx_intc_create(irq_addr, cpu_irq[0], 2);
+    for (i = 0; i < nrIrqs; i++) {
+        irq[i] = qdev_get_gpio_in(dev, i);
+    }
+}
+
+devinfo_t xilinx_interrupt_controller_device = {

static const?

Quoted 12 lines
hw/labx_microblaze_devicetree.c new file +12 -0
+    .probe = &xilinx_interrupt_controller_probe,
+    .pass = 0,
+    .compat = (const char * []) { "xlnx,xps-intc-1.00.a", NULL }
+};
+
+/*
+ * Flash device
+ */
+static void flash_probe(void *fdt, int node)
+{
+    int reglen;
+    const void *reg = qemu_devtree_getprop_offset(fdt, node, "reg", &reglen);

hw/labx_microblaze_devicetree.c line 218

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    uint32_t flash_addr = qemu_devtree_int_array_index(reg, 0);
+    uint32_t flash_size = qemu_devtree_int_array_index(reg, 1);
+
+    DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
+    pflash_cfi01_register(flash_addr, NULL,
+                          qemu_devtree_get_name(fdt, node, NULL), flash_size,
+                          dinfo ? dinfo->bdrv : NULL, (64 * 1024),
+                          flash_size >> 16,
+                          1, 0x89, 0x18, 0x0000, 0x0, 1);
+    printf("-- loaded %d bytes to %08X\n",

Leftover debugging?

hw/labx_microblaze_devicetree.c line 224

Quoted 6 lines
hw/labx_microblaze_devicetree.c new file +6 -0
+           load_image_targphys(qemu_devtree_get_name(fdt, node, NULL),
+                               flash_addr, flash_size),
+           flash_addr);
+}
+
+devinfo_t flash_device = {

static const?

Quoted 4 lines
hw/labx_microblaze_devicetree.c new file +4 -0
+    .probe = &flash_probe,
+    .pass = 1,
+    .compat = (const char * []) { "cfi-flash", NULL }
+};

hw/labx_microblaze_devicetree.c line 238

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+
+/*
+ * Xilinx timer device
+ */
+static void xilinx_timer_probe(void *fdt, int node)
+{
+    uint32_t timer_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t timer_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    printf("  TIMER BASE %08X IRQ %d\n", timer_addr, timer_irq);

Please fix all of these.

hw/labx_microblaze_devicetree.c line 244

Quoted 6 lines
hw/labx_microblaze_devicetree.c new file +6 -0
+
+    /* 2 timers at irq 2 @ 62 Mhz.  */
+    xilinx_timer_create(timer_addr, irq[timer_irq], 0, 62 * 1000000);
+}
+
+devinfo_t xilinx_timer_device = {

Likewise.

Quoted 24 lines
hw/labx_microblaze_devicetree.c new file +24 -0
+    .probe = &xilinx_timer_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-timer-1.00.a", NULL }
+};
+
+/*
+ * Xilinx uartlite device
+ */
+static void xilinx_uartlite_probe(void *fdt, int node)
+{
+    uint32_t uart_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t uart_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    printf("  UART BASE %08X IRQ %d\n", uart_addr, uart_irq);
+
+    sysbus_create_simple("xlnx.xps-uartlite", uart_addr, irq[uart_irq]);
+}
+
+devinfo_t xilinx_uartlite_device = {
+    .probe = &xilinx_uartlite_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-uartlite-1.00.a", NULL }
+};
+

hw/labx_microblaze_devicetree.c line 278

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+/*
+ * Xilinx ethernetlite device
+ */
+static void xilinx_ethlite_probe(void *fdt, int node)
+{
+    uint32_t eth_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t eth_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    xilinx_ethlite_create(&nd_table[++eth_dev_index], eth_addr,
+                          irq[eth_irq], 0, 0);

The device should not access nd_table, this should be handled at board level.

Quoted 133 lines
hw/labx_microblaze_devicetree.c new file +133 -0
+}
+
+devinfo_t xilinx_ethlite_device = {
+    .probe = &xilinx_ethlite_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-ethernetlite-2.00.b", NULL }
+};
+
+/*
+ * LabX audio packetizer device
+ */
+static void labx_audio_packetizer_probe(void *fdt, int node)
+{
+    uint32_t packetizer_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t packetizer_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t clock_domains =
+        fdt_get_int_from_array(fdt, node, "xlnx,num-clock-domains", 0);
+    uint32_t cache_words =
+        fdt_get_int_from_array(fdt, node, "xlnx,cache-data-words", 0);
+
+    labx_audio_packetizer_create(packetizer_addr, irq[packetizer_irq],
+                                 clock_domains, cache_words);
+}
+
+devinfo_t labx_audio_packetizer_device = {
+    .probe = &labx_audio_packetizer_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-audio-packetizer-1.00.a", NULL }
+};
+
+/*
+ * LabX audio depacketizer device
+ */
+static void labx_audio_depacketizer_probe(void *fdt, int node)
+{
+    uint32_t depacketizer_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t depacketizer_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t clock_domains =
+        fdt_get_int_from_array(fdt, node, "xlnx,num-clock-domains", 0);
+    uint32_t cache_words =
+        fdt_get_int_from_array(fdt, node, "xlnx,cache-data-words", 0);
+
+    int ifLen;
+    const void *ifType =
+        qemu_devtree_getprop_offset(fdt, node, "xlnx,interface-type", &ifLen);
+    int hasDMA = (0 != strncmp("CACHE_RAM", (const char *)ifType, ifLen));
+
+    labx_audio_depacketizer_create(depacketizer_addr, irq[depacketizer_irq],
+                                   clock_domains, cache_words, hasDMA);
+}
+
+devinfo_t labx_audio_depacketizer_device = {
+    .probe = &labx_audio_depacketizer_probe,
+    .pass = 1,
+    .compat = (const char * []) {
+        "xlnx,labx-audio-depacketizer-1.00.a",
+        "xlnx,labx-audio-depacketizer-1.01.a",
+        NULL
+    }
+};
+
+/*
+ * LabX dma device
+ */
+static void labx_dma_probe(void *fdt, int node)
+{
+    uint32_t dma_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+
+    labx_dma_create(dma_addr, 1024);
+}
+
+devinfo_t labx_dma_device = {
+    .probe = &labx_dma_probe,
+    .pass = 1,
+    .compat = (const char * []) {
+        "xlnx,labx-dma-1.00.a",
+        "xlnx,labx-dma-1.01.a",
+        "xlnx,labx-local-audio-1.00.a",
+        NULL
+    }
+};
+
+/*
+ * LabX ethernet device
+ */
+static void labx_ethernet_probe(void *fdt, int node)
+{
+    uint32_t ethernet_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t host_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t fifo_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 2);
+    uint32_t phy_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 4);
+
+    labx_ethernet_create(&nd_table[++eth_dev_index], ethernet_addr,
+                         irq[host_irq], irq[fifo_irq], irq[phy_irq]);
+}
+
+devinfo_t labx_ethernet_device = {
+    .probe = &labx_ethernet_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-ethernet-1.00.a", NULL }
+};
+
+/*
+ * LabX ptp device
+ */
+static void labx_ptp_probe(void *fdt, int node)
+{
+    uint32_t ptp_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+
+    labx_ptp_create(ptp_addr);
+}
+
+devinfo_t labx_ptp_device = {
+    .probe = &labx_ptp_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-ptp-1.00.a", NULL }
+};
+
+/*
+ * Table of available devices
+ */
+devinfo_t *devices[] = {
+    &xilinx_interrupt_controller_device,
+    &flash_device,
+    &xilinx_timer_device,
+    &xilinx_uartlite_device,
+    &xilinx_ethlite_device,
+    &labx_audio_packetizer_device,

hw/labx_microblaze_devicetree.c line 421

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    &labx_audio_depacketizer_device,
+    &labx_dma_device,
+    &labx_ethernet_device,
+    &labx_ptp_device,
+    NULL
+};
+
+static int plb_device_probe(void *fdt, int node, int pass)
+{
+    devinfo_t **dev = &(devices[0]);

Unnecessary parentheses.

hw/labx_microblaze_devicetree.c line 426

Quoted 5 lines
hw/labx_microblaze_devicetree.c new file +5 -0
+
+    while (*dev) {
+        const char **compat = &((*dev)->compat[0]);
+        while (*compat) {
+            if (0 == qemu_devtree_node_check_compatible(fdt, node, *compat)) {

Usually the constant is on right hand side.

Quoted 108 lines
hw/labx_microblaze_devicetree.c new file +108 -0
+                if (pass == (*dev)->pass) {
+                    printf("Adding a device for node %s\n",
+                           qemu_devtree_get_name(fdt, node, NULL));
+
+                    (*dev)->probe(fdt, node);
+                    return 0;
+                }
+
+                if (pass < (*dev)->pass) {
+                    /* Probe again on the next pass */
+                    return 1;
+                }
+            }
+
+            compat++;
+        }
+
+        dev++;
+    }
+
+    return 0;
+}
+
+static void plb_bus_probe(void *fdt)
+{
+    int root = qemu_devtree_node_offset(fdt, "/");
+    int plb = qemu_devtree_subnode_offset_namelen(fdt, root, "plb", 3);
+    if (plb > 0) {
+        /* Do multiple passes through the devices. Some have dependencies
+           on others being first */
+        int pass = 0;
+        int again = 0;
+        do {
+            int child = plb;
+            again = 0;
+            do {
+                child = qemu_devtree_next_child_offset(fdt, plb, child);
+                if (child < 0) {
+                    break;
+                }
+
+                again |= plb_device_probe(fdt, child, pass);
+
+            } while (1);
+
+            pass++;
+
+        } while (again);
+    }
+}
+
+static void
+labx_microblaze_init(ram_addr_t ram_size,
+                     const char *boot_device,
+                     const char *kernel_filename,
+                     const char *kernel_cmdline,
+                     const char *initrd_filename,
+                     const char *cpu_model)
+{
+    MemoryRegion *address_space_mem = get_system_memory();
+
+    int kernel_size;
+    int fdt_size;
+    void *fdt = get_device_tree(&fdt_size);
+    target_phys_addr_t ddr_base = get_dram_base(fdt);
+    MemoryRegion *phys_lmb_bram = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
+    CPUMBState *env;
+
+    /* init CPUs */
+    if (cpu_model == NULL) {
+        cpu_model = "microblaze";
+    }
+    g_cpu = cpu_mb_init(cpu_model);
+    env = &g_cpu->env;
+
+    env->pvr.regs[10] = 0x0c000000; /* spartan 3a dsp family.  */
+    qemu_register_reset(main_cpu_reset, g_cpu);
+
+    /* Attach emulated BRAM through the LMB. LMB size is not specified in the
+       device-tree but there must be one to hold the vector table. */
+    memory_region_init_ram(phys_lmb_bram, "labx_microblaze.lmb_bram",
+                           LMB_BRAM_SIZE);
+    vmstate_register_ram_global(phys_lmb_bram);
+    memory_region_add_subregion(address_space_mem, 0x00000000, phys_lmb_bram);
+
+    memory_region_init_ram(phys_ram, "labx_microblaze.ram", ram_size);
+    vmstate_register_ram_global(phys_ram);
+    memory_region_add_subregion(address_space_mem, ddr_base, phys_ram);
+
+
+    /* Create other devices listed in the device-tree */
+    plb_bus_probe(fdt);
+
+    if (kernel_filename) {
+        uint64_t entry, low, high;
+        uint32_t base32;
+
+        /* Boots a kernel elf binary.  */
+        kernel_size = load_elf(kernel_filename, NULL, NULL,
+                               &entry, &low, &high,
+                               1, ELF_MACHINE, 0);
+        base32 = entry;
+        if (base32 == 0xc0000000) {
+            kernel_size = load_elf(kernel_filename, translate_kernel_address,
+                                   NULL, &entry, NULL, NULL,
+                                   1, ELF_MACHINE, 0);
+        }

hw/labx_microblaze_devicetree.c line 544

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+        /* Always boot into physical ram.  */
+        boot_info.bootstrap_pc = ddr_base + (entry & 0x07ffffff);
+
+        /* If it wasn't an ELF image, try an u-boot image.  */
+        if (kernel_size < 0) {
+            target_phys_addr_t uentry, loadaddr;
+
+            kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0);
+            boot_info.bootstrap_pc = uentry;
+            high = (loadaddr + kernel_size + 3) & ~3;

The constants should use ULL so that they are not truncated to 32 bits.

hw/labx_microblaze_devicetree.c line 552

Quoted 8 lines
hw/labx_microblaze_devicetree.c new file +8 -0
+        }
+
+        /* Not an ELF image nor an u-boot image, try a RAW image.  */
+        if (kernel_size < 0) {
+            kernel_size = load_image_targphys(kernel_filename, ddr_base,
+                                              ram_size);
+            boot_info.bootstrap_pc = ddr_base;
+            high = (ddr_base + kernel_size + 3) & ~3;

Also here.

Quoted 35 lines
hw/labx_microblaze_devicetree.c new file +35 -0
+        }
+
+        if (initrd_filename) {
+            uint32_t initrd_base = 0x88c00000;
+            uint32_t initrd_size =
+                    load_image_targphys(initrd_filename, initrd_base,
+                                        ram_size - initrd_base);
+            if (initrd_size <= 0) {
+                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
+                initrd_filename);
+                exit(1);
+            }
+
+            boot_info.initrd = initrd_base;
+        } else {
+            boot_info.initrd = 0x00000000;
+        }
+
+        boot_info.cmdline = high + 4096;
+        if (kernel_cmdline && strlen(kernel_cmdline)) {
+            pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
+        }
+        /* Provide a device-tree.  */
+        boot_info.fdt = boot_info.cmdline + 4096;
+        labx_microblaze_load_device_tree(boot_info.fdt, ram_size,
+                                   0, 0,
+                                   kernel_cmdline);
+    }
+}
+
+static QEMUMachine labx_microblaze_machine = {
+    .name = "labx-microblaze-devicetree",
+    .desc = "Microblaze design based on the peripherals specified "
+            "in the device-tree.",
+    .init = labx_microblaze_init,

hw/labx_microblaze_devicetree.c line 597

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    .is_default = 1
+};
+
+static void labx_microblaze_machine_init(void)
+{
+    qemu_register_machine(&labx_microblaze_machine);
+}
+
+machine_init(labx_microblaze_machine_init);
+

Useless newline before EOF.

Quoted 15 lines
hw/microblaze/Makefile.objs +2 -2
index 274d2c5..632cd60 100644
@@ -1,4 +1,5 @@
-obj-y = petalogix_s3adsp1800_mmu.o
+obj-y  = labx_microblaze_devicetree.o
+obj-y += petalogix_s3adsp1800_mmu.o
 obj-y += petalogix_ml605_mmu.o
 obj-y += microblaze_boot.o
 
--

1.7.9.5

#25 lore raw

Re: [Qemu-devel] [PATCH 8/9] MicroBlaze: Add a config that is dynamically set up by a device tree file.

Quoted 16 lines · Blue Swirl

hw/labx_microblaze_devicetree.c line 278

Quoted 12 lines · On Mon, Sep 10, 2012 at 12:20 AM, <[email protected]> wrote:

From: Chris Wulff <[email protected]>

hw/labx_microblaze_devicetree.c new file +10 -0
+/*
+ * Xilinx ethernetlite device
+ */
+static void xilinx_ethlite_probe(void *fdt, int node)
+{
+    uint32_t eth_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t eth_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    xilinx_ethlite_create(&nd_table[++eth_dev_index], eth_addr,
+                          irq[eth_irq], 0, 0);

The device should not access nd_table, this should be handled at board level.

This is board level isn't it? This function calls xilinx_ethlite_create() which in turn does the qdev_create so this is pre device creation. Im open to suggestions on how to do this in a non-global hacking way however as I have the same problem in my tree.

Regards,
Peter

Quoted 8 lines
Quoted 8 lines
hw/labx_microblaze_devicetree.c new file +8 -0
+}
+
+devinfo_t xilinx_ethlite_device = {
+    .probe = &xilinx_ethlite_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-ethernetlite-2.00.b", NULL }
+};
+
#26 lore raw

Re: [Qemu-devel] [PATCH 8/9] MicroBlaze: Add a config that is dynamically set up by a device tree file.

Quoted 18 lines · Peter Crosthwaite
Quoted 16 lines · Blue Swirl

hw/labx_microblaze_devicetree.c line 278

Quoted 12 lines · On Mon, Sep 10, 2012 at 12:20 AM, <[email protected]> wrote:

From: Chris Wulff <[email protected]>

hw/labx_microblaze_devicetree.c new file +10 -0
+/*
+ * Xilinx ethernetlite device
+ */
+static void xilinx_ethlite_probe(void *fdt, int node)
+{
+    uint32_t eth_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t eth_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    xilinx_ethlite_create(&nd_table[++eth_dev_index], eth_addr,
+                          irq[eth_irq], 0, 0);

The device should not access nd_table, this should be handled at board level.

Quoted 4 lines

This is board level isn't it? This function calls xilinx_ethlite_create() which in turn does the qdev_create so this is pre device creation. Im open to suggestions on how to do this in a non-global hacking way however as I have the same problem in my tree.

OK, the device code is mixed with the board code and that was confusing me. It could be better to move the device part to a separate file.

Quoted 14 lines

Regards,
Peter

Quoted 8 lines
Quoted 8 lines
hw/labx_microblaze_devicetree.c new file +8 -0
+}
+
+devinfo_t xilinx_ethlite_device = {
+    .probe = &xilinx_ethlite_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-ethernetlite-2.00.b", NULL }
+};
+
#27 lore raw

Re: [Qemu-devel] [PATCH 8/9] MicroBlaze: Add a config that is dynamically set up by a device tree file.

In reply to [email protected]

Hi Chris,

This needs to be reviewed along with the petalogix implementation, but a few top level comments.

Regards,
Peter

Quoted 78 lines · On Sun, 2012-09-09 at 20:20 -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

---
 default-configs/microblaze-softmmu.mak |    1 +
 hw/labx_microblaze_devicetree.c        |  597 ++++++++++++++++++++++++++++++++
 hw/microblaze/Makefile.objs            |    3 +-
 3 files changed, 600 insertions(+), 1 deletion(-)
 create mode 100644 hw/labx_microblaze_devicetree.c
default-configs/microblaze-softmmu.mak +1 -0
index 64c9485..7e4cab8 100644
@@ -5,3 +5,4 @@ CONFIG_PFLASH_CFI01=y
 CONFIG_SERIAL=y
 CONFIG_XILINX=y
 CONFIG_XILINX_AXI=y
+CONFIG_LABX=y
hw/labx_microblaze_devicetree.c new file +53 -0
new file mode 100644
index 0000000..581ad14
@@ -0,0 +1,597 @@
+/*
+ * Flexible model of microblaze designs that use a device-tree to determine
+ * the hardware configuration.
+ *
+ * Copyright (c) 2010 LabX Technologies, LLC
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/lgpl-2.1.html>
+ */
+
+#include "sysbus.h"
+#include "hw.h"
+#include "net.h"
+#include "flash.h"
+#include "sysemu.h"
+#include "devices.h"
+#include "boards.h"
+#include "device_tree.h"
+#include "xilinx.h"
+#include "labx_devices.h"
+#include "loader.h"
+#include "elf.h"
+#include "blockdev.h"
+#include "exec-memory.h"
+#include "microblaze_pic_cpu.h"
+
+#define LMB_BRAM_SIZE  (128 * 1024)
+
+static struct
+{
+    uint32_t bootstrap_pc;
+    uint32_t cmdline;
+    uint32_t initrd;
+    uint32_t fdt;
+} boot_info;
+
+/* Current ethernet device index for multiple network interfaces */
+static int eth_dev_index = -1;
+
+static void main_cpu_reset(void *opaque)
+{
+    MicroBlazeCPU *cpu = opaque;

hw/labx_microblaze_devicetree.c line 63

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    CPUMBState *env = &cpu->env;
+
+    cpu_reset(CPU(cpu));
+
+    env->regs[5] = boot_info.cmdline;
+    env->regs[6] = boot_info.initrd;
+    env->regs[7] = boot_info.fdt;
+    env->sregs[SR_PC] = boot_info.bootstrap_pc;
+}
+

Please use microblaze bootloader (hw/microblaze_boot.c). We need to consolidate bootloaders. If there is functionality missing from mb_boot, please add it there.

hw/labx_microblaze_devicetree.c line 67

Quoted 4 lines
hw/labx_microblaze_devicetree.c new file +4 -0
+#ifndef CONFIG_FDT
+#error "Device-tree support is required for this target to function"
+#endif
+

This should be handled by the make infrastructure. This machine model should be obj-(CONIFG_FDT)+= .. so this machine model just dissappears when QEMU is build without FDT.

Quoted 164 lines
hw/labx_microblaze_devicetree.c new file +164 -0
+static uint32_t fdt_get_int_from_array(void *fdt, int node,
+                                       const char *name, int index)
+{
+    int array_length;
+    const void *array = qemu_devtree_getprop_offset(fdt, node, name,
+                                                    &array_length);
+    if (index >= array_length) {
+        printf("fdt_get_int_from_array: requesting %s from node %d, "
+               "index %d out of range (%d max)\n",
+               name, node, index, array_length);
+        return 0;
+    } else {
+        return qemu_devtree_int_array_index(array, index);
+    }
+}
+
+#define BINARY_DEVICE_TREE_FILE "labx-microblaze.dtb"
+static void *get_device_tree(int *fdt_size)
+{
+    char *path;
+    void *fdt;
+
+    /* Try the local "mb.dtb" override.  */
+    fdt = load_device_tree("mb.dtb", fdt_size);
+    if (!fdt) {
+        path = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
+        if (path) {
+            fdt = load_device_tree(path, fdt_size);
+            g_free(path);
+        }
+    }
+
+    return fdt;
+}
+
+static int labx_microblaze_load_device_tree(target_phys_addr_t addr,
+                                      uint32_t ramsize,
+                                      target_phys_addr_t initrd_base,
+                                      target_phys_addr_t initrd_size,
+                                      const char *kernel_cmdline)
+{
+    int fdt_size;
+    void *fdt;
+    int r;
+
+    fdt = get_device_tree(&fdt_size);
+
+    if (!fdt) {
+        return 0;
+    }
+
+    if (kernel_cmdline && strlen(kernel_cmdline)) {
+        r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
+                                        kernel_cmdline);
+        if (r < 0) {
+            fprintf(stderr, "couldn't set /chosen/bootargs\n");
+        }
+    }
+    cpu_physical_memory_write(addr, (void *)fdt, fdt_size);
+
+    return fdt_size;
+}
+
+static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
+{
+    return addr - 0x30000000LL;
+}
+
+static ram_addr_t get_dram_base(void *fdt)
+{
+    int root = qemu_devtree_node_offset(fdt, "/");
+    int memory = qemu_devtree_subnode_offset_namelen(fdt, root, "memory", 6);
+    if (memory > 0) {
+        int reglen;
+        const void *reg =
+            qemu_devtree_getprop_offset(fdt, memory, "reg", &reglen);
+
+        if (reglen >= 4) {
+            printf("DRAM base %08X, size %08X\n",
+                   qemu_devtree_int_array_index(reg, 0),
+                   qemu_devtree_int_array_index(reg, 1));
+            return qemu_devtree_int_array_index(reg, 0);
+        }
+    }
+
+    printf("DRAM base not found. Defaulting to 0x88000000\n");
+
+    return 0x88000000; /* Default to something reasonable */
+}
+
+typedef void (*device_init_func_t)(void *fdt, int node);
+
+typedef struct {
+    device_init_func_t probe;
+    int pass;
+    const char **compat;
+
+} devinfo_t;
+
+/*
+ * Xilinx interrupt controller device
+ */
+MicroBlazeCPU *g_cpu;
+static qemu_irq irq[32] = {};
+static qemu_irq *cpu_irq;
+
+static void xilinx_interrupt_controller_probe(void *fdt, int node)
+{
+    int i;
+    DeviceState *dev;
+    int reglen;
+    const void *reg = qemu_devtree_getprop_offset(fdt, node, "reg", &reglen);
+    uint32_t irq_addr = qemu_devtree_int_array_index(reg, 0);
+    int nrIrqLen;
+    const void *nrIrq =
+        qemu_devtree_getprop_offset(fdt, node, "xlnx,num-intr-inputs",
+                                    &nrIrqLen);
+    uint32_t nrIrqs = qemu_devtree_int_array_index(nrIrq, 0);
+
+    printf("  IRQ BASE %08X NIRQS %d\n", irq_addr, nrIrqs);
+
+    cpu_irq = microblaze_pic_init_cpu(&g_cpu->env);
+    dev = xilinx_intc_create(irq_addr, cpu_irq[0], 2);
+    for (i = 0; i < nrIrqs; i++) {
+        irq[i] = qdev_get_gpio_in(dev, i);
+    }
+}
+
+devinfo_t xilinx_interrupt_controller_device = {
+    .probe = &xilinx_interrupt_controller_probe,
+    .pass = 0,
+    .compat = (const char * []) { "xlnx,xps-intc-1.00.a", NULL }
+};
+
+/*
+ * Flash device
+ */
+static void flash_probe(void *fdt, int node)
+{
+    int reglen;
+    const void *reg = qemu_devtree_getprop_offset(fdt, node, "reg", &reglen);
+    uint32_t flash_addr = qemu_devtree_int_array_index(reg, 0);
+    uint32_t flash_size = qemu_devtree_int_array_index(reg, 1);
+
+    DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
+    pflash_cfi01_register(flash_addr, NULL,
+                          qemu_devtree_get_name(fdt, node, NULL), flash_size,
+                          dinfo ? dinfo->bdrv : NULL, (64 * 1024),
+                          flash_size >> 16,
+                          1, 0x89, 0x18, 0x0000, 0x0, 1);
+    printf("-- loaded %d bytes to %08X\n",
+           load_image_targphys(qemu_devtree_get_name(fdt, node, NULL),
+                               flash_addr, flash_size),
+           flash_addr);
+}
+
+devinfo_t flash_device = {
+    .probe = &flash_probe,
+    .pass = 1,
+    .compat = (const char * []) { "cfi-flash", NULL }
+};
+
+/*
+ * Xilinx timer device

hw/labx_microblaze_devicetree.c line 241

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+ */
+static void xilinx_timer_probe(void *fdt, int node)
+{
+    uint32_t timer_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t timer_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    printf("  TIMER BASE %08X IRQ %d\n", timer_addr, timer_irq);
+
+    /* 2 timers at irq 2 @ 62 Mhz.  */
+    xilinx_timer_create(timer_addr, irq[timer_irq], 0, 62 * 1000000);

No check is made for the one_timer_only property.

Quoted 168 lines
hw/labx_microblaze_devicetree.c new file +168 -0
+}
+
+devinfo_t xilinx_timer_device = {
+    .probe = &xilinx_timer_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-timer-1.00.a", NULL }
+};
+
+/*
+ * Xilinx uartlite device
+ */
+static void xilinx_uartlite_probe(void *fdt, int node)
+{
+    uint32_t uart_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t uart_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    printf("  UART BASE %08X IRQ %d\n", uart_addr, uart_irq);
+
+    sysbus_create_simple("xlnx.xps-uartlite", uart_addr, irq[uart_irq]);
+}
+
+devinfo_t xilinx_uartlite_device = {
+    .probe = &xilinx_uartlite_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-uartlite-1.00.a", NULL }
+};
+
+/*
+ * Xilinx ethernetlite device
+ */
+static void xilinx_ethlite_probe(void *fdt, int node)
+{
+    uint32_t eth_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t eth_irq = fdt_get_int_from_array(fdt, node, "interrupts", 0);
+
+    xilinx_ethlite_create(&nd_table[++eth_dev_index], eth_addr,
+                          irq[eth_irq], 0, 0);
+}
+
+devinfo_t xilinx_ethlite_device = {
+    .probe = &xilinx_ethlite_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-ethernetlite-2.00.b", NULL }
+};
+
+/*
+ * LabX audio packetizer device
+ */
+static void labx_audio_packetizer_probe(void *fdt, int node)
+{
+    uint32_t packetizer_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t packetizer_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t clock_domains =
+        fdt_get_int_from_array(fdt, node, "xlnx,num-clock-domains", 0);
+    uint32_t cache_words =
+        fdt_get_int_from_array(fdt, node, "xlnx,cache-data-words", 0);
+
+    labx_audio_packetizer_create(packetizer_addr, irq[packetizer_irq],
+                                 clock_domains, cache_words);
+}
+
+devinfo_t labx_audio_packetizer_device = {
+    .probe = &labx_audio_packetizer_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-audio-packetizer-1.00.a", NULL }
+};
+
+/*
+ * LabX audio depacketizer device
+ */
+static void labx_audio_depacketizer_probe(void *fdt, int node)
+{
+    uint32_t depacketizer_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t depacketizer_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t clock_domains =
+        fdt_get_int_from_array(fdt, node, "xlnx,num-clock-domains", 0);
+    uint32_t cache_words =
+        fdt_get_int_from_array(fdt, node, "xlnx,cache-data-words", 0);
+
+    int ifLen;
+    const void *ifType =
+        qemu_devtree_getprop_offset(fdt, node, "xlnx,interface-type", &ifLen);
+    int hasDMA = (0 != strncmp("CACHE_RAM", (const char *)ifType, ifLen));
+
+    labx_audio_depacketizer_create(depacketizer_addr, irq[depacketizer_irq],
+                                   clock_domains, cache_words, hasDMA);
+}
+
+devinfo_t labx_audio_depacketizer_device = {
+    .probe = &labx_audio_depacketizer_probe,
+    .pass = 1,
+    .compat = (const char * []) {
+        "xlnx,labx-audio-depacketizer-1.00.a",
+        "xlnx,labx-audio-depacketizer-1.01.a",
+        NULL
+    }
+};
+
+/*
+ * LabX dma device
+ */
+static void labx_dma_probe(void *fdt, int node)
+{
+    uint32_t dma_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+
+    labx_dma_create(dma_addr, 1024);
+}
+
+devinfo_t labx_dma_device = {
+    .probe = &labx_dma_probe,
+    .pass = 1,
+    .compat = (const char * []) {
+        "xlnx,labx-dma-1.00.a",
+        "xlnx,labx-dma-1.01.a",
+        "xlnx,labx-local-audio-1.00.a",
+        NULL
+    }
+};
+
+/*
+ * LabX ethernet device
+ */
+static void labx_ethernet_probe(void *fdt, int node)
+{
+    uint32_t ethernet_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t host_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t fifo_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 2);
+    uint32_t phy_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 4);
+
+    labx_ethernet_create(&nd_table[++eth_dev_index], ethernet_addr,
+                         irq[host_irq], irq[fifo_irq], irq[phy_irq]);
+}
+
+devinfo_t labx_ethernet_device = {
+    .probe = &labx_ethernet_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-ethernet-1.00.a", NULL }
+};
+
+/*
+ * LabX ptp device
+ */
+static void labx_ptp_probe(void *fdt, int node)
+{
+    uint32_t ptp_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+
+    labx_ptp_create(ptp_addr);
+}
+
+devinfo_t labx_ptp_device = {
+    .probe = &labx_ptp_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-ptp-1.00.a", NULL }
+};
+
+/*
+ * Table of available devices
+ */
+devinfo_t *devices[] = {
+    &xilinx_interrupt_controller_device,
+    &flash_device,
+    &xilinx_timer_device,
+    &xilinx_uartlite_device,

hw/labx_microblaze_devicetree.c line 419

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    &xilinx_ethlite_device,
+    &labx_audio_packetizer_device,
+    &labx_audio_depacketizer_device,
+    &labx_dma_device,
+    &labx_ethernet_device,
+    &labx_ptp_device,
+    NULL
+};
+
+static int plb_device_probe(void *fdt, int node, int pass)

QEMU abstacts away interconnect implementations. You should probably also drive off the "simple-bus" compat rather than PLB for machine model compatibility. s/plb/bus/g or something like it.

Quoted 74 lines
hw/labx_microblaze_devicetree.c new file +74 -0
+{
+    devinfo_t **dev = &(devices[0]);
+
+    while (*dev) {
+        const char **compat = &((*dev)->compat[0]);
+        while (*compat) {
+            if (0 == qemu_devtree_node_check_compatible(fdt, node, *compat)) {
+                if (pass == (*dev)->pass) {
+                    printf("Adding a device for node %s\n",
+                           qemu_devtree_get_name(fdt, node, NULL));
+
+                    (*dev)->probe(fdt, node);
+                    return 0;
+                }
+
+                if (pass < (*dev)->pass) {
+                    /* Probe again on the next pass */
+                    return 1;
+                }
+            }
+
+            compat++;
+        }
+
+        dev++;
+    }
+
+    return 0;
+}
+
+static void plb_bus_probe(void *fdt)
+{
+    int root = qemu_devtree_node_offset(fdt, "/");
+    int plb = qemu_devtree_subnode_offset_namelen(fdt, root, "plb", 3);
+    if (plb > 0) {
+        /* Do multiple passes through the devices. Some have dependencies
+           on others being first */
+        int pass = 0;
+        int again = 0;
+        do {
+            int child = plb;
+            again = 0;
+            do {
+                child = qemu_devtree_next_child_offset(fdt, plb, child);
+                if (child < 0) {
+                    break;
+                }
+
+                again |= plb_device_probe(fdt, child, pass);
+
+            } while (1);
+
+            pass++;
+
+        } while (again);
+    }
+}
+
+static void
+labx_microblaze_init(ram_addr_t ram_size,
+                     const char *boot_device,
+                     const char *kernel_filename,
+                     const char *kernel_cmdline,
+                     const char *initrd_filename,
+                     const char *cpu_model)
+{
+    MemoryRegion *address_space_mem = get_system_memory();
+
+    int kernel_size;
+    int fdt_size;
+    void *fdt = get_device_tree(&fdt_size);
+    target_phys_addr_t ddr_base = get_dram_base(fdt);
+    MemoryRegion *phys_lmb_bram = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);

hw/labx_microblaze_devicetree.c line 503

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    CPUMBState *env;
+
+    /* init CPUs */
+    if (cpu_model == NULL) {
+        cpu_model = "microblaze";
+    }
+    g_cpu = cpu_mb_init(cpu_model);
+    env = &g_cpu->env;
+
+    env->pvr.regs[10] = 0x0c000000; /* spartan 3a dsp family.  */

I have dtb driven solution for this.

Quoted 70 lines
hw/labx_microblaze_devicetree.c new file +70 -0
+    qemu_register_reset(main_cpu_reset, g_cpu);
+
+    /* Attach emulated BRAM through the LMB. LMB size is not specified in the
+       device-tree but there must be one to hold the vector table. */
+    memory_region_init_ram(phys_lmb_bram, "labx_microblaze.lmb_bram",
+                           LMB_BRAM_SIZE);
+    vmstate_register_ram_global(phys_lmb_bram);
+    memory_region_add_subregion(address_space_mem, 0x00000000, phys_lmb_bram);
+
+    memory_region_init_ram(phys_ram, "labx_microblaze.ram", ram_size);
+    vmstate_register_ram_global(phys_ram);
+    memory_region_add_subregion(address_space_mem, ddr_base, phys_ram);
+
+
+    /* Create other devices listed in the device-tree */
+    plb_bus_probe(fdt);
+
+    if (kernel_filename) {
+        uint64_t entry, low, high;
+        uint32_t base32;
+
+        /* Boots a kernel elf binary.  */
+        kernel_size = load_elf(kernel_filename, NULL, NULL,
+                               &entry, &low, &high,
+                               1, ELF_MACHINE, 0);
+        base32 = entry;
+        if (base32 == 0xc0000000) {
+            kernel_size = load_elf(kernel_filename, translate_kernel_address,
+                                   NULL, &entry, NULL, NULL,
+                                   1, ELF_MACHINE, 0);
+        }
+        /* Always boot into physical ram.  */
+        boot_info.bootstrap_pc = ddr_base + (entry & 0x07ffffff);
+
+        /* If it wasn't an ELF image, try an u-boot image.  */
+        if (kernel_size < 0) {
+            target_phys_addr_t uentry, loadaddr;
+
+            kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0);
+            boot_info.bootstrap_pc = uentry;
+            high = (loadaddr + kernel_size + 3) & ~3;
+        }
+
+        /* Not an ELF image nor an u-boot image, try a RAW image.  */
+        if (kernel_size < 0) {
+            kernel_size = load_image_targphys(kernel_filename, ddr_base,
+                                              ram_size);
+            boot_info.bootstrap_pc = ddr_base;
+            high = (ddr_base + kernel_size + 3) & ~3;
+        }
+
+        if (initrd_filename) {
+            uint32_t initrd_base = 0x88c00000;
+            uint32_t initrd_size =
+                    load_image_targphys(initrd_filename, initrd_base,
+                                        ram_size - initrd_base);
+            if (initrd_size <= 0) {
+                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
+                initrd_filename);
+                exit(1);
+            }
+
+            boot_info.initrd = initrd_base;
+        } else {
+            boot_info.initrd = 0x00000000;
+        }
+
+        boot_info.cmdline = high + 4096;
+        if (kernel_cmdline && strlen(kernel_cmdline)) {
+            pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);

hw/labx_microblaze_devicetree.c line 583

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+        }
+        /* Provide a device-tree.  */
+        boot_info.fdt = boot_info.cmdline + 4096;
+        labx_microblaze_load_device_tree(boot_info.fdt, ram_size,
+                                   0, 0,
+                                   kernel_cmdline);
+    }
+}
+
+static QEMUMachine labx_microblaze_machine = {

Its a generic machine, so should it be badged as labx?

Quoted 20 lines
hw/labx_microblaze_devicetree.c new file +14 -0
+    .name = "labx-microblaze-devicetree",
+    .desc = "Microblaze design based on the peripherals specified "
+            "in the device-tree.",
+    .init = labx_microblaze_init,
+    .is_default = 1
 
+};
+
+static void labx_microblaze_machine_init(void)
+{
+    qemu_register_machine(&labx_microblaze_machine);
+}
+
+machine_init(labx_microblaze_machine_init);
+
hw/microblaze/Makefile.objs +0 -0
index 274d2c5..632cd60 100644

hw/microblaze/Makefile.objs line 1

Quoted 2 lines
hw/microblaze/Makefile.objs +1 -1
@@ -1,4 +1,5 @@
-obj-y = petalogix_s3adsp1800_mmu.o
+obj-y  = labx_microblaze_devicetree.o

obj-y += labx_microblaze_devicetree.(CONFIG-FDT)

Quoted 4 lines
hw/microblaze/Makefile.objs +1 -0
+obj-y += petalogix_s3adsp1800_mmu.o
 obj-y += petalogix_ml605_mmu.o
 obj-y += microblaze_boot.o
 
#28 lore raw

Re: [Qemu-devel] [PATCH 8/9] MicroBlaze: Add a config that is dynamically set up by a device tree file.

I'm all for consolidating the different device-tree based "boards" as much as possible. This, the petalogix variant, and the NiosII one that I created as part of this patch set too, are all very similar. We just need some generic way to register the set of devices that can be created and then the walking of the device tree itself could probably be generic. I'd be happy to work with you to move in that direction.

The only thing that I ran into that is a bit different is that the NiosII declares the type of attached interrupt controller inside the CPU block and I ended up enumerating that as if the cpu were itself a bus.

-- Chris Wulff

Quoted 700 lines · Peter Crosthwaite

Hi Chris,

This needs to be reviewed along with the petalogix implementation, but a few top level comments.

Regards,
Peter

Quoted 6 lines · On Sun, 2012-09-09 at 20:20 -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

---
 default-configs/microblaze-softmmu.mak |    1 +
 hw/labx_microblaze_devicetree.c        |  597

++++++++++++++++++++++++++++++++

Review

Quoted 5 lines
 hw/microblaze/Makefile.objs            |    3 +-
 3 files changed, 600 insertions(+), 1 deletion(-)
 create mode 100644 hw/labx_microblaze_devicetree.c
(quoted diff) +0 -0

b/default-configs/microblaze-softmmu.mak

Review

Quoted 9 lines

index 64c9485..7e4cab8 100644

default-configs/microblaze-softmmu.mak +1 -0
@@ -5,3 +5,4 @@ CONFIG_PFLASH_CFI01=y
 CONFIG_SERIAL=y
 CONFIG_XILINX=y
 CONFIG_XILINX_AXI=y
+CONFIG_LABX=y
(quoted diff) +0 -0

b/hw/labx_microblaze_devicetree.c

hw/labx_microblaze_devicetree.c line 2

Quoted 7 lines

new file mode 100644
index 0000000..581ad14

hw/labx_microblaze_devicetree.c new file +2 -0
@@ -0,0 +1,597 @@
+/*
+ * Flexible model of microblaze designs that use a device-tree to

determine

Quoted 51 lines
hw/labx_microblaze_devicetree.c new file +51 -0
+ * the hardware configuration.
+ *
+ * Copyright (c) 2010 LabX Technologies, LLC
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/lgpl-2.1.html>
+ */
+
+#include "sysbus.h"
+#include "hw.h"
+#include "net.h"
+#include "flash.h"
+#include "sysemu.h"
+#include "devices.h"
+#include "boards.h"
+#include "device_tree.h"
+#include "xilinx.h"
+#include "labx_devices.h"
+#include "loader.h"
+#include "elf.h"
+#include "blockdev.h"
+#include "exec-memory.h"
+#include "microblaze_pic_cpu.h"
+
+#define LMB_BRAM_SIZE  (128 * 1024)
+
+static struct
+{
+    uint32_t bootstrap_pc;
+    uint32_t cmdline;
+    uint32_t initrd;
+    uint32_t fdt;
+} boot_info;
+
+/* Current ethernet device index for multiple network interfaces */
+static int eth_dev_index = -1;
+
+static void main_cpu_reset(void *opaque)
+{
+    MicroBlazeCPU *cpu = opaque;

hw/labx_microblaze_devicetree.c line 63

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    CPUMBState *env = &cpu->env;
+
+    cpu_reset(CPU(cpu));
+
+    env->regs[5] = boot_info.cmdline;
+    env->regs[6] = boot_info.initrd;
+    env->regs[7] = boot_info.fdt;
+    env->sregs[SR_PC] = boot_info.bootstrap_pc;
+}
+

Please use microblaze bootloader (hw/microblaze_boot.c). We need to consolidate bootloaders. If there is functionality missing from mb_boot, please add it there.

hw/labx_microblaze_devicetree.c line 67

Quoted 4 lines
hw/labx_microblaze_devicetree.c new file +4 -0
+#ifndef CONFIG_FDT
+#error "Device-tree support is required for this target to function"
+#endif
+

This should be handled by the make infrastructure. This machine model should be obj-(CONIFG_FDT)+= .. so this machine model just dissappears when QEMU is build without FDT.

Quoted 16 lines
hw/labx_microblaze_devicetree.c new file +16 -0
+static uint32_t fdt_get_int_from_array(void *fdt, int node,
+                                       const char *name, int index)
+{
+    int array_length;
+    const void *array = qemu_devtree_getprop_offset(fdt, node, name,
+                                                    &array_length);
+    if (index >= array_length) {
+        printf("fdt_get_int_from_array: requesting %s from node %d, "
+               "index %d out of range (%d max)\n",
+               name, node, index, array_length);
+        return 0;
+    } else {
+        return qemu_devtree_int_array_index(array, index);
+    }
+}
+

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+#define BINARY_DEVICE_TREE_FILE "labx-microblaze.dtb"
+static void *get_device_tree(int *fdt_size)
+{
+    char *path;
+    void *fdt;
+
+    /* Try the local "mb.dtb" override.  */
+    fdt = load_device_tree("mb.dtb", fdt_size);
+    if (!fdt) {
+        path = qemu_find_file(QEMU_FILE_TYPE_BIOS,

BINARY_DEVICE_TREE_FILE);

Quoted 36 lines
hw/labx_microblaze_devicetree.c new file +36 -0
+        if (path) {
+            fdt = load_device_tree(path, fdt_size);
+            g_free(path);
+        }
+    }
+
+    return fdt;
+}
+
+static int labx_microblaze_load_device_tree(target_phys_addr_t addr,
+                                      uint32_t ramsize,
+                                      target_phys_addr_t initrd_base,
+                                      target_phys_addr_t initrd_size,
+                                      const char *kernel_cmdline)
+{
+    int fdt_size;
+    void *fdt;
+    int r;
+
+    fdt = get_device_tree(&fdt_size);
+
+    if (!fdt) {
+        return 0;
+    }
+
+    if (kernel_cmdline && strlen(kernel_cmdline)) {
+        r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
+                                        kernel_cmdline);
+        if (r < 0) {
+            fprintf(stderr, "couldn't set /chosen/bootargs\n");
+        }
+    }
+    cpu_physical_memory_write(addr, (void *)fdt, fdt_size);
+
+    return fdt_size;
+}

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+
+static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
+{
+    return addr - 0x30000000LL;
+}
+
+static ram_addr_t get_dram_base(void *fdt)
+{
+    int root = qemu_devtree_node_offset(fdt, "/");
+    int memory = qemu_devtree_subnode_offset_namelen(fdt, root,

"memory", 6);

Quoted 30 lines
hw/labx_microblaze_devicetree.c new file +30 -0
+    if (memory > 0) {
+        int reglen;
+        const void *reg =
+            qemu_devtree_getprop_offset(fdt, memory, "reg", &reglen);
+
+        if (reglen >= 4) {
+            printf("DRAM base %08X, size %08X\n",
+                   qemu_devtree_int_array_index(reg, 0),
+                   qemu_devtree_int_array_index(reg, 1));
+            return qemu_devtree_int_array_index(reg, 0);
+        }
+    }
+
+    printf("DRAM base not found. Defaulting to 0x88000000\n");
+
+    return 0x88000000; /* Default to something reasonable */
+}
+
+typedef void (*device_init_func_t)(void *fdt, int node);
+
+typedef struct {
+    device_init_func_t probe;
+    int pass;
+    const char **compat;
+
+} devinfo_t;
+
+/*
+ * Xilinx interrupt controller device
+ */

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+MicroBlazeCPU *g_cpu;
+static qemu_irq irq[32] = {};
+static qemu_irq *cpu_irq;
+
+static void xilinx_interrupt_controller_probe(void *fdt, int node)
+{
+    int i;
+    DeviceState *dev;
+    int reglen;
+    const void *reg = qemu_devtree_getprop_offset(fdt, node, "reg",

&reglen);

Quoted 19 lines
hw/labx_microblaze_devicetree.c new file +19 -0
+    uint32_t irq_addr = qemu_devtree_int_array_index(reg, 0);
+    int nrIrqLen;
+    const void *nrIrq =
+        qemu_devtree_getprop_offset(fdt, node, "xlnx,num-intr-inputs",
+                                    &nrIrqLen);
+    uint32_t nrIrqs = qemu_devtree_int_array_index(nrIrq, 0);
+
+    printf("  IRQ BASE %08X NIRQS %d\n", irq_addr, nrIrqs);
+
+    cpu_irq = microblaze_pic_init_cpu(&g_cpu->env);
+    dev = xilinx_intc_create(irq_addr, cpu_irq[0], 2);
+    for (i = 0; i < nrIrqs; i++) {
+        irq[i] = qdev_get_gpio_in(dev, i);
+    }
+}
+
+devinfo_t xilinx_interrupt_controller_device = {
+    .probe = &xilinx_interrupt_controller_probe,
+    .pass = 0,

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    .compat = (const char * []) { "xlnx,xps-intc-1.00.a", NULL }
+};
+
+/*
+ * Flash device
+ */
+static void flash_probe(void *fdt, int node)
+{
+    int reglen;
+    const void *reg = qemu_devtree_getprop_offset(fdt, node, "reg",

&reglen);

hw/labx_microblaze_devicetree.c

Quoted 6 lines
hw/labx_microblaze_devicetree.c new file +6 -0
+    uint32_t flash_addr = qemu_devtree_int_array_index(reg, 0);
+    uint32_t flash_size = qemu_devtree_int_array_index(reg, 1);
+
+    DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
+    pflash_cfi01_register(flash_addr, NULL,
+                          qemu_devtree_get_name(fdt, node, NULL),

flash_size,

Quoted 12 lines
hw/labx_microblaze_devicetree.c new file +12 -0
+                          dinfo ? dinfo->bdrv : NULL, (64 * 1024),
+                          flash_size >> 16,
+                          1, 0x89, 0x18, 0x0000, 0x0, 1);
+    printf("-- loaded %d bytes to %08X\n",
+           load_image_targphys(qemu_devtree_get_name(fdt, node, NULL),
+                               flash_addr, flash_size),
+           flash_addr);
+}
+
+devinfo_t flash_device = {
+    .probe = &flash_probe,
+    .pass = 1,

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    .compat = (const char * []) { "cfi-flash", NULL }
+};
+
+/*
+ * Xilinx timer device
+ */
+static void xilinx_timer_probe(void *fdt, int node)
+{
+    uint32_t timer_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t timer_irq = fdt_get_int_from_array(fdt, node,

"interrupts", 0);

hw/labx_microblaze_devicetree.c line 241

Quoted 5 lines
hw/labx_microblaze_devicetree.c new file +5 -0
+
+    printf("  TIMER BASE %08X IRQ %d\n", timer_addr, timer_irq);
+
+    /* 2 timers at irq 2 @ 62 Mhz.  */
+    xilinx_timer_create(timer_addr, irq[timer_irq], 0, 62 * 1000000);

No check is made for the one_timer_only property.

Quoted 5 lines
hw/labx_microblaze_devicetree.c new file +5 -0
+}
+
+devinfo_t xilinx_timer_device = {
+    .probe = &xilinx_timer_probe,
+    .pass = 1,

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    .compat = (const char * []) { "xlnx,xps-timer-1.00.a", NULL }
+};
+
+/*
+ * Xilinx uartlite device
+ */
+static void xilinx_uartlite_probe(void *fdt, int node)
+{
+    uint32_t uart_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t uart_irq = fdt_get_int_from_array(fdt, node, "interrupts",

0);

Quoted 9 lines
hw/labx_microblaze_devicetree.c new file +9 -0
+
+    printf("  UART BASE %08X IRQ %d\n", uart_addr, uart_irq);
+
+    sysbus_create_simple("xlnx.xps-uartlite", uart_addr, irq[uart_irq]);
+}
+
+devinfo_t xilinx_uartlite_device = {
+    .probe = &xilinx_uartlite_probe,
+    .pass = 1,

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    .compat = (const char * []) { "xlnx,xps-uartlite-1.00.a", NULL }
+};
+
+/*
+ * Xilinx ethernetlite device
+ */
+static void xilinx_ethlite_probe(void *fdt, int node)
+{
+    uint32_t eth_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+    uint32_t eth_irq = fdt_get_int_from_array(fdt, node, "interrupts",

0);

Quoted 7 lines
hw/labx_microblaze_devicetree.c new file +7 -0
+
+    xilinx_ethlite_create(&nd_table[++eth_dev_index], eth_addr,
+                          irq[eth_irq], 0, 0);
+}
+
+devinfo_t xilinx_ethlite_device = {
+    .probe = &xilinx_ethlite_probe,

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,xps-ethernetlite-2.00.b", NULL }
+};
+
+/*
+ * LabX audio packetizer device
+ */
+static void labx_audio_packetizer_probe(void *fdt, int node)
+{
+    uint32_t packetizer_addr = fdt_get_int_from_array(fdt, node, "reg",

0);

Quoted 5 lines
hw/labx_microblaze_devicetree.c new file +5 -0
+    uint32_t packetizer_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t clock_domains =
+        fdt_get_int_from_array(fdt, node, "xlnx,num-clock-domains", 0);
+    uint32_t cache_words =

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+        fdt_get_int_from_array(fdt, node, "xlnx,cache-data-words", 0);
+
+    labx_audio_packetizer_create(packetizer_addr, irq[packetizer_irq],
+                                 clock_domains, cache_words);
+}
+
+devinfo_t labx_audio_packetizer_device = {
+    .probe = &labx_audio_packetizer_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-audio-packetizer-1.00.a",

NULL }

hw/labx_microblaze_devicetree.c

Quoted 8 lines
hw/labx_microblaze_devicetree.c new file +8 -0
+};
+
+/*
+ * LabX audio depacketizer device
+ */
+static void labx_audio_depacketizer_probe(void *fdt, int node)
+{
+    uint32_t depacketizer_addr = fdt_get_int_from_array(fdt, node,

"reg", 0);

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    uint32_t depacketizer_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t clock_domains =
+        fdt_get_int_from_array(fdt, node, "xlnx,num-clock-domains", 0);
+    uint32_t cache_words =
+        fdt_get_int_from_array(fdt, node, "xlnx,cache-data-words", 0);
+
+    int ifLen;
+    const void *ifType =
+        qemu_devtree_getprop_offset(fdt, node, "xlnx,interface-type",

&ifLen);

hw/labx_microblaze_devicetree.c line 326

Quoted 1 line
hw/labx_microblaze_devicetree.c new file +1 -0
+    int hasDMA = (0 != strncmp("CACHE_RAM", (const char *)ifType,

ifLen));

hw/labx_microblaze_devicetree.c line 328

Quoted 2 lines
hw/labx_microblaze_devicetree.c new file +2 -0
+
+    labx_audio_depacketizer_create(depacketizer_addr,

irq[depacketizer_irq],

Quoted 30 lines
hw/labx_microblaze_devicetree.c new file +30 -0
+                                   clock_domains, cache_words, hasDMA);
+}
+
+devinfo_t labx_audio_depacketizer_device = {
+    .probe = &labx_audio_depacketizer_probe,
+    .pass = 1,
+    .compat = (const char * []) {
+        "xlnx,labx-audio-depacketizer-1.00.a",
+        "xlnx,labx-audio-depacketizer-1.01.a",
+        NULL
+    }
+};
+
+/*
+ * LabX dma device
+ */
+static void labx_dma_probe(void *fdt, int node)
+{
+    uint32_t dma_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+
+    labx_dma_create(dma_addr, 1024);
+}
+
+devinfo_t labx_dma_device = {
+    .probe = &labx_dma_probe,
+    .pass = 1,
+    .compat = (const char * []) {
+        "xlnx,labx-dma-1.00.a",
+        "xlnx,labx-dma-1.01.a",
+        "xlnx,labx-local-audio-1.00.a",

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+        NULL
+    }
+};
+
+/*
+ * LabX ethernet device
+ */
+static void labx_ethernet_probe(void *fdt, int node)
+{
+    uint32_t ethernet_addr = fdt_get_int_from_array(fdt, node, "reg",

0);

Quoted 41 lines
hw/labx_microblaze_devicetree.c new file +41 -0
+    uint32_t host_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 0);
+    uint32_t fifo_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 2);
+    uint32_t phy_irq =
+        fdt_get_int_from_array(fdt, node, "interrupts", 4);
+
+    labx_ethernet_create(&nd_table[++eth_dev_index], ethernet_addr,
+                         irq[host_irq], irq[fifo_irq], irq[phy_irq]);
+}
+
+devinfo_t labx_ethernet_device = {
+    .probe = &labx_ethernet_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-ethernet-1.00.a", NULL }
+};
+
+/*
+ * LabX ptp device
+ */
+static void labx_ptp_probe(void *fdt, int node)
+{
+    uint32_t ptp_addr = fdt_get_int_from_array(fdt, node, "reg", 0);
+
+    labx_ptp_create(ptp_addr);
+}
+
+devinfo_t labx_ptp_device = {
+    .probe = &labx_ptp_probe,
+    .pass = 1,
+    .compat = (const char * []) { "xlnx,labx-ptp-1.00.a", NULL }
+};
+
+/*
+ * Table of available devices
+ */
+devinfo_t *devices[] = {
+    &xilinx_interrupt_controller_device,
+    &flash_device,
+    &xilinx_timer_device,
+    &xilinx_uartlite_device,

hw/labx_microblaze_devicetree.c line 419

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    &xilinx_ethlite_device,
+    &labx_audio_packetizer_device,
+    &labx_audio_depacketizer_device,
+    &labx_dma_device,
+    &labx_ethernet_device,
+    &labx_ptp_device,
+    NULL
+};
+
+static int plb_device_probe(void *fdt, int node, int pass)

QEMU abstacts away interconnect implementations. You should probably also drive off the "simple-bus" compat rather than PLB for machine model compatibility. s/plb/bus/g or something like it.

hw/labx_microblaze_devicetree.c

Quoted 7 lines
hw/labx_microblaze_devicetree.c new file +7 -0
+{
+    devinfo_t **dev = &(devices[0]);
+
+    while (*dev) {
+        const char **compat = &((*dev)->compat[0]);
+        while (*compat) {
+            if (0 == qemu_devtree_node_check_compatible(fdt, node,

*compat)) {

Quoted 19 lines
hw/labx_microblaze_devicetree.c new file +19 -0
+                if (pass == (*dev)->pass) {
+                    printf("Adding a device for node %s\n",
+                           qemu_devtree_get_name(fdt, node, NULL));
+
+                    (*dev)->probe(fdt, node);
+                    return 0;
+                }
+
+                if (pass < (*dev)->pass) {
+                    /* Probe again on the next pass */
+                    return 1;
+                }
+            }
+
+            compat++;
+        }
+
+        dev++;
+    }

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+
+    return 0;
+}
+
+static void plb_bus_probe(void *fdt)
+{
+    int root = qemu_devtree_node_offset(fdt, "/");
+    int plb = qemu_devtree_subnode_offset_namelen(fdt, root, "plb", 3);
+    if (plb > 0) {
+        /* Do multiple passes through the devices. Some have

dependencies

Quoted 38 lines
hw/labx_microblaze_devicetree.c new file +38 -0
+           on others being first */
+        int pass = 0;
+        int again = 0;
+        do {
+            int child = plb;
+            again = 0;
+            do {
+                child = qemu_devtree_next_child_offset(fdt, plb, child);
+                if (child < 0) {
+                    break;
+                }
+
+                again |= plb_device_probe(fdt, child, pass);
+
+            } while (1);
+
+            pass++;
+
+        } while (again);
+    }
+}
+
+static void
+labx_microblaze_init(ram_addr_t ram_size,
+                     const char *boot_device,
+                     const char *kernel_filename,
+                     const char *kernel_cmdline,
+                     const char *initrd_filename,
+                     const char *cpu_model)
+{
+    MemoryRegion *address_space_mem = get_system_memory();
+
+    int kernel_size;
+    int fdt_size;
+    void *fdt = get_device_tree(&fdt_size);
+    target_phys_addr_t ddr_base = get_dram_base(fdt);
+    MemoryRegion *phys_lmb_bram = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);

hw/labx_microblaze_devicetree.c line 503

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+    CPUMBState *env;
+
+    /* init CPUs */
+    if (cpu_model == NULL) {
+        cpu_model = "microblaze";
+    }
+    g_cpu = cpu_mb_init(cpu_model);
+    env = &g_cpu->env;
+
+    env->pvr.regs[10] = 0x0c000000; /* spartan 3a dsp family.  */

I have dtb driven solution for this.

hw/labx_microblaze_devicetree.c

Quoted 3 lines
hw/labx_microblaze_devicetree.c new file +3 -0
+    qemu_register_reset(main_cpu_reset, g_cpu);
+
+    /* Attach emulated BRAM through the LMB. LMB size is not specified

in the

hw/labx_microblaze_devicetree.c

Quoted 5 lines
hw/labx_microblaze_devicetree.c new file +5 -0
+       device-tree but there must be one to hold the vector table. */
+    memory_region_init_ram(phys_lmb_bram, "labx_microblaze.lmb_bram",
+                           LMB_BRAM_SIZE);
+    vmstate_register_ram_global(phys_lmb_bram);
+    memory_region_add_subregion(address_space_mem, 0x00000000,

phys_lmb_bram);

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+
+    memory_region_init_ram(phys_ram, "labx_microblaze.ram", ram_size);
+    vmstate_register_ram_global(phys_ram);
+    memory_region_add_subregion(address_space_mem, ddr_base, phys_ram);
+
+
+    /* Create other devices listed in the device-tree */
+    plb_bus_probe(fdt);
+
+    if (kernel_filename) {

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+        uint64_t entry, low, high;
+        uint32_t base32;
+
+        /* Boots a kernel elf binary.  */
+        kernel_size = load_elf(kernel_filename, NULL, NULL,
+                               &entry, &low, &high,
+                               1, ELF_MACHINE, 0);
+        base32 = entry;
+        if (base32 == 0xc0000000) {
+            kernel_size = load_elf(kernel_filename,

translate_kernel_address,

hw/labx_microblaze_devicetree.c

Quoted 11 lines
hw/labx_microblaze_devicetree.c new file +11 -0
+                                   NULL, &entry, NULL, NULL,
+                                   1, ELF_MACHINE, 0);
+        }
+        /* Always boot into physical ram.  */
+        boot_info.bootstrap_pc = ddr_base + (entry & 0x07ffffff);
+
+        /* If it wasn't an ELF image, try an u-boot image.  */
+        if (kernel_size < 0) {
+            target_phys_addr_t uentry, loadaddr;
+
+            kernel_size = load_uimage(kernel_filename, &uentry,

&loadaddr, 0);

Quoted 9 lines
hw/labx_microblaze_devicetree.c new file +9 -0
+            boot_info.bootstrap_pc = uentry;
+            high = (loadaddr + kernel_size + 3) & ~3;
+        }
+
+        /* Not an ELF image nor an u-boot image, try a RAW image.  */
+        if (kernel_size < 0) {
+            kernel_size = load_image_targphys(kernel_filename, ddr_base,
+                                              ram_size);
+            boot_info.bootstrap_pc = ddr_base;

hw/labx_microblaze_devicetree.c

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+            high = (ddr_base + kernel_size + 3) & ~3;
+        }
+
+        if (initrd_filename) {
+            uint32_t initrd_base = 0x88c00000;
+            uint32_t initrd_size =
+                    load_image_targphys(initrd_filename, initrd_base,
+                                        ram_size - initrd_base);
+            if (initrd_size <= 0) {
+                fprintf(stderr, "qemu: could not load initial ram disk

'%s'\n",

hw/labx_microblaze_devicetree.c

Quoted 12 lines
hw/labx_microblaze_devicetree.c new file +12 -0
+                initrd_filename);
+                exit(1);
+            }
+
+            boot_info.initrd = initrd_base;
+        } else {
+            boot_info.initrd = 0x00000000;
+        }
+
+        boot_info.cmdline = high + 4096;
+        if (kernel_cmdline && strlen(kernel_cmdline)) {
+            pstrcpy_targphys("cmdline", boot_info.cmdline, 256,

kernel_cmdline);

hw/labx_microblaze_devicetree.c line 583

Quoted 10 lines
hw/labx_microblaze_devicetree.c new file +10 -0
+        }
+        /* Provide a device-tree.  */
+        boot_info.fdt = boot_info.cmdline + 4096;
+        labx_microblaze_load_device_tree(boot_info.fdt, ram_size,
+                                   0, 0,
+                                   kernel_cmdline);
+    }
+}
+
+static QEMUMachine labx_microblaze_machine = {

Its a generic machine, so should it be badged as labx?

Quoted 20 lines
hw/labx_microblaze_devicetree.c new file +14 -0
+    .name = "labx-microblaze-devicetree",
+    .desc = "Microblaze design based on the peripherals specified "
+            "in the device-tree.",
+    .init = labx_microblaze_init,
+    .is_default = 1
 
+};
+
+static void labx_microblaze_machine_init(void)
+{
+    qemu_register_machine(&labx_microblaze_machine);
+}
+
+machine_init(labx_microblaze_machine_init);
+
hw/microblaze/Makefile.objs +0 -0
index 274d2c5..632cd60 100644

hw/microblaze/Makefile.objs line 1

Quoted 2 lines
hw/microblaze/Makefile.objs +1 -1
@@ -1,4 +1,5 @@
-obj-y = petalogix_s3adsp1800_mmu.o
+obj-y  = labx_microblaze_devicetree.o

obj-y += labx_microblaze_devicetree.(CONFIG-FDT)

Quoted 4 lines
hw/microblaze/Makefile.objs +1 -0
+obj-y += petalogix_s3adsp1800_mmu.o
 obj-y += petalogix_ml605_mmu.o
 obj-y += microblaze_boot.o
 
#29 lore raw

PATCH 9/9 xilinx_timer: Fix a compile error if debug messages are enabled.

In reply to [email protected]

From: Chris Wulff <[email protected]>

---
 hw/xilinx_timer.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/xilinx_timer.c +2 -1
index b562bd0..18f354b 100644
@@ -24,6 +24,7 @@
 
 #include "sysbus.h"
 #include "ptimer.h"
+#include "qemu-log.h"
 
 #define D(x)
 
@@ -189,7 +190,7 @@ static void timer_hit(void *opaque)
 {
     struct xlx_timer *xt = opaque;
     struct timerblock *t = xt->parent;
-    D(printf("%s %d\n", __func__, timer));
+    D(printf("%s %d\n", __func__, xt->nr));
     xt->regs[R_TCSR] |= TCSR_TINT;
 
     if (xt->regs[R_TCSR] & TCSR_ARHT)
Signature · 1 line
1.7.9.5
Reviewed-by on v1 9/9 Applied
#30 lore raw

Re: [Qemu-devel] [PATCH 9/9] xilinx_timer: Fix a compile error if debug messages are enabled.

Queued to microblaze-devs.

Thanks,
Peter

Quoted 3 lines · On Mon, Sep 10, 2012 at 10:20 AM, <[email protected]> wrote:

From: Chris Wulff <[email protected]>

Quoted 29 lines
---
 hw/xilinx_timer.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/xilinx_timer.c +2 -2
index b562bd0..18f354b 100644
@@ -24,6 +24,7 @@
 
 #include "sysbus.h"
 #include "ptimer.h"
+#include "qemu-log.h"
 
 #define D(x)
 
@@ -189,7 +190,7 @@ static void timer_hit(void *opaque)
 {
     struct xlx_timer *xt = opaque;
     struct timerblock *t = xt->parent;
-    D(printf("%s %d\n", __func__, timer));
+    D(printf("%s %d\n", __func__, xt->nr));
     xt->regs[R_TCSR] |= TCSR_TINT;
 
     if (xt->regs[R_TCSR] & TCSR_ARHT)
--

1.7.9.5

#31 lore raw
In reply to [email protected]

Hi Chris,

Quoted 10 lines · On Sun, 2012-09-09 at 20:19 -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

This set of patches adds support for the Altera NiosII soft-core processor, along with the hardware drivers needed to boot linux. I have tested the soft-mmu target and can boot to a shell with uClinux and a linux 3.0 kernel.

Also included is a MicroBlaze configuration that is dynamically created from a device tree file that matches the NiosII one. These assign QEMU device emulation to the address space where specified in the device tree file.

We are currently maintaining a system out of tree that does exactly this. Ill send patches shortly. We need to compare and converge implementations.

We sent this to the mailing list about a year ago:

http://lists.gnu.org/archive/html/qemu-devel/2011-08/msg02953.html

We currently in an effort to make this machine independent, rather than having to put dtb glue code in machine models.

Regards,
Peter

Quoted 110 lines

Chris Wulff (9):
NiosII: Add support for the Altera NiosII soft-core CPU.
NiosII: Disassembly of NiosII instructions ported from GDB.
Altera: Add support for Altera devices required to boot linux on
NiosII.
LabX: Support for some Lab X FPGA devices.
FDT: Add additional access methods for array types and walking
children.
NiosII: Build system and documentation integration.
NiosII: Add a config that is dynamically set up by a device tree
file.
MicroBlaze: Add a config that is dynamically set up by a device tree
file.
xilinx_timer: Fix a compile error if debug messages are enabled.

 MAINTAINERS                            |    5 +
 Makefile.objs                          |    2 +
 arch_init.c                            |    2 +
 arch_init.h                            |    1 +
 configure                              |   11 +
 cpu-exec.c                             |   12 +-
 default-configs/microblaze-softmmu.mak |    1 +
 default-configs/nios2-linux-user.mak   |    1 +
 default-configs/nios2-softmmu.mak      |    6 +
 device_tree.c                          |   88 ++
 device_tree.h                          |   18 +
 dis-asm.h                              |    3 +
 disas.c                                |    3 +
 elf.h                                  |    2 +
 exec.c                                 |    6 +-
 gdbstub.c                              |   29 +
 hw/Makefile.objs                       |   12 +
 hw/altera.h                            |   34 +
 hw/altera_timer.c                      |  198 +++
 hw/altera_uart.c                       |  218 ++++
 hw/altera_vic.c                        |  195 +++
 hw/labx_audio_depacketizer.c           |  409 ++++++
 hw/labx_audio_packetizer.c             |  397 ++++++
 hw/labx_devices.h                      |  103 ++
 hw/labx_dma.c                          |  241 ++++
 hw/labx_ethernet.c                     |  615 +++++++++
 hw/labx_microblaze_devicetree.c        |  597 +++++++++
 hw/labx_nios2_devicetree.c             |  665 ++++++++++
 hw/labx_ptp.c                          |  291 +++++
 hw/microblaze/Makefile.objs            |    3 +-
 hw/nios2.h                             |   46 +
 hw/nios2/Makefile.objs                 |    6 +
 hw/nios2_pic_cpu.c                     |   48 +
 hw/xilinx_timer.c                      |    3 +-
 linux-user/elfload.c                   |   29 +
 monitor.c                              |    5 +-
 nios2-dis.c                            |  417 +++++++
 nios2-isa.h                            | 2150 ++++++++++++++++++++++++++++++++
 nios2-opc.c                            |  540 ++++++++
 nios2.h                                |  366 ++++++
 qapi-schema.json                       |    2 +-
 qemu-doc.texi                          |    3 +
 target-nios2/Makefile.objs             |    5 +
 target-nios2/altera_iic.c              |  100 ++
 target-nios2/cpu-qom.h                 |   69 +
 target-nios2/cpu.c                     |   83 ++
 target-nios2/cpu.h                     |  259 ++++
 target-nios2/exec.h                    |   60 +
 target-nios2/helper.c                  |  291 +++++
 target-nios2/helper.h                  |   45 +
 target-nios2/instruction.c             | 1463 ++++++++++++++++++++++
 target-nios2/instruction.h             |  290 +++++
 target-nios2/machine.c                 |   33 +
 target-nios2/mmu.c                     |  273 ++++
 target-nios2/mmu.h                     |   49 +
 target-nios2/op_helper.c               |  125 ++
 target-nios2/translate.c               |  252 ++++
 57 files changed, 11172 insertions(+), 8 deletions(-)
 create mode 100644 default-configs/nios2-linux-user.mak
 create mode 100644 default-configs/nios2-softmmu.mak
 create mode 100644 hw/altera.h
 create mode 100644 hw/altera_timer.c
 create mode 100644 hw/altera_uart.c
 create mode 100644 hw/altera_vic.c
 create mode 100644 hw/labx_audio_depacketizer.c
 create mode 100644 hw/labx_audio_packetizer.c
 create mode 100644 hw/labx_devices.h
 create mode 100644 hw/labx_dma.c
 create mode 100644 hw/labx_ethernet.c
 create mode 100644 hw/labx_microblaze_devicetree.c
 create mode 100644 hw/labx_nios2_devicetree.c
 create mode 100644 hw/labx_ptp.c
 create mode 100644 hw/nios2.h
 create mode 100644 hw/nios2/Makefile.objs
 create mode 100644 hw/nios2_pic_cpu.c
 create mode 100644 nios2-dis.c
 create mode 100644 nios2-isa.h
 create mode 100644 nios2-opc.c
 create mode 100644 nios2.h
 create mode 100644 target-nios2/Makefile.objs
 create mode 100644 target-nios2/altera_iic.c
 create mode 100644 target-nios2/cpu-qom.h
 create mode 100644 target-nios2/cpu.c
 create mode 100644 target-nios2/cpu.h
 create mode 100644 target-nios2/exec.h
 create mode 100644 target-nios2/helper.c
 create mode 100644 target-nios2/helper.h
 create mode 100644 target-nios2/instruction.c
 create mode 100644 target-nios2/instruction.h
 create mode 100644 target-nios2/machine.c
 create mode 100644 target-nios2/mmu.c
 create mode 100644 target-nios2/mmu.h
 create mode 100644 target-nios2/op_helper.c
 create mode 100644 target-nios2/translate.c