hw/arm/sysbus-fdt: enable amd-xgbe dynamic instantiation
[qemu.git] / hw / arm / sysbus-fdt.c
blob99203886865a2489288b1bf7df09494c3e50ceb8
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 "qemu/osdep.h"
25 #include <libfdt.h>
26 #include "qemu-common.h"
27 #ifdef CONFIG_LINUX
28 #include <linux/vfio.h>
29 #endif
30 #include "hw/arm/sysbus-fdt.h"
31 #include "qemu/error-report.h"
32 #include "sysemu/device_tree.h"
33 #include "hw/platform-bus.h"
34 #include "sysemu/sysemu.h"
35 #include "hw/vfio/vfio-platform.h"
36 #include "hw/vfio/vfio-calxeda-xgmac.h"
37 #include "hw/vfio/vfio-amd-xgbe.h"
38 #include "hw/arm/fdt.h"
41 * internal struct that contains the information to create dynamic
42 * sysbus device node
44 typedef struct PlatformBusFDTData {
45 void *fdt; /* device tree handle */
46 int irq_start; /* index of the first IRQ usable by platform bus devices */
47 const char *pbus_node_name; /* name of the platform bus node */
48 PlatformBusDevice *pbus;
49 } PlatformBusFDTData;
52 * struct used when calling the machine init done notifier
53 * that constructs the fdt nodes of platform bus devices
55 typedef struct PlatformBusFDTNotifierParams {
56 Notifier notifier;
57 ARMPlatformBusFDTParams *fdt_params;
58 } PlatformBusFDTNotifierParams;
60 /* struct that associates a device type name and a node creation function */
61 typedef struct NodeCreationPair {
62 const char *typename;
63 int (*add_fdt_node_fn)(SysBusDevice *sbdev, void *opaque);
64 } NodeCreationPair;
66 /* helpers */
68 typedef struct HostProperty {
69 const char *name;
70 bool optional;
71 } HostProperty;
73 #ifdef CONFIG_LINUX
75 /**
76 * copy_properties_from_host
78 * copies properties listed in an array from host device tree to
79 * guest device tree. If a non optional property is not found, the
80 * function asserts. An optional property is ignored if not found
81 * in the host device tree.
82 * @props: array of HostProperty to copy
83 * @nb_props: number of properties in the array
84 * @host_dt: host device tree blob
85 * @guest_dt: guest device tree blob
86 * @node_path: host dt node path where the property is supposed to be
87 found
88 * @nodename: guest node name the properties should be added to
90 static void copy_properties_from_host(HostProperty *props, int nb_props,
91 void *host_fdt, void *guest_fdt,
92 char *node_path, char *nodename)
94 int i, prop_len;
95 const void *r;
96 Error *err = NULL;
98 for (i = 0; i < nb_props; i++) {
99 r = qemu_fdt_getprop(host_fdt, node_path,
100 props[i].name,
101 &prop_len,
102 props[i].optional ? &err : &error_fatal);
103 if (r) {
104 qemu_fdt_setprop(guest_fdt, nodename,
105 props[i].name, r, prop_len);
106 } else {
107 if (prop_len != -FDT_ERR_NOTFOUND) {
108 /* optional property not returned although property exists */
109 error_report_err(err);
110 } else {
111 error_free(err);
117 /* clock properties whose values are copied/pasted from host */
118 static HostProperty clock_copied_properties[] = {
119 {"compatible", false},
120 {"#clock-cells", false},
121 {"clock-frequency", true},
122 {"clock-output-names", true},
126 * fdt_build_clock_node
128 * Build a guest clock node, used as a dependency from a passthrough'ed
129 * device. Most information are retrieved from the host clock node.
130 * Also check the host clock is a fixed one.
132 * @host_fdt: host device tree blob from which info are retrieved
133 * @guest_fdt: guest device tree blob where the clock node is added
134 * @host_phandle: phandle of the clock in host device tree
135 * @guest_phandle: phandle to assign to the guest node
137 static void fdt_build_clock_node(void *host_fdt, void *guest_fdt,
138 uint32_t host_phandle,
139 uint32_t guest_phandle)
141 char *node_path = NULL;
142 char *nodename;
143 const void *r;
144 int ret, node_offset, prop_len, path_len = 16;
146 node_offset = fdt_node_offset_by_phandle(host_fdt, host_phandle);
147 if (node_offset <= 0) {
148 error_setg(&error_fatal,
149 "not able to locate clock handle %d in host device tree",
150 host_phandle);
152 node_path = g_malloc(path_len);
153 while ((ret = fdt_get_path(host_fdt, node_offset, node_path, path_len))
154 == -FDT_ERR_NOSPACE) {
155 path_len += 16;
156 node_path = g_realloc(node_path, path_len);
158 if (ret < 0) {
159 error_setg(&error_fatal,
160 "not able to retrieve node path for clock handle %d",
161 host_phandle);
164 r = qemu_fdt_getprop(host_fdt, node_path, "compatible", &prop_len,
165 &error_fatal);
166 if (strcmp(r, "fixed-clock")) {
167 error_setg(&error_fatal,
168 "clock handle %d is not a fixed clock", host_phandle);
171 nodename = strrchr(node_path, '/');
172 qemu_fdt_add_subnode(guest_fdt, nodename);
174 copy_properties_from_host(clock_copied_properties,
175 ARRAY_SIZE(clock_copied_properties),
176 host_fdt, guest_fdt,
177 node_path, nodename);
179 qemu_fdt_setprop_cell(guest_fdt, nodename, "phandle", guest_phandle);
181 g_free(node_path);
185 * sysfs_to_dt_name: convert the name found in sysfs into the node name
186 * for instance e0900000.xgmac is converted into xgmac@e0900000
187 * @sysfs_name: directory name in sysfs
189 * returns the device tree name upon success or NULL in case the sysfs name
190 * does not match the expected format
192 static char *sysfs_to_dt_name(const char *sysfs_name)
194 gchar **substrings = g_strsplit(sysfs_name, ".", 2);
195 char *dt_name = NULL;
197 if (!substrings || !substrings[0] || !substrings[1]) {
198 goto out;
200 dt_name = g_strdup_printf("%s@%s", substrings[1], substrings[0]);
201 out:
202 g_strfreev(substrings);
203 return dt_name;
206 /* Device Specific Code */
209 * add_calxeda_midway_xgmac_fdt_node
211 * Generates a simple node with following properties:
212 * compatible string, regs, interrupts, dma-coherent
214 static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
216 PlatformBusFDTData *data = opaque;
217 PlatformBusDevice *pbus = data->pbus;
218 void *fdt = data->fdt;
219 const char *parent_node = data->pbus_node_name;
220 int compat_str_len, i, ret = -1;
221 char *nodename;
222 uint32_t *irq_attr, *reg_attr;
223 uint64_t mmio_base, irq_number;
224 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
225 VFIODevice *vbasedev = &vdev->vbasedev;
227 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
228 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
229 vbasedev->name, mmio_base);
230 qemu_fdt_add_subnode(fdt, nodename);
232 compat_str_len = strlen(vdev->compat) + 1;
233 qemu_fdt_setprop(fdt, nodename, "compatible",
234 vdev->compat, compat_str_len);
236 qemu_fdt_setprop(fdt, nodename, "dma-coherent", "", 0);
238 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
239 for (i = 0; i < vbasedev->num_regions; i++) {
240 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
241 reg_attr[2 * i] = cpu_to_be32(mmio_base);
242 reg_attr[2 * i + 1] = cpu_to_be32(
243 memory_region_size(&vdev->regions[i]->mem));
245 ret = qemu_fdt_setprop(fdt, nodename, "reg", reg_attr,
246 vbasedev->num_regions * 2 * sizeof(uint32_t));
247 if (ret) {
248 error_report("could not set reg property of node %s", nodename);
249 goto fail_reg;
252 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
253 for (i = 0; i < vbasedev->num_irqs; i++) {
254 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
255 + data->irq_start;
256 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
257 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
258 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
260 ret = qemu_fdt_setprop(fdt, nodename, "interrupts",
261 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
262 if (ret) {
263 error_report("could not set interrupts property of node %s",
264 nodename);
266 g_free(irq_attr);
267 fail_reg:
268 g_free(reg_attr);
269 g_free(nodename);
270 return ret;
273 /* AMD xgbe properties whose values are copied/pasted from host */
274 static HostProperty amd_xgbe_copied_properties[] = {
275 {"compatible", false},
276 {"dma-coherent", true},
277 {"amd,per-channel-interrupt", true},
278 {"phy-mode", false},
279 {"mac-address", true},
280 {"amd,speed-set", false},
281 {"amd,serdes-blwc", true},
282 {"amd,serdes-cdr-rate", true},
283 {"amd,serdes-pq-skew", true},
284 {"amd,serdes-tx-amp", true},
285 {"amd,serdes-dfe-tap-config", true},
286 {"amd,serdes-dfe-tap-enable", true},
287 {"clock-names", false},
291 * add_amd_xgbe_fdt_node
293 * Generates the combined xgbe/phy node following kernel >=4.2
294 * binding documentation:
295 * Documentation/devicetree/bindings/net/amd-xgbe.txt:
296 * Also 2 clock nodes are created (dma and ptp)
298 * Asserts in case of error
300 static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque)
302 PlatformBusFDTData *data = opaque;
303 PlatformBusDevice *pbus = data->pbus;
304 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
305 VFIODevice *vbasedev = &vdev->vbasedev;
306 VFIOINTp *intp;
307 const char *parent_node = data->pbus_node_name;
308 char **node_path, *nodename, *dt_name;
309 void *guest_fdt = data->fdt, *host_fdt;
310 const void *r;
311 int i, prop_len;
312 uint32_t *irq_attr, *reg_attr, *host_clock_phandles;
313 uint64_t mmio_base, irq_number;
314 uint32_t guest_clock_phandles[2];
316 host_fdt = load_device_tree_from_sysfs();
318 dt_name = sysfs_to_dt_name(vbasedev->name);
319 if (!dt_name) {
320 error_setg(&error_fatal, "%s incorrect sysfs device name %s",
321 __func__, vbasedev->name);
323 node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
324 &error_fatal);
325 if (!node_path || !node_path[0]) {
326 error_setg(&error_fatal, "%s unable to retrieve node path for %s/%s",
327 __func__, dt_name, vdev->compat);
330 if (node_path[1]) {
331 error_setg(&error_fatal, "%s more than one node matching %s/%s!",
332 __func__, dt_name, vdev->compat);
335 g_free(dt_name);
337 if (vbasedev->num_regions != 5) {
338 error_setg(&error_fatal, "%s Does the host dt node combine XGBE/PHY?",
339 __func__);
342 /* generate nodes for DMA_CLK and PTP_CLK */
343 r = qemu_fdt_getprop(host_fdt, node_path[0], "clocks",
344 &prop_len, &error_fatal);
345 if (prop_len != 8) {
346 error_setg(&error_fatal, "%s clocks property should contain 2 handles",
347 __func__);
349 host_clock_phandles = (uint32_t *)r;
350 guest_clock_phandles[0] = qemu_fdt_alloc_phandle(guest_fdt);
351 guest_clock_phandles[1] = qemu_fdt_alloc_phandle(guest_fdt);
354 * clock handles fetched from host dt are in be32 layout whereas
355 * rest of the code uses cpu layout. Also guest clock handles are
356 * in cpu layout.
358 fdt_build_clock_node(host_fdt, guest_fdt,
359 be32_to_cpu(host_clock_phandles[0]),
360 guest_clock_phandles[0]);
362 fdt_build_clock_node(host_fdt, guest_fdt,
363 be32_to_cpu(host_clock_phandles[1]),
364 guest_clock_phandles[1]);
366 /* combined XGBE/PHY node */
367 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
368 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
369 vbasedev->name, mmio_base);
370 qemu_fdt_add_subnode(guest_fdt, nodename);
372 copy_properties_from_host(amd_xgbe_copied_properties,
373 ARRAY_SIZE(amd_xgbe_copied_properties),
374 host_fdt, guest_fdt,
375 node_path[0], nodename);
377 qemu_fdt_setprop_cells(guest_fdt, nodename, "clocks",
378 guest_clock_phandles[0],
379 guest_clock_phandles[1]);
381 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
382 for (i = 0; i < vbasedev->num_regions; i++) {
383 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
384 reg_attr[2 * i] = cpu_to_be32(mmio_base);
385 reg_attr[2 * i + 1] = cpu_to_be32(
386 memory_region_size(&vdev->regions[i]->mem));
388 qemu_fdt_setprop(guest_fdt, nodename, "reg", reg_attr,
389 vbasedev->num_regions * 2 * sizeof(uint32_t));
391 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
392 for (i = 0; i < vbasedev->num_irqs; i++) {
393 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
394 + data->irq_start;
395 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
396 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
398 * General device interrupt and PCS auto-negotiation interrupts are
399 * level-sensitive while the 4 per-channel interrupts are edge
400 * sensitive
402 QLIST_FOREACH(intp, &vdev->intp_list, next) {
403 if (intp->pin == i) {
404 break;
407 if (intp->flags & VFIO_IRQ_INFO_AUTOMASKED) {
408 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
409 } else {
410 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
413 qemu_fdt_setprop(guest_fdt, nodename, "interrupts",
414 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
416 g_free(host_fdt);
417 g_strfreev(node_path);
418 g_free(irq_attr);
419 g_free(reg_attr);
420 g_free(nodename);
421 return 0;
424 #endif /* CONFIG_LINUX */
426 /* list of supported dynamic sysbus devices */
427 static const NodeCreationPair add_fdt_node_functions[] = {
428 #ifdef CONFIG_LINUX
429 {TYPE_VFIO_CALXEDA_XGMAC, add_calxeda_midway_xgmac_fdt_node},
430 {TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node},
431 #endif
432 {"", NULL}, /* last element */
435 /* Generic Code */
438 * add_fdt_node - add the device tree node of a dynamic sysbus device
440 * @sbdev: handle to the sysbus device
441 * @opaque: handle to the PlatformBusFDTData
443 * Checks the sysbus type belongs to the list of device types that
444 * are dynamically instantiable and if so call the node creation
445 * function.
447 static int add_fdt_node(SysBusDevice *sbdev, void *opaque)
449 int i, ret;
451 for (i = 0; i < ARRAY_SIZE(add_fdt_node_functions); i++) {
452 if (!strcmp(object_get_typename(OBJECT(sbdev)),
453 add_fdt_node_functions[i].typename)) {
454 ret = add_fdt_node_functions[i].add_fdt_node_fn(sbdev, opaque);
455 assert(!ret);
456 return 0;
459 error_report("Device %s can not be dynamically instantiated",
460 qdev_fw_name(DEVICE(sbdev)));
461 exit(1);
465 * add_all_platform_bus_fdt_nodes - create all the platform bus nodes
467 * builds the parent platform bus node and all the nodes of dynamic
468 * sysbus devices attached to it.
470 static void add_all_platform_bus_fdt_nodes(ARMPlatformBusFDTParams *fdt_params)
472 const char platcomp[] = "qemu,platform\0simple-bus";
473 PlatformBusDevice *pbus;
474 DeviceState *dev;
475 gchar *node;
476 uint64_t addr, size;
477 int irq_start, dtb_size;
478 struct arm_boot_info *info = fdt_params->binfo;
479 const ARMPlatformBusSystemParams *params = fdt_params->system_params;
480 const char *intc = fdt_params->intc;
481 void *fdt = info->get_dtb(info, &dtb_size);
484 * If the user provided a dtb, we assume the dynamic sysbus nodes
485 * already are integrated there. This corresponds to a use case where
486 * the dynamic sysbus nodes are complex and their generation is not yet
487 * supported. In that case the user can take charge of the guest dt
488 * while qemu takes charge of the qom stuff.
490 if (info->dtb_filename) {
491 return;
494 assert(fdt);
496 node = g_strdup_printf("/platform@%"PRIx64, params->platform_bus_base);
497 addr = params->platform_bus_base;
498 size = params->platform_bus_size;
499 irq_start = params->platform_bus_first_irq;
501 /* Create a /platform node that we can put all devices into */
502 qemu_fdt_add_subnode(fdt, node);
503 qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp));
505 /* Our platform bus region is less than 32bits, so 1 cell is enough for
506 * address and size
508 qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1);
509 qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1);
510 qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, size);
512 qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc);
514 dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE);
515 pbus = PLATFORM_BUS_DEVICE(dev);
517 /* We can only create dt nodes for dynamic devices when they're ready */
518 assert(pbus->done_gathering);
520 PlatformBusFDTData data = {
521 .fdt = fdt,
522 .irq_start = irq_start,
523 .pbus_node_name = node,
524 .pbus = pbus,
527 /* Loop through all dynamic sysbus devices and create their node */
528 foreach_dynamic_sysbus_device(add_fdt_node, &data);
530 g_free(node);
533 static void platform_bus_fdt_notify(Notifier *notifier, void *data)
535 PlatformBusFDTNotifierParams *p = DO_UPCAST(PlatformBusFDTNotifierParams,
536 notifier, notifier);
538 add_all_platform_bus_fdt_nodes(p->fdt_params);
539 g_free(p->fdt_params);
540 g_free(p);
543 void arm_register_platform_bus_fdt_creator(ARMPlatformBusFDTParams *fdt_params)
545 PlatformBusFDTNotifierParams *p = g_new(PlatformBusFDTNotifierParams, 1);
547 p->fdt_params = fdt_params;
548 p->notifier.notify = platform_bus_fdt_notify;
549 qemu_add_machine_init_done_notifier(&p->notifier);