]> Git Repo - qemu.git/blob - hw/arm/sysbus-fdt.c
Merge remote-tracking branch 'remotes/afaerber/tags/qom-devices-for-peter' into staging
[qemu.git] / hw / arm / sysbus-fdt.c
1 /*
2  * ARM Platform Bus device tree generation helpers
3  *
4  * Copyright (c) 2014 Linaro Limited
5  *
6  * Authors:
7  *  Alex Graf <[email protected]>
8  *  Eric Auger <[email protected]>
9  *
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.
13  *
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.
18  *
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/>.
21  *
22  */
23
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"
33
34 /*
35  * internal struct that contains the information to create dynamic
36  * sysbus device node
37  */
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;
43 } PlatformBusFDTData;
44
45 /*
46  * struct used when calling the machine init done notifier
47  * that constructs the fdt nodes of platform bus devices
48  */
49 typedef struct PlatformBusFDTNotifierParams {
50     Notifier notifier;
51     ARMPlatformBusFDTParams *fdt_params;
52 } PlatformBusFDTNotifierParams;
53
54 /* struct that associates a device type name and a node creation function */
55 typedef struct NodeCreationPair {
56     const char *typename;
57     int (*add_fdt_node_fn)(SysBusDevice *sbdev, void *opaque);
58 } NodeCreationPair;
59
60 /* Device Specific Code */
61
62 /**
63  * add_calxeda_midway_xgmac_fdt_node
64  *
65  * Generates a simple node with following properties:
66  * compatible string, regs, interrupts, dma-coherent
67  */
68 static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
69 {
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;
75     char *nodename;
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;
80
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);
85
86     compat_str_len = strlen(vdev->compat) + 1;
87     qemu_fdt_setprop(fdt, nodename, "compatible",
88                           vdev->compat, compat_str_len);
89
90     qemu_fdt_setprop(fdt, nodename, "dma-coherent", "", 0);
91
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));
98     }
99     ret = qemu_fdt_setprop(fdt, nodename, "reg", reg_attr,
100                            vbasedev->num_regions * 2 * sizeof(uint32_t));
101     if (ret) {
102         error_report("could not set reg property of node %s", nodename);
103         goto fail_reg;
104     }
105
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)
109                          + data->irq_start;
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);
113     }
114     ret = qemu_fdt_setprop(fdt, nodename, "interrupts",
115                      irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
116     if (ret) {
117         error_report("could not set interrupts property of node %s",
118                      nodename);
119     }
120     g_free(irq_attr);
121 fail_reg:
122     g_free(reg_attr);
123     g_free(nodename);
124     return ret;
125 }
126
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 */
131 };
132
133 /* Generic Code */
134
135 /**
136  * add_fdt_node - add the device tree node of a dynamic sysbus device
137  *
138  * @sbdev: handle to the sysbus device
139  * @opaque: handle to the PlatformBusFDTData
140  *
141  * Checks the sysbus type belongs to the list of device types that
142  * are dynamically instantiable and if so call the node creation
143  * function.
144  */
145 static int add_fdt_node(SysBusDevice *sbdev, void *opaque)
146 {
147     int i, ret;
148
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);
153             assert(!ret);
154             return 0;
155         }
156     }
157     error_report("Device %s can not be dynamically instantiated",
158                      qdev_fw_name(DEVICE(sbdev)));
159     exit(1);
160 }
161
162 /**
163  * add_all_platform_bus_fdt_nodes - create all the platform bus nodes
164  *
165  * builds the parent platform bus node and all the nodes of dynamic
166  * sysbus devices attached to it.
167  */
168 static void add_all_platform_bus_fdt_nodes(ARMPlatformBusFDTParams *fdt_params)
169 {
170     const char platcomp[] = "qemu,platform\0simple-bus";
171     PlatformBusDevice *pbus;
172     DeviceState *dev;
173     gchar *node;
174     uint64_t addr, size;
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);
180
181     /*
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.
187      */
188     if (info->dtb_filename) {
189         return;
190     }
191
192     assert(fdt);
193
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;
198
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));
202
203     /* Our platform bus region is less than 32bits, so 1 cell is enough for
204      * address and size
205      */
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);
209
210     qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc);
211
212     dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE);
213     pbus = PLATFORM_BUS_DEVICE(dev);
214
215     /* We can only create dt nodes for dynamic devices when they're ready */
216     assert(pbus->done_gathering);
217
218     PlatformBusFDTData data = {
219         .fdt = fdt,
220         .irq_start = irq_start,
221         .pbus_node_name = node,
222         .pbus = pbus,
223     };
224
225     /* Loop through all dynamic sysbus devices and create their node */
226     foreach_dynamic_sysbus_device(add_fdt_node, &data);
227
228     g_free(node);
229 }
230
231 static void platform_bus_fdt_notify(Notifier *notifier, void *data)
232 {
233     PlatformBusFDTNotifierParams *p = DO_UPCAST(PlatformBusFDTNotifierParams,
234                                                 notifier, notifier);
235
236     add_all_platform_bus_fdt_nodes(p->fdt_params);
237     g_free(p->fdt_params);
238     g_free(p);
239 }
240
241 void arm_register_platform_bus_fdt_creator(ARMPlatformBusFDTParams *fdt_params)
242 {
243     PlatformBusFDTNotifierParams *p = g_new(PlatformBusFDTNotifierParams, 1);
244
245     p->fdt_params = fdt_params;
246     p->notifier.notify = platform_bus_fdt_notify;
247     qemu_add_machine_init_done_notifier(&p->notifier);
248 }
This page took 0.039756 seconds and 4 git commands to generate.