xref: /openbmc/qemu/hw/ppc/spapr_vio.c (revision 2d7fedeb)
1 /*
2  * QEMU sPAPR VIO code
3  *
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>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21 
22 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "hw/hw.h"
25 #include "qemu/log.h"
26 #include "sysemu/sysemu.h"
27 #include "hw/boards.h"
28 #include "hw/loader.h"
29 #include "elf.h"
30 #include "hw/sysbus.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/device_tree.h"
33 #include "kvm_ppc.h"
34 
35 #include "hw/ppc/spapr.h"
36 #include "hw/ppc/spapr_vio.h"
37 #include "hw/ppc/xics.h"
38 
39 #include <libfdt.h>
40 
41 /* #define DEBUG_SPAPR */
42 
43 #ifdef DEBUG_SPAPR
44 #define DPRINTF(fmt, ...) \
45     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46 #else
47 #define DPRINTF(fmt, ...) \
48     do { } while (0)
49 #endif
50 
51 static Property spapr_vio_props[] = {
52     DEFINE_PROP_UINT32("irq", VIOsPAPRDevice, irq, 0), \
53     DEFINE_PROP_END_OF_LIST(),
54 };
55 
56 static char *spapr_vio_get_dev_name(DeviceState *qdev)
57 {
58     VIOsPAPRDevice *dev = VIO_SPAPR_DEVICE(qdev);
59     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
60     char *name;
61 
62     /* Device tree style name device@reg */
63     name = g_strdup_printf("%s@%x", pc->dt_name, dev->reg);
64 
65     return name;
66 }
67 
68 static void spapr_vio_bus_class_init(ObjectClass *klass, void *data)
69 {
70     BusClass *k = BUS_CLASS(klass);
71 
72     k->get_dev_path = spapr_vio_get_dev_name;
73     k->get_fw_dev_path = spapr_vio_get_dev_name;
74 }
75 
76 static const TypeInfo spapr_vio_bus_info = {
77     .name = TYPE_SPAPR_VIO_BUS,
78     .parent = TYPE_BUS,
79     .class_init = spapr_vio_bus_class_init,
80     .instance_size = sizeof(VIOsPAPRBus),
81 };
82 
83 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
84 {
85     BusChild *kid;
86     VIOsPAPRDevice *dev = NULL;
87 
88     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
89         dev = (VIOsPAPRDevice *)kid->child;
90         if (dev->reg == reg) {
91             return dev;
92         }
93     }
94 
95     return NULL;
96 }
97 
98 static int vio_make_devnode(VIOsPAPRDevice *dev,
99                             void *fdt)
100 {
101     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
102     int vdevice_off, node_off, ret;
103     char *dt_name;
104 
105     vdevice_off = fdt_path_offset(fdt, "/vdevice");
106     if (vdevice_off < 0) {
107         return vdevice_off;
108     }
109 
110     dt_name = spapr_vio_get_dev_name(DEVICE(dev));
111     node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
112     g_free(dt_name);
113     if (node_off < 0) {
114         return node_off;
115     }
116 
117     ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
118     if (ret < 0) {
119         return ret;
120     }
121 
122     if (pc->dt_type) {
123         ret = fdt_setprop_string(fdt, node_off, "device_type",
124                                  pc->dt_type);
125         if (ret < 0) {
126             return ret;
127         }
128     }
129 
130     if (pc->dt_compatible) {
131         ret = fdt_setprop_string(fdt, node_off, "compatible",
132                                  pc->dt_compatible);
133         if (ret < 0) {
134             return ret;
135         }
136     }
137 
138     if (dev->irq) {
139         uint32_t ints_prop[] = {cpu_to_be32(dev->irq), 0};
140 
141         ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
142                           sizeof(ints_prop));
143         if (ret < 0) {
144             return ret;
145         }
146     }
147 
148     ret = spapr_tcet_dma_dt(fdt, node_off, "ibm,my-dma-window", dev->tcet);
149     if (ret < 0) {
150         return ret;
151     }
152 
153     if (pc->devnode) {
154         ret = (pc->devnode)(dev, fdt, node_off);
155         if (ret < 0) {
156             return ret;
157         }
158     }
159 
160     return node_off;
161 }
162 
163 /*
164  * CRQ handling
165  */
166 static target_ulong h_reg_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
167                               target_ulong opcode, target_ulong *args)
168 {
169     target_ulong reg = args[0];
170     target_ulong queue_addr = args[1];
171     target_ulong queue_len = args[2];
172     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
173 
174     if (!dev) {
175         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
176         return H_PARAMETER;
177     }
178 
179     /* We can't grok a queue size bigger than 256M for now */
180     if (queue_len < 0x1000 || queue_len > 0x10000000) {
181         hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
182                       ")\n", queue_len);
183         return H_PARAMETER;
184     }
185 
186     /* Check queue alignment */
187     if (queue_addr & 0xfff) {
188         hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx ")\n", queue_addr);
189         return H_PARAMETER;
190     }
191 
192     /* Check if device supports CRQs */
193     if (!dev->crq.SendFunc) {
194         hcall_dprintf("Device does not support CRQ\n");
195         return H_NOT_FOUND;
196     }
197 
198     /* Already a queue ? */
199     if (dev->crq.qsize) {
200         hcall_dprintf("CRQ already registered\n");
201         return H_RESOURCE;
202     }
203     dev->crq.qladdr = queue_addr;
204     dev->crq.qsize = queue_len;
205     dev->crq.qnext = 0;
206 
207     DPRINTF("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
208             TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
209             reg, queue_addr, queue_len);
210     return H_SUCCESS;
211 }
212 
213 static target_ulong free_crq(VIOsPAPRDevice *dev)
214 {
215     dev->crq.qladdr = 0;
216     dev->crq.qsize = 0;
217     dev->crq.qnext = 0;
218 
219     DPRINTF("CRQ for dev 0x%" PRIx32 " freed\n", dev->reg);
220 
221     return H_SUCCESS;
222 }
223 
224 static target_ulong h_free_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
225                                target_ulong opcode, target_ulong *args)
226 {
227     target_ulong reg = args[0];
228     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
229 
230     if (!dev) {
231         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
232         return H_PARAMETER;
233     }
234 
235     return free_crq(dev);
236 }
237 
238 static target_ulong h_send_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
239                                target_ulong opcode, target_ulong *args)
240 {
241     target_ulong reg = args[0];
242     target_ulong msg_hi = args[1];
243     target_ulong msg_lo = args[2];
244     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
245     uint64_t crq_mangle[2];
246 
247     if (!dev) {
248         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
249         return H_PARAMETER;
250     }
251     crq_mangle[0] = cpu_to_be64(msg_hi);
252     crq_mangle[1] = cpu_to_be64(msg_lo);
253 
254     if (dev->crq.SendFunc) {
255         return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
256     }
257 
258     return H_HARDWARE;
259 }
260 
261 static target_ulong h_enable_crq(PowerPCCPU *cpu, sPAPRMachineState *spapr,
262                                  target_ulong opcode, target_ulong *args)
263 {
264     target_ulong reg = args[0];
265     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
266 
267     if (!dev) {
268         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
269         return H_PARAMETER;
270     }
271 
272     return 0;
273 }
274 
275 /* Returns negative error, 0 success, or positive: queue full */
276 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
277 {
278     int rc;
279     uint8_t byte;
280 
281     if (!dev->crq.qsize) {
282         fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
283         return -1;
284     }
285 
286     /* Maybe do a fast path for KVM just writing to the pages */
287     rc = spapr_vio_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
288     if (rc) {
289         return rc;
290     }
291     if (byte != 0) {
292         return 1;
293     }
294 
295     rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
296                              &crq[8], 8);
297     if (rc) {
298         return rc;
299     }
300 
301     kvmppc_eieio();
302 
303     rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
304     if (rc) {
305         return rc;
306     }
307 
308     dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
309 
310     if (dev->signal_state & 1) {
311         qemu_irq_pulse(spapr_vio_qirq(dev));
312     }
313 
314     return 0;
315 }
316 
317 /* "quiesce" handling */
318 
319 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
320 {
321     if (dev->tcet) {
322         device_reset(DEVICE(dev->tcet));
323     }
324     free_crq(dev);
325 }
326 
327 void spapr_vio_set_bypass(VIOsPAPRDevice *dev, bool bypass)
328 {
329     if (!dev->tcet) {
330         return;
331     }
332 
333     memory_region_set_enabled(&dev->mrbypass, bypass);
334     memory_region_set_enabled(spapr_tce_get_iommu(dev->tcet), !bypass);
335 
336     dev->tcet->bypass = bypass;
337 }
338 
339 static void rtas_set_tce_bypass(PowerPCCPU *cpu, sPAPRMachineState *spapr,
340                                 uint32_t token,
341                                 uint32_t nargs, target_ulong args,
342                                 uint32_t nret, target_ulong rets)
343 {
344     VIOsPAPRBus *bus = spapr->vio_bus;
345     VIOsPAPRDevice *dev;
346     uint32_t unit, enable;
347 
348     if (nargs != 2) {
349         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
350         return;
351     }
352     unit = rtas_ld(args, 0);
353     enable = rtas_ld(args, 1);
354     dev = spapr_vio_find_by_reg(bus, unit);
355     if (!dev) {
356         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
357         return;
358     }
359 
360     if (!dev->tcet) {
361         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
362         return;
363     }
364 
365     spapr_vio_set_bypass(dev, !!enable);
366 
367     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
368 }
369 
370 static void rtas_quiesce(PowerPCCPU *cpu, sPAPRMachineState *spapr,
371                          uint32_t token,
372                          uint32_t nargs, target_ulong args,
373                          uint32_t nret, target_ulong rets)
374 {
375     VIOsPAPRBus *bus = spapr->vio_bus;
376     BusChild *kid;
377     VIOsPAPRDevice *dev = NULL;
378 
379     if (nargs != 0) {
380         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
381         return;
382     }
383 
384     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
385         dev = (VIOsPAPRDevice *)kid->child;
386         spapr_vio_quiesce_one(dev);
387     }
388 
389     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
390 }
391 
392 static VIOsPAPRDevice *reg_conflict(VIOsPAPRDevice *dev)
393 {
394     VIOsPAPRBus *bus = SPAPR_VIO_BUS(dev->qdev.parent_bus);
395     BusChild *kid;
396     VIOsPAPRDevice *other;
397 
398     /*
399      * Check for a device other than the given one which is already
400      * using the requested address. We have to open code this because
401      * the given dev might already be in the list.
402      */
403     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
404         other = VIO_SPAPR_DEVICE(kid->child);
405 
406         if (other != dev && other->reg == dev->reg) {
407             return other;
408         }
409     }
410 
411     return 0;
412 }
413 
414 static void spapr_vio_busdev_reset(DeviceState *qdev)
415 {
416     VIOsPAPRDevice *dev = VIO_SPAPR_DEVICE(qdev);
417     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
418 
419     /* Shut down the request queue and TCEs if necessary */
420     spapr_vio_quiesce_one(dev);
421 
422     dev->signal_state = 0;
423 
424     spapr_vio_set_bypass(dev, false);
425     if (pc->reset) {
426         pc->reset(dev);
427     }
428 }
429 
430 static void spapr_vio_busdev_realize(DeviceState *qdev, Error **errp)
431 {
432     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
433     VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
434     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
435     char *id;
436     Error *local_err = NULL;
437 
438     if (dev->reg != -1) {
439         /*
440          * Explicitly assigned address, just verify that no-one else
441          * is using it.  other mechanism). We have to open code this
442          * rather than using spapr_vio_find_by_reg() because sdev
443          * itself is already in the list.
444          */
445         VIOsPAPRDevice *other = reg_conflict(dev);
446 
447         if (other) {
448             error_setg(errp, "%s and %s devices conflict at address %#x",
449                        object_get_typename(OBJECT(qdev)),
450                        object_get_typename(OBJECT(&other->qdev)),
451                        dev->reg);
452             return;
453         }
454     } else {
455         /* Need to assign an address */
456         VIOsPAPRBus *bus = SPAPR_VIO_BUS(dev->qdev.parent_bus);
457 
458         do {
459             dev->reg = bus->next_reg++;
460         } while (reg_conflict(dev));
461     }
462 
463     /* Don't overwrite ids assigned on the command line */
464     if (!dev->qdev.id) {
465         id = spapr_vio_get_dev_name(DEVICE(dev));
466         dev->qdev.id = id;
467     }
468 
469     dev->irq = xics_alloc(spapr->icp, 0, dev->irq, false, &local_err);
470     if (local_err) {
471         error_propagate(errp, local_err);
472         return;
473     }
474 
475     if (pc->rtce_window_size) {
476         uint32_t liobn = SPAPR_VIO_LIOBN(dev->reg);
477 
478         memory_region_init(&dev->mrroot, OBJECT(dev), "iommu-spapr-root",
479                            ram_size);
480         memory_region_init_alias(&dev->mrbypass, OBJECT(dev),
481                                  "iommu-spapr-bypass", get_system_memory(),
482                                  0, ram_size);
483         memory_region_add_subregion_overlap(&dev->mrroot, 0, &dev->mrbypass, 1);
484         address_space_init(&dev->as, &dev->mrroot, qdev->id);
485 
486         dev->tcet = spapr_tce_new_table(qdev, liobn);
487         spapr_tce_table_enable(dev->tcet, SPAPR_TCE_PAGE_SHIFT, 0,
488                                pc->rtce_window_size >> SPAPR_TCE_PAGE_SHIFT);
489         dev->tcet->vdev = dev;
490         memory_region_add_subregion_overlap(&dev->mrroot, 0,
491                                             spapr_tce_get_iommu(dev->tcet), 2);
492     }
493 
494     pc->realize(dev, errp);
495 }
496 
497 static target_ulong h_vio_signal(PowerPCCPU *cpu, sPAPRMachineState *spapr,
498                                  target_ulong opcode,
499                                  target_ulong *args)
500 {
501     target_ulong reg = args[0];
502     target_ulong mode = args[1];
503     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
504     VIOsPAPRDeviceClass *pc;
505 
506     if (!dev) {
507         return H_PARAMETER;
508     }
509 
510     pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
511 
512     if (mode & ~pc->signal_mask) {
513         return H_PARAMETER;
514     }
515 
516     dev->signal_state = mode;
517 
518     return H_SUCCESS;
519 }
520 
521 VIOsPAPRBus *spapr_vio_bus_init(void)
522 {
523     VIOsPAPRBus *bus;
524     BusState *qbus;
525     DeviceState *dev;
526 
527     /* Create bridge device */
528     dev = qdev_create(NULL, TYPE_SPAPR_VIO_BRIDGE);
529     qdev_init_nofail(dev);
530 
531     /* Create bus on bridge device */
532     qbus = qbus_create(TYPE_SPAPR_VIO_BUS, dev, "spapr-vio");
533     bus = SPAPR_VIO_BUS(qbus);
534     bus->next_reg = 0x71000000;
535 
536     /* hcall-vio */
537     spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
538 
539     /* hcall-crq */
540     spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
541     spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
542     spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
543     spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
544 
545     /* RTAS calls */
546     spapr_rtas_register(RTAS_IBM_SET_TCE_BYPASS, "ibm,set-tce-bypass",
547                         rtas_set_tce_bypass);
548     spapr_rtas_register(RTAS_QUIESCE, "quiesce", rtas_quiesce);
549 
550     return bus;
551 }
552 
553 /* Represents sPAPR hcall VIO devices */
554 
555 static int spapr_vio_bridge_init(SysBusDevice *dev)
556 {
557     /* nothing */
558     return 0;
559 }
560 
561 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
562 {
563     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
564     DeviceClass *dc = DEVICE_CLASS(klass);
565 
566     dc->fw_name = "vdevice";
567     k->init = spapr_vio_bridge_init;
568 }
569 
570 static const TypeInfo spapr_vio_bridge_info = {
571     .name          = TYPE_SPAPR_VIO_BRIDGE,
572     .parent        = TYPE_SYS_BUS_DEVICE,
573     .class_init    = spapr_vio_bridge_class_init,
574 };
575 
576 const VMStateDescription vmstate_spapr_vio = {
577     .name = "spapr_vio",
578     .version_id = 1,
579     .minimum_version_id = 1,
580     .fields = (VMStateField[]) {
581         /* Sanity check */
582         VMSTATE_UINT32_EQUAL(reg, VIOsPAPRDevice),
583         VMSTATE_UINT32_EQUAL(irq, VIOsPAPRDevice),
584 
585         /* General VIO device state */
586         VMSTATE_UINT64(signal_state, VIOsPAPRDevice),
587         VMSTATE_UINT64(crq.qladdr, VIOsPAPRDevice),
588         VMSTATE_UINT32(crq.qsize, VIOsPAPRDevice),
589         VMSTATE_UINT32(crq.qnext, VIOsPAPRDevice),
590 
591         VMSTATE_END_OF_LIST()
592     },
593 };
594 
595 static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
596 {
597     DeviceClass *k = DEVICE_CLASS(klass);
598     k->realize = spapr_vio_busdev_realize;
599     k->reset = spapr_vio_busdev_reset;
600     k->bus_type = TYPE_SPAPR_VIO_BUS;
601     k->props = spapr_vio_props;
602 }
603 
604 static const TypeInfo spapr_vio_type_info = {
605     .name = TYPE_VIO_SPAPR_DEVICE,
606     .parent = TYPE_DEVICE,
607     .instance_size = sizeof(VIOsPAPRDevice),
608     .abstract = true,
609     .class_size = sizeof(VIOsPAPRDeviceClass),
610     .class_init = vio_spapr_device_class_init,
611 };
612 
613 static void spapr_vio_register_types(void)
614 {
615     type_register_static(&spapr_vio_bus_info);
616     type_register_static(&spapr_vio_bridge_info);
617     type_register_static(&spapr_vio_type_info);
618 }
619 
620 type_init(spapr_vio_register_types)
621 
622 static int compare_reg(const void *p1, const void *p2)
623 {
624     VIOsPAPRDevice const *dev1, *dev2;
625 
626     dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
627     dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
628 
629     if (dev1->reg < dev2->reg) {
630         return -1;
631     }
632     if (dev1->reg == dev2->reg) {
633         return 0;
634     }
635 
636     /* dev1->reg > dev2->reg */
637     return 1;
638 }
639 
640 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
641 {
642     DeviceState *qdev, **qdevs;
643     BusChild *kid;
644     int i, num, ret = 0;
645 
646     /* Count qdevs on the bus list */
647     num = 0;
648     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
649         num++;
650     }
651 
652     /* Copy out into an array of pointers */
653     qdevs = g_malloc(sizeof(qdev) * num);
654     num = 0;
655     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
656         qdevs[num++] = kid->child;
657     }
658 
659     /* Sort the array */
660     qsort(qdevs, num, sizeof(qdev), compare_reg);
661 
662     /* Hack alert. Give the devices to libfdt in reverse order, we happen
663      * to know that will mean they are in forward order in the tree. */
664     for (i = num - 1; i >= 0; i--) {
665         VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
666 
667         ret = vio_make_devnode(dev, fdt);
668 
669         if (ret < 0) {
670             goto out;
671         }
672     }
673 
674     ret = 0;
675 out:
676     g_free(qdevs);
677 
678     return ret;
679 }
680 
681 int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus)
682 {
683     VIOsPAPRDevice *dev;
684     char *name, *path;
685     int ret, offset;
686 
687     dev = spapr_vty_get_default(bus);
688     if (!dev)
689         return 0;
690 
691     offset = fdt_path_offset(fdt, "/chosen");
692     if (offset < 0) {
693         return offset;
694     }
695 
696     name = spapr_vio_get_dev_name(DEVICE(dev));
697     path = g_strdup_printf("/vdevice/%s", name);
698 
699     ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path);
700 
701     g_free(name);
702     g_free(path);
703 
704     return ret;
705 }
706