target-s390x: add a cpu_mmu_idx_to_asc function
[qemu/ar7.git] / hw / arm / sysbus-fdt.c
blob3038b94b4ad5578e19c90894fb743b5062170123
1 /*
2 * ARM Platform Bus device tree generation helpers
4 * Copyright (c) 2014 Linaro Limited
6 * Authors:
7 * Alex Graf <agraf@suse.de>
8 * Eric Auger <eric.auger@linaro.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2 or later, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "hw/arm/sysbus-fdt.h"
25 #include "qemu/error-report.h"
26 #include "sysemu/device_tree.h"
27 #include "hw/platform-bus.h"
28 #include "sysemu/sysemu.h"
31 * internal struct that contains the information to create dynamic
32 * sysbus device node
34 typedef struct PlatformBusFDTData {
35 void *fdt; /* device tree handle */
36 int irq_start; /* index of the first IRQ usable by platform bus devices */
37 const char *pbus_node_name; /* name of the platform bus node */
38 PlatformBusDevice *pbus;
39 } PlatformBusFDTData;
42 * struct used when calling the machine init done notifier
43 * that constructs the fdt nodes of platform bus devices
45 typedef struct PlatformBusFDTNotifierParams {
46 Notifier notifier;
47 ARMPlatformBusFDTParams *fdt_params;
48 } PlatformBusFDTNotifierParams;
50 /* struct that associates a device type name and a node creation function */
51 typedef struct NodeCreationPair {
52 const char *typename;
53 int (*add_fdt_node_fn)(SysBusDevice *sbdev, void *opaque);
54 } NodeCreationPair;
56 /* list of supported dynamic sysbus devices */
57 static const NodeCreationPair add_fdt_node_functions[] = {
58 {"", NULL}, /* last element */
61 /**
62 * add_fdt_node - add the device tree node of a dynamic sysbus device
64 * @sbdev: handle to the sysbus device
65 * @opaque: handle to the PlatformBusFDTData
67 * Checks the sysbus type belongs to the list of device types that
68 * are dynamically instantiable and if so call the node creation
69 * function.
71 static int add_fdt_node(SysBusDevice *sbdev, void *opaque)
73 int i, ret;
75 for (i = 0; i < ARRAY_SIZE(add_fdt_node_functions); i++) {
76 if (!strcmp(object_get_typename(OBJECT(sbdev)),
77 add_fdt_node_functions[i].typename)) {
78 ret = add_fdt_node_functions[i].add_fdt_node_fn(sbdev, opaque);
79 assert(!ret);
80 return 0;
83 error_report("Device %s can not be dynamically instantiated",
84 qdev_fw_name(DEVICE(sbdev)));
85 exit(1);
88 /**
89 * add_all_platform_bus_fdt_nodes - create all the platform bus nodes
91 * builds the parent platform bus node and all the nodes of dynamic
92 * sysbus devices attached to it.
94 static void add_all_platform_bus_fdt_nodes(ARMPlatformBusFDTParams *fdt_params)
96 const char platcomp[] = "qemu,platform\0simple-bus";
97 PlatformBusDevice *pbus;
98 DeviceState *dev;
99 gchar *node;
100 uint64_t addr, size;
101 int irq_start, dtb_size;
102 struct arm_boot_info *info = fdt_params->binfo;
103 const ARMPlatformBusSystemParams *params = fdt_params->system_params;
104 const char *intc = fdt_params->intc;
105 void *fdt = info->get_dtb(info, &dtb_size);
108 * If the user provided a dtb, we assume the dynamic sysbus nodes
109 * already are integrated there. This corresponds to a use case where
110 * the dynamic sysbus nodes are complex and their generation is not yet
111 * supported. In that case the user can take charge of the guest dt
112 * while qemu takes charge of the qom stuff.
114 if (info->dtb_filename) {
115 return;
118 assert(fdt);
120 node = g_strdup_printf("/platform@%"PRIx64, params->platform_bus_base);
121 addr = params->platform_bus_base;
122 size = params->platform_bus_size;
123 irq_start = params->platform_bus_first_irq;
125 /* Create a /platform node that we can put all devices into */
126 qemu_fdt_add_subnode(fdt, node);
127 qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp));
129 /* Our platform bus region is less than 32bits, so 1 cell is enough for
130 * address and size
132 qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1);
133 qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1);
134 qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, size);
136 qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc);
138 dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE);
139 pbus = PLATFORM_BUS_DEVICE(dev);
141 /* We can only create dt nodes for dynamic devices when they're ready */
142 assert(pbus->done_gathering);
144 PlatformBusFDTData data = {
145 .fdt = fdt,
146 .irq_start = irq_start,
147 .pbus_node_name = node,
148 .pbus = pbus,
151 /* Loop through all dynamic sysbus devices and create their node */
152 foreach_dynamic_sysbus_device(add_fdt_node, &data);
154 g_free(node);
157 static void platform_bus_fdt_notify(Notifier *notifier, void *data)
159 PlatformBusFDTNotifierParams *p = DO_UPCAST(PlatformBusFDTNotifierParams,
160 notifier, notifier);
162 add_all_platform_bus_fdt_nodes(p->fdt_params);
163 g_free(p->fdt_params);
164 g_free(p);
167 void arm_register_platform_bus_fdt_creator(ARMPlatformBusFDTParams *fdt_params)
169 PlatformBusFDTNotifierParams *p = g_new(PlatformBusFDTNotifierParams, 1);
171 p->fdt_params = fdt_params;
172 p->notifier.notify = platform_bus_fdt_notify;
173 qemu_add_machine_init_done_notifier(&p->notifier);