Revert "target-alpha: Add vector implementation for CMPBGE"
[qemu.git] / device_tree.c
blob3d119ef0bdb15db633a97bf737a706782cbb3361
1 /*
2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
4 * interface.
6 * Copyright 2008 IBM Corporation.
7 * Authors: Jerone Young <jyoung5@us.ibm.com>
8 * Hollis Blanchard <hollisb@us.ibm.com>
10 * This work is licensed under the GNU GPL license version 2 or later.
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <stdlib.h>
21 #include "config.h"
22 #include "qemu-common.h"
23 #include "qemu/error-report.h"
24 #include "sysemu/device_tree.h"
25 #include "sysemu/sysemu.h"
26 #include "hw/loader.h"
27 #include "hw/boards.h"
28 #include "qemu/config-file.h"
30 #include <libfdt.h>
32 #define FDT_MAX_SIZE 0x10000
34 void *create_device_tree(int *sizep)
36 void *fdt;
37 int ret;
39 *sizep = FDT_MAX_SIZE;
40 fdt = g_malloc0(FDT_MAX_SIZE);
41 ret = fdt_create(fdt, FDT_MAX_SIZE);
42 if (ret < 0) {
43 goto fail;
45 ret = fdt_finish_reservemap(fdt);
46 if (ret < 0) {
47 goto fail;
49 ret = fdt_begin_node(fdt, "");
50 if (ret < 0) {
51 goto fail;
53 ret = fdt_end_node(fdt);
54 if (ret < 0) {
55 goto fail;
57 ret = fdt_finish(fdt);
58 if (ret < 0) {
59 goto fail;
61 ret = fdt_open_into(fdt, fdt, *sizep);
62 if (ret) {
63 error_report("Unable to copy device tree in memory");
64 exit(1);
67 return fdt;
68 fail:
69 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
70 exit(1);
73 void *load_device_tree(const char *filename_path, int *sizep)
75 int dt_size;
76 int dt_file_load_size;
77 int ret;
78 void *fdt = NULL;
80 *sizep = 0;
81 dt_size = get_image_size(filename_path);
82 if (dt_size < 0) {
83 error_report("Unable to get size of device tree file '%s'",
84 filename_path);
85 goto fail;
88 /* Expand to 2x size to give enough room for manipulation. */
89 dt_size += 10000;
90 dt_size *= 2;
91 /* First allocate space in qemu for device tree */
92 fdt = g_malloc0(dt_size);
94 dt_file_load_size = load_image(filename_path, fdt);
95 if (dt_file_load_size < 0) {
96 error_report("Unable to open device tree file '%s'",
97 filename_path);
98 goto fail;
101 ret = fdt_open_into(fdt, fdt, dt_size);
102 if (ret) {
103 error_report("Unable to copy device tree in memory");
104 goto fail;
107 /* Check sanity of device tree */
108 if (fdt_check_header(fdt)) {
109 error_report("Device tree file loaded into memory is invalid: %s",
110 filename_path);
111 goto fail;
113 *sizep = dt_size;
114 return fdt;
116 fail:
117 g_free(fdt);
118 return NULL;
121 static int findnode_nofail(void *fdt, const char *node_path)
123 int offset;
125 offset = fdt_path_offset(fdt, node_path);
126 if (offset < 0) {
127 error_report("%s Couldn't find node %s: %s", __func__, node_path,
128 fdt_strerror(offset));
129 exit(1);
132 return offset;
135 int qemu_fdt_setprop(void *fdt, const char *node_path,
136 const char *property, const void *val, int size)
138 int r;
140 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
141 if (r < 0) {
142 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
143 property, fdt_strerror(r));
144 exit(1);
147 return r;
150 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
151 const char *property, uint32_t val)
153 int r;
155 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
156 if (r < 0) {
157 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
158 node_path, property, val, fdt_strerror(r));
159 exit(1);
162 return r;
165 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
166 const char *property, uint64_t val)
168 val = cpu_to_be64(val);
169 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
172 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
173 const char *property, const char *string)
175 int r;
177 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
178 if (r < 0) {
179 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
180 node_path, property, string, fdt_strerror(r));
181 exit(1);
184 return r;
187 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
188 const char *property, int *lenp)
190 int len;
191 const void *r;
192 if (!lenp) {
193 lenp = &len;
195 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
196 if (!r) {
197 error_report("%s: Couldn't get %s/%s: %s", __func__,
198 node_path, property, fdt_strerror(*lenp));
199 exit(1);
201 return r;
204 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
205 const char *property)
207 int len;
208 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
209 if (len != 4) {
210 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
211 __func__, node_path, property);
212 exit(1);
214 return be32_to_cpu(*p);
217 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
219 uint32_t r;
221 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
222 if (r == 0) {
223 error_report("%s: Couldn't get phandle for %s: %s", __func__,
224 path, fdt_strerror(r));
225 exit(1);
228 return r;
231 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
232 const char *property,
233 const char *target_node_path)
235 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
236 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
239 uint32_t qemu_fdt_alloc_phandle(void *fdt)
241 static int phandle = 0x0;
244 * We need to find out if the user gave us special instruction at
245 * which phandle id to start allocting phandles.
247 if (!phandle) {
248 phandle = machine_phandle_start(current_machine);
251 if (!phandle) {
253 * None or invalid phandle given on the command line, so fall back to
254 * default starting point.
256 phandle = 0x8000;
259 return phandle++;
262 int qemu_fdt_nop_node(void *fdt, const char *node_path)
264 int r;
266 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
267 if (r < 0) {
268 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
269 fdt_strerror(r));
270 exit(1);
273 return r;
276 int qemu_fdt_add_subnode(void *fdt, const char *name)
278 char *dupname = g_strdup(name);
279 char *basename = strrchr(dupname, '/');
280 int retval;
281 int parent = 0;
283 if (!basename) {
284 g_free(dupname);
285 return -1;
288 basename[0] = '\0';
289 basename++;
291 if (dupname[0]) {
292 parent = findnode_nofail(fdt, dupname);
295 retval = fdt_add_subnode(fdt, parent, basename);
296 if (retval < 0) {
297 error_report("FDT: Failed to create subnode %s: %s", name,
298 fdt_strerror(retval));
299 exit(1);
302 g_free(dupname);
303 return retval;
306 void qemu_fdt_dumpdtb(void *fdt, int size)
308 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
310 if (dumpdtb) {
311 /* Dump the dtb to a file and quit */
312 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
316 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
317 const char *node_path,
318 const char *property,
319 int numvalues,
320 uint64_t *values)
322 uint32_t *propcells;
323 uint64_t value;
324 int cellnum, vnum, ncells;
325 uint32_t hival;
326 int ret;
328 propcells = g_new0(uint32_t, numvalues * 2);
330 cellnum = 0;
331 for (vnum = 0; vnum < numvalues; vnum++) {
332 ncells = values[vnum * 2];
333 if (ncells != 1 && ncells != 2) {
334 ret = -1;
335 goto out;
337 value = values[vnum * 2 + 1];
338 hival = cpu_to_be32(value >> 32);
339 if (ncells > 1) {
340 propcells[cellnum++] = hival;
341 } else if (hival != 0) {
342 ret = -1;
343 goto out;
345 propcells[cellnum++] = cpu_to_be32(value);
348 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
349 cellnum * sizeof(uint32_t));
350 out:
351 g_free(propcells);
352 return ret;