xref: /qemu/include/system/device_tree.h (revision 60e43e987c3d188855dc7e95ff4979be78db3574)
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 int qemu_fdt_setprop(void *fdt, const char *node_path,
29                      const char *property, const void *val, int size);
30 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
31                           const char *property, uint32_t val);
32 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
33                          const char *property, uint64_t val);
34 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
35                             const char *property, const char *string);
36 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
37                              const char *property,
38                              const char *target_node_path);
39 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
40                              const char *property, int *lenp);
41 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
42                                const char *property);
43 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path);
44 uint32_t qemu_fdt_alloc_phandle(void *fdt);
45 int qemu_fdt_nop_node(void *fdt, const char *node_path);
46 int qemu_fdt_add_subnode(void *fdt, const char *name);
47 
48 #define qemu_fdt_setprop_cells(fdt, node_path, property, ...)                 \
49     do {                                                                      \
50         uint32_t qdt_tmp[] = { __VA_ARGS__ };                                 \
51         int i;                                                                \
52                                                                               \
53         for (i = 0; i < ARRAY_SIZE(qdt_tmp); i++) {                           \
54             qdt_tmp[i] = cpu_to_be32(qdt_tmp[i]);                             \
55         }                                                                     \
56         qemu_fdt_setprop(fdt, node_path, property, qdt_tmp,                   \
57                          sizeof(qdt_tmp));                                    \
58     } while (0)
59 
60 void qemu_fdt_dumpdtb(void *fdt, int size);
61 
62 /**
63  * qemu_fdt_setprop_sized_cells_from_array:
64  * @fdt: device tree blob
65  * @node_path: node to set property on
66  * @property: property to set
67  * @numvalues: number of values
68  * @values: array of number-of-cells, value pairs
69  *
70  * Set the specified property on the specified node in the device tree
71  * to be an array of cells. The values of the cells are specified via
72  * the values list, which alternates between "number of cells used by
73  * this value" and "value".
74  * number-of-cells must be either 1 or 2 (other values will result in
75  * an error being returned). If a value is too large to fit in the
76  * number of cells specified for it, an error is returned.
77  *
78  * This function is useful because device tree nodes often have cell arrays
79  * which are either lists of addresses or lists of address,size tuples, but
80  * the number of cells used for each element vary depending on the
81  * #address-cells and #size-cells properties of their parent node.
82  * If you know all your cell elements are one cell wide you can use the
83  * simpler qemu_fdt_setprop_cells(). If you're not setting up the
84  * array programmatically, qemu_fdt_setprop_sized_cells may be more
85  * convenient.
86  *
87  * Return value: 0 on success, <0 on error.
88  */
89 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
90                                             const char *node_path,
91                                             const char *property,
92                                             int numvalues,
93                                             uint64_t *values);
94 
95 /**
96  * qemu_fdt_setprop_sized_cells:
97  * @fdt: device tree blob
98  * @node_path: node to set property on
99  * @property: property to set
100  * @...: list of number-of-cells, value pairs
101  *
102  * Set the specified property on the specified node in the device tree
103  * to be an array of cells. The values of the cells are specified via
104  * the variable arguments, which alternates between "number of cells
105  * used by this value" and "value".
106  *
107  * This is a convenience wrapper for the function
108  * qemu_fdt_setprop_sized_cells_from_array().
109  *
110  * Return value: 0 on success, <0 on error.
111  */
112 #define qemu_fdt_setprop_sized_cells(fdt, node_path, property, ...)       \
113     ({                                                                    \
114         uint64_t qdt_tmp[] = { __VA_ARGS__ };                             \
115         qemu_fdt_setprop_sized_cells_from_array(fdt, node_path,           \
116                                                 property,                 \
117                                                 ARRAY_SIZE(qdt_tmp) / 2,  \
118                                                 qdt_tmp);                 \
119     })
120 
121 #define FDT_PCI_RANGE_RELOCATABLE          0x80000000
122 #define FDT_PCI_RANGE_PREFETCHABLE         0x40000000
123 #define FDT_PCI_RANGE_ALIASED              0x20000000
124 #define FDT_PCI_RANGE_TYPE_MASK            0x03000000
125 #define FDT_PCI_RANGE_MMIO_64BIT           0x03000000
126 #define FDT_PCI_RANGE_MMIO                 0x02000000
127 #define FDT_PCI_RANGE_IOPORT               0x01000000
128 #define FDT_PCI_RANGE_CONFIG               0x00000000
129 
130 #endif /* __DEVICE_TREE_H__ */
131