Skip to contentSkip to conversation

arm_boot/vexpress-a15: Support >4GB of RAM

PATCH Superseded Peter Maydell 5 participants 27 messages Jul 5, 2012 – Jul 13, 2012 Arm/versatile express platform

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover arm_boot/vexpress-a15: Support >4GB of RAM

This patchset adds support for booting with >4GB of RAM on the Versatile Express Cortex-A15 model. There are some caveats:
* you need an LPAE A15 kernel
* you need to be booting with device tree
* your device tree blob needs to specify #address-cells and #size-cells as 2 (so addresses and sizes are 64 bit), which means you'll need to tweak the stock kernel dtb
* you need a minor kernel patch which stops the kernel throwing away the high 32 bits of the RAM size: http://patches.linaro.org/9856/
* you need a 64 bit host, obviously

This patchset sits on top of the LPAE patches and that bugfix patch for 64 bit cp register writes.

Peter: I've cc'd you as device-tree.[ch] maintainer since I've added some new helper functions there.

Peter Maydell (6):
hw/arm_boot.c: Make ram_size a target_phys_addr_t hw/arm_boot.c: Consistently use ram_size from arm_boot_info struct hw/arm_boot.c: Check for RAM sizes exceeding ATAGS capacity
device_tree: Add support for reading device tree properties hw/arm_boot.c: Support DTBs which use 64 bit addresses hw/vexpress.c: Allow >4GB of RAM for Cortex-A15 daughterboard

 device_tree.c |   30 ++++++++++++++++++++++++++++++
 device_tree.h |    4 ++++
 hw/arm-misc.h |    2 +-
 hw/arm_boot.c |   46 +++++++++++++++++++++++++++++++++++++++++-----
 hw/vexpress.c |   13 ++++++++++---
 5 files changed, 86 insertions(+), 9 deletions(-)
#1 lore raw

PATCH 1/6 hw/arm_boot.c: Make ram_size a target_phys_addr_t

Make the RAM size in arm_boot_info a target_phys_addr_t so it can express RAM sizes up to the limit imposed by the physical address size.

---
 hw/arm-misc.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/arm-misc.h +1 -1
