qxl: remove flipped
[qemu/agraf.git] / hw / spapr_vio.c
blobea317efbe49d03666206c0f97b4a54514bf6d8d2
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 "hw.h"
23 #include "sysemu.h"
24 #include "boards.h"
25 #include "monitor.h"
26 #include "loader.h"
27 #include "elf.h"
28 #include "hw/sysbus.h"
29 #include "kvm.h"
30 #include "device_tree.h"
31 #include "kvm_ppc.h"
33 #include "hw/spapr.h"
34 #include "hw/spapr_vio.h"
35 #include "hw/xics.h"
37 #ifdef CONFIG_FDT
38 #include <libfdt.h>
39 #endif /* CONFIG_FDT */
41 /* #define DEBUG_SPAPR */
42 /* #define DEBUG_TCE */
44 #ifdef DEBUG_SPAPR
45 #define dprintf(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #else
48 #define dprintf(fmt, ...) \
49 do { } while (0)
50 #endif
52 static struct BusInfo spapr_vio_bus_info = {
53 .name = "spapr-vio",
54 .size = sizeof(VIOsPAPRBus),
55 .props = (Property[]) {
56 DEFINE_PROP_UINT32("irq", VIOsPAPRDevice, vio_irq_num, 0), \
57 DEFINE_PROP_END_OF_LIST(),
61 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
63 DeviceState *qdev;
64 VIOsPAPRDevice *dev = NULL;
66 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
67 dev = (VIOsPAPRDevice *)qdev;
68 if (dev->reg == reg) {
69 return dev;
73 return NULL;
76 static char *vio_format_dev_name(VIOsPAPRDevice *dev)
78 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
79 char *name;
81 /* Device tree style name device@reg */
82 if (asprintf(&name, "%s@%x", pc->dt_name, dev->reg) < 0) {
83 return NULL;
86 return name;
89 #ifdef CONFIG_FDT
90 static int vio_make_devnode(VIOsPAPRDevice *dev,
91 void *fdt)
93 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
94 int vdevice_off, node_off, ret;
95 char *dt_name;
97 vdevice_off = fdt_path_offset(fdt, "/vdevice");
98 if (vdevice_off < 0) {
99 return vdevice_off;
102 dt_name = vio_format_dev_name(dev);
103 if (!dt_name) {
104 return -ENOMEM;
107 node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
108 free(dt_name);
109 if (node_off < 0) {
110 return node_off;
113 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
114 if (ret < 0) {
115 return ret;
118 if (pc->dt_type) {
119 ret = fdt_setprop_string(fdt, node_off, "device_type",
120 pc->dt_type);
121 if (ret < 0) {
122 return ret;
126 if (pc->dt_compatible) {
127 ret = fdt_setprop_string(fdt, node_off, "compatible",
128 pc->dt_compatible);
129 if (ret < 0) {
130 return ret;
134 if (dev->qirq) {
135 uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0};
137 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
138 sizeof(ints_prop));
139 if (ret < 0) {
140 return ret;
144 if (dev->rtce_window_size) {
145 uint32_t dma_prop[] = {cpu_to_be32(dev->reg),
146 0, 0,
147 0, cpu_to_be32(dev->rtce_window_size)};
149 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
150 if (ret < 0) {
151 return ret;
154 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
155 if (ret < 0) {
156 return ret;
159 ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop,
160 sizeof(dma_prop));
161 if (ret < 0) {
162 return ret;
166 if (pc->devnode) {
167 ret = (pc->devnode)(dev, fdt, node_off);
168 if (ret < 0) {
169 return ret;
173 return node_off;
175 #endif /* CONFIG_FDT */
178 * RTCE handling
181 static void rtce_init(VIOsPAPRDevice *dev)
183 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
184 * sizeof(VIOsPAPR_RTCE);
186 if (size) {
187 dev->rtce_table = kvmppc_create_spapr_tce(dev->reg,
188 dev->rtce_window_size,
189 &dev->kvmtce_fd);
191 if (!dev->rtce_table) {
192 dev->rtce_table = g_malloc0(size);
197 static target_ulong h_put_tce(CPUState *env, sPAPREnvironment *spapr,
198 target_ulong opcode, target_ulong *args)
200 target_ulong liobn = args[0];
201 target_ulong ioba = args[1];
202 target_ulong tce = args[2];
203 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, liobn);
204 VIOsPAPR_RTCE *rtce;
206 if (!dev) {
207 hcall_dprintf("spapr_vio_put_tce on non-existent LIOBN "
208 TARGET_FMT_lx "\n", liobn);
209 return H_PARAMETER;
212 ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1);
214 #ifdef DEBUG_TCE
215 fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx
216 " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce);
217 #endif
219 if (ioba >= dev->rtce_window_size) {
220 hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
221 TARGET_FMT_lx "\n", ioba);
222 return H_PARAMETER;
225 rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT);
226 rtce->tce = tce;
228 return H_SUCCESS;
231 int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba,
232 target_ulong len, enum VIOsPAPR_TCEAccess access)
234 int start, end, i;
236 start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT;
237 end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT;
239 for (i = start; i <= end; i++) {
240 if ((dev->rtce_table[i].tce & access) != access) {
241 #ifdef DEBUG_TCE
242 fprintf(stderr, "FAIL on %d\n", i);
243 #endif
244 return -1;
248 return 0;
251 int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf,
252 uint32_t size)
254 #ifdef DEBUG_TCE
255 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
256 (unsigned long long)taddr, size);
257 #endif
259 /* Check for bypass */
260 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
261 cpu_physical_memory_write(taddr, buf, size);
262 return 0;
265 while (size) {
266 uint64_t tce;
267 uint32_t lsize;
268 uint64_t txaddr;
270 /* Check if we are in bound */
271 if (taddr >= dev->rtce_window_size) {
272 #ifdef DEBUG_TCE
273 fprintf(stderr, "spapr_tce_dma_write out of bounds\n");
274 #endif
275 return H_DEST_PARM;
277 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
279 /* How much til end of page ? */
280 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
282 /* Check TCE */
283 if (!(tce & 2)) {
284 return H_DEST_PARM;
287 /* Translate */
288 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
289 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
291 #ifdef DEBUG_TCE
292 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
293 (unsigned long long)txaddr, lsize);
294 #endif
296 /* Do it */
297 cpu_physical_memory_write(txaddr, buf, lsize);
298 buf += lsize;
299 taddr += lsize;
300 size -= lsize;
302 return 0;
305 int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size)
307 /* FIXME: allocating a temp buffer is nasty, but just stepping
308 * through writing zeroes is awkward. This will do for now. */
309 uint8_t zeroes[size];
311 #ifdef DEBUG_TCE
312 fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
313 (unsigned long long)taddr, size);
314 #endif
316 memset(zeroes, 0, size);
317 return spapr_tce_dma_write(dev, taddr, zeroes, size);
320 void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val)
322 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
325 void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val)
327 val = tswap16(val);
328 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
332 void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val)
334 val = tswap32(val);
335 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
338 void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val)
340 val = tswap64(val);
341 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
344 int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf,
345 uint32_t size)
347 #ifdef DEBUG_TCE
348 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
349 (unsigned long long)taddr, size);
350 #endif
352 /* Check for bypass */
353 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
354 cpu_physical_memory_read(taddr, buf, size);
355 return 0;
358 while (size) {
359 uint64_t tce;
360 uint32_t lsize;
361 uint64_t txaddr;
363 /* Check if we are in bound */
364 if (taddr >= dev->rtce_window_size) {
365 #ifdef DEBUG_TCE
366 fprintf(stderr, "spapr_tce_dma_read out of bounds\n");
367 #endif
368 return H_DEST_PARM;
370 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
372 /* How much til end of page ? */
373 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
375 /* Check TCE */
376 if (!(tce & 1)) {
377 return H_DEST_PARM;
380 /* Translate */
381 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
382 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
384 #ifdef DEBUG_TCE
385 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
386 (unsigned long long)txaddr, lsize);
387 #endif
388 /* Do it */
389 cpu_physical_memory_read(txaddr, buf, lsize);
390 buf += lsize;
391 taddr += lsize;
392 size -= lsize;
394 return H_SUCCESS;
397 uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr)
399 uint64_t val;
401 spapr_tce_dma_read(dev, taddr, &val, sizeof(val));
402 return tswap64(val);
406 * CRQ handling
408 static target_ulong h_reg_crq(CPUState *env, sPAPREnvironment *spapr,
409 target_ulong opcode, target_ulong *args)
411 target_ulong reg = args[0];
412 target_ulong queue_addr = args[1];
413 target_ulong queue_len = args[2];
414 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
416 if (!dev) {
417 hcall_dprintf("h_reg_crq on non-existent unit 0x"
418 TARGET_FMT_lx "\n", reg);
419 return H_PARAMETER;
422 /* We can't grok a queue size bigger than 256M for now */
423 if (queue_len < 0x1000 || queue_len > 0x10000000) {
424 hcall_dprintf("h_reg_crq, queue size too small or too big (0x%llx)\n",
425 (unsigned long long)queue_len);
426 return H_PARAMETER;
429 /* Check queue alignment */
430 if (queue_addr & 0xfff) {
431 hcall_dprintf("h_reg_crq, queue not aligned (0x%llx)\n",
432 (unsigned long long)queue_addr);
433 return H_PARAMETER;
436 /* Check if device supports CRQs */
437 if (!dev->crq.SendFunc) {
438 return H_NOT_FOUND;
442 /* Already a queue ? */
443 if (dev->crq.qsize) {
444 return H_RESOURCE;
446 dev->crq.qladdr = queue_addr;
447 dev->crq.qsize = queue_len;
448 dev->crq.qnext = 0;
450 dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
451 TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
452 reg, queue_addr, queue_len);
453 return H_SUCCESS;
456 static target_ulong h_free_crq(CPUState *env, sPAPREnvironment *spapr,
457 target_ulong opcode, target_ulong *args)
459 target_ulong reg = args[0];
460 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
462 if (!dev) {
463 hcall_dprintf("h_free_crq on non-existent unit 0x"
464 TARGET_FMT_lx "\n", reg);
465 return H_PARAMETER;
468 dev->crq.qladdr = 0;
469 dev->crq.qsize = 0;
470 dev->crq.qnext = 0;
472 dprintf("CRQ for dev 0x" TARGET_FMT_lx " freed\n", reg);
474 return H_SUCCESS;
477 static target_ulong h_send_crq(CPUState *env, sPAPREnvironment *spapr,
478 target_ulong opcode, target_ulong *args)
480 target_ulong reg = args[0];
481 target_ulong msg_hi = args[1];
482 target_ulong msg_lo = args[2];
483 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
484 uint64_t crq_mangle[2];
486 if (!dev) {
487 hcall_dprintf("h_send_crq on non-existent unit 0x"
488 TARGET_FMT_lx "\n", reg);
489 return H_PARAMETER;
491 crq_mangle[0] = cpu_to_be64(msg_hi);
492 crq_mangle[1] = cpu_to_be64(msg_lo);
494 if (dev->crq.SendFunc) {
495 return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
498 return H_HARDWARE;
501 static target_ulong h_enable_crq(CPUState *env, sPAPREnvironment *spapr,
502 target_ulong opcode, target_ulong *args)
504 target_ulong reg = args[0];
505 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
507 if (!dev) {
508 hcall_dprintf("h_enable_crq on non-existent unit 0x"
509 TARGET_FMT_lx "\n", reg);
510 return H_PARAMETER;
513 return 0;
516 /* Returns negative error, 0 success, or positive: queue full */
517 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
519 int rc;
520 uint8_t byte;
522 if (!dev->crq.qsize) {
523 fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
524 return -1;
527 /* Maybe do a fast path for KVM just writing to the pages */
528 rc = spapr_tce_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
529 if (rc) {
530 return rc;
532 if (byte != 0) {
533 return 1;
536 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
537 &crq[8], 8);
538 if (rc) {
539 return rc;
542 kvmppc_eieio();
544 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
545 if (rc) {
546 return rc;
549 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
551 if (dev->signal_state & 1) {
552 qemu_irq_pulse(dev->qirq);
555 return 0;
558 /* "quiesce" handling */
560 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
562 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
564 if (dev->rtce_table) {
565 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
566 * sizeof(VIOsPAPR_RTCE);
567 memset(dev->rtce_table, 0, size);
570 dev->crq.qladdr = 0;
571 dev->crq.qsize = 0;
572 dev->crq.qnext = 0;
575 static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token,
576 uint32_t nargs, target_ulong args,
577 uint32_t nret, target_ulong rets)
579 VIOsPAPRBus *bus = spapr->vio_bus;
580 VIOsPAPRDevice *dev;
581 uint32_t unit, enable;
583 if (nargs != 2) {
584 rtas_st(rets, 0, -3);
585 return;
587 unit = rtas_ld(args, 0);
588 enable = rtas_ld(args, 1);
589 dev = spapr_vio_find_by_reg(bus, unit);
590 if (!dev) {
591 rtas_st(rets, 0, -3);
592 return;
594 if (enable) {
595 dev->flags |= VIO_PAPR_FLAG_DMA_BYPASS;
596 } else {
597 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
600 rtas_st(rets, 0, 0);
603 static void rtas_quiesce(sPAPREnvironment *spapr, uint32_t token,
604 uint32_t nargs, target_ulong args,
605 uint32_t nret, target_ulong rets)
607 VIOsPAPRBus *bus = spapr->vio_bus;
608 DeviceState *qdev;
609 VIOsPAPRDevice *dev = NULL;
611 if (nargs != 0) {
612 rtas_st(rets, 0, -3);
613 return;
616 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
617 dev = (VIOsPAPRDevice *)qdev;
618 spapr_vio_quiesce_one(dev);
621 rtas_st(rets, 0, 0);
624 static int spapr_vio_check_reg(VIOsPAPRDevice *sdev)
626 VIOsPAPRDevice *other_sdev;
627 DeviceState *qdev;
628 VIOsPAPRBus *sbus;
630 sbus = DO_UPCAST(VIOsPAPRBus, bus, sdev->qdev.parent_bus);
633 * Check two device aren't given clashing addresses by the user (or some
634 * other mechanism). We have to open code this because we have to check
635 * for matches with devices other than us.
637 QTAILQ_FOREACH(qdev, &sbus->bus.children, sibling) {
638 other_sdev = DO_UPCAST(VIOsPAPRDevice, qdev, qdev);
640 if (other_sdev != sdev && other_sdev->reg == sdev->reg) {
641 fprintf(stderr, "vio: %s and %s devices conflict at address %#x\n",
642 object_get_typename(OBJECT(sdev)),
643 object_get_typename(OBJECT(qdev)),
644 sdev->reg);
645 return -EEXIST;
649 return 0;
652 static int spapr_vio_busdev_init(DeviceState *qdev)
654 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
655 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
656 char *id;
657 int ret;
659 ret = spapr_vio_check_reg(dev);
660 if (ret) {
661 return ret;
664 /* Don't overwrite ids assigned on the command line */
665 if (!dev->qdev.id) {
666 id = vio_format_dev_name(dev);
667 if (!id) {
668 return -1;
670 dev->qdev.id = id;
673 dev->qirq = spapr_allocate_irq(dev->vio_irq_num, &dev->vio_irq_num);
674 if (!dev->qirq) {
675 return -1;
678 rtce_init(dev);
680 return pc->init(dev);
683 static target_ulong h_vio_signal(CPUState *env, sPAPREnvironment *spapr,
684 target_ulong opcode,
685 target_ulong *args)
687 target_ulong reg = args[0];
688 target_ulong mode = args[1];
689 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
690 VIOsPAPRDeviceClass *pc;
692 if (!dev) {
693 return H_PARAMETER;
696 pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
698 if (mode & ~pc->signal_mask) {
699 return H_PARAMETER;
702 dev->signal_state = mode;
704 return H_SUCCESS;
707 VIOsPAPRBus *spapr_vio_bus_init(void)
709 VIOsPAPRBus *bus;
710 BusState *qbus;
711 DeviceState *dev;
713 /* Create bridge device */
714 dev = qdev_create(NULL, "spapr-vio-bridge");
715 qdev_init_nofail(dev);
717 /* Create bus on bridge device */
719 qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio");
720 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
722 /* hcall-vio */
723 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
725 /* hcall-tce */
726 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
728 /* hcall-crq */
729 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
730 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
731 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
732 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
734 /* RTAS calls */
735 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass);
736 spapr_rtas_register("quiesce", rtas_quiesce);
738 return bus;
741 /* Represents sPAPR hcall VIO devices */
743 static int spapr_vio_bridge_init(SysBusDevice *dev)
745 /* nothing */
746 return 0;
749 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
751 DeviceClass *dc = DEVICE_CLASS(klass);
752 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
754 k->init = spapr_vio_bridge_init;
755 dc->no_user = 1;
758 static TypeInfo spapr_vio_bridge_info = {
759 .name = "spapr-vio-bridge",
760 .parent = TYPE_SYS_BUS_DEVICE,
761 .instance_size = sizeof(SysBusDevice),
762 .class_init = spapr_vio_bridge_class_init,
765 static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
767 DeviceClass *k = DEVICE_CLASS(klass);
768 k->init = spapr_vio_busdev_init;
769 k->bus_info = &spapr_vio_bus_info;
772 static TypeInfo spapr_vio_type_info = {
773 .name = TYPE_VIO_SPAPR_DEVICE,
774 .parent = TYPE_DEVICE,
775 .instance_size = sizeof(VIOsPAPRDevice),
776 .abstract = true,
777 .class_size = sizeof(VIOsPAPRDeviceClass),
778 .class_init = vio_spapr_device_class_init,
781 static void spapr_vio_register_types(void)
783 type_register_static(&spapr_vio_bridge_info);
784 type_register_static(&spapr_vio_type_info);
787 type_init(spapr_vio_register_types)
789 #ifdef CONFIG_FDT
790 static int compare_reg(const void *p1, const void *p2)
792 VIOsPAPRDevice const *dev1, *dev2;
794 dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
795 dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
797 if (dev1->reg < dev2->reg) {
798 return -1;
800 if (dev1->reg == dev2->reg) {
801 return 0;
804 /* dev1->reg > dev2->reg */
805 return 1;
808 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
810 DeviceState *qdev, **qdevs;
811 int i, num, ret = 0;
813 /* Count qdevs on the bus list */
814 num = 0;
815 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
816 num++;
819 /* Copy out into an array of pointers */
820 qdevs = g_malloc(sizeof(qdev) * num);
821 num = 0;
822 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
823 qdevs[num++] = qdev;
826 /* Sort the array */
827 qsort(qdevs, num, sizeof(qdev), compare_reg);
829 /* Hack alert. Give the devices to libfdt in reverse order, we happen
830 * to know that will mean they are in forward order in the tree. */
831 for (i = num - 1; i >= 0; i--) {
832 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
834 ret = vio_make_devnode(dev, fdt);
836 if (ret < 0) {
837 goto out;
841 ret = 0;
842 out:
843 free(qdevs);
845 return ret;
848 int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus)
850 VIOsPAPRDevice *dev;
851 char *name, *path;
852 int ret, offset;
854 dev = spapr_vty_get_default(bus);
855 if (!dev)
856 return 0;
858 offset = fdt_path_offset(fdt, "/chosen");
859 if (offset < 0) {
860 return offset;
863 name = vio_format_dev_name(dev);
864 if (!name) {
865 return -ENOMEM;
868 if (asprintf(&path, "/vdevice/%s", name) < 0) {
869 path = NULL;
870 ret = -ENOMEM;
871 goto out;
874 ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path);
875 out:
876 free(name);
877 free(path);
879 return ret;
881 #endif /* CONFIG_FDT */