xref: /qemu/include/system/device_tree.h (revision 6ff5da16000f908140723e164d33a0b51a6c4162)
1 /*
2  * Header with function prototypes to help device tree manipulation using
3  * libfdt. It also provides functions to read entries from device tree proc
4  * interface.
5  *
6  * Copyright 2008 IBM Corporation.
7  * Authors: Jerone Young <jyoung5@us.ibm.com>
8  *          Hollis Blanchard <hollisb@us.ibm.com>
9  *
10  * This work is licensed under the GNU GPL license version 2 or later.
11  *
12  */
13 
14 #ifndef DEVICE_TREE_H
15 #define DEVICE_TREE_H
16 
17 void *create_device_tree(int *sizep);
18 void *load_device_tree(const char *filename_path, int *sizep);
19 #ifdef CONFIG_LINUX
20 /**
21  * load_device_tree_from_sysfs: reads the device tree information in the
22  * /proc/device-tree directory and return the corresponding binary blob
23  * buffer pointer. Asserts in case of error.
24  */
25 void *load_device_tree_from_sysfs(void);
26 #endif
27 
28 /**
29  * qemu_fdt_node_path: return the paths of nodes matching a given
30  * name and compat string
31  * @fdt: pointer to the dt blob
32  * @name: node name
33  * @compat: compatibility string
34  * @errp: handle to an error object
35  *
36  * returns a newly allocated NULL-terminated array of node paths.
37  * Use g_strfreev() to free it. If one or more nodes were found, the
38  * array contains the path of each node and the last element equals to
39  * NULL. If there is no error but no matching node was found, the
40  * returned array contains a single element equal to NULL. If an error
41  * was encountered when parsing the blob, the function returns NULL
42  *
43  * @name may be NULL to wildcard names and only match compatibility
44  * strings.
45  */
46 char **qemu_fdt_node_path(void *fdt, const char *name, const char *compat,
47                           Error **errp);
48 
49 /**
50  * qemu_fdt_node_unit_path: return the paths of nodes matching a given
51  * node-name, ie. node-name and node-name@unit-address
52  * @fdt: pointer to the dt blob
53  * @name: node name
54  * @errp: handle to an error object
55  *
56  * returns a newly allocated NULL-terminated array of node paths.
57  * Use g_strfreev() to free it. If one or more nodes were found, the
58  * array contains the path of each node and the last element equals to
59  * NULL. If there is no error but no matching node was found, the
60  * returned array contains a single element equal to NULL. If an error
61  * was encountered when parsing the blob, the function returns NULL
62  */
63 char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp);
64 
65 int qemu_fdt_setprop(void *fdt, const char *node_path,
66                      const char *property, const void *val, int size);
67 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
68                           const char *property, uint32_t val);
69 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
70                          const char *property, uint64_t val);
71 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
72                             const char *property, const char *string);
73 
74 /**
75  * qemu_fdt_setprop_string_array: set a string array property
76  *
77  * @fdt: pointer to the dt blob
78  * @name: node name
79  * @prop: property array
80  * @array: pointer to an array of string pointers
81  * @len: length of array
82  *
83  * assigns a string array to a property. This function converts and
84  * array of strings to a sequential string with \0 separators before
85  * setting the property.
86  */
87 int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
88                                   const char *prop, char **array, int len);
89 
90 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
91                              const char *property,
92                              const char *target_node_path);
93 /**
94  * qemu_fdt_getprop: retrieve the value of a given property
95  * @fdt: pointer to the device tree blob
96  * @node_path: node path
97  * @property: name of the property to find
98  * @lenp: fdt error if any or length of the property on success
99  * @errp: handle to an error object
100  *
101  * returns a pointer to the property on success and NULL on failure
102  */
103 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
104                              const char *property, int *lenp,
105                              Error **errp);
106 /**
107  * qemu_fdt_getprop_cell: retrieve the value of a given 4 byte property
108  * @fdt: pointer to the device tree blob
109  * @node_path: node path
110  * @property: name of the property to find
111  * @lenp: fdt error if any or -EINVAL if the property size is different from
112  *        4 bytes, or 4 (expected length of the property) upon success.
113  * @errp: handle to an error object
114  *
115  * returns the property value on success
116  */
117 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
118                                const char *property, int *lenp,
119                                Error **errp);
120 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path);
121 uint32_t qemu_fdt_alloc_phandle(void *fdt);
122 int qemu_fdt_nop_node(void *fdt, const char *node_path);
123 int qemu_fdt_add_subnode(void *fdt, const char *name);
124 int qemu_fdt_add_path(void *fdt, const char *path);
125 
126 #define qemu_fdt_setprop_cells(fdt, node_path, property, ...)                 \
127     do {                                                                      \
128         uint32_t qdt_tmp[] = { __VA_ARGS__ };                                 \
129         for (unsigned i_ = 0; i_ < ARRAY_SIZE(qdt_tmp); i_++) {               \
130             qdt_tmp[i_] = cpu_to_be32(qdt_tmp[i_]);                           \
131         }                                                                     \
132         qemu_fdt_setprop(fdt, node_path, property, qdt_tmp,                   \
133                          sizeof(qdt_tmp));                                    \
134     } while (0)
135 
136 /**
137  * qemu_fdt_setprop_sized_cells_from_array:
138  * @fdt: device tree blob
139  * @node_path: node to set property on
140  * @property: property to set
141  * @numvalues: number of values
142  * @values: array of number-of-cells, value pairs
143  *
144  * Set the specified property on the specified node in the device tree
145  * to be an array of cells. The values of the cells are specified via
146  * the values list, which alternates between "number of cells used by
147  * this value" and "value".
148  * number-of-cells must be either 1 or 2 (other values will result in
149  * an error being returned). If a value is too large to fit in the
150  * number of cells specified for it, an error is returned.
151  *
152  * This function is useful because device tree nodes often have cell arrays
153  * which are either lists of addresses or lists of address,size tuples, but
154  * the number of cells used for each element vary depending on the
155  * #address-cells and #size-cells properties of their parent node.
156  * If you know all your cell elements are one cell wide you can use the
157  * simpler qemu_fdt_setprop_cells(). If you're not setting up the
158  * array programmatically, qemu_fdt_setprop_sized_cells may be more
159  * convenient.
160  *
161  * Return value: 0 on success, <0 on error.
162  */
163 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
164                                             const char *node_path,
165                                             const char *property,
166                                             int numvalues,
167                                             uint64_t *values);
168 
169 /**
170  * qemu_fdt_setprop_sized_cells:
171  * @fdt: device tree blob
172  * @node_path: node to set property on
173  * @property: property to set
174  * @...: list of number-of-cells, value pairs
175  *
176  * Set the specified property on the specified node in the device tree
177  * to be an array of cells. The values of the cells are specified via
178  * the variable arguments, which alternates between "number of cells
179  * used by this value" and "value".
180  *
181  * This is a convenience wrapper for the function
182  * qemu_fdt_setprop_sized_cells_from_array().
183  *
184  * Return value: 0 on success, <0 on error.
185  */
186 #define qemu_fdt_setprop_sized_cells(fdt, node_path, property, ...)       \
187     ({                                                                    \
188         uint64_t qdt_tmp[] = { __VA_ARGS__ };                             \
189         qemu_fdt_setprop_sized_cells_from_array(fdt, node_path,           \
190                                                 property,                 \
191                                                 ARRAY_SIZE(qdt_tmp) / 2,  \
192                                                 qdt_tmp);                 \
193     })
194 
195 
196 /**
197  * qemu_fdt_randomize_seeds:
198  * @fdt: device tree blob
199  *
200  * Re-randomize all "rng-seed" properties with new seeds.
201  */
202 void qemu_fdt_randomize_seeds(void *fdt);
203 
204 #define FDT_PCI_RANGE_RELOCATABLE          0x80000000
205 #define FDT_PCI_RANGE_PREFETCHABLE         0x40000000
206 #define FDT_PCI_RANGE_ALIASED              0x20000000
207 #define FDT_PCI_RANGE_TYPE_MASK            0x03000000
208 #define FDT_PCI_RANGE_MMIO_64BIT           0x03000000
209 #define FDT_PCI_RANGE_MMIO                 0x02000000
210 #define FDT_PCI_RANGE_IOPORT               0x01000000
211 #define FDT_PCI_RANGE_CONFIG               0x00000000
212 
213 #endif /* DEVICE_TREE_H */
214