index 1f96229..c313027 100644
@@ -25,7 +25,7 @@ qemu_irq *armv7m_init(MemoryRegion *address_space_mem,
 
 /* arm_boot.c */
 struct arm_boot_info {
-    int ram_size;
+    target_phys_addr_t ram_size;
     const char *kernel_filename;
     const char *kernel_cmdline;
     const char *initrd_filename;
Signature · 1 line
1.7.1
Reviewed-by on v1 1/6
#2 lore raw

Re: [Qemu-devel] [PATCH 1/6] hw/arm_boot.c: Make ram_size a target_phys_addr_t

Quoted 5 lines · Peter Maydell

Make the RAM size in arm_boot_info a target_phys_addr_t so it can express RAM sizes up to the limit imposed by the physical address size.

Quoted 20 lines
---
 hw/arm-misc.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/arm-misc.h +1 -2
index 1f96229..c313027 100644
@@ -25,7 +25,7 @@ qemu_irq *armv7m_init(MemoryRegion *address_space_mem,
 
 /* arm_boot.c */
 struct arm_boot_info {
-    int ram_size;
+    target_phys_addr_t ram_size;
     const char *kernel_filename;
     const char *kernel_cmdline;
     const char *initrd_filename;
--

1.7.1

#3 lore raw

Re: [Qemu-devel] [PATCH 1/6] hw/arm_boot.c: Make ram_size a target_phys_addr_t

In reply to Peter Maydell

Am 05.07.2012 19:00, schrieb Peter Maydell:

Quoted 14 lines

Make the RAM size in arm_boot_info a target_phys_addr_t so it can express RAM sizes up to the limit imposed by the physical address size.

---
 hw/arm-misc.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/arm-misc.h +0 -0
index 1f96229..c313027 100644

hw/arm-misc.h qemu_irq *armv7m_init(MemoryRegion *address_space_mem, · line 31

Quoted 8 lines
hw/arm-misc.h +1 -1
@@ -25,7 +25,7 @@ qemu_irq *armv7m_init(MemoryRegion *address_space_mem,
 
 /* arm_boot.c */
 struct arm_boot_info {
-    int ram_size;
+    target_phys_addr_t ram_size;
     const char *kernel_filename;
     const char *kernel_cmdline;
     const char *initrd_filename;

Didn't we conclude in lengthy and emotional discussions that int64_t was the best compromise to solve the highbank and pseries image loading issues?

What I still dislike about target_phys_addr_t is that "ram_size" is not an address but a size.

Andreas

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

Re: [Qemu-devel] [PATCH 1/6] hw/arm_boot.c: Make ram_size a target_phys_addr_t

Quoted 27 lines · Andreas Färber

Am 05.07.2012 19:00, schrieb Peter Maydell:

Quoted 14 lines

Make the RAM size in arm_boot_info a target_phys_addr_t so it can express RAM sizes up to the limit imposed by the physical address size.

---
hw/arm-misc.h | 2 +-

1 files changed, 1 insertions(+), 1 deletions(-)
hw/arm-misc.h +0 -0
index 1f96229..c313027 100644

hw/arm-misc.h qemu_irq *armv7m_init(MemoryRegion *address_space_mem, · line 31

Quoted 8 lines
hw/arm-misc.h +1 -1
@@ -25,7 +25,7 @@ qemu_irq *armv7m_init(MemoryRegion *address_space_mem,
 
 /* arm_boot.c */
 struct arm_boot_info {
-    int ram_size;
+    target_phys_addr_t ram_size;
    const char *kernel_filename;
    const char *kernel_cmdline;
    const char *initrd_filename;

Didn't we conclude in lengthy and emotional discussions that int64_t was the best compromise to solve the highbank and pseries image loading issues?

Quoted 2 lines

What I still dislike about target_phys_addr_t is that "ram_size" is not an address but a size.

But isn't MAX(size) always defined to be smaller than or equal to MAX(addr)? So target_phys_addr_t is _always_ a type that is big enough to hold the information.

Alex

#5 lore raw

Re: [Qemu-devel] [PATCH 1/6] hw/arm_boot.c: Make ram_size a target_phys_addr_t

Am 06.07.2012 15:54, schrieb Alexander Graf:

Quoted 30 lines · Andreas Färber
Quoted 28 lines

Am 05.07.2012 19:00, schrieb Peter Maydell:

Quoted 14 lines

Make the RAM size in arm_boot_info a target_phys_addr_t so it can express RAM sizes up to the limit imposed by the physical address size.

---
hw/arm-misc.h | 2 +-

1 files changed, 1 insertions(+), 1 deletions(-)
hw/arm-misc.h +0 -0
index 1f96229..c313027 100644

hw/arm-misc.h qemu_irq *armv7m_init(MemoryRegion *address_space_mem, · line 31

Quoted 8 lines
hw/arm-misc.h +1 -1
@@ -25,7 +25,7 @@ qemu_irq *armv7m_init(MemoryRegion *address_space_mem,
 
 /* arm_boot.c */
 struct arm_boot_info {
-    int ram_size;
+    target_phys_addr_t ram_size;
    const char *kernel_filename;
    const char *kernel_cmdline;
    const char *initrd_filename;

Didn't we conclude in lengthy and emotional discussions that int64_t was the best compromise to solve the highbank and pseries image loading issues?

Quoted 4 lines
Quoted 2 lines

What I still dislike about target_phys_addr_t is that "ram_size" is not an address but a size.

But isn't MAX(size) always defined to be smaller than or equal to MAX(addr)? So target_phys_addr_t is _always_ a type that is big enough to hold the information.

I'm not disputing that. If you do
typedef target_phys_addr_t/* or whatever */ target_phys_size_t; target_phys_size_t ram_size;
then I'm happy as well, I just dislike writing target_phys_addr_t size.

Andreas

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

Re: [Qemu-devel] [PATCH 1/6] hw/arm_boot.c: Make ram_size a target_phys_addr_t

In reply to Andreas Färber
Quoted 7 lines · Andreas Färber

Am 05.07.2012 19:00, schrieb Peter Maydell:

Quoted 3 lines

Make the RAM size in arm_boot_info a target_phys_addr_t so it can express RAM sizes up to the limit imposed by the physical address size.

Didn't we conclude in lengthy and emotional discussions that int64_t was the best compromise to solve the highbank and pseries image loading issues?

uint64_t, but yes, you're right. Will change.

-- PMM

#7 lore raw

PATCH 2/6 hw/arm_boot.c: Consistently use ram_size from arm_boot_info struct

In reply to Peter Maydell

Clean up the mix of getting the RAM size from the global ram_size and from the ram_size field in the arm_boot_info structure, so that we always use the structure field.

---
 hw/arm_boot.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
hw/arm_boot.c +3 -2
index a1e6ddb..29ae324 100644
@@ -357,7 +357,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
     if (kernel_size < 0) {
         entry = info->loader_start + KERNEL_LOAD_ADDR;
         kernel_size = load_image_targphys(info->kernel_filename, entry,
-                                          ram_size - KERNEL_LOAD_ADDR);
+                                          info->ram_size - KERNEL_LOAD_ADDR);
         is_linux = 1;
     }
     if (kernel_size < 0) {
@@ -371,7 +371,8 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
             initrd_size = load_image_targphys(info->initrd_filename,
                                               info->loader_start
                                               + INITRD_LOAD_ADDR,
-                                              ram_size - INITRD_LOAD_ADDR);
+                                              info->ram_size
+                                              - INITRD_LOAD_ADDR);
             if (initrd_size < 0) {
                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
                         info->initrd_filename);
Signature · 1 line
1.7.1
Reviewed-by on v1 2/6
#8 lore raw

Re: [Qemu-devel] [PATCH 2/6] hw/arm_boot.c: Consistently use ram_size from arm_boot_info struct

Quoted 5 lines · Peter Maydell

Clean up the mix of getting the RAM size from the global ram_size and from the ram_size field in the arm_boot_info structure, so that we always use the structure field.

Quoted 18 lines
---
 hw/arm_boot.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
hw/arm_boot.c +1 -1
index a1e6ddb..29ae324 100644
@@ -357,7 +357,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
     if (kernel_size < 0) {
         entry = info->loader_start + KERNEL_LOAD_ADDR;
         kernel_size = load_image_targphys(info->kernel_filename, entry,
-                                          ram_size - KERNEL_LOAD_ADDR);
+                                          info->ram_size - KERNEL_LOAD_ADDR);
         is_linux = 1;
     }
     if (kernel_size < 0) {

hw/arm_boot.c void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) · line 375

Quoted 6 lines
hw/arm_boot.c +2 -1
@@ -371,7 +371,8 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
             initrd_size = load_image_targphys(info->initrd_filename,
                                               info->loader_start
                                               + INITRD_LOAD_ADDR,
-                                              ram_size - INITRD_LOAD_ADDR);
+                                              info->ram_size
+                                              - INITRD_LOAD_ADDR);

No blocker, but can we de-indent one or two tabs, to not have to have this arg split this across so many lines?

Quoted 6 lines
hw/arm_boot.c +0 -1
             if (initrd_size < 0) {
                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
                         info->initrd_filename);
--

1.7.1

#9 lore raw

Re: [Qemu-devel] [PATCH 2/6] hw/arm_boot.c: Consistently use ram_size from arm_boot_info struct

Quoted 11 lines · Peter Crosthwaite

hw/arm_boot.c void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) · line 375

Quoted 7 lines · Peter Maydell
hw/arm_boot.c +2 -1
@@ -371,7 +371,8 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
             initrd_size = load_image_targphys(info->initrd_filename,
                                               info->loader_start
                                               + INITRD_LOAD_ADDR,
-                                              ram_size - INITRD_LOAD_ADDR);
+                                              info->ram_size
+                                              - INITRD_LOAD_ADDR);

No blocker, but can we de-indent one or two tabs, to not have to have this arg split this across so many lines?

The general style at least in ARM related QEMU code is that function arguments always line up (ie indent to just after the opening paren). This is what the older code written by Paul does and it's also what my editor does. I prefor not to break that convention. (I agree this looks a little ugly, but that's what 80-column restrictions get you.)

-- PMM

#10 lore raw

PATCH 3/6 hw/arm_boot.c: Check for RAM sizes exceeding ATAGS capacity

In reply to Peter Maydell

The legacy ATAGS format for passing information to the kernel only allows RAM sizes which fit in 32 bits; enforce this restriction rather than silently doing something weird.

---
 hw/arm_boot.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
hw/arm_boot.c +5 -0
index 29ae324..7366427 100644
@@ -399,6 +399,11 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
             bootloader[5] = dtb_start;
         } else {
             bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
+            if (info->ram_size >= (1ULL << 32)) {
+                fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
+                        " Linux kernel without device tree\n");
+                exit(1);
+            }
         }
         bootloader[6] = entry;
         for (n = 0; n < sizeof(bootloader) / 4; n++) {
Signature · 1 line
1.7.1
#11 lore raw

Re: [Qemu-devel] [PATCH 3/6] hw/arm_boot.c: Check for RAM sizes exceeding ATAGS capacity

Quoted 14 lines · Peter Maydell

The legacy ATAGS format for passing information to the kernel only allows RAM sizes which fit in 32 bits; enforce this restriction rather than silently doing something weird.

---
 hw/arm_boot.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
hw/arm_boot.c +0 -0
index 29ae324..7366427 100644

hw/arm_boot.c void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) · line 404

Quoted 6 lines
hw/arm_boot.c +3 -0
@@ -399,6 +399,11 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
             bootloader[5] = dtb_start;
         } else {
             bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
+            if (info->ram_size >= (1ULL << 32)) {
+                fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
+                        " Linux kernel without device tree\n");

Error message is a bit weird. Shouldnt it be "RAM size must be less than 4GB to boot Linux kernel with ATAG command line". DTB shouldnt be the only way to boot with >4GB.

Regards,
Peter

Quoted 8 lines
hw/arm_boot.c +2 -1
+                exit(1);
+            }
         }
         bootloader[6] = entry;
         for (n = 0; n < sizeof(bootloader) / 4; n++) {
--

1.7.1

#12 lore raw

Re: [Qemu-devel] [PATCH 3/6] hw/arm_boot.c: Check for RAM sizes exceeding ATAGS capacity

Quoted 8 lines · Peter Crosthwaite

hw/arm_boot.c void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) · line 404

Quoted 3 lines · Peter Maydell
hw/arm_boot.c +3 -0
+            if (info->ram_size >= (1ULL << 32)) {
+                fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
+                        " Linux kernel without device tree\n");

Error message is a bit weird. Shouldnt it be "RAM size must be less than 4GB to boot Linux kernel with ATAG command line". DTB shouldnt be the only way to boot with >4GB.

I'm trying to steer the user toward the way to solve their problem, which is to use a device tree. I expect less than 1 QEMU user in 100 has any idea what ATAGS are.

Quoted 1 line

DTB shouldnt be the only way to boot with >4GB.

I'm not sure what you mean here -- we have two boot methods and only DTB handles large RAM sizes. Or do you mean machines with eg flash where you could boot by putting a bootloader in flash?

-- PMM

#13 lore raw

PATCH 4/6 device_tree: Add support for reading device tree properties

In reply to Peter Maydell

Add support for reading device tree properties (both generic and single-cell ones) to QEMU's convenience wrapper layer.

---
 device_tree.c |   30 ++++++++++++++++++++++++++++++
 device_tree.h |    4 ++++
 2 files changed, 34 insertions(+), 0 deletions(-)
device_tree.c +30 -0
index b366fdd..d7a9b6b 100644
@@ -178,6 +178,36 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path,
     return r;
 }
 
+const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+                                 const char *property, int *lenp)
+{
+    int len;
+    const void *r;
+    if (!lenp) {
+        lenp = &len;
+    }
+    r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
+    if (!r) {
+        fprintf(stderr, "%s: Couldn't get %s/%s: %s\n", __func__,
+                node_path, property, fdt_strerror(*lenp));
+        exit(1);
+    }
+    return r;
+}
+
+uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
+                                   const char *property)
+{
+    int len;
+    const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len);
+    if (len != 4) {
+        fprintf(stderr, "%s: %s/%s not 4 bytes long (not a cell?)\n",
+                __func__, node_path, property);
+        exit(1);
+    }
+    return be32_to_cpu(*p);
+}
+
 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
 {
     uint32_t r;
device_tree.h +4 -0
index 2244270..f7a3e6c 100644
@@ -28,6 +28,10 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path,
 int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
                                  const char *property,
                                  const char *target_node_path);
