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 "qemu/osdep.h"
25 #include "hw/arm/sysbus-fdt.h"
26 #include "qemu/error-report.h"
27 #include "sysemu/device_tree.h"
28 #include "hw/platform-bus.h"
29 #include "sysemu/sysemu.h"
30 #include "hw/vfio/vfio-platform.h"
31 #include "hw/vfio/vfio-calxeda-xgmac.h"
32 #include "hw/arm/fdt.h"
35 * internal struct that contains the information to create dynamic
38 typedef struct PlatformBusFDTData
{
39 void *fdt
; /* device tree handle */
40 int irq_start
; /* index of the first IRQ usable by platform bus devices */
41 const char *pbus_node_name
; /* name of the platform bus node */
42 PlatformBusDevice
*pbus
;
46 * struct used when calling the machine init done notifier
47 * that constructs the fdt nodes of platform bus devices
49 typedef struct PlatformBusFDTNotifierParams
{
51 ARMPlatformBusFDTParams
*fdt_params
;
52 } PlatformBusFDTNotifierParams
;
54 /* struct that associates a device type name and a node creation function */
55 typedef struct NodeCreationPair
{
57 int (*add_fdt_node_fn
)(SysBusDevice
*sbdev
, void *opaque
);
60 /* Device Specific Code */
63 * add_calxeda_midway_xgmac_fdt_node
65 * Generates a simple node with following properties:
66 * compatible string, regs, interrupts, dma-coherent
68 static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice
*sbdev
, void *opaque
)
70 PlatformBusFDTData
*data
= opaque
;
71 PlatformBusDevice
*pbus
= data
->pbus
;
72 void *fdt
= data
->fdt
;
73 const char *parent_node
= data
->pbus_node_name
;
74 int compat_str_len
, i
, ret
= -1;
76 uint32_t *irq_attr
, *reg_attr
;
77 uint64_t mmio_base
, irq_number
;
78 VFIOPlatformDevice
*vdev
= VFIO_PLATFORM_DEVICE(sbdev
);
79 VFIODevice
*vbasedev
= &vdev
->vbasedev
;
81 mmio_base
= platform_bus_get_mmio_addr(pbus
, sbdev
, 0);
82 nodename
= g_strdup_printf("%s/%s@%" PRIx64
, parent_node
,
83 vbasedev
->name
, mmio_base
);
84 qemu_fdt_add_subnode(fdt
, nodename
);
86 compat_str_len
= strlen(vdev
->compat
) + 1;
87 qemu_fdt_setprop(fdt
, nodename
, "compatible",
88 vdev
->compat
, compat_str_len
);
90 qemu_fdt_setprop(fdt
, nodename
, "dma-coherent", "", 0);
92 reg_attr
= g_new(uint32_t, vbasedev
->num_regions
* 2);
93 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
94 mmio_base
= platform_bus_get_mmio_addr(pbus
, sbdev
, i
);
95 reg_attr
[2 * i
] = cpu_to_be32(mmio_base
);
96 reg_attr
[2 * i
+ 1] = cpu_to_be32(
97 memory_region_size(&vdev
->regions
[i
]->mem
));
99 ret
= qemu_fdt_setprop(fdt
, nodename
, "reg", reg_attr
,
100 vbasedev
->num_regions
* 2 * sizeof(uint32_t));
102 error_report("could not set reg property of node %s", nodename
);
106 irq_attr
= g_new(uint32_t, vbasedev
->num_irqs
* 3);
107 for (i
= 0; i
< vbasedev
->num_irqs
; i
++) {
108 irq_number
= platform_bus_get_irqn(pbus
, sbdev
, i
)
110 irq_attr
[3 * i
] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI
);
111 irq_attr
[3 * i
+ 1] = cpu_to_be32(irq_number
);
112 irq_attr
[3 * i
+ 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI
);
114 ret
= qemu_fdt_setprop(fdt
, nodename
, "interrupts",
115 irq_attr
, vbasedev
->num_irqs
* 3 * sizeof(uint32_t));
117 error_report("could not set interrupts property of node %s",
127 /* list of supported dynamic sysbus devices */
128 static const NodeCreationPair add_fdt_node_functions
[] = {
129 {TYPE_VFIO_CALXEDA_XGMAC
, add_calxeda_midway_xgmac_fdt_node
},
130 {"", NULL
}, /* last element */
136 * add_fdt_node - add the device tree node of a dynamic sysbus device
138 * @sbdev: handle to the sysbus device
139 * @opaque: handle to the PlatformBusFDTData
141 * Checks the sysbus type belongs to the list of device types that
142 * are dynamically instantiable and if so call the node creation
145 static int add_fdt_node(SysBusDevice
*sbdev
, void *opaque
)
149 for (i
= 0; i
< ARRAY_SIZE(add_fdt_node_functions
); i
++) {
150 if (!strcmp(object_get_typename(OBJECT(sbdev
)),
151 add_fdt_node_functions
[i
].typename
)) {
152 ret
= add_fdt_node_functions
[i
].add_fdt_node_fn(sbdev
, opaque
);
157 error_report("Device %s can not be dynamically instantiated",
158 qdev_fw_name(DEVICE(sbdev
)));
163 * add_all_platform_bus_fdt_nodes - create all the platform bus nodes
165 * builds the parent platform bus node and all the nodes of dynamic
166 * sysbus devices attached to it.
168 static void add_all_platform_bus_fdt_nodes(ARMPlatformBusFDTParams
*fdt_params
)
170 const char platcomp
[] = "qemu,platform\0simple-bus";
171 PlatformBusDevice
*pbus
;
175 int irq_start
, dtb_size
;
176 struct arm_boot_info
*info
= fdt_params
->binfo
;
177 const ARMPlatformBusSystemParams
*params
= fdt_params
->system_params
;
178 const char *intc
= fdt_params
->intc
;
179 void *fdt
= info
->get_dtb(info
, &dtb_size
);
182 * If the user provided a dtb, we assume the dynamic sysbus nodes
183 * already are integrated there. This corresponds to a use case where
184 * the dynamic sysbus nodes are complex and their generation is not yet
185 * supported. In that case the user can take charge of the guest dt
186 * while qemu takes charge of the qom stuff.
188 if (info
->dtb_filename
) {
194 node
= g_strdup_printf("/platform@%"PRIx64
, params
->platform_bus_base
);
195 addr
= params
->platform_bus_base
;
196 size
= params
->platform_bus_size
;
197 irq_start
= params
->platform_bus_first_irq
;
199 /* Create a /platform node that we can put all devices into */
200 qemu_fdt_add_subnode(fdt
, node
);
201 qemu_fdt_setprop(fdt
, node
, "compatible", platcomp
, sizeof(platcomp
));
203 /* Our platform bus region is less than 32bits, so 1 cell is enough for
206 qemu_fdt_setprop_cells(fdt
, node
, "#size-cells", 1);
207 qemu_fdt_setprop_cells(fdt
, node
, "#address-cells", 1);
208 qemu_fdt_setprop_cells(fdt
, node
, "ranges", 0, addr
>> 32, addr
, size
);
210 qemu_fdt_setprop_phandle(fdt
, node
, "interrupt-parent", intc
);
212 dev
= qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE
);
213 pbus
= PLATFORM_BUS_DEVICE(dev
);
215 /* We can only create dt nodes for dynamic devices when they're ready */
216 assert(pbus
->done_gathering
);
218 PlatformBusFDTData data
= {
220 .irq_start
= irq_start
,
221 .pbus_node_name
= node
,
225 /* Loop through all dynamic sysbus devices and create their node */
226 foreach_dynamic_sysbus_device(add_fdt_node
, &data
);
231 static void platform_bus_fdt_notify(Notifier
*notifier
, void *data
)
233 PlatformBusFDTNotifierParams
*p
= DO_UPCAST(PlatformBusFDTNotifierParams
,
236 add_all_platform_bus_fdt_nodes(p
->fdt_params
);
237 g_free(p
->fdt_params
);
241 void arm_register_platform_bus_fdt_creator(ARMPlatformBusFDTParams
*fdt_params
)
243 PlatformBusFDTNotifierParams
*p
= g_new(PlatformBusFDTNotifierParams
, 1);
245 p
->fdt_params
= fdt_params
;
246 p
->notifier
.notify
= platform_bus_fdt_notify
;
247 qemu_add_machine_init_done_notifier(&p
->notifier
);