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/>.
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(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
);
207 hcall_dprintf("spapr_vio_put_tce on non-existent LIOBN "
208 TARGET_FMT_lx
"\n", liobn
);
212 ioba
&= ~(SPAPR_VIO_TCE_PAGE_SIZE
- 1);
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
);
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
);
225 rtce
= dev
->rtce_table
+ (ioba
>> SPAPR_VIO_TCE_PAGE_SHIFT
);
231 int spapr_vio_check_tces(VIOsPAPRDevice
*dev
, target_ulong ioba
,
232 target_ulong len
, enum VIOsPAPR_TCEAccess access
)
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
) {
242 fprintf(stderr
, "FAIL on %d\n", i
);
251 int spapr_tce_dma_write(VIOsPAPRDevice
*dev
, uint64_t taddr
, const void *buf
,
255 fprintf(stderr
, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
256 (unsigned long long)taddr
, size
);
259 /* Check for bypass */
260 if (dev
->flags
& VIO_PAPR_FLAG_DMA_BYPASS
) {
261 cpu_physical_memory_write(taddr
, buf
, size
);
270 /* Check if we are in bound */
271 if (taddr
>= dev
->rtce_window_size
) {
273 fprintf(stderr
, "spapr_tce_dma_write out of bounds\n");
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);
288 txaddr
= (tce
& ~SPAPR_VIO_TCE_PAGE_MASK
) |
289 (taddr
& SPAPR_VIO_TCE_PAGE_MASK
);
292 fprintf(stderr
, " -> write to txaddr=0x%llx, size=0x%x\n",
293 (unsigned long long)txaddr
, lsize
);
297 cpu_physical_memory_write(txaddr
, buf
, lsize
);
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
];
312 fprintf(stderr
, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
313 (unsigned long long)taddr
, size
);
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
)
328 spapr_tce_dma_write(dev
, taddr
, &val
, sizeof(val
));
332 void stw_tce(VIOsPAPRDevice
*dev
, uint64_t taddr
, uint32_t val
)
335 spapr_tce_dma_write(dev
, taddr
, &val
, sizeof(val
));
338 void stq_tce(VIOsPAPRDevice
*dev
, uint64_t taddr
, uint64_t val
)
341 spapr_tce_dma_write(dev
, taddr
, &val
, sizeof(val
));
344 int spapr_tce_dma_read(VIOsPAPRDevice
*dev
, uint64_t taddr
, void *buf
,
348 fprintf(stderr
, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
349 (unsigned long long)taddr
, size
);
352 /* Check for bypass */
353 if (dev
->flags
& VIO_PAPR_FLAG_DMA_BYPASS
) {
354 cpu_physical_memory_read(taddr
, buf
, size
);
363 /* Check if we are in bound */
364 if (taddr
>= dev
->rtce_window_size
) {
366 fprintf(stderr
, "spapr_tce_dma_read out of bounds\n");
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);
381 txaddr
= (tce
& ~SPAPR_VIO_TCE_PAGE_MASK
) |
382 (taddr
& SPAPR_VIO_TCE_PAGE_MASK
);
385 fprintf(stderr
, " -> write to txaddr=0x%llx, size=0x%x\n",
386 (unsigned long long)txaddr
, lsize
);
389 cpu_physical_memory_read(txaddr
, buf
, lsize
);
397 uint64_t ldq_tce(VIOsPAPRDevice
*dev
, uint64_t taddr
)
401 spapr_tce_dma_read(dev
, taddr
, &val
, sizeof(val
));
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
);
417 hcall_dprintf("h_reg_crq on non-existent unit 0x"
418 TARGET_FMT_lx
"\n", reg
);
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
);
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
);
436 /* Check if device supports CRQs */
437 if (!dev
->crq
.SendFunc
) {
442 /* Already a queue ? */
443 if (dev
->crq
.qsize
) {
446 dev
->crq
.qladdr
= queue_addr
;
447 dev
->crq
.qsize
= queue_len
;
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
);
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
);
463 hcall_dprintf("h_free_crq on non-existent unit 0x"
464 TARGET_FMT_lx
"\n", reg
);
472 dprintf("CRQ for dev 0x" TARGET_FMT_lx
" freed\n", reg
);
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];
487 hcall_dprintf("h_send_crq on non-existent unit 0x"
488 TARGET_FMT_lx
"\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(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
);
508 hcall_dprintf("h_enable_crq on non-existent unit 0x"
509 TARGET_FMT_lx
"\n", reg
);
516 /* Returns negative error, 0 success, or positive: queue full */
517 int spapr_vio_send_crq(VIOsPAPRDevice
*dev
, uint8_t *crq
)
522 if (!dev
->crq
.qsize
) {
523 fprintf(stderr
, "spapr_vio_send_creq on uninitialized queue\n");
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);
536 rc
= spapr_tce_dma_write(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
+ 8,
544 rc
= spapr_tce_dma_write(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
, crq
, 8);
549 dev
->crq
.qnext
= (dev
->crq
.qnext
+ 16) % dev
->crq
.qsize
;
551 if (dev
->signal_state
& 1) {
552 qemu_irq_pulse(dev
->qirq
);
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
);
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
;
581 uint32_t unit
, enable
;
584 rtas_st(rets
, 0, -3);
587 unit
= rtas_ld(args
, 0);
588 enable
= rtas_ld(args
, 1);
589 dev
= spapr_vio_find_by_reg(bus
, unit
);
591 rtas_st(rets
, 0, -3);
595 dev
->flags
|= VIO_PAPR_FLAG_DMA_BYPASS
;
597 dev
->flags
&= ~VIO_PAPR_FLAG_DMA_BYPASS
;
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
;
609 VIOsPAPRDevice
*dev
= NULL
;
612 rtas_st(rets
, 0, -3);
616 QTAILQ_FOREACH(qdev
, &bus
->bus
.children
, sibling
) {
617 dev
= (VIOsPAPRDevice
*)qdev
;
618 spapr_vio_quiesce_one(dev
);
624 static int spapr_vio_check_reg(VIOsPAPRDevice
*sdev
)
626 VIOsPAPRDevice
*other_sdev
;
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
)),
652 static int spapr_vio_busdev_init(DeviceState
*qdev
)
654 VIOsPAPRDevice
*dev
= (VIOsPAPRDevice
*)qdev
;
655 VIOsPAPRDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
659 ret
= spapr_vio_check_reg(dev
);
664 /* Don't overwrite ids assigned on the command line */
666 id
= vio_format_dev_name(dev
);
673 dev
->qirq
= spapr_allocate_irq(dev
->vio_irq_num
, &dev
->vio_irq_num
);
680 return pc
->init(dev
);
683 static target_ulong
h_vio_signal(CPUState
*env
, sPAPREnvironment
*spapr
,
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
;
696 pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
698 if (mode
& ~pc
->signal_mask
) {
702 dev
->signal_state
= mode
;
707 VIOsPAPRBus
*spapr_vio_bus_init(void)
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
);
723 spapr_register_hypercall(H_VIO_SIGNAL
, h_vio_signal
);
726 spapr_register_hypercall(H_PUT_TCE
, h_put_tce
);
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
);
735 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass
);
736 spapr_rtas_register("quiesce", rtas_quiesce
);
741 /* Represents sPAPR hcall VIO devices */
743 static int spapr_vio_bridge_init(SysBusDevice
*dev
)
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
;
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
),
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
)
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
) {
800 if (dev1
->reg
== dev2
->reg
) {
804 /* dev1->reg > dev2->reg */
808 int spapr_populate_vdevice(VIOsPAPRBus
*bus
, void *fdt
)
810 DeviceState
*qdev
, **qdevs
;
813 /* Count qdevs on the bus list */
815 QTAILQ_FOREACH(qdev
, &bus
->bus
.children
, sibling
) {
819 /* Copy out into an array of pointers */
820 qdevs
= g_malloc(sizeof(qdev
) * num
);
822 QTAILQ_FOREACH(qdev
, &bus
->bus
.children
, sibling
) {
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
);
848 int spapr_populate_chosen_stdout(void *fdt
, VIOsPAPRBus
*bus
)
854 dev
= spapr_vty_get_default(bus
);
858 offset
= fdt_path_offset(fdt
, "/chosen");
863 name
= vio_format_dev_name(dev
);
868 if (asprintf(&path
, "/vdevice/%s", name
) < 0) {
874 ret
= fdt_setprop_string(fdt
, offset
, "linux,stdout-path", path
);
881 #endif /* CONFIG_FDT */