+const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+                                 const char *property, int *lenp);
+uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
+                                   const char *property);
 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path);
 uint32_t qemu_devtree_alloc_phandle(void *fdt);
 int qemu_devtree_nop_node(void *fdt, const char *node_path);
Signature · 1 line
1.7.1
#14 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

Quoted 26 lines · Peter Maydell

Add support for reading device tree properties (both generic and single-cell ones) to QEMU's convenience wrapper layer.

---
 device_tree.c |   30 ++++++++++++++++++++++++++++++
 device_tree.h |    4 ++++
 2 files changed, 34 insertions(+), 0 deletions(-)
device_tree.c +9 -0
index b366fdd..d7a9b6b 100644
@@ -178,6 +178,36 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path,
     return r;
 }
 
+const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+                                 const char *property, int *lenp)
+{
+    int len;
+    const void *r;
+    if (!lenp) {
+        lenp = &len;
+    }
+    r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);

device_tree.c int qemu_devtree_setprop_string(void *fdt, const char *node_path, · line 199

Quoted 10 lines
device_tree.c +10 -0
+    if (!r) {
+        fprintf(stderr, "%s: Couldn't get %s/%s: %s\n", __func__,
+                node_path, property, fdt_strerror(*lenp));
+        exit(1);
+    }
+    return r;
+}
+
+uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
+                                   const char *property)

