xref: /openbmc/qemu/include/sysemu/device_tree.h (revision 720d6bcd)
19c17d615SPaolo Bonzini /*
29c17d615SPaolo Bonzini  * Header with function prototypes to help device tree manipulation using
39c17d615SPaolo Bonzini  * libfdt. It also provides functions to read entries from device tree proc
49c17d615SPaolo Bonzini  * interface.
59c17d615SPaolo Bonzini  *
69c17d615SPaolo Bonzini  * Copyright 2008 IBM Corporation.
79c17d615SPaolo Bonzini  * Authors: Jerone Young <jyoung5@us.ibm.com>
89c17d615SPaolo Bonzini  *          Hollis Blanchard <hollisb@us.ibm.com>
99c17d615SPaolo Bonzini  *
109c17d615SPaolo Bonzini  * This work is licensed under the GNU GPL license version 2 or later.
119c17d615SPaolo Bonzini  *
129c17d615SPaolo Bonzini  */
139c17d615SPaolo Bonzini 
142a6a4076SMarkus Armbruster #ifndef DEVICE_TREE_H
152a6a4076SMarkus Armbruster #define DEVICE_TREE_H
169c17d615SPaolo Bonzini 
179c17d615SPaolo Bonzini void *create_device_tree(int *sizep);
189c17d615SPaolo Bonzini 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
279c17d615SPaolo Bonzini 
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
4280972d3bSEdgar E. Iglesias  *
4380972d3bSEdgar E. Iglesias  * @name may be NULL to wildcard names and only match compatibility
4480972d3bSEdgar E. Iglesias  * strings.
456d79566aSEric Auger  */
46958bae18SEdgar E. Iglesias char **qemu_fdt_node_path(void *fdt, const char *name, const 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,
689c17d615SPaolo Bonzini                           const char *property, uint32_t val);
695a4348d1SPeter Crosthwaite int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
709c17d615SPaolo Bonzini                          const char *property, uint64_t val);
715a4348d1SPeter Crosthwaite int qemu_fdt_setprop_string(void *fdt, const char *node_path,
729c17d615SPaolo Bonzini                             const char *property, const char *string);
7378da6a1bSAlex Bennée 
7478da6a1bSAlex Bennée /**
7578da6a1bSAlex Bennée  * qemu_fdt_setprop_string_array: set a string array property
7678da6a1bSAlex Bennée  *
7778da6a1bSAlex Bennée  * @fdt: pointer to the dt blob
7878da6a1bSAlex Bennée  * @name: node name
7978da6a1bSAlex Bennée  * @prop: property array
8078da6a1bSAlex Bennée  * @array: pointer to an array of string pointers
8178da6a1bSAlex Bennée  * @len: length of array
8278da6a1bSAlex Bennée  *
8378da6a1bSAlex Bennée  * assigns a string array to a property. This function converts and
8478da6a1bSAlex Bennée  * array of strings to a sequential string with \0 separators before
8578da6a1bSAlex Bennée  * setting the property.
8678da6a1bSAlex Bennée  */
8778da6a1bSAlex Bennée int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
8878da6a1bSAlex Bennée                                   const char *prop, char **array, int len);
8978da6a1bSAlex Bennée 
905a4348d1SPeter Crosthwaite int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
919c17d615SPaolo Bonzini                              const char *property,
929c17d615SPaolo Bonzini                              const char *target_node_path);
9378e24f23SEric Auger /**
9478e24f23SEric Auger  * qemu_fdt_getprop: retrieve the value of a given property
9578e24f23SEric Auger  * @fdt: pointer to the device tree blob
9678e24f23SEric Auger  * @node_path: node path
9778e24f23SEric Auger  * @property: name of the property to find
9878e24f23SEric Auger  * @lenp: fdt error if any or length of the property on success
9978e24f23SEric Auger  * @errp: handle to an error object
10078e24f23SEric Auger  *
10178e24f23SEric Auger  * returns a pointer to the property on success and NULL on failure
10278e24f23SEric Auger  */
1035a4348d1SPeter Crosthwaite const void *qemu_fdt_getprop(void *fdt, const char *node_path,
10478e24f23SEric Auger                              const char *property, int *lenp,
10578e24f23SEric Auger                              Error **errp);
10658e71097SEric Auger /**
10758e71097SEric Auger  * qemu_fdt_getprop_cell: retrieve the value of a given 4 byte property
10858e71097SEric Auger  * @fdt: pointer to the device tree blob
10958e71097SEric Auger  * @node_path: node path
11058e71097SEric Auger  * @property: name of the property to find
11158e71097SEric Auger  * @lenp: fdt error if any or -EINVAL if the property size is different from
11258e71097SEric Auger  *        4 bytes, or 4 (expected length of the property) upon success.
11358e71097SEric Auger  * @errp: handle to an error object
11458e71097SEric Auger  *
11558e71097SEric Auger  * returns the property value on success
11658e71097SEric Auger  */
1175a4348d1SPeter Crosthwaite uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
11858e71097SEric Auger                                const char *property, int *lenp,
11958e71097SEric Auger                                Error **errp);
1205a4348d1SPeter Crosthwaite uint32_t qemu_fdt_get_phandle(void *fdt, const char *path);
1215a4348d1SPeter Crosthwaite uint32_t qemu_fdt_alloc_phandle(void *fdt);
1225a4348d1SPeter Crosthwaite int qemu_fdt_nop_node(void *fdt, const char *node_path);
1235a4348d1SPeter Crosthwaite int qemu_fdt_add_subnode(void *fdt, const char *name);
124b863f0b7SYanan Wang int qemu_fdt_add_path(void *fdt, const char *path);
1259c17d615SPaolo Bonzini 
1265a4348d1SPeter Crosthwaite #define qemu_fdt_setprop_cells(fdt, node_path, property, ...)                 \
1279c17d615SPaolo Bonzini     do {                                                                      \
1289c17d615SPaolo Bonzini         uint32_t qdt_tmp[] = { __VA_ARGS__ };                                 \
129*720d6bcdSPhilippe Mathieu-Daudé         for (unsigned i_ = 0; i_ < ARRAY_SIZE(qdt_tmp); i_++) {               \
130*720d6bcdSPhilippe Mathieu-Daudé             qdt_tmp[i_] = cpu_to_be32(qdt_tmp[i_]);                           \
1319c17d615SPaolo Bonzini         }                                                                     \
1325a4348d1SPeter Crosthwaite         qemu_fdt_setprop(fdt, node_path, property, qdt_tmp,                   \
1339c17d615SPaolo Bonzini                          sizeof(qdt_tmp));                                    \
1349c17d615SPaolo Bonzini     } while (0)
1359c17d615SPaolo Bonzini 
1365a4348d1SPeter Crosthwaite void qemu_fdt_dumpdtb(void *fdt, int size);
137bf353ad5SDaniel Henrique Barboza void hmp_dumpdtb(Monitor *mon, const QDict *qdict);
1389c17d615SPaolo Bonzini 
13997c38f8cSPeter Maydell /**
1405a4348d1SPeter Crosthwaite  * qemu_fdt_setprop_sized_cells_from_array:
14197c38f8cSPeter Maydell  * @fdt: device tree blob
14297c38f8cSPeter Maydell  * @node_path: node to set property on
14397c38f8cSPeter Maydell  * @property: property to set
14497c38f8cSPeter Maydell  * @numvalues: number of values
14597c38f8cSPeter Maydell  * @values: array of number-of-cells, value pairs
14697c38f8cSPeter Maydell  *
14797c38f8cSPeter Maydell  * Set the specified property on the specified node in the device tree
14897c38f8cSPeter Maydell  * to be an array of cells. The values of the cells are specified via
14997c38f8cSPeter Maydell  * the values list, which alternates between "number of cells used by
15097c38f8cSPeter Maydell  * this value" and "value".
15197c38f8cSPeter Maydell  * number-of-cells must be either 1 or 2 (other values will result in
15297c38f8cSPeter Maydell  * an error being returned). If a value is too large to fit in the
15397c38f8cSPeter Maydell  * number of cells specified for it, an error is returned.
15497c38f8cSPeter Maydell  *
15597c38f8cSPeter Maydell  * This function is useful because device tree nodes often have cell arrays
15697c38f8cSPeter Maydell  * which are either lists of addresses or lists of address,size tuples, but
15797c38f8cSPeter Maydell  * the number of cells used for each element vary depending on the
15897c38f8cSPeter Maydell  * #address-cells and #size-cells properties of their parent node.
15997c38f8cSPeter Maydell  * If you know all your cell elements are one cell wide you can use the
1605a4348d1SPeter Crosthwaite  * simpler qemu_fdt_setprop_cells(). If you're not setting up the
1615a4348d1SPeter Crosthwaite  * array programmatically, qemu_fdt_setprop_sized_cells may be more
16297c38f8cSPeter Maydell  * convenient.
16397c38f8cSPeter Maydell  *
16497c38f8cSPeter Maydell  * Return value: 0 on success, <0 on error.
16597c38f8cSPeter Maydell  */
1665a4348d1SPeter Crosthwaite int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
16797c38f8cSPeter Maydell                                             const char *node_path,
16897c38f8cSPeter Maydell                                             const char *property,
16997c38f8cSPeter Maydell                                             int numvalues,
17097c38f8cSPeter Maydell                                             uint64_t *values);
17197c38f8cSPeter Maydell 
17297c38f8cSPeter Maydell /**
1735a4348d1SPeter Crosthwaite  * qemu_fdt_setprop_sized_cells:
17497c38f8cSPeter Maydell  * @fdt: device tree blob
17597c38f8cSPeter Maydell  * @node_path: node to set property on
17697c38f8cSPeter Maydell  * @property: property to set
17797c38f8cSPeter Maydell  * @...: list of number-of-cells, value pairs
17897c38f8cSPeter Maydell  *
17997c38f8cSPeter Maydell  * Set the specified property on the specified node in the device tree
18097c38f8cSPeter Maydell  * to be an array of cells. The values of the cells are specified via
18197c38f8cSPeter Maydell  * the variable arguments, which alternates between "number of cells
18297c38f8cSPeter Maydell  * used by this value" and "value".
18397c38f8cSPeter Maydell  *
18497c38f8cSPeter Maydell  * This is a convenience wrapper for the function
1855a4348d1SPeter Crosthwaite  * qemu_fdt_setprop_sized_cells_from_array().
18697c38f8cSPeter Maydell  *
18797c38f8cSPeter Maydell  * Return value: 0 on success, <0 on error.
18897c38f8cSPeter Maydell  */
1895a4348d1SPeter Crosthwaite #define qemu_fdt_setprop_sized_cells(fdt, node_path, property, ...)       \
19097c38f8cSPeter Maydell     ({                                                                    \
19197c38f8cSPeter Maydell         uint64_t qdt_tmp[] = { __VA_ARGS__ };                             \
1925a4348d1SPeter Crosthwaite         qemu_fdt_setprop_sized_cells_from_array(fdt, node_path,           \
19397c38f8cSPeter Maydell                                                 property,                 \
19497c38f8cSPeter Maydell                                                 ARRAY_SIZE(qdt_tmp) / 2,  \
19597c38f8cSPeter Maydell                                                 qdt_tmp);                 \
19697c38f8cSPeter Maydell     })
19797c38f8cSPeter Maydell 
198e1e618b9SJason A. Donenfeld 
199e1e618b9SJason A. Donenfeld /**
200e1e618b9SJason A. Donenfeld  * qemu_fdt_randomize_seeds:
201e1e618b9SJason A. Donenfeld  * @fdt: device tree blob
202e1e618b9SJason A. Donenfeld  *
203e1e618b9SJason A. Donenfeld  * Re-randomize all "rng-seed" properties with new seeds.
204e1e618b9SJason A. Donenfeld  */
205e1e618b9SJason A. Donenfeld void qemu_fdt_randomize_seeds(void *fdt);
206e1e618b9SJason A. Donenfeld 
2074ab29b82SAlexander Graf #define FDT_PCI_RANGE_RELOCATABLE          0x80000000
2084ab29b82SAlexander Graf #define FDT_PCI_RANGE_PREFETCHABLE         0x40000000
2094ab29b82SAlexander Graf #define FDT_PCI_RANGE_ALIASED              0x20000000
2104ab29b82SAlexander Graf #define FDT_PCI_RANGE_TYPE_MASK            0x03000000
2114ab29b82SAlexander Graf #define FDT_PCI_RANGE_MMIO_64BIT           0x03000000
2124ab29b82SAlexander Graf #define FDT_PCI_RANGE_MMIO                 0x02000000
2134ab29b82SAlexander Graf #define FDT_PCI_RANGE_IOPORT               0x01000000
2144ab29b82SAlexander Graf #define FDT_PCI_RANGE_CONFIG               0x00000000
2154ab29b82SAlexander Graf 
2162a6a4076SMarkus Armbruster #endif /* DEVICE_TREE_H */
217