xref: /openbmc/qemu/include/sysemu/device_tree.h (revision 48151859)
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 char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
44                           Error **errp);
45 
46 int qemu_fdt_setprop(void *fdt, const char *node_path,
47                      const char *property, const void *val, int size);
48 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
49                           const char *property, uint32_t val);
50 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
51                          const char *property, uint64_t val);
52 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
53                             const char *property, const char *string);
54 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
55                              const char *property,
56                              const char *target_node_path);
57 /**
58  * qemu_fdt_getprop: retrieve the value of a given property
59  * @fdt: pointer to the device tree blob
60  * @node_path: node path
61  * @property: name of the property to find
62  * @lenp: fdt error if any or length of the property on success
63  * @errp: handle to an error object
64  *
65  * returns a pointer to the property on success and NULL on failure
66  */
67 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
68                              const char *property, int *lenp,
69                              Error **errp);
70 /**
71  * qemu_fdt_getprop_cell: retrieve the value of a given 4 byte property
72  * @fdt: pointer to the device tree blob
73  * @node_path: node path
74  * @property: name of the property to find
75  * @lenp: fdt error if any or -EINVAL if the property size is different from
76  *        4 bytes, or 4 (expected length of the property) upon success.
77  * @errp: handle to an error object
78  *
79  * returns the property value on success
80  */
81 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
82                                const char *property, int *lenp,
83                                Error **errp);
84 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path);
85 uint32_t qemu_fdt_alloc_phandle(void *fdt);
86 int qemu_fdt_nop_node(void *fdt, const char *node_path);
87 int qemu_fdt_add_subnode(void *fdt, const char *name);
88 
89 #define qemu_fdt_setprop_cells(fdt, node_path, property, ...)                 \
90     do {                                                                      \
91         uint32_t qdt_tmp[] = { __VA_ARGS__ };                                 \
92         int i;                                                                \
93                                                                               \
94         for (i = 0; i < ARRAY_SIZE(qdt_tmp); i++) {                           \
95             qdt_tmp[i] = cpu_to_be32(qdt_tmp[i]);                             \
96         }                                                                     \
97         qemu_fdt_setprop(fdt, node_path, property, qdt_tmp,                   \
98                          sizeof(qdt_tmp));                                    \
99     } while (0)
100 
101 void qemu_fdt_dumpdtb(void *fdt, int size);
102 
103 /**
104  * qemu_fdt_setprop_sized_cells_from_array:
105  * @fdt: device tree blob
106  * @node_path: node to set property on
107  * @property: property to set
108  * @numvalues: number of values
109  * @values: array of number-of-cells, value pairs
110  *
111  * Set the specified property on the specified node in the device tree
112  * to be an array of cells. The values of the cells are specified via
113  * the values list, which alternates between "number of cells used by
114  * this value" and "value".
115  * number-of-cells must be either 1 or 2 (other values will result in
116  * an error being returned). If a value is too large to fit in the
117  * number of cells specified for it, an error is returned.
118  *
119  * This function is useful because device tree nodes often have cell arrays
120  * which are either lists of addresses or lists of address,size tuples, but
121  * the number of cells used for each element vary depending on the
122  * #address-cells and #size-cells properties of their parent node.
123  * If you know all your cell elements are one cell wide you can use the
124  * simpler qemu_fdt_setprop_cells(). If you're not setting up the
125  * array programmatically, qemu_fdt_setprop_sized_cells may be more
126  * convenient.
127  *
128  * Return value: 0 on success, <0 on error.
129  */
130 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
131                                             const char *node_path,
132                                             const char *property,
133                                             int numvalues,
134                                             uint64_t *values);
135 
136 /**
137  * qemu_fdt_setprop_sized_cells:
138  * @fdt: device tree blob
139  * @node_path: node to set property on
140  * @property: property to set
141  * @...: list of number-of-cells, value pairs
142  *
143  * Set the specified property on the specified node in the device tree
144  * to be an array of cells. The values of the cells are specified via
145  * the variable arguments, which alternates between "number of cells
146  * used by this value" and "value".
147  *
148  * This is a convenience wrapper for the function
149  * qemu_fdt_setprop_sized_cells_from_array().
150  *
151  * Return value: 0 on success, <0 on error.
152  */
153 #define qemu_fdt_setprop_sized_cells(fdt, node_path, property, ...)       \
154     ({                                                                    \
155         uint64_t qdt_tmp[] = { __VA_ARGS__ };                             \
156         qemu_fdt_setprop_sized_cells_from_array(fdt, node_path,           \
157                                                 property,                 \
158                                                 ARRAY_SIZE(qdt_tmp) / 2,  \
159                                                 qdt_tmp);                 \
160     })
161 
162 #define FDT_PCI_RANGE_RELOCATABLE          0x80000000
163 #define FDT_PCI_RANGE_PREFETCHABLE         0x40000000
164 #define FDT_PCI_RANGE_ALIASED              0x20000000
165 #define FDT_PCI_RANGE_TYPE_MASK            0x03000000
166 #define FDT_PCI_RANGE_MMIO_64BIT           0x03000000
167 #define FDT_PCI_RANGE_MMIO                 0x02000000
168 #define FDT_PCI_RANGE_IOPORT               0x01000000
169 #define FDT_PCI_RANGE_CONFIG               0x00000000
170 
171 #endif /* DEVICE_TREE_H */
172