qemu/include/sysemu/device_tree.h
<<
>>
Prefs
   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
  17void *create_device_tree(int *sizep);
  18void *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 */
  25void *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 */
  43char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
  44                          Error **errp);
  45
  46int qemu_fdt_setprop(void *fdt, const char *node_path,
  47                     const char *property, const void *val, int size);
  48int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
  49                          const char *property, uint32_t val);
  50int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
  51                         const char *property, uint64_t val);
  52int qemu_fdt_setprop_string(void *fdt, const char *node_path,
  53                            const char *property, const char *string);
  54int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
  55                             const char *property,
  56                             const char *target_node_path);
  57void *qemu_fdt_getprop(void *fdt, const char *node_path,
  58                             const char *property, int *lenp,
  59                             bool inherit, Error **errp);
  60uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
  61                               const char *property, int offset,
  62                               bool inherit, Error **errp);
  63uint64_t qemu_fdt_getprop_sized_cell(void *fdt, const char *node_path,
  64                                     const char *property, int offset,
  65                                     int size, Error **errp);
  66char *qemu_fdt_getprop_string(void *fdt, const char*node_path,
  67                              const char *property, int cell,
  68                              bool inherit, Error **errp);
  69uint32_t qemu_fdt_get_phandle(void *fdt, const char *path);
  70uint32_t qemu_fdt_check_phandle(void *fdt, const char *path);
  71uint32_t qemu_fdt_alloc_phandle(void *fdt);
  72int qemu_fdt_nop_node(void *fdt, const char *node_path);
  73int qemu_fdt_add_subnode(void *fdt, const char *name);
  74
  75#define qemu_fdt_setprop_cells(fdt, node_path, property, ...)                 \
  76    do {                                                                      \
  77        uint32_t qdt_tmp[] = { __VA_ARGS__ };                                 \
  78        int i;                                                                \
  79                                                                              \
  80        for (i = 0; i < ARRAY_SIZE(qdt_tmp); i++) {                           \
  81            qdt_tmp[i] = cpu_to_be32(qdt_tmp[i]);                             \
  82        }                                                                     \
  83        qemu_fdt_setprop(fdt, node_path, property, qdt_tmp,                   \
  84                         sizeof(qdt_tmp));                                    \
  85    } while (0)
  86
  87void qemu_fdt_dumpdtb(void *fdt, int size);
  88
  89/**
  90 * qemu_fdt_setprop_sized_cells_from_array:
  91 * @fdt: device tree blob
  92 * @node_path: node to set property on
  93 * @property: property to set
  94 * @numvalues: number of values
  95 * @values: array of number-of-cells, value pairs
  96 *
  97 * Set the specified property on the specified node in the device tree
  98 * to be an array of cells. The values of the cells are specified via
  99 * the values list, which alternates between "number of cells used by
 100 * this value" and "value".
 101 * number-of-cells must be either 1 or 2 (other values will result in
 102 * an error being returned). If a value is too large to fit in the
 103 * number of cells specified for it, an error is returned.
 104 *
 105 * This function is useful because device tree nodes often have cell arrays
 106 * which are either lists of addresses or lists of address,size tuples, but
 107 * the number of cells used for each element vary depending on the
 108 * #address-cells and #size-cells properties of their parent node.
 109 * If you know all your cell elements are one cell wide you can use the
 110 * simpler qemu_fdt_setprop_cells(). If you're not setting up the
 111 * array programmatically, qemu_fdt_setprop_sized_cells may be more
 112 * convenient.
 113 *
 114 * Return value: 0 on success, <0 on error.
 115 */
 116int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
 117                                            const char *node_path,
 118                                            const char *property,
 119                                            int numvalues,
 120                                            uint64_t *values);
 121
 122/**
 123 * qemu_fdt_setprop_sized_cells:
 124 * @fdt: device tree blob
 125 * @node_path: node to set property on
 126 * @property: property to set
 127 * @...: list of number-of-cells, value pairs
 128 *
 129 * Set the specified property on the specified node in the device tree
 130 * to be an array of cells. The values of the cells are specified via
 131 * the variable arguments, which alternates between "number of cells
 132 * used by this value" and "value".
 133 *
 134 * This is a convenience wrapper for the function
 135 * qemu_fdt_setprop_sized_cells_from_array().
 136 *
 137 * Return value: 0 on success, <0 on error.
 138 */
 139#define qemu_fdt_setprop_sized_cells(fdt, node_path, property, ...)       \
 140    ({                                                                    \
 141        uint64_t qdt_tmp[] = { __VA_ARGS__ };                             \
 142        qemu_fdt_setprop_sized_cells_from_array(fdt, node_path,           \
 143                                                property,                 \
 144                                                ARRAY_SIZE(qdt_tmp) / 2,  \
 145                                                qdt_tmp);                 \
 146    })
 147
 148typedef struct QEMUDevtreeProp {
 149    char *name;
 150    int len;
 151    void *value;
 152} QEMUDevtreeProp;
 153
 154/* node queries */
 155
 156char *qemu_devtree_get_node_name(void *fdt, const char *node_path);
 157int qemu_devtree_get_node_depth(void *fdt, const char *node_path);
 158int qemu_devtree_get_num_children(void *fdt, const char *node_path, int depth);
 159char **qemu_devtree_get_children(void *fdt, const char *node_path, int depth);
 160int qemu_devtree_num_props(void *fdt, const char *node_path);
 161QEMUDevtreeProp *qemu_devtree_get_props(void *fdt, const char *node_path);
 162
 163/* node getters */
 164
 165int qemu_devtree_node_by_compatible(void *fdt, char *node_path,
 166                        const char *compats);
 167int qemu_devtree_get_node_by_name(void *fdt, char *node_path,
 168                        const char *cmpname);
 169int qemu_devtree_get_node_by_phandle(void *fdt, char *node_path, int phandle);
 170int qemu_devtree_getparent(void *fdt, char *node_path,
 171                        const char *current);
 172int qemu_devtree_get_root_node(void *fdt, char *node_path);
 173
 174/* qemu_devtree_get_child_by_name: Check for the matching node name under
 175 * structural block of parent node and returns node path.
 176 * args:
 177 *     fdt: flatend device tree fp
 178 *     parent_path : path of the parent, whose child to be searched
 179 *     cmpname : node name of child
 180 * return:
 181 *     Node path of the child
 182 * Note:
 183 *     Returned string memory should be deallocated by g_free()
 184 */
 185char *qemu_devtree_get_child_by_name(void *fdt, char *parent_path,
 186                                     const char *cmpname);
 187
 188/* qemu_devtree_get_n_nodes_by_name: Same as qemu_devtree_get_node_by_name but
 189 * returns all the possible node paths matching the node name.
 190 * args:
 191 *     fdt: flatend device tree
 192 *     array: pointer to hold array of strings
 193 *     cmpname: node name to search for
 194 * return:
 195 *     Returns number of matching nodes found
 196 * Note:
 197 *     Array of strings should be released after usage. Each of the individual
 198 *     strings in the array and the array itself should be released.
 199 */
 200int qemu_devtree_get_n_nodes_by_name(void *fdt, char ***array,
 201                                     const char *cmpname);
 202
 203/* misc */
 204
 205int devtree_get_num_nodes(void *fdt);
 206void devtree_info_dump(void *fdt);
 207
 208#define DT_PATH_LENGTH 1024
 209
 210#define FDT_PCI_RANGE_RELOCATABLE          0x80000000
 211#define FDT_PCI_RANGE_PREFETCHABLE         0x40000000
 212#define FDT_PCI_RANGE_ALIASED              0x20000000
 213#define FDT_PCI_RANGE_TYPE_MASK            0x03000000
 214#define FDT_PCI_RANGE_MMIO_64BIT           0x03000000
 215#define FDT_PCI_RANGE_MMIO                 0x02000000
 216#define FDT_PCI_RANGE_IOPORT               0x01000000
 217#define FDT_PCI_RANGE_CONFIG               0x00000000
 218
 219#endif /* __DEVICE_TREE_H__ */
 220