RISC-V: Add more machine memory
Subsystems (2)
v3 cover RISC-V: Add more machine memory
This series aims to improve the use of QEMU for developing boot code. It does a few things:
- sifive_u machine:
- Adds a chunk of memory in the Flash area. This allows boot loaders to use this memory. I can't find details on the QSPI flash used on the real board, so this is the best bet at the moment.
- Adds a chunk of memory in the L2-LIM area. This is actualy the L2 cache and should shrink as the L2 cache is enalbed. Unfortunatley I don't see a nice way to shrink this memory.
- Adds a property that allows users to specify if QEMU should jump to flash or DRAM after the ROM code.
- virt machine:
- Add the pflash_cfi01 flash device. This is based on the ARM virt board implementation
- Adjusts QEMU to jump to the flash if a user has speciefied any pflash.
Both machines have been tested with oreboot, but this should also help the coreboot developers.
v3:
- Use the start_addr variable instead of editing reset vector
- Fix function names
v2:
- Address comments
- Fixup addresses
- Don't use macro for machine definition of RISC-V virt machine
Alistair Francis (7):
riscv/sifive_u: Add L2-LIM cache memory
riscv/sifive_u: Add QSPI memory region
riscv/sifive_u: Manually define the machine
riscv/sifive_u: Add the start-in-flash property
riscv/virt: Manually define the machine
riscv/virt: Add the PFlash CFI01 device
riscv/virt: Jump to pflash if specified
hw/riscv/Kconfig | 1 + hw/riscv/sifive_u.c | 98 ++++++++++++++++++++++++---- hw/riscv/virt.c | 127 ++++++++++++++++++++++++++++++++++-- include/hw/riscv/sifive_u.h | 11 +++- include/hw/riscv/virt.h | 10 ++- 5 files changed, 224 insertions(+), 23 deletions(-)
Signature · 1 line
2.23.0
PATCH v3 1/7 riscv/sifive_u: Add L2-LIM cache memory
On reset only a single L2 cache way is enabled, the others are exposed as memory that can be used by early boot firmware. This L2 region is generally disabled using the WayEnable register at a later stage in the boot process. To allow firmware to target QEMU and the HiFive Unleashed let's add the L2 LIM (LooselyIntegrated Memory).
Ideally we would want to adjust the size of this chunk of memory as the L2 Cache Controller WayEnable register is incremented. Unfortunately I don't see a nice way to handle reducing or blocking out the L2 LIM while still allowing it be re returned to all enabled from a reset.
--- hw/riscv/sifive_u.c | 16 ++++++++++++++++ include/hw/riscv/sifive_u.h | 1 + 2 files changed, 17 insertions(+)
hw/riscv/sifive_u.c +16 -0
@@ -65,6 +65,7 @@ static const struct MemmapEntry { [SIFIVE_U_DEBUG] = { 0x0, 0x100 }, [SIFIVE_U_MROM] = { 0x1000, 0x11000 }, [SIFIVE_U_CLINT] = { 0x2000000, 0x10000 }, + [SIFIVE_U_L2LIM] = { 0x8000000, 0x2000000 }, [SIFIVE_U_PLIC] = { 0xc000000, 0x4000000 }, [SIFIVE_U_PRCI] = { 0x10000000, 0x1000 }, [SIFIVE_U_UART0] = { 0x10010000, 0x1000 },
@@ -431,6 +432,7 @@ static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp)const struct MemmapEntry *memmap = sifive_u_memmap;MemoryRegion *system_memory =get_system_memory ();MemoryRegion *mask_rom =g_new (MemoryRegion,1 ); +MemoryRegion *l2lim_mem =g_new (MemoryRegion,1 );qemu_irq plic_gpios[SIFIVE_U_PLIC_NUM_SOURCES ];char *plic_hart_config;size_t plic_hart_config_len;
@@ -459,6 +461,20 @@ static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp)memory_region_add_subregion (system_memory, memmap[SIFIVE_U_MROM ].base, mask_rom); +/* +* Add L2-LIM at reset size. +* This should be reduced in size as the L2 Cache Controller WayEnable +* register is incremented. Unfortunately I don't see a nice (or any) way +* to handle reducing or blocking out the L2 LIM while still allowing it +* be re returned to all enabled after a reset. For the time being, just +* leave it enabled all the time. This won't break anything, but will be +* too generous to misbehaving guests. +*/ +memory_region_init_ram (l2lim_mem,NULL ,"riscv.sifive.u.l2lim" , + memmap[SIFIVE_U_L2LIM ].size, &error_fatal); +memory_region_add_subregion (system_memory, memmap[SIFIVE_U_L2LIM ].base, + l2lim_mem); +/* create PLIC hart topology configuration string */ plic_hart_config_len = (strlen (SIFIVE_U_PLIC_HART_CONFIG ) +1 ) * ms->smp.cpus;
include/hw/riscv/sifive_u.h +1 -0
@@ -58,6 +58,7 @@ enum {SIFIVE_U_DEBUG ,SIFIVE_U_MROM ,SIFIVE_U_CLINT , +SIFIVE_U_L2LIM ,SIFIVE_U_PLIC ,SIFIVE_U_PRCI , SIFIVE_U_UART0,
Signature · 1 line
2.23.0
PATCH v3 2/7 riscv/sifive_u: Add QSPI memory region
In reply to Alistair FrancisThe HiFive Unleashed uses is25wp256 SPI NOR flash. There is currently no model of this in QEMU, so to allow boot firmware developers to use QEMU to target the Unleashed let's add a chunk of memory to represent the QSPI0 memory mapped flash. This can be targeted using QEMU's -device loader command line option.
In the future we can look at adding a model for the is25wp256 flash.
--- hw/riscv/sifive_u.c | 8 ++++++++ include/hw/riscv/sifive_u.h | 1 + 2 files changed, 9 insertions(+)
hw/riscv/sifive_u.c +8 -0
@@ -71,6 +71,7 @@ static const struct MemmapEntry { [SIFIVE_U_UART0] = { 0x10010000, 0x1000 }, [SIFIVE_U_UART1] = { 0x10011000, 0x1000 }, [SIFIVE_U_OTP] = { 0x10070000, 0x1000 }, + [SIFIVE_U_FLASH0] = { 0x20000000, 0x10000000 }, [SIFIVE_U_DRAM] = { 0x80000000, 0x0 }, [SIFIVE_U_GEM] = { 0x10090000, 0x2000 }, [SIFIVE_U_GEM_MGMT] = { 0x100a0000, 0x1000 },
@@ -313,6 +314,7 @@ static void riscv_sifive_u_init(MachineState *machine)SiFiveUState *s =g_new0 (SiFiveUState,1 );MemoryRegion *system_memory =get_system_memory ();MemoryRegion *main_mem =g_new (MemoryRegion,1 ); +MemoryRegion *flash0 =g_new (MemoryRegion,1 );int i;/* Initialize SoC */
@@ -328,6 +330,12 @@ static void riscv_sifive_u_init(MachineState *machine)memory_region_add_subregion (system_memory, memmap[SIFIVE_U_DRAM ].base, main_mem); +/* register QSPI0 Flash */ +memory_region_init_ram (flash0,NULL ,"riscv.sifive.u.flash0" , + memmap[SIFIVE_U_FLASH0 ].size, &error_fatal); +memory_region_add_subregion (system_memory, memmap[SIFIVE_U_FLASH0 ].base, + flash0); +/* create device tree */ create_fdt (s, memmap, machine->ram_size, machine->kernel_cmdline);
include/hw/riscv/sifive_u.h +1 -0
@@ -64,6 +64,7 @@ enum {SIFIVE_U_UART0 ,SIFIVE_U_UART1 ,SIFIVE_U_OTP , +SIFIVE_U_FLASH0 ,SIFIVE_U_DRAM ,SIFIVE_U_GEM ,SIFIVE_U_GEM_MGMT
Signature · 1 line
2.23.0
PATCH v3 3/7 riscv/sifive_u: Manually define the machine
In reply to Alistair FrancisInstead of using the DEFINE_MACHINE() macro to define the machine let's do it manually. This allows us to specify machine properties.
This patch is no functional change.
--- hw/riscv/sifive_u.c | 44 ++++++++++++++++++++++++++----------- include/hw/riscv/sifive_u.h | 7 +++++- 2 files changed, 37 insertions(+), 14 deletions(-)
hw/riscv/sifive_u.c +31 -13
@@ -310,8 +310,7 @@ static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,static void riscv_sifive_u_init (MachineState *machine) {const struct MemmapEntry *memmap = sifive_u_memmap; - -SiFiveUState *s =g_new0 (SiFiveUState,1 ); +SiFiveUState *s =RISCV_U_MACHINE (machine);MemoryRegion *system_memory =get_system_memory ();MemoryRegion *main_mem =g_new (MemoryRegion,1 );MemoryRegion *flash0 =g_new (MemoryRegion,1 );
@@ -433,6 +432,10 @@ static void riscv_sifive_u_soc_init(Object *obj) TYPE_CADENCE_GEM); } +static void riscv_sifive_u_machine_instance_init (Object *obj) +{ +} +static void riscv_sifive_u_soc_realize (DeviceState *dev,Error **errp) {MachineState *ms =MACHINE (qdev_get_machine ());
@@ -546,17 +549,6 @@ static void riscv_sifive_u_soc_realize(DeviceState *dev, Error **errp) memmap[SIFIVE_U_GEM_MGMT].base, memmap[SIFIVE_U_GEM_MGMT].size); } -static void riscv_sifive_u_machine_init (MachineClass *mc) -{ - mc->desc ="RISC-V Board compatible with SiFive U SDK" ; - mc->init = riscv_sifive_u_init; - mc->max_cpus =SIFIVE_U_MANAGEMENT_CPU_COUNT +SIFIVE_U_COMPUTE_CPU_COUNT ; - mc->min_cpus =SIFIVE_U_MANAGEMENT_CPU_COUNT +1 ; - mc->default_cpus = mc->min_cpus; -} - -DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init) -static void riscv_sifive_u_soc_class_init (ObjectClass *oc,void *data) {DeviceClass *dc =DEVICE_CLASS (oc);
@@ -580,3 +572,29 @@ static void riscv_sifive_u_soc_register_types(void) }type_init (riscv_sifive_u_soc_register_types) + +static void riscv_sifive_u_machine_class_init(ObjectClass *oc, void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); + + mc->desc ="RISC-V Board compatible with SiFive U SDK" ; + mc->init = riscv_sifive_u_init; + mc->max_cpus =SIFIVE_U_MANAGEMENT_CPU_COUNT +SIFIVE_U_COMPUTE_CPU_COUNT ; + mc->min_cpus =SIFIVE_U_MANAGEMENT_CPU_COUNT +1 ; + mc->default_cpus = mc->min_cpus; +} + +static const TypeInfo riscv_sifive_u_machine_typeinfo = { + .name =MACHINE_TYPE_NAME ("sifive_u" ), + .parent =TYPE_MACHINE , + .class_init = riscv_sifive_u_machine_class_init, + .instance_init = riscv_sifive_u_machine_instance_init, + .instance_size =sizeof (SiFiveUState), +}; + +static void riscv_sifive_u_machine_init_register_types (void ) +{ +type_register_static (&riscv_sifive_u_machine_typeinfo); +} + +type_init (riscv_sifive_u_machine_init_register_types)
include/hw/riscv/sifive_u.h +6 -1
@@ -44,12 +44,17 @@ typedef struct SiFiveUSoCState {CadenceGEMState gem; } SiFiveUSoCState; +#define TYPE_RISCV_U_MACHINE MACHINE_TYPE_NAME("sifive_u") +#define RISCV_U_MACHINE (obj) \ + OBJECT_CHECK(SiFiveUState, (obj), TYPE_RISCV_U_MACHINE) +typedef struct SiFiveUState {/*< private >*/ -SysBusDevice parent_obj; +MachineState parent_obj;/*< public >*/ SiFiveUSoCState soc; +void *fdt;int fdt_size; }SiFiveUState ;
Signature · 1 line
2.23.0
PATCH v3 4/7 riscv/sifive_u: Add the start-in-flash property
In reply to Alistair FrancisAdd a property that when set to true QEMU will jump from the ROM code to the start of flash memory instead of DRAM which is the default behaviour.
---
v3:
- Use the start_addr variable instead of editing reset vector
- Fix function names
hw/riscv/sifive_u.c | 30 +++++++++++++++++++++++++++++- include/hw/riscv/sifive_u.h | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-)
hw/riscv/sifive_u.c +29 -1
@@ -314,6 +314,7 @@ static void riscv_sifive_u_init(MachineState *machine)MemoryRegion *system_memory =get_system_memory ();MemoryRegion *main_mem =g_new (MemoryRegion,1 );MemoryRegion *flash0 =g_new (MemoryRegion,1 ); +target_ulong start_addr = memmap[SIFIVE_U_DRAM ].base;int i;/* Initialize SoC */
@@ -356,6 +357,10 @@ static void riscv_sifive_u_init(MachineState *machine) } } +if (s->start_in_flash) { + start_addr = memmap[SIFIVE_U_FLASH0 ].base; + } +/* reset vector */ uint32_t reset_vec[8 ] = {0x00000297 ,/* 1: auipc t0, %pcrel_hi(dtb) */
@@ -368,7 +373,7 @@ static void riscv_sifive_u_init(MachineState *machine)#endif 0x00028067 ,/* jr t0 */ 0x00000000 , - memmap[SIFIVE_U_DRAM ].base,/* start: .dword DRAM_BASE */ + start_addr,/* start: .dword */ 0x00000000, /* dtb: */ };
@@ -432,8 +437,31 @@ static void riscv_sifive_u_soc_init(Object *obj) TYPE_CADENCE_GEM); } +static bool sifive_u_get_start_in_flash (Object *obj,Error **errp) +{ +SiFiveUState *s =RISCV_U_MACHINE (obj); + +return s->start_in_flash; +} + +static void sifive_u_set_start_in_flash (Object *obj,bool value,Error **errp) +{ +SiFiveUState *s =RISCV_U_MACHINE (obj); + + s->start_in_flash = value; +} +static void riscv_sifive_u_machine_instance_init (Object *obj) { +SiFiveUState *s =RISCV_U_MACHINE (obj); + + s->start_in_flash = false; +object_property_add_bool (obj,"start-in-flash" , sifive_u_get_start_in_flash, + sifive_u_set_start_in_flash,NULL ); +object_property_set_description (obj,"start-in-flash" , +"Set on to tell QEMU's ROM to jump to " \ +"flash. Otherwise QEMU will jump to DRAM" , +NULL ); }static void riscv_sifive_u_soc_realize (DeviceState *dev,Error **errp)
include/hw/riscv/sifive_u.h +2 -0
@@ -57,6 +57,8 @@ typedef struct SiFiveUState {void *fdt;int fdt_size; + +bool start_in_flash; } SiFiveUState;enum {
Signature · 1 line
2.23.0
Re: [PATCH v3 4/7] riscv/sifive_u: Add the start-in-flash property
Quoted 6 lines · Alistair Francis
Add a property that when set to true QEMU will jump from the ROM code to the start of flash memory instead of DRAM which is the default behaviour.
Signed-off-by Alistair Francis <[email protected]>
Quoted 9 lines
---
v3:
- Use thestart_addrvariable instead of editing reset vector
- Fix function nameshw/riscv/sifive_u.c | 30 +++++++++++++++++++++++++++++- include/hw/riscv/sifive_u.h | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-)
PATCH v3 5/7 riscv/virt: Manually define the machine
In reply to Alistair FrancisInstead of using the DEFINE_MACHINE() macro to define the machine let's do it manually. This allows us to use the machine object to create RISCVVirtState. This is required to add children and aliases to the machine.
This patch is no functional change.
--- hw/riscv/virt.c | 30 ++++++++++++++++++++++++------ include/hw/riscv/virt.h | 7 ++++++- 2 files changed, 30 insertions(+), 7 deletions(-)
hw/riscv/virt.c +24 -6
@@ -362,8 +362,7 @@ static inline DeviceState *gpex_pcie_init(MemoryRegion *sys_mem,static void riscv_virt_board_init (MachineState *machine) {const struct MemmapEntry *memmap = virt_memmap; - -RISCVVirtState *s =g_new0 (RISCVVirtState,1 ); +RISCVVirtState *s =RISCV_VIRT_MACHINE (machine);MemoryRegion *system_memory =get_system_memory ();MemoryRegion *main_mem =g_new (MemoryRegion,1 );MemoryRegion *mask_rom =g_new (MemoryRegion,1 );
@@ -499,12 +498,31 @@ static void riscv_virt_board_init(MachineState *machine)g_free (plic_hart_config); } -static void riscv_virt_board_machine_init (MachineClass *mc) +static void riscv_virt_machine_instance_init (Object *obj) { - mc->desc ="RISC-V VirtIO Board (Privileged ISA v1.10)" ; +} + +static void riscv_virt_machine_class_init (ObjectClass *oc,void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); + + mc->desc ="RISC-V VirtIO board" ; mc->init = riscv_virt_board_init; - mc->max_cpus =8 ;/* hardcoded limit in BBL */ + mc->max_cpus =8 ; mc->default_cpu_type =VIRT_CPU ; } -DEFINE_MACHINE ("virt" , riscv_virt_board_machine_init) +static const TypeInfo riscv_virt_machine_typeinfo = { + .name =MACHINE_TYPE_NAME ("virt" ), + .parent =TYPE_MACHINE , + .class_init = riscv_virt_machine_class_init, + .instance_init = riscv_virt_machine_instance_init, + .instance_size =sizeof (RISCVVirtState), +}; + +static void riscv_virt_machine_init_register_types (void ) +{ +type_register_static (&riscv_virt_machine_typeinfo); +} + +type_init (riscv_virt_machine_init_register_types)
include/hw/riscv/virt.h +6 -1
@@ -22,13 +22,18 @@#include "hw/riscv/riscv_hart.h" #include "hw/sysbus.h" +#define TYPE_RISCV_VIRT_MACHINE MACHINE_TYPE_NAME("virt") +#define RISCV_VIRT_MACHINE (obj) \ + OBJECT_CHECK(RISCVVirtState, (obj), TYPE_RISCV_VIRT_MACHINE) +typedef struct {/*< private >*/ -SysBusDevice parent_obj; +MachineState parent;/*< public >*/ RISCVHartArrayState soc;DeviceState *plic; +void *fdt;int fdt_size; }RISCVVirtState ;
Signature · 1 line
2.23.0
PATCH v3 6/7 riscv/virt: Add the PFlash CFI01 device
In reply to Alistair FrancisAdd the CFI01 PFlash to the RISC-V virt board. This is the same PFlash from the ARM Virt board and the implementation is based on the ARM Virt board. This allows users to specify flash files from the command line.
--- hw/riscv/Kconfig | 1 + hw/riscv/virt.c | 86 +++++++++++++++++++++++++++++++++++++++++ include/hw/riscv/virt.h | 3 ++ 3 files changed, 90 insertions(+)
hw/riscv/Kconfig +1 -0
@@ -36,4 +36,5 @@ config RISCV_VIRT select SERIAL select VIRTIO_MMIO select PCI_EXPRESS_GENERIC_BRIDGE + select PFLASH_CFI01 select SIFIVE
hw/riscv/virt.c +86 -0
122 lines not on this page · raw message
include/hw/riscv/virt.h +3 -0
@@ -21,6 +21,7 @@#include "hw/riscv/riscv_hart.h" #include "hw/sysbus.h" +#include "hw/block/flash.h" #define TYPE_RISCV_VIRT_MACHINE MACHINE_TYPE_NAME("virt")#define RISCV_VIRT_MACHINE (obj) \
@@ -33,6 +34,7 @@ typedef struct {/*< public >*/ RISCVHartArrayState soc;DeviceState *plic; +PFlashCFI01 *flash[2 ];void *fdt;int fdt_size;
@@ -46,6 +48,7 @@ enum {VIRT_PLIC ,VIRT_UART0 ,VIRT_VIRTIO , +VIRT_FLASH ,VIRT_DRAM ,VIRT_PCIE_MMIO , VIRT_PCIE_PIO,
Signature · 1 line
2.23.0
PATCH v3 7/7 riscv/virt: Jump to pflash if specified
In reply to Alistair FrancisIf the user supplied pflash to QEMU then change the reset code to jump to the pflash base address instead of the DRAM base address.
--- hw/riscv/virt.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
hw/riscv/virt.c +10 -1
@@ -445,6 +445,7 @@ static void riscv_virt_board_init(MachineState *machine)MemoryRegion *mask_rom =g_new (MemoryRegion,1 );char *plic_hart_config;size_t plic_hart_config_len; +target_ulong start_addr = memmap[VIRT_DRAM ].base;int i;unsigned int smp_cpus = machine->smp.cpus;
@@ -491,6 +492,14 @@ static void riscv_virt_board_init(MachineState *machine) } } +if (drive_get (IF_PFLASH ,0 ,0 )) { +/* +* Pflash was supplied, let's overwrite the address we jump to after +* reset to the base of the flash. +*/ + start_addr = virt_memmap[VIRT_FLASH ].base; + } +/* reset vector */ uint32_t reset_vec[8 ] = {0x00000297 ,/* 1: auipc t0, %pcrel_hi(dtb) */
@@ -503,7 +512,7 @@ static void riscv_virt_board_init(MachineState *machine)#endif 0x00028067 ,/* jr t0 */ 0x00000000 , - memmap[VIRT_DRAM ].base,/* start: .dword memmap[VIRT_DRAM].base */ + start_addr,/* start: .dword */ 0x00000000, /* dtb: */ };
Signature · 1 line
2.23.0