qemu-nbd: Avoid strtol open-coding
[qemu/ar7.git] / hw / ppc / spapr_vio.c
blob7e8a9ad0933783347cb0d91c6f6986b881a37bd4
1 /*
2 * QEMU sPAPR VIO code
4 * Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
5 * Based on the s390 virtio bus code:
6 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
25 #include "qapi/visitor.h"
26 #include "hw/hw.h"
27 #include "qemu/log.h"
28 #include "sysemu/sysemu.h"
29 #include "hw/boards.h"
30 #include "hw/loader.h"
31 #include "elf.h"
32 #include "hw/sysbus.h"
33 #include "sysemu/kvm.h"
34 #include "sysemu/device_tree.h"
35 #include "kvm_ppc.h"
36 #include "sysemu/qtest.h"
38 #include "hw/ppc/spapr.h"
39 #include "hw/ppc/spapr_vio.h"
40 #include "hw/ppc/fdt.h"
41 #include "trace.h"
43 #include <libfdt.h>
45 #define SPAPR_VIO_REG_BASE 0x71000000
47 static void spapr_vio_get_irq(Object *obj, Visitor *v, const char *name,
48 void *opaque, Error **errp)
50 Property *prop = opaque;
51 uint32_t *ptr = qdev_get_prop_ptr(DEVICE(obj), prop);
53 visit_type_uint32(v, name, ptr, errp);
56 static void spapr_vio_set_irq(Object *obj, Visitor *v, const char *name,
57 void *opaque, Error **errp)
59 Property *prop = opaque;
60 uint32_t *ptr = qdev_get_prop_ptr(DEVICE(obj), prop);
62 if (!qtest_enabled()) {
63 warn_report(TYPE_VIO_SPAPR_DEVICE " '%s' property is deprecated", name);
65 visit_type_uint32(v, name, ptr, errp);
68 static const PropertyInfo spapr_vio_irq_propinfo = {
69 .name = "irq",
70 .get = spapr_vio_get_irq,
71 .set = spapr_vio_set_irq,
74 static Property spapr_vio_props[] = {
75 DEFINE_PROP("irq", VIOsPAPRDevice, irq, spapr_vio_irq_propinfo, uint32_t),
76 DEFINE_PROP_END_OF_LIST(),
79 static char *spapr_vio_get_dev_name(DeviceState *qdev)
81 VIOsPAPRDevice *dev = VIO_SPAPR_DEVICE(qdev);
82 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
84 /* Device tree style name device@reg */
85 return g_strdup_printf("%s@%x", pc->dt_name, dev->reg);
88 static void spapr_vio_bus_class_init(ObjectClass *klass, void *data)
90 BusClass *k = BUS_CLASS(klass);
92 k->get_dev_path = spapr_vio_get_dev_name;
93 k->get_fw_dev_path = spapr_vio_get_dev_name;
96 static const TypeInfo spapr_vio_bus_info = {
97 .name = TYPE_SPAPR_VIO_BUS,
98 .parent = TYPE_BUS,
99 .class_init = spapr_vio_bus_class_init,
100 .instance_size = sizeof(VIOsPAPRBus),
103 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
105 BusChild *kid;
106 VIOsPAPRDevice *dev = NULL;
108 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
109 dev = (VIOsPAPRDevice *)kid->child;
110 if (dev->reg == reg) {
111 return dev;
115 return NULL;
118 static int vio_make_devnode(VIOsPAPRDevice *dev,
119 void *fdt)
121 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
122 int vdevice_off, node_off, ret;
123 char *dt_name;
125 vdevice_off = fdt_path_offset(fdt, "/vdevice");
126 if (vdevice_off < 0) {
127 return vdevice_off;
130 dt_name = spapr_vio_get_dev_name(DEVICE(dev));
131 node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
132 g_free(dt_name);
133 if (node_off < 0) {
134 return node_off;
137 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
138 if (ret < 0) {
139 return ret;
142 if (pc->dt_type) {
143 ret = fdt_setprop_string(fdt, node_off, "device_type",
144 pc->dt_type);
145 if (ret < 0) {
146 return ret;
150 if (pc->dt_compatible) {
151 ret = fdt_setprop_string(fdt, node_off, "compatible",
152 pc->dt_compatible);
153 if (ret < 0) {
154 return ret;
158 if (dev->irq) {
159 uint32_t ints_prop[2];
161 spapr_dt_xics_irq(ints_prop, dev->irq, false);
162 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
163 sizeof(ints_prop));
164 if (ret < 0) {
165 return ret;
169 ret = spapr_tcet_dma_dt(fdt, node_off, "ibm,my-dma-window", dev->tcet);
170 if (ret < 0) {
171 return ret;
174 if (pc->devnode) {
175 ret = (pc->devnode)(dev, fdt, node_off);
176 if (ret < 0) {
177 return ret;
181 return node_off;
185 * CRQ handling
187 static target_ulong h_reg_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
188 target_ulong opcode, target_ulong *args)
190 target_ulong reg = args[0];
191 target_ulong queue_addr = args[1];
192 target_ulong queue_len = args[2];
193 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
195 if (!dev) {
196 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
197 return H_PARAMETER;
200 /* We can't grok a queue size bigger than 256M for now */
201 if (queue_len < 0x1000 || queue_len > 0x10000000) {
202 hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
203 ")\n", queue_len);
204 return H_PARAMETER;
207 /* Check queue alignment */
208 if (queue_addr & 0xfff) {
209 hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx ")\n", queue_addr);
210 return H_PARAMETER;
213 /* Check if device supports CRQs */
214 if (!dev->crq.SendFunc) {
215 hcall_dprintf("Device does not support CRQ\n");
216 return H_NOT_FOUND;
219 /* Already a queue ? */
220 if (dev->crq.qsize) {
221 hcall_dprintf("CRQ already registered\n");
222 return H_RESOURCE;
224 dev->crq.qladdr = queue_addr;
225 dev->crq.qsize = queue_len;
226 dev->crq.qnext = 0;
228 trace_spapr_vio_h_reg_crq(reg, queue_addr, queue_len);
229 return H_SUCCESS;
232 static target_ulong free_crq(VIOsPAPRDevice *dev)
234 dev->crq.qladdr = 0;
235 dev->crq.qsize = 0;
236 dev->crq.qnext = 0;
238 trace_spapr_vio_free_crq(dev->reg);
240 return H_SUCCESS;
243 static target_ulong h_free_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
244 target_ulong opcode, target_ulong *args)
246 target_ulong reg = args[0];
247 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
249 if (!dev) {
250 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
251 return H_PARAMETER;
254 return free_crq(dev);
257 static target_ulong h_send_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
258 target_ulong opcode, target_ulong *args)
260 target_ulong reg = args[0];
261 target_ulong msg_hi = args[1];
262 target_ulong msg_lo = args[2];
263 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
264 uint64_t crq_mangle[2];
266 if (!dev) {
267 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
268 return H_PARAMETER;
270 crq_mangle[0] = cpu_to_be64(msg_hi);
271 crq_mangle[1] = cpu_to_be64(msg_lo);
273 if (dev->crq.SendFunc) {
274 return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
277 return H_HARDWARE;
280 static target_ulong h_enable_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
281 target_ulong opcode, target_ulong *args)
283 target_ulong reg = args[0];
284 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
286 if (!dev) {
287 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
288 return H_PARAMETER;
291 return 0;
294 /* Returns negative error, 0 success, or positive: queue full */
295 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
297 int rc;
298 uint8_t byte;
300 if (!dev->crq.qsize) {
301 error_report("spapr_vio_send_creq on uninitialized queue");
302 return -1;
305 /* Maybe do a fast path for KVM just writing to the pages */
306 rc = spapr_vio_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
307 if (rc) {
308 return rc;
310 if (byte != 0) {
311 return 1;
314 rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
315 &crq[8], 8);
316 if (rc) {
317 return rc;
320 kvmppc_eieio();
322 rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
323 if (rc) {
324 return rc;
327 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
329 if (dev->signal_state & 1) {
330 qemu_irq_pulse(spapr_vio_qirq(dev));
333 return 0;
336 /* "quiesce" handling */
338 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
340 if (dev->tcet) {
341 device_reset(DEVICE(dev->tcet));
343 free_crq(dev);
346 void spapr_vio_set_bypass(VIOsPAPRDevice *dev, bool bypass)
348 if (!dev->tcet) {
349 return;
352 memory_region_set_enabled(&dev->mrbypass, bypass);
353 memory_region_set_enabled(spapr_tce_get_iommu(dev->tcet), !bypass);
355 dev->tcet->bypass = bypass;
358 static void rtas_set_tce_bypass(PowerPCCPU *cpu, sPAPRMachineState *spapr,
359 uint32_t token,
360 uint32_t nargs, target_ulong args,
361 uint32_t nret, target_ulong rets)
363 VIOsPAPRBus *bus = spapr->vio_bus;
364 VIOsPAPRDevice *dev;
365 uint32_t unit, enable;
367 if (nargs != 2) {
368 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
369 return;
371 unit = rtas_ld(args, 0);
372 enable = rtas_ld(args, 1);
373 dev = spapr_vio_find_by_reg(bus, unit);
374 if (!dev) {
375 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
376 return;
379 if (!dev->tcet) {
380 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
381 return;
384 spapr_vio_set_bypass(dev, !!enable);
386 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
389 static void rtas_quiesce(PowerPCCPU *cpu, sPAPRMachineState *spapr,
390 uint32_t token,
391 uint32_t nargs, target_ulong args,
392 uint32_t nret, target_ulong rets)
394 VIOsPAPRBus *bus = spapr->vio_bus;
395 BusChild *kid;
396 VIOsPAPRDevice *dev = NULL;
398 if (nargs != 0) {
399 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
400 return;
403 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
404 dev = (VIOsPAPRDevice *)kid->child;
405 spapr_vio_quiesce_one(dev);
408 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
411 static VIOsPAPRDevice *reg_conflict(VIOsPAPRDevice *dev)
413 VIOsPAPRBus *bus = SPAPR_VIO_BUS(dev->qdev.parent_bus);
414 BusChild *kid;
415 VIOsPAPRDevice *other;
418 * Check for a device other than the given one which is already
419 * using the requested address. We have to open code this because
420 * the given dev might already be in the list.
422 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
423 other = VIO_SPAPR_DEVICE(kid->child);
425 if (other != dev && other->reg == dev->reg) {
426 return other;
430 return 0;
433 static void spapr_vio_busdev_reset(DeviceState *qdev)
435 VIOsPAPRDevice *dev = VIO_SPAPR_DEVICE(qdev);
436 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
438 /* Shut down the request queue and TCEs if necessary */
439 spapr_vio_quiesce_one(dev);
441 dev->signal_state = 0;
443 spapr_vio_set_bypass(dev, false);
444 if (pc->reset) {
445 pc->reset(dev);
450 * The register property of a VIO device is defined in livirt using
451 * 0x1000 as a base register number plus a 0x1000 increment. For the
452 * VIO tty device, the base number is changed to 0x30000000. QEMU uses
453 * a base register number of 0x71000000 and then a simple increment.
455 * The formula below tries to compute a unique index number from the
456 * register value that will be used to define the IRQ number of the
457 * VIO device.
459 * A maximum of 256 VIO devices is covered. Collisions are possible
460 * but they will be detected when the IRQ is claimed.
462 static inline uint32_t spapr_vio_reg_to_irq(uint32_t reg)
464 uint32_t irq;
466 if (reg >= SPAPR_VIO_REG_BASE) {
468 * VIO device register values when allocated by QEMU. For
469 * these, we simply mask the high bits to fit the overall
470 * range: [0x00 - 0xff].
472 * The nvram VIO device (reg=0x71000000) is a static device of
473 * the pseries machine and so is always allocated by QEMU. Its
474 * IRQ number is 0x0.
476 irq = reg & 0xff;
478 } else if (reg >= 0x30000000) {
480 * VIO tty devices register values, when allocated by livirt,
481 * are mapped in range [0xf0 - 0xff], gives us a maximum of 16
482 * vtys.
484 irq = 0xf0 | ((reg >> 12) & 0xf);
486 } else {
488 * Other VIO devices register values, when allocated by
489 * livirt, should be mapped in range [0x00 - 0xef]. Conflicts
490 * will be detected when IRQ is claimed.
492 irq = (reg >> 12) & 0xff;
495 return SPAPR_IRQ_VIO | irq;
498 static void spapr_vio_busdev_realize(DeviceState *qdev, Error **errp)
500 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
501 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
502 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
503 char *id;
504 Error *local_err = NULL;
506 if (dev->reg != -1) {
508 * Explicitly assigned address, just verify that no-one else
509 * is using it. other mechanism). We have to open code this
510 * rather than using spapr_vio_find_by_reg() because sdev
511 * itself is already in the list.
513 VIOsPAPRDevice *other = reg_conflict(dev);
515 if (other) {
516 error_setg(errp, "%s and %s devices conflict at address %#x",
517 object_get_typename(OBJECT(qdev)),
518 object_get_typename(OBJECT(&other->qdev)),
519 dev->reg);
520 return;
522 } else {
523 /* Need to assign an address */
524 VIOsPAPRBus *bus = SPAPR_VIO_BUS(dev->qdev.parent_bus);
526 do {
527 dev->reg = bus->next_reg++;
528 } while (reg_conflict(dev));
531 /* Don't overwrite ids assigned on the command line */
532 if (!dev->qdev.id) {
533 id = spapr_vio_get_dev_name(DEVICE(dev));
534 dev->qdev.id = id;
537 if (!dev->irq) {
538 dev->irq = spapr_vio_reg_to_irq(dev->reg);
540 if (SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
541 dev->irq = spapr_irq_findone(spapr, &local_err);
542 if (local_err) {
543 error_propagate(errp, local_err);
544 return;
549 spapr_irq_claim(spapr, dev->irq, false, &local_err);
550 if (local_err) {
551 error_propagate(errp, local_err);
552 return;
555 if (pc->rtce_window_size) {
556 uint32_t liobn = SPAPR_VIO_LIOBN(dev->reg);
558 memory_region_init(&dev->mrroot, OBJECT(dev), "iommu-spapr-root",
559 ram_size);
560 memory_region_init_alias(&dev->mrbypass, OBJECT(dev),
561 "iommu-spapr-bypass", get_system_memory(),
562 0, ram_size);
563 memory_region_add_subregion_overlap(&dev->mrroot, 0, &dev->mrbypass, 1);
564 address_space_init(&dev->as, &dev->mrroot, qdev->id);
566 dev->tcet = spapr_tce_new_table(qdev, liobn);
567 spapr_tce_table_enable(dev->tcet, SPAPR_TCE_PAGE_SHIFT, 0,
568 pc->rtce_window_size >> SPAPR_TCE_PAGE_SHIFT);
569 dev->tcet->vdev = dev;
570 memory_region_add_subregion_overlap(&dev->mrroot, 0,
571 spapr_tce_get_iommu(dev->tcet), 2);
574 pc->realize(dev, errp);
577 static target_ulong h_vio_signal(PowerPCCPU *cpu, sPAPRMachineState *spapr,
578 target_ulong opcode,
579 target_ulong *args)
581 target_ulong reg = args[0];
582 target_ulong mode = args[1];
583 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
584 VIOsPAPRDeviceClass *pc;
586 if (!dev) {
587 return H_PARAMETER;
590 pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
592 if (mode & ~pc->signal_mask) {
593 return H_PARAMETER;
596 dev->signal_state = mode;
598 return H_SUCCESS;
601 VIOsPAPRBus *spapr_vio_bus_init(void)
603 VIOsPAPRBus *bus;
604 BusState *qbus;
605 DeviceState *dev;
607 /* Create bridge device */
608 dev = qdev_create(NULL, TYPE_SPAPR_VIO_BRIDGE);
609 qdev_init_nofail(dev);
611 /* Create bus on bridge device */
612 qbus = qbus_create(TYPE_SPAPR_VIO_BUS, dev, "spapr-vio");
613 bus = SPAPR_VIO_BUS(qbus);
614 bus->next_reg = SPAPR_VIO_REG_BASE;
616 /* hcall-vio */
617 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
619 /* hcall-crq */
620 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
621 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
622 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
623 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
625 /* RTAS calls */
626 spapr_rtas_register(RTAS_IBM_SET_TCE_BYPASS, "ibm,set-tce-bypass",
627 rtas_set_tce_bypass);
628 spapr_rtas_register(RTAS_QUIESCE, "quiesce", rtas_quiesce);
630 return bus;
633 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
635 DeviceClass *dc = DEVICE_CLASS(klass);
637 dc->fw_name = "vdevice";
640 static const TypeInfo spapr_vio_bridge_info = {
641 .name = TYPE_SPAPR_VIO_BRIDGE,
642 .parent = TYPE_SYS_BUS_DEVICE,
643 .class_init = spapr_vio_bridge_class_init,
646 const VMStateDescription vmstate_spapr_vio = {
647 .name = "spapr_vio",
648 .version_id = 1,
649 .minimum_version_id = 1,
650 .fields = (VMStateField[]) {
651 /* Sanity check */
652 VMSTATE_UINT32_EQUAL(reg, VIOsPAPRDevice, NULL),
653 VMSTATE_UINT32_EQUAL(irq, VIOsPAPRDevice, NULL),
655 /* General VIO device state */
656 VMSTATE_UINT64(signal_state, VIOsPAPRDevice),
657 VMSTATE_UINT64(crq.qladdr, VIOsPAPRDevice),
658 VMSTATE_UINT32(crq.qsize, VIOsPAPRDevice),
659 VMSTATE_UINT32(crq.qnext, VIOsPAPRDevice),
661 VMSTATE_END_OF_LIST()
665 static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
667 DeviceClass *k = DEVICE_CLASS(klass);
668 k->realize = spapr_vio_busdev_realize;
669 k->reset = spapr_vio_busdev_reset;
670 k->bus_type = TYPE_SPAPR_VIO_BUS;
671 k->props = spapr_vio_props;
674 static const TypeInfo spapr_vio_type_info = {
675 .name = TYPE_VIO_SPAPR_DEVICE,
676 .parent = TYPE_DEVICE,
677 .instance_size = sizeof(VIOsPAPRDevice),
678 .abstract = true,
679 .class_size = sizeof(VIOsPAPRDeviceClass),
680 .class_init = vio_spapr_device_class_init,
683 static void spapr_vio_register_types(void)
685 type_register_static(&spapr_vio_bus_info);
686 type_register_static(&spapr_vio_bridge_info);
687 type_register_static(&spapr_vio_type_info);
690 type_init(spapr_vio_register_types)
692 static int compare_reg(const void *p1, const void *p2)
694 VIOsPAPRDevice const *dev1, *dev2;
696 dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
697 dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
699 if (dev1->reg < dev2->reg) {
700 return -1;
702 if (dev1->reg == dev2->reg) {
703 return 0;
706 /* dev1->reg > dev2->reg */
707 return 1;
710 void spapr_dt_vdevice(VIOsPAPRBus *bus, void *fdt)
712 DeviceState *qdev, **qdevs;
713 BusChild *kid;
714 int i, num, ret = 0;
715 int node;
717 _FDT(node = fdt_add_subnode(fdt, 0, "vdevice"));
719 _FDT(fdt_setprop_string(fdt, node, "device_type", "vdevice"));
720 _FDT(fdt_setprop_string(fdt, node, "compatible", "IBM,vdevice"));
721 _FDT(fdt_setprop_cell(fdt, node, "#address-cells", 1));
722 _FDT(fdt_setprop_cell(fdt, node, "#size-cells", 0));
723 _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
724 _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
726 /* Count qdevs on the bus list */
727 num = 0;
728 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
729 num++;
732 /* Copy out into an array of pointers */
733 qdevs = g_new(DeviceState *, num);
734 num = 0;
735 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
736 qdevs[num++] = kid->child;
739 /* Sort the array */
740 qsort(qdevs, num, sizeof(qdev), compare_reg);
742 /* Hack alert. Give the devices to libfdt in reverse order, we happen
743 * to know that will mean they are in forward order in the tree. */
744 for (i = num - 1; i >= 0; i--) {
745 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
746 VIOsPAPRDeviceClass *vdc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
748 ret = vio_make_devnode(dev, fdt);
749 if (ret < 0) {
750 error_report("Couldn't create device node /vdevice/%s@%"PRIx32,
751 vdc->dt_name, dev->reg);
752 exit(1);
756 g_free(qdevs);
759 gchar *spapr_vio_stdout_path(VIOsPAPRBus *bus)
761 VIOsPAPRDevice *dev;
762 char *name, *path;
764 dev = spapr_vty_get_default(bus);
765 if (!dev) {
766 return NULL;
769 name = spapr_vio_get_dev_name(DEVICE(dev));
770 path = g_strdup_printf("/vdevice/%s", name);
772 g_free(name);
773 return path;