2 * ARM Platform Bus device tree generation helpers
4 * Copyright (c) 2014 Linaro Limited
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
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
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
;
42 * struct used when calling the machine init done notifier
43 * that constructs the fdt nodes of platform bus devices
45 typedef struct PlatformBusFDTNotifierParams
{
47 ARMPlatformBusFDTParams
*fdt_params
;
48 } PlatformBusFDTNotifierParams
;
50 /* struct that associates a device type name and a node creation function */
51 typedef struct NodeCreationPair
{
53 int (*add_fdt_node_fn
)(SysBusDevice
*sbdev
, void *opaque
);
56 /* list of supported dynamic sysbus devices */
57 static const NodeCreationPair add_fdt_node_functions
[] = {
58 {"", NULL
}, /* last element */
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
71 static int add_fdt_node(SysBusDevice
*sbdev
, void *opaque
)
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
);
83 error_report("Device %s can not be dynamically instantiated",
84 qdev_fw_name(DEVICE(sbdev
)));
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
;
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
) {
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
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
= {
146 .irq_start
= irq_start
,
147 .pbus_node_name
= node
,
151 /* Loop through all dynamic sysbus devices and create their node */
152 foreach_dynamic_sysbus_device(add_fdt_node
, &data
);
157 static void platform_bus_fdt_notify(Notifier
*notifier
, void *data
)
159 PlatformBusFDTNotifierParams
*p
= DO_UPCAST(PlatformBusFDTNotifierParams
,
162 add_all_platform_bus_fdt_nodes(p
->fdt_params
);
163 g_free(p
->fdt_params
);
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
);