Hi Peter,

Can we generalise and get functionality for reading cells with offsets as well? Your function assumes (and asserts) that the property is a single cell, but can we add a index parameter for reading a non-0th property out of a multi-cell prop? Needed for reading things like ranges, regs and interrupt properties.

Regards,
Peter

Quoted 32 lines
device_tree.c +11 -0
+{
+    int len;
+    const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len);
+    if (len != 4) {
+        fprintf(stderr, "%s: %s/%s not 4 bytes long (not a cell?)\n",
+                __func__, node_path, property);
+        exit(1);
+    }
+    return be32_to_cpu(*p);
+}
+
 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
 {
     uint32_t r;
device_tree.h +4 -1
index 2244270..f7a3e6c 100644
@@ -28,6 +28,10 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path,
 int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
                                  const char *property,
                                  const char *target_node_path);
+const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+                                 const char *property, int *lenp);
+uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
+                                   const char *property);
 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path);
 uint32_t qemu_devtree_alloc_phandle(void *fdt);
 int qemu_devtree_nop_node(void *fdt, const char *node_path);
--

1.7.1

#15 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

Quoted 11 lines · Peter Crosthwaite

device_tree.c int qemu_devtree_setprop_string(void *fdt, const char *node_path, · line 199

Quoted 2 lines · Peter Maydell
device_tree.c +2 -0
+uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
+                                   const char *property)

