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

[Minios-devel] [UNIKRAFT PATCHv9 4/7] plat/common: Introduce 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>
Signed-off-by: Jia He <justin.he@xxxxxxx>
Reviewed-by: Sharan Santhanam <sharan.santhanam@xxxxxxxxx>
---
 plat/drivers/include/ofw/fdt.h | 18 +++++++++++++++
 plat/drivers/ofw/fdt.c         | 41 ++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/plat/drivers/include/ofw/fdt.h b/plat/drivers/include/ofw/fdt.h
index 8a0bdcf..cb941b6 100644
--- a/plat/drivers/include/ofw/fdt.h
+++ b/plat/drivers/include/ofw/fdt.h
@@ -73,4 +73,22 @@ static inline uint64_t fdt_reg_read_number(const fdt32_t 
*regs, uint32_t size)
 
        return number;
 }
+
+/**
+ * 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, uint32_t index,
+                       uint64_t *addr, uint64_t *size);
 #endif
diff --git a/plat/drivers/ofw/fdt.c b/plat/drivers/ofw/fdt.c
index e21cf8f..f5102b4 100644
--- a/plat/drivers/ofw/fdt.c
+++ b/plat/drivers/ofw/fdt.c
@@ -200,3 +200,44 @@ bail:
        return result;
 }
 
+int fdt_get_address(const void *fdt, int nodeoffset, uint32_t index,
+                       uint64_t *addr, uint64_t *size)
+{
+       int parent;
+       int len, prop_addr, prop_size;
+       int naddr, nsize, term_size;
+       const void *regs;
+
+       UK_ASSERT(addr && size);
+
+       /* Get address,size cell from parent */
+       parent = fdt_parent_offset(fdt, nodeoffset);
+       naddr = fdt_address_cells(fdt, parent);
+       if (naddr < 0 || naddr >= FDT_MAX_NCELLS)
+               return naddr;
+
+       nsize = fdt_size_cells(fdt, parent);
+       if (nsize < 0 || nsize >= FDT_MAX_NCELLS)
+               return nsize;
+
+       /* Get reg content */
+       regs = fdt_getprop(fdt, nodeoffset, "reg", &len);
+       if (regs == NULL)
+               return len;
+
+       term_size = sizeof(fdt32_t) * (nsize + naddr);
+       prop_addr = term_size * index;
+       prop_size = prop_addr + 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;
+
+       *size = fdt_reg_read_number(regs + prop_size, nsize);
+       /* Handle ranges property, currently only 1:1 mapping is supported */
+       *addr = fdt_translate_address_by_ranges(fdt, nodeoffset,
+                                               regs + prop_addr);
+       if (*addr == FDT_BAD_ADDR)
+               return -FDT_ERR_NOTFOUND;
+       return 0;
+}
-- 
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®.