[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH 5/8] lib/fdt: Introduce a fdt_get_address helper



This helper will be used very frequently for device libraries
to parse their addresses. Introduce this helper to avoid using
fdt_address_cells and fdt_size_cells everywhere.

Signed-off-by: Wei Chen <wei.chen@xxxxxxx>
Signed-off-by: Jianyong Wu <jianyong.wu@xxxxxxx>
---
 lib/fdt/exportsyms.uk    |  1 +
 lib/fdt/fdt_addresses.c  | 50 ++++++++++++++++++++++++++++++++++++++++
 lib/fdt/include/libfdt.h | 18 +++++++++++++++
 3 files changed, 69 insertions(+)

diff --git a/lib/fdt/exportsyms.uk b/lib/fdt/exportsyms.uk
index b11df90..c893caf 100644
--- a/lib/fdt/exportsyms.uk
+++ b/lib/fdt/exportsyms.uk
@@ -62,3 +62,4 @@ fdt_resize
 fdt_overlay_apply
 fdt_getprop_u32_by_offset
 fdt_interrupt_cells
+fdt_get_address
diff --git a/lib/fdt/fdt_addresses.c b/lib/fdt/fdt_addresses.c
index bccb11c..b186fc0 100644
--- a/lib/fdt/fdt_addresses.c
+++ b/lib/fdt/fdt_addresses.c
@@ -64,3 +64,53 @@ int fdt_size_cells(const void *fdt, int nodeoffset)
 {
        return fdt_get_cells(fdt, "#size-cells", nodeoffset);
 }
+
+static uint64_t fdt_reg_read_number(const fdt32_t *regs, uint32_t size)
+{
+       uint64_t number = 0;
+
+       if (size >= 3 || size <= 0)
+               return -FDT_ERR_BADNCELLS;
+
+       for(uint32_t i = 0; i < size; i++) {
+               number <<= 32;
+               number |= fdt32_to_cpu(*regs);
+               regs++;
+       }
+
+       return number;
+}
+
+int fdt_get_address(const void *fdt, int nodeoffset, int index,
+                       uint64_t *addr, uint64_t *size)
+{
+       int len, prop_addr, prop_size;
+       int naddr, nsize, term_size;
+       const void *regs;
+
+       naddr = fdt_address_cells(fdt, nodeoffset);
+       if (naddr < 0 || naddr >= FDT_MAX_NCELLS)
+               return -FDT_ERR_BADNCELLS;
+
+       nsize = fdt_size_cells(fdt, nodeoffset);
+       if (nsize < 0 || nsize >= FDT_MAX_NCELLS)
+               return -FDT_ERR_BADNCELLS;
+
+       /* Get reg content */
+       regs = fdt_getprop(fdt, nodeoffset, "reg", &len);
+       if (regs == NULL)
+               return -FDT_ERR_NOTFOUND;
+
+       term_size = (int)sizeof(fdt32_t) * (nsize + naddr);
+       prop_addr = term_size * index;
+       prop_size = prop_addr + (int)sizeof(fdt32_t) * naddr;
+
+       /* The reg content must cover the reg term[index] at least */
+       if (len < (prop_addr + term_size))
+               return -FDT_ERR_NOSPACE;
+
+       *addr = fdt_reg_read_number(regs + prop_addr, naddr);
+       *size = fdt_reg_read_number(regs + prop_size, nsize);
+
+       return 0;
+}
diff --git a/lib/fdt/include/libfdt.h b/lib/fdt/include/libfdt.h
index b0815af..d43e710 100644
--- a/lib/fdt/include/libfdt.h
+++ b/lib/fdt/include/libfdt.h
@@ -1161,6 +1161,24 @@ int fdt_size_cells(const void *fdt, int nodeoffset);
  */
 int fdt_interrupt_cells(const void *fdt, int nodeoffset);
 
+/**
+ * fdt_get_address - retrieve device address of a given index
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node to find the address for.
+ * @index: index of region
+ * @addr: return the region address
+ * @size: return the region size
+ *
+ * returns:
+ *     0, on success
+ *      -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
+ *             address property
+ *      -FDT_ERR_NOTFOUND, if the node doesn't have address property
+ *      -FDT_ERR_NOSPACE, if the node doesn't have address for index
+ */
+int fdt_get_address(const void *fdt, int nodeoffset, int index,
+                       uint64_t *addr, uint64_t *size);
+
 /**********************************************************************/
 /* Write-in-place functions                                           */
 /**********************************************************************/
-- 
2.17.1


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.