Hi Peter,

Can we generalise and get functionality for reading cells with offsets as well? Your function assumes (and asserts) that the property is a single cell, but can we add a index parameter for reading a non-0th property out of a multi-cell prop? Needed for reading things like ranges, regs and interrupt properties.

We could, but in this instance I was just following the pattern of the existing qemu_devtree_setprop_cell(), which also works only on single-celled properties. Maybe 'set one cell in a multicell prop' should be qemu_devtree_setprop_one_cell() ?

-- PMM

#16 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

In reply to Peter Crosthwaite
Quoted 5 lines · Peter Crosthwaite

Can we generalise and get functionality for reading cells with offsets as well? Your function assumes (and asserts) that the property is a single cell, but can we add a index parameter for reading a non-0th property out of a multi-cell prop? Needed for reading things like ranges, regs and interrupt properties.

I was playing about with this and I'm really not sure that we should be providing a "read a single u32 from a u32 array property" at the device_tree.c layer. For example, for handling the ranges property what you really want to do is treat it as a list of tuples (including doing something sensible if it doesn't have the right length to be a complete list), so the code that knows the structure of the ranges property is better off calling qemu_devtree_getprop to get a uint32_t* for the whole array. Then it has the whole thing as a straightforward C array which will be much easier and more efficient to handle than constantly bouncing back into the fdt layer to read each uint32_t.

I've also just realised that I'm assuming that the pointer returned by fdt_getprop() is naturally aligned for a 32 bit integer if the property is a 32 bit integer -- is that valid?

-- PMM

#17 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

Quoted 3 lines · Peter Maydell

I've also just realised that I'm assuming that the pointer returned by fdt_getprop() is naturally aligned for a 32 bit integer if the property is a 32 bit integer -- is that valid?

To answer my own question here, the dtb format mandates that property data starts at a 4-aligned address, so casting the pointer to a uint32_t* is safe.

-- PMM

#18 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell
Quoted 5 lines · Peter Crosthwaite

Can we generalise and get functionality for reading cells with offsets as well? Your function assumes (and asserts) that the property is a single cell, but can we add a index parameter for reading a non-0th property out of a multi-cell prop? Needed for reading things like ranges, regs and interrupt properties.

I was playing about with this and I'm really not sure that we should be providing a "read a single u32 from a u32 array property" at the device_tree.c layer. For example, for handling the ranges property what you really want to do is treat it as a list of tuples

The tuples concept layers on top of the cells concept. The meaning of cells will differ from property to property but cells are cells. The setter API manages cells but not tuples, so the same should be true of the getter API. So if you are dealing with tuples, the device tree layer should provide access to the cells (which is essentially reading u32 from u32 array) then you can interpret them as tuples if you wish.

I guess what i'm trying to say is that tuples and cells should be managed quite separately, the former in the client and the latter in the device tree API.

