5 * Based on the s390 virtio bus code:
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/>.
28 #include "hw/sysbus.h"
30 #include "device_tree.h"
34 #include "hw/spapr_vio.h"
39 #endif /* CONFIG_FDT */
41 /* #define DEBUG_SPAPR */
42 /* #define DEBUG_TCE */
45 #define dprintf(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
48 #define dprintf(fmt, ...) \
52 static struct BusInfo spapr_vio_bus_info = {
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)
64 VIOsPAPRDevice *dev = NULL;
66 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
67 dev = (VIOsPAPRDevice *)qdev;
68 if (dev->reg == reg) {
76 static char *vio_format_dev_name(VIOsPAPRDevice *dev)
78 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
81 /* Device tree style name device@reg */
82 if (asprintf(&name, "%s@%x", pc->dt_name, dev->reg) < 0) {
90 static int vio_make_devnode(VIOsPAPRDevice *dev,
93 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
94 int vdevice_off, node_off, ret;
97 vdevice_off = fdt_path_offset(fdt, "/vdevice");
98 if (vdevice_off < 0) {
102 dt_name = vio_format_dev_name(dev);
107 node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
113 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
119 ret = fdt_setprop_string(fdt, node_off, "device_type",
126 if (pc->dt_compatible) {
127 ret = fdt_setprop_string(fdt, node_off, "compatible",
135 uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0};
137 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
144 if (dev->rtce_window_size) {
145 uint32_t dma_prop[] = {cpu_to_be32(dev->reg),
147 0, cpu_to_be32(dev->rtce_window_size)};
149 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
154 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
159 ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop,
167 ret = (pc->devnode)(dev, fdt, node_off);
175 #endif /* CONFIG_FDT */
181 static void rtce_init(VIOsPAPRDevice *dev)
183 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
184 * sizeof(VIOsPAPR_RTCE);
187 dev->rtce_table = kvmppc_create_spapr_tce(dev->reg,
188 dev->rtce_window_size,
191 if (!dev->rtce_table) {
192 dev->rtce_table = g_malloc0(size);
197 static target_ulong h_put_tce(CPUPPCState *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);
207 hcall_dprintf("LIOBN 0x" TARGET_FMT_lx " does not exist\n", liobn);
211 ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1);
214 fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx
215 " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce);
218 if (ioba >= dev->rtce_window_size) {
219 hcall_dprintf("Out-of-bounds IOBA 0x" TARGET_FMT_lx "\n", ioba);
223 rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT);
229 int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba,
230 target_ulong len, enum VIOsPAPR_TCEAccess access)
234 start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT;
235 end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT;
237 for (i = start; i <= end; i++) {
238 if ((dev->rtce_table[i].tce & access) != access) {
240 fprintf(stderr, "FAIL on %d\n", i);
249 int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf,
253 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
254 (unsigned long long)taddr, size);
257 /* Check for bypass */
258 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
259 cpu_physical_memory_write(taddr, buf, size);
268 /* Check if we are in bound */
269 if (taddr >= dev->rtce_window_size) {
271 fprintf(stderr, "spapr_tce_dma_write out of bounds\n");
275 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
277 /* How much til end of page ? */
278 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
286 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
287 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
290 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
291 (unsigned long long)txaddr, lsize);
295 cpu_physical_memory_write(txaddr, buf, lsize);
303 int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size)
305 /* FIXME: allocating a temp buffer is nasty, but just stepping
306 * through writing zeroes is awkward. This will do for now. */
307 uint8_t zeroes[size];
310 fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
311 (unsigned long long)taddr, size);
314 memset(zeroes, 0, size);
315 return spapr_tce_dma_write(dev, taddr, zeroes, size);
318 void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val)
320 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
323 void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val)
326 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
330 void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val)
333 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
336 void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val)
339 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
342 int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf,
346 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
347 (unsigned long long)taddr, size);
350 /* Check for bypass */
351 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
352 cpu_physical_memory_read(taddr, buf, size);
361 /* Check if we are in bound */
362 if (taddr >= dev->rtce_window_size) {
364 fprintf(stderr, "spapr_tce_dma_read out of bounds\n");
368 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
370 /* How much til end of page ? */
371 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
379 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
380 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
383 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
384 (unsigned long long)txaddr, lsize);
387 cpu_physical_memory_read(txaddr, buf, lsize);
395 uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr)
399 spapr_tce_dma_read(dev, taddr, &val, sizeof(val));
406 static target_ulong h_reg_crq(CPUPPCState *env, sPAPREnvironment *spapr,
407 target_ulong opcode, target_ulong *args)
409 target_ulong reg = args[0];
410 target_ulong queue_addr = args[1];
411 target_ulong queue_len = args[2];
412 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
415 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
419 /* We can't grok a queue size bigger than 256M for now */
420 if (queue_len < 0x1000 || queue_len > 0x10000000) {
421 hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
426 /* Check queue alignment */
427 if (queue_addr & 0xfff) {
428 hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx ")\n", queue_addr);
432 /* Check if device supports CRQs */
433 if (!dev->crq.SendFunc) {
434 hcall_dprintf("Device does not support CRQ\n");
438 /* Already a queue ? */
439 if (dev->crq.qsize) {
440 hcall_dprintf("CRQ already registered\n");
443 dev->crq.qladdr = queue_addr;
444 dev->crq.qsize = queue_len;
447 dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
448 TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
449 reg, queue_addr, queue_len);
453 static target_ulong free_crq(VIOsPAPRDevice *dev)
459 dprintf("CRQ for dev 0x%" PRIx32 " freed\n", dev->reg);
464 static target_ulong h_free_crq(CPUPPCState *env, sPAPREnvironment *spapr,
465 target_ulong opcode, target_ulong *args)
467 target_ulong reg = args[0];
468 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
471 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
475 return free_crq(dev);
478 static target_ulong h_send_crq(CPUPPCState *env, sPAPREnvironment *spapr,
479 target_ulong opcode, target_ulong *args)
481 target_ulong reg = args[0];
482 target_ulong msg_hi = args[1];
483 target_ulong msg_lo = args[2];
484 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
485 uint64_t crq_mangle[2];
488 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
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);
501 static target_ulong h_enable_crq(CPUPPCState *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);
508 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
515 /* Returns negative error, 0 success, or positive: queue full */
516 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
521 if (!dev->crq.qsize) {
522 fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
526 /* Maybe do a fast path for KVM just writing to the pages */
527 rc = spapr_tce_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
535 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
543 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
548 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
550 if (dev->signal_state & 1) {
551 qemu_irq_pulse(dev->qirq);
557 /* "quiesce" handling */
559 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
561 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
563 if (dev->rtce_table) {
564 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
565 * sizeof(VIOsPAPR_RTCE);
566 memset(dev->rtce_table, 0, size);
574 static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token,
575 uint32_t nargs, target_ulong args,
576 uint32_t nret, target_ulong rets)
578 VIOsPAPRBus *bus = spapr->vio_bus;
580 uint32_t unit, enable;
583 rtas_st(rets, 0, -3);
586 unit = rtas_ld(args, 0);
587 enable = rtas_ld(args, 1);
588 dev = spapr_vio_find_by_reg(bus, unit);
590 rtas_st(rets, 0, -3);
594 dev->flags |= VIO_PAPR_FLAG_DMA_BYPASS;
596 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
602 static void rtas_quiesce(sPAPREnvironment *spapr, uint32_t token,
603 uint32_t nargs, target_ulong args,
604 uint32_t nret, target_ulong rets)
606 VIOsPAPRBus *bus = spapr->vio_bus;
608 VIOsPAPRDevice *dev = NULL;
611 rtas_st(rets, 0, -3);
615 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
616 dev = (VIOsPAPRDevice *)qdev;
617 spapr_vio_quiesce_one(dev);
623 static VIOsPAPRDevice *reg_conflict(VIOsPAPRDevice *dev)
625 VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
627 VIOsPAPRDevice *other;
630 * Check for a device other than the given one which is already
631 * using the requested address. We have to open code this because
632 * the given dev might already be in the list.
634 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
635 other = DO_UPCAST(VIOsPAPRDevice, qdev, qdev);
637 if (other != dev && other->reg == dev->reg) {
645 static void spapr_vio_busdev_reset(DeviceState *qdev)
647 VIOsPAPRDevice *dev = DO_UPCAST(VIOsPAPRDevice, qdev, qdev);
648 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
650 if (dev->crq.qsize) {
659 static int spapr_vio_busdev_init(DeviceState *qdev)
661 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
662 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
665 if (dev->reg != -1) {
667 * Explicitly assigned address, just verify that no-one else
668 * is using it. other mechanism). We have to open code this
669 * rather than using spapr_vio_find_by_reg() because sdev
670 * itself is already in the list.
672 VIOsPAPRDevice *other = reg_conflict(dev);
675 fprintf(stderr, "vio: %s and %s devices conflict at address %#x\n",
676 object_get_typename(OBJECT(qdev)),
677 object_get_typename(OBJECT(&other->qdev)),
682 /* Need to assign an address */
683 VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
686 dev->reg = bus->next_reg++;
687 } while (reg_conflict(dev));
690 /* Don't overwrite ids assigned on the command line */
692 id = vio_format_dev_name(dev);
699 dev->qirq = spapr_allocate_msi(dev->vio_irq_num, &dev->vio_irq_num);
706 return pc->init(dev);
709 static target_ulong h_vio_signal(CPUPPCState *env, sPAPREnvironment *spapr,
713 target_ulong reg = args[0];
714 target_ulong mode = args[1];
715 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
716 VIOsPAPRDeviceClass *pc;
722 pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
724 if (mode & ~pc->signal_mask) {
728 dev->signal_state = mode;
733 VIOsPAPRBus *spapr_vio_bus_init(void)
739 /* Create bridge device */
740 dev = qdev_create(NULL, "spapr-vio-bridge");
741 qdev_init_nofail(dev);
743 /* Create bus on bridge device */
745 qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio");
746 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
747 bus->next_reg = 0x1000;
750 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
753 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
756 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
757 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
758 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
759 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
762 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass);
763 spapr_rtas_register("quiesce", rtas_quiesce);
768 /* Represents sPAPR hcall VIO devices */
770 static int spapr_vio_bridge_init(SysBusDevice *dev)
776 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
778 DeviceClass *dc = DEVICE_CLASS(klass);
779 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
781 k->init = spapr_vio_bridge_init;
785 static TypeInfo spapr_vio_bridge_info = {
786 .name = "spapr-vio-bridge",
787 .parent = TYPE_SYS_BUS_DEVICE,
788 .instance_size = sizeof(SysBusDevice),
789 .class_init = spapr_vio_bridge_class_init,
792 static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
794 DeviceClass *k = DEVICE_CLASS(klass);
795 k->init = spapr_vio_busdev_init;
796 k->reset = spapr_vio_busdev_reset;
797 k->bus_info = &spapr_vio_bus_info;
800 static TypeInfo spapr_vio_type_info = {
801 .name = TYPE_VIO_SPAPR_DEVICE,
802 .parent = TYPE_DEVICE,
803 .instance_size = sizeof(VIOsPAPRDevice),
805 .class_size = sizeof(VIOsPAPRDeviceClass),
806 .class_init = vio_spapr_device_class_init,
809 static void spapr_vio_register_types(void)
811 type_register_static(&spapr_vio_bridge_info);
812 type_register_static(&spapr_vio_type_info);
815 type_init(spapr_vio_register_types)
818 static int compare_reg(const void *p1, const void *p2)
820 VIOsPAPRDevice const *dev1, *dev2;
822 dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
823 dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
825 if (dev1->reg < dev2->reg) {
828 if (dev1->reg == dev2->reg) {
832 /* dev1->reg > dev2->reg */
836 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
838 DeviceState *qdev, **qdevs;
841 /* Count qdevs on the bus list */
843 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
847 /* Copy out into an array of pointers */
848 qdevs = g_malloc(sizeof(qdev) * num);
850 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
855 qsort(qdevs, num, sizeof(qdev), compare_reg);
857 /* Hack alert. Give the devices to libfdt in reverse order, we happen
858 * to know that will mean they are in forward order in the tree. */
859 for (i = num - 1; i >= 0; i--) {
860 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
862 ret = vio_make_devnode(dev, fdt);
876 int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus)
882 dev = spapr_vty_get_default(bus);
886 offset = fdt_path_offset(fdt, "/chosen");
891 name = vio_format_dev_name(dev);
896 if (asprintf(&path, "/vdevice/%s", name) < 0) {
902 ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path);
909 #endif /* CONFIG_FDT */