xref: /qemu/include/system/device_tree.h (revision 80972d3bb2dbc6a47bae84b694c14acba9c1de64)
1f652e6afSaurel32 /*
2f652e6afSaurel32  * Header with function prototypes to help device tree manipulation using
3f652e6afSaurel32  * libfdt. It also provides functions to read entries from device tree proc
4f652e6afSaurel32  * interface.
5f652e6afSaurel32  *
6f652e6afSaurel32  * Copyright 2008 IBM Corporation.
7f652e6afSaurel32  * Authors: Jerone Young <jyoung5@us.ibm.com>
8f652e6afSaurel32  *          Hollis Blanchard <hollisb@us.ibm.com>
9f652e6afSaurel32  *
10f652e6afSaurel32  * This work is licensed under the GNU GPL license version 2 or later.
11f652e6afSaurel32  *
12f652e6afSaurel32  */
13f652e6afSaurel32 
142a6a4076SMarkus Armbruster #ifndef DEVICE_TREE_H
152a6a4076SMarkus Armbruster #define DEVICE_TREE_H
16f652e6afSaurel32 
17ce36252cSAlexander Graf void *create_device_tree(int *sizep);
187ec632b4Spbrook void *load_device_tree(const char *filename_path, int *sizep);
1960e43e98SEric Auger #ifdef CONFIG_LINUX
2060e43e98SEric Auger /**
2160e43e98SEric Auger  * load_device_tree_from_sysfs: reads the device tree information in the
2260e43e98SEric Auger  * /proc/device-tree directory and return the corresponding binary blob
2360e43e98SEric Auger  * buffer pointer. Asserts in case of error.
2460e43e98SEric Auger  */
2560e43e98SEric Auger void *load_device_tree_from_sysfs(void);
2660e43e98SEric Auger #endif
27f652e6afSaurel32 
286d79566aSEric Auger /**
296d79566aSEric Auger  * qemu_fdt_node_path: return the paths of nodes matching a given
306d79566aSEric Auger  * name and compat string
316d79566aSEric Auger  * @fdt: pointer to the dt blob
326d79566aSEric Auger  * @name: node name
336d79566aSEric Auger  * @compat: compatibility string
346d79566aSEric Auger  * @errp: handle to an error object
356d79566aSEric Auger  *
366d79566aSEric Auger  * returns a newly allocated NULL-terminated array of node paths.
376d79566aSEric Auger  * Use g_strfreev() to free it. If one or more nodes were found, the
386d79566aSEric Auger  * array contains the path of each node and the last element equals to
396d79566aSEric Auger  * NULL. If there is no error but no matching node was found, the
406d79566aSEric Auger  * returned array contains a single element equal to NULL. If an error
416d79566aSEric Auger  * was encountered when parsing the blob, the function returns NULL
42*80972d3bSEdgar E. Iglesias  *
43*80972d3bSEdgar E. Iglesias  * @name may be NULL to wildcard names and only match compatibility
44*80972d3bSEdgar E. Iglesias  * strings.
456d79566aSEric Auger  */
466d79566aSEric Auger char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
476d79566aSEric Auger                           Error **errp);
486d79566aSEric Auger 
49f963cc26SEric Auger /**
50f963cc26SEric Auger  * qemu_fdt_node_unit_path: return the paths of nodes matching a given
51f963cc26SEric Auger  * node-name, ie. node-name and node-name@unit-address
52f963cc26SEric Auger  * @fdt: pointer to the dt blob
53f963cc26SEric Auger  * @name: node name
54f963cc26SEric Auger  * @errp: handle to an error object
55f963cc26SEric Auger  *
56f963cc26SEric Auger  * returns a newly allocated NULL-terminated array of node paths.
57f963cc26SEric Auger  * Use g_strfreev() to free it. If one or more nodes were found, the
58f963cc26SEric Auger  * array contains the path of each node and the last element equals to
59f963cc26SEric Auger  * NULL. If there is no error but no matching node was found, the
60f963cc26SEric Auger  * returned array contains a single element equal to NULL. If an error
61f963cc26SEric Auger  * was encountered when parsing the blob, the function returns NULL
62f963cc26SEric Auger  */
63f963cc26SEric Auger char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp);
64f963cc26SEric Auger 
655a4348d1SPeter Crosthwaite int qemu_fdt_setprop(void *fdt, const char *node_path,
66be5907f2SPeter Crosthwaite                      const char *property, const void *val, int size);
675a4348d1SPeter Crosthwaite int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
68f652e6afSaurel32                           const char *property, uint32_t val);
695a4348d1SPeter Crosthwaite int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
70bb28eb37SAlexander Graf                          const char *property, uint64_t val);
715a4348d1SPeter Crosthwaite int qemu_fdt_setprop_string(void *fdt, const char *node_path,
72f652e6afSaurel32                             const char *property, const char *string);
735a4348d1SPeter Crosthwaite int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
748535ab12SAlexander Graf                              const char *property,
758535ab12SAlexander Graf                              const char *target_node_path);
7678e24f23SEric Auger /**
7778e24f23SEric Auger  * qemu_fdt_getprop: retrieve the value of a given property
7878e24f23SEric Auger  * @fdt: pointer to the device tree blob
7978e24f23SEric Auger  * @node_path: node path
8078e24f23SEric Auger  * @property: name of the property to find
8178e24f23SEric Auger  * @lenp: fdt error if any or length of the property on success
8278e24f23SEric Auger  * @errp: handle to an error object
8378e24f23SEric Auger  *
8478e24f23SEric Auger  * returns a pointer to the property on success and NULL on failure
8578e24f23SEric Auger  */
865a4348d1SPeter Crosthwaite const void *qemu_fdt_getprop(void *fdt, const char *node_path,
8778e24f23SEric Auger                              const char *property, int *lenp,
8878e24f23SEric Auger                              Error **errp);
8958e71097SEric Auger /**
9058e71097SEric Auger  * qemu_fdt_getprop_cell: retrieve the value of a given 4 byte property
9158e71097SEric Auger  * @fdt: pointer to the device tree blob
9258e71097SEric Auger  * @node_path: node path
9358e71097SEric Auger  * @property: name of the property to find
9458e71097SEric Auger  * @lenp: fdt error if any or -EINVAL if the property size is different from
9558e71097SEric Auger  *        4 bytes, or 4 (expected length of the property) upon success.
9658e71097SEric Auger  * @errp: handle to an error object
9758e71097SEric Auger  *
9858e71097SEric Auger  * returns the property value on success
9958e71097SEric Auger  */
1005a4348d1SPeter Crosthwaite uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
10158e71097SEric Auger                                const char *property, int *lenp,
10258e71097SEric Auger                                Error **errp);
1035a4348d1SPeter Crosthwaite uint32_t qemu_fdt_get_phandle(void *fdt, const char *path);
1045a4348d1SPeter Crosthwaite uint32_t qemu_fdt_alloc_phandle(void *fdt);
1055a4348d1SPeter Crosthwaite int qemu_fdt_nop_node(void *fdt, const char *node_path);
1065a4348d1SPeter Crosthwaite int qemu_fdt_add_subnode(void *fdt, const char *name);
107f652e6afSaurel32 
1085a4348d1SPeter Crosthwaite #define qemu_fdt_setprop_cells(fdt, node_path, property, ...)                 \
1097ae2291eSAlexander Graf     do {                                                                      \
1107ae2291eSAlexander Graf         uint32_t qdt_tmp[] = { __VA_ARGS__ };                                 \
1117ae2291eSAlexander Graf         int i;                                                                \
1127ae2291eSAlexander Graf                                                                               \
1137ae2291eSAlexander Graf         for (i = 0; i < ARRAY_SIZE(qdt_tmp); i++) {                           \
1147ae2291eSAlexander Graf             qdt_tmp[i] = cpu_to_be32(qdt_tmp[i]);                             \
1157ae2291eSAlexander Graf         }                                                                     \
1165a4348d1SPeter Crosthwaite         qemu_fdt_setprop(fdt, node_path, property, qdt_tmp,                   \
1177ae2291eSAlexander Graf                          sizeof(qdt_tmp));                                    \
1187ae2291eSAlexander Graf     } while (0)
1197ae2291eSAlexander Graf 
1205a4348d1SPeter Crosthwaite void qemu_fdt_dumpdtb(void *fdt, int size);
12171193433SAlexander Graf 
12297c38f8cSPeter Maydell /**
1235a4348d1SPeter Crosthwaite  * qemu_fdt_setprop_sized_cells_from_array:
12497c38f8cSPeter Maydell  * @fdt: device tree blob
12597c38f8cSPeter Maydell  * @node_path: node to set property on
12697c38f8cSPeter Maydell  * @property: property to set
12797c38f8cSPeter Maydell  * @numvalues: number of values
12897c38f8cSPeter Maydell  * @values: array of number-of-cells, value pairs
12997c38f8cSPeter Maydell  *
13097c38f8cSPeter Maydell  * Set the specified property on the specified node in the device tree
13197c38f8cSPeter Maydell  * to be an array of cells. The values of the cells are specified via
13297c38f8cSPeter Maydell  * the values list, which alternates between "number of cells used by
13397c38f8cSPeter Maydell  * this value" and "value".
13497c38f8cSPeter Maydell  * number-of-cells must be either 1 or 2 (other values will result in
13597c38f8cSPeter Maydell  * an error being returned). If a value is too large to fit in the
13697c38f8cSPeter Maydell  * number of cells specified for it, an error is returned.
13797c38f8cSPeter Maydell  *
13897c38f8cSPeter Maydell  * This function is useful because device tree nodes often have cell arrays
13997c38f8cSPeter Maydell  * which are either lists of addresses or lists of address,size tuples, but
14097c38f8cSPeter Maydell  * the number of cells used for each element vary depending on the
14197c38f8cSPeter Maydell  * #address-cells and #size-cells properties of their parent node.
14297c38f8cSPeter Maydell  * If you know all your cell elements are one cell wide you can use the
1435a4348d1SPeter Crosthwaite  * simpler qemu_fdt_setprop_cells(). If you're not setting up the
1445a4348d1SPeter Crosthwaite  * array programmatically, qemu_fdt_setprop_sized_cells may be more
14597c38f8cSPeter Maydell  * convenient.
14697c38f8cSPeter Maydell  *
14797c38f8cSPeter Maydell  * Return value: 0 on success, <0 on error.
14897c38f8cSPeter Maydell  */
1495a4348d1SPeter Crosthwaite int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
15097c38f8cSPeter Maydell                                             const char *node_path,
15197c38f8cSPeter Maydell                                             const char *property,
15297c38f8cSPeter Maydell                                             int numvalues,
15397c38f8cSPeter Maydell                                             uint64_t *values);
15497c38f8cSPeter Maydell 
15597c38f8cSPeter Maydell /**
1565a4348d1SPeter Crosthwaite  * qemu_fdt_setprop_sized_cells:
15797c38f8cSPeter Maydell  * @fdt: device tree blob
15897c38f8cSPeter Maydell  * @node_path: node to set property on
15997c38f8cSPeter Maydell  * @property: property to set
16097c38f8cSPeter Maydell  * @...: list of number-of-cells, value pairs
16197c38f8cSPeter Maydell  *
16297c38f8cSPeter Maydell  * Set the specified property on the specified node in the device tree
16397c38f8cSPeter Maydell  * to be an array of cells. The values of the cells are specified via
16497c38f8cSPeter Maydell  * the variable arguments, which alternates between "number of cells
16597c38f8cSPeter Maydell  * used by this value" and "value".
16697c38f8cSPeter Maydell  *
16797c38f8cSPeter Maydell  * This is a convenience wrapper for the function
1685a4348d1SPeter Crosthwaite  * qemu_fdt_setprop_sized_cells_from_array().
16997c38f8cSPeter Maydell  *
17097c38f8cSPeter Maydell  * Return value: 0 on success, <0 on error.
17197c38f8cSPeter Maydell  */
1725a4348d1SPeter Crosthwaite #define qemu_fdt_setprop_sized_cells(fdt, node_path, property, ...)       \
17397c38f8cSPeter Maydell     ({                                                                    \
17497c38f8cSPeter Maydell         uint64_t qdt_tmp[] = { __VA_ARGS__ };                             \
1755a4348d1SPeter Crosthwaite         qemu_fdt_setprop_sized_cells_from_array(fdt, node_path,           \
17697c38f8cSPeter Maydell                                                 property,                 \
17797c38f8cSPeter Maydell                                                 ARRAY_SIZE(qdt_tmp) / 2,  \
17897c38f8cSPeter Maydell                                                 qdt_tmp);                 \
17997c38f8cSPeter Maydell     })
18097c38f8cSPeter Maydell 
1814ab29b82SAlexander Graf #define FDT_PCI_RANGE_RELOCATABLE          0x80000000
1824ab29b82SAlexander Graf #define FDT_PCI_RANGE_PREFETCHABLE         0x40000000
1834ab29b82SAlexander Graf #define FDT_PCI_RANGE_ALIASED              0x20000000
1844ab29b82SAlexander Graf #define FDT_PCI_RANGE_TYPE_MASK            0x03000000
1854ab29b82SAlexander Graf #define FDT_PCI_RANGE_MMIO_64BIT           0x03000000
1864ab29b82SAlexander Graf #define FDT_PCI_RANGE_MMIO                 0x02000000
1874ab29b82SAlexander Graf #define FDT_PCI_RANGE_IOPORT               0x01000000
1884ab29b82SAlexander Graf #define FDT_PCI_RANGE_CONFIG               0x00000000
1894ab29b82SAlexander Graf 
1902a6a4076SMarkus Armbruster #endif /* DEVICE_TREE_H */
191