(including

Quoted 4 lines

doing something sensible if it doesn't have the right length to be a complete list), so the code that knows the structure of the ranges property is better off calling qemu_devtree_getprop to get a uint32_t* for the whole array.

The logic for the byte reversal should still be in device tree however. You have to be_to_cpu each read value after reading that array which is tedious and an easy omission to make. I think if we are going to wrap for single cell props then it makes sense for multi cell as well.

Then it has the whole thing as a straightforward

Quoted 1 line

C array which will be much easier and more efficient to handle than

Efficient yes, but easier no because of the endian issue. For my few use cases out of tree I only ever get the one property at a time. I never parse an entire cell array.

Quoted 2 lines

constantly bouncing back into the fdt layer to read each uint32_t.

Constantly bouncing back is safer however. If you hang on to an in-place pointer into the FDT (as returned by get_prop) and someone comes along and set_props() then your pointer is corrupted. Ive been snagged before by doing exactly this and eventually came to the brute-force approach of just requerying the DTB every touch rather than try to work with pointers to arrays. duping the property could work, but its a bit of a mess trying to free the returned copies.

(As an aside this is one of the reasons why my chicken-and-egg machine model uses coroutines instead of threads. I need the get_prop() followed by its immediate usage to be atomic).

If user care about efficiency over safety, then your get_prop + cast + endian_swap approach will always be available to them. I just think a single idx arg at the end creates more options for users. We could vararg it and if its omitted it just takes the 0th element to keep the call sites for the 90% case a little more concise.

Regards,
Peter

Quoted 5 lines

I've also just realised that I'm assuming that the pointer returned by fdt_getprop() is naturally aligned for a 32 bit integer if the property is a 32 bit integer -- is that valid?

-- PMM

#19 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

Quoted 5 lines · Peter Crosthwaite

If user care about efficiency over safety, then your get_prop + cast + endian_swap approach will always be available to them. I just think a single idx arg at the end creates more options for users. We could vararg it and if its omitted it just takes the 0th element to keep the call sites for the 90% case a little more concise.

Hmm, OK, I'll have another go and see how the patch comes out.

-- PMM

#20 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

In reply to Peter Crosthwaite
Quoted 7 lines · Peter Crosthwaite

Constantly bouncing back is safer however. If you hang on to an in-place pointer into the FDT (as returned by get_prop) and someone comes along and set_props() then your pointer is corrupted. Ive been snagged before by doing exactly this and eventually came to the brute-force approach of just requerying the DTB every touch rather than try to work with pointers to arrays. duping the property could work, but its a bit of a mess trying to free the returned copies.

Incidentally, if you have two separate bits of code both accessing the DTB in parallel then this sounds like a really weird corner case use. I would expect that the standard thing would be "at startup we read the DTB, modify it slightly and after that ignore it", all of which should be straightforward single threaded code with no particular control flow/threading/coroutine issues.

-- PMM

Reviewed-by on v1 4/6
#21 lore raw

Re: [Qemu-devel] [PATCH 4/6] device_tree: Add support for reading device tree properties

In reply to Peter Maydell
Quoted 4 lines · Peter Maydell

Add support for reading device tree properties (both generic and single-cell ones) to QEMU's convenience wrapper layer.

Quoted 64 lines
---
 device_tree.c |   30 ++++++++++++++++++++++++++++++
 device_tree.h |    4 ++++
 2 files changed, 34 insertions(+), 0 deletions(-)
device_tree.c +30 -0
index b366fdd..d7a9b6b 100644
@@ -178,6 +178,36 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path,
     return r;
 }
 
+const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+                                 const char *property, int *lenp)
+{
+    int len;
+    const void *r;
+    if (!lenp) {
+        lenp = &len;
+    }
+    r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
+    if (!r) {
+        fprintf(stderr, "%s: Couldn't get %s/%s: %s\n", __func__,
+                node_path, property, fdt_strerror(*lenp));
+        exit(1);
+    }
+    return r;
+}
+
+uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
+                                   const char *property)
+{
+    int len;
+    const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len);
+    if (len != 4) {
+        fprintf(stderr, "%s: %s/%s not 4 bytes long (not a cell?)\n",
+                __func__, node_path, property);
+        exit(1);
+    }
+    return be32_to_cpu(*p);
+}
+
 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
 {
     uint32_t r;
device_tree.h +4 -1
index 2244270..f7a3e6c 100644
@@ -28,6 +28,10 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path,
 int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
                                  const char *property,
                                  const char *target_node_path);
+const void *qemu_devtree_getprop(void *fdt, const char *node_path,
+                                 const char *property, int *lenp);
+uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
+                                   const char *property);
 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path);
 uint32_t qemu_devtree_alloc_phandle(void *fdt);
 int qemu_devtree_nop_node(void *fdt, const char *node_path);
--

1.7.1

#22 lore raw

PATCH 5/6 hw/arm_boot.c: Support DTBs which use 64 bit addresses

In reply to Peter Maydell

Support the case where the device tree blob specifies that #address-cells and #size-cells are greater than 1. (This is needed for device trees which can handle 64 bit physical addresses and thus total RAM sizes over 4GB.)

---
 hw/arm_boot.c |   36 +++++++++++++++++++++++++++++++++---
 1 files changed, 33 insertions(+), 3 deletions(-)
hw/arm_boot.c +33 -3
index 7366427..8203422 100644
@@ -216,11 +216,12 @@ static void set_kernel_args_old(const struct arm_boot_info *info)
 static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
 {
 #ifdef CONFIG_FDT
-    uint32_t mem_reg_property[] = { cpu_to_be32(binfo->loader_start),
-                                    cpu_to_be32(binfo->ram_size) };
+    uint32_t *mem_reg_property;
+    uint32_t mem_reg_propsize;
     void *fdt = NULL;
     char *filename;
     int size, rc;
+    uint32_t acells, scells, hival;
 
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
     if (!filename) {
@@ -236,8 +237,37 @@ static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
     }
     g_free(filename);
 
+    acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
+    scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
+    printf("fdt: acells %d scells %d\n", acells, scells);
+    if (acells == 0 || scells == 0) {
+        fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
+        return -1;
+    }
+
+    mem_reg_propsize = acells + scells;
+    mem_reg_property = g_new0(uint32_t, mem_reg_propsize);
+    mem_reg_property[acells - 1] = cpu_to_be32(binfo->loader_start);
+    hival = cpu_to_be32(binfo->loader_start >> 32);
+    if (acells > 1) {
+        mem_reg_property[acells - 2] = hival;
+    } else if (hival != 0) {
+        fprintf(stderr, "qemu: dtb file not compatible with "
+                "RAM start address > 4GB\n");
+        exit(1);
+    }
+    mem_reg_property[acells + scells - 1] = cpu_to_be32(binfo->ram_size);
+    hival = cpu_to_be32(binfo->ram_size >> 32);
+    if (scells > 1) {
+        mem_reg_property[acells + scells - 2] = hival;
+    } else if (hival != 0) {
+        fprintf(stderr, "qemu: dtb file not compatible with "
+                "RAM size > 4GB\n");
+        exit(1);
+    }
+
     rc = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
-                               sizeof(mem_reg_property));
+                              mem_reg_propsize * sizeof(uint32_t));
     if (rc < 0) {
         fprintf(stderr, "couldn't set /memory/reg\n");
     }
Signature · 1 line
1.7.1
#23 lore raw

Re: [Qemu-devel] [PATCH 5/6] hw/arm_boot.c: Support DTBs which use 64 bit addresses

Quoted 30 lines · Peter Maydell

Support the case where the device tree blob specifies that #address-cells and #size-cells are greater than 1. (This is needed for device trees which can handle 64 bit physical addresses and thus total RAM sizes over 4GB.)

---
 hw/arm_boot.c |   36 +++++++++++++++++++++++++++++++++---
 1 files changed, 33 insertions(+), 3 deletions(-)
hw/arm_boot.c +3 -2
index 7366427..8203422 100644
@@ -216,11 +216,12 @@ static void set_kernel_args_old(const struct arm_boot_info *info)
 static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
 {
 #ifdef CONFIG_FDT
-    uint32_t mem_reg_property[] = { cpu_to_be32(binfo->loader_start),
-                                    cpu_to_be32(binfo->ram_size) };
+    uint32_t *mem_reg_property;
+    uint32_t mem_reg_propsize;
     void *fdt = NULL;
     char *filename;
     int size, rc;
+    uint32_t acells, scells, hival;
 
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
     if (!filename) {

hw/arm_boot.c static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo) · line 242

Quoted 6 lines
hw/arm_boot.c +3 -0
@@ -236,8 +237,37 @@ static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
     }
     g_free(filename);
 
+    acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
+    scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
+    printf("fdt: acells %d scells %d\n", acells, scells);

Leftover debugging?

Quoted 36 lines
hw/arm_boot.c +27 -2
+    if (acells == 0 || scells == 0) {
+        fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
+        return -1;
+    }
+
+    mem_reg_propsize = acells + scells;
+    mem_reg_property = g_new0(uint32_t, mem_reg_propsize);
+    mem_reg_property[acells - 1] = cpu_to_be32(binfo->loader_start);
+    hival = cpu_to_be32(binfo->loader_start >> 32);
+    if (acells > 1) {
+        mem_reg_property[acells - 2] = hival;
+    } else if (hival != 0) {
+        fprintf(stderr, "qemu: dtb file not compatible with "
+                "RAM start address > 4GB\n");
+        exit(1);
+    }
+    mem_reg_property[acells + scells - 1] = cpu_to_be32(binfo->ram_size);
+    hival = cpu_to_be32(binfo->ram_size >> 32);
+    if (scells > 1) {
+        mem_reg_property[acells + scells - 2] = hival;
+    } else if (hival != 0) {
+        fprintf(stderr, "qemu: dtb file not compatible with "
+                "RAM size > 4GB\n");
+        exit(1);
+    }
+
     rc = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
-                               sizeof(mem_reg_property));
+                              mem_reg_propsize * sizeof(uint32_t));
     if (rc < 0) {
         fprintf(stderr, "couldn't set /memory/reg\n");
     }
--

1.7.1

#24 lore raw

Re: [Qemu-devel] [PATCH 5/6] hw/arm_boot.c: Support DTBs which use 64 bit addresses

Quoted 10 lines · Blue Swirl

hw/arm_boot.c static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo) · line 242

Quoted 7 lines · Peter Maydell
hw/arm_boot.c +3 -0
@@ -236,8 +237,37 @@ static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
     }
     g_free(filename);
 
+    acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
+    scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
+    printf("fdt: acells %d scells %d\n", acells, scells);

Leftover debugging?

Doh. Yes, will remove printf in v2.

-- PMM

Reviewed-by on v1 5/6
#25 lore raw

Re: [Qemu-devel] [PATCH 5/6] hw/arm_boot.c: Support DTBs which use 64 bit addresses

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Support the case where the device tree blob specifies that #address-cells and #size-cells are greater than 1. (This is needed for device trees which can handle 64 bit physical addresses and thus total RAM sizes over 4GB.)

Quoted 65 lines
---
 hw/arm_boot.c |   36 +++++++++++++++++++++++++++++++++---
 1 files changed, 33 insertions(+), 3 deletions(-)
hw/arm_boot.c +33 -4
index 7366427..8203422 100644
@@ -216,11 +216,12 @@ static void set_kernel_args_old(const struct arm_boot_info *info)
 static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
 {
 #ifdef CONFIG_FDT
-    uint32_t mem_reg_property[] = { cpu_to_be32(binfo->loader_start),
-                                    cpu_to_be32(binfo->ram_size) };
+    uint32_t *mem_reg_property;
+    uint32_t mem_reg_propsize;
     void *fdt = NULL;
     char *filename;
     int size, rc;
+    uint32_t acells, scells, hival;
 
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
     if (!filename) {
@@ -236,8 +237,37 @@ static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
     }
     g_free(filename);
 
+    acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
+    scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
+    printf("fdt: acells %d scells %d\n", acells, scells);
+    if (acells == 0 || scells == 0) {
+        fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
+        return -1;
+    }
+
+    mem_reg_propsize = acells + scells;
+    mem_reg_property = g_new0(uint32_t, mem_reg_propsize);
+    mem_reg_property[acells - 1] = cpu_to_be32(binfo->loader_start);
+    hival = cpu_to_be32(binfo->loader_start >> 32);
+    if (acells > 1) {
+        mem_reg_property[acells - 2] = hival;
+    } else if (hival != 0) {
+        fprintf(stderr, "qemu: dtb file not compatible with "
+                "RAM start address > 4GB\n");
+        exit(1);
+    }
+    mem_reg_property[acells + scells - 1] = cpu_to_be32(binfo->ram_size);
+    hival = cpu_to_be32(binfo->ram_size >> 32);
+    if (scells > 1) {
+        mem_reg_property[acells + scells - 2] = hival;
+    } else if (hival != 0) {
+        fprintf(stderr, "qemu: dtb file not compatible with "
+                "RAM size > 4GB\n");
+        exit(1);
+    }
+
     rc = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
-                               sizeof(mem_reg_property));
+                              mem_reg_propsize * sizeof(uint32_t));
     if (rc < 0) {
         fprintf(stderr, "couldn't set /memory/reg\n");
     }
--

1.7.1

#26 lore raw

PATCH 6/6 hw/vexpress.c: Allow >4GB of RAM for Cortex-A15 daughterboard

In reply to Peter Maydell

Now that we have LPAE support and can handle passing 64 bit RAM sizes to Linux via the device tree, we can lift the restriction in the Versatile Express A15 daughterboard model on not having more than 2GB of RAM. Allow up to 30GB, which is the maximum that can fit in the address map before running into the (unmodelled) aliases of the first 2GB.

---
 hw/vexpress.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
hw/vexpress.c +10 -3
index 8072c5a..b2dc8a5 100644
@@ -284,9 +284,16 @@ static void a15_daughterboard_init(const VEDBoardInfo *daughterboard,
         cpu_irq[n] = irqp[ARM_PIC_CPU_IRQ];
     }
 
-    if (ram_size > 0x80000000) {
-        fprintf(stderr, "vexpress-a15: cannot model more than 2GB RAM\n");
-        exit(1);
+    {
+        /* We have to use a separate 64 bit variable here to avoid the gcc
+         * "comparison is always false due to limited range of data type"
+         * warning if we are on a host where ram_addr_t is 32 bits.
+         */
+        uint64_t rsz = ram_size;
+        if (rsz > (30ULL * 1024 * 1024 * 1024)) {
+            fprintf(stderr, "vexpress-a15: cannot model more than 30GB RAM\n");
+            exit(1);
+        }
     }
 
     memory_region_init_ram(ram, "vexpress.highmem", ram_size);
Signature · 1 line
1.7.1