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/>.
23 #include "sysemu/sysemu.h"
25 #include "monitor/monitor.h"
28 #include "hw/sysbus.h"
29 #include "sysemu/kvm.h"
30 #include "sysemu/device_tree.h"
34 #include "hw/spapr_vio.h"
39 #endif /* CONFIG_FDT */
41 /* #define DEBUG_SPAPR */
44 #define dprintf(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #define dprintf(fmt, ...) \
51 static Property spapr_vio_props
[] = {
52 DEFINE_PROP_UINT32("irq", VIOsPAPRDevice
, irq
, 0), \
53 DEFINE_PROP_END_OF_LIST(),
56 static const TypeInfo spapr_vio_bus_info
= {
57 .name
= TYPE_SPAPR_VIO_BUS
,
59 .instance_size
= sizeof(VIOsPAPRBus
),
62 VIOsPAPRDevice
*spapr_vio_find_by_reg(VIOsPAPRBus
*bus
, uint32_t reg
)
65 VIOsPAPRDevice
*dev
= NULL
;
67 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
68 dev
= (VIOsPAPRDevice
*)kid
->child
;
69 if (dev
->reg
== reg
) {
77 static char *vio_format_dev_name(VIOsPAPRDevice
*dev
)
79 VIOsPAPRDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
82 /* Device tree style name device@reg */
83 name
= g_strdup_printf("%s@%x", pc
->dt_name
, dev
->reg
);
89 static int vio_make_devnode(VIOsPAPRDevice
*dev
,
92 VIOsPAPRDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
93 int vdevice_off
, node_off
, ret
;
96 vdevice_off
= fdt_path_offset(fdt
, "/vdevice");
97 if (vdevice_off
< 0) {
101 dt_name
= vio_format_dev_name(dev
);
102 node_off
= fdt_add_subnode(fdt
, vdevice_off
, dt_name
);
108 ret
= fdt_setprop_cell(fdt
, node_off
, "reg", dev
->reg
);
114 ret
= fdt_setprop_string(fdt
, node_off
, "device_type",
121 if (pc
->dt_compatible
) {
122 ret
= fdt_setprop_string(fdt
, node_off
, "compatible",
130 uint32_t ints_prop
[] = {cpu_to_be32(dev
->irq
), 0};
132 ret
= fdt_setprop(fdt
, node_off
, "interrupts", ints_prop
,
139 ret
= spapr_tcet_dma_dt(fdt
, node_off
, "ibm,my-dma-window", dev
->dma
);
145 ret
= (pc
->devnode
)(dev
, fdt
, node_off
);
153 #endif /* CONFIG_FDT */
158 static target_ulong
h_reg_crq(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
159 target_ulong opcode
, target_ulong
*args
)
161 target_ulong reg
= args
[0];
162 target_ulong queue_addr
= args
[1];
163 target_ulong queue_len
= args
[2];
164 VIOsPAPRDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
167 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
171 /* We can't grok a queue size bigger than 256M for now */
172 if (queue_len
< 0x1000 || queue_len
> 0x10000000) {
173 hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
178 /* Check queue alignment */
179 if (queue_addr
& 0xfff) {
180 hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx
")\n", queue_addr
);
184 /* Check if device supports CRQs */
185 if (!dev
->crq
.SendFunc
) {
186 hcall_dprintf("Device does not support CRQ\n");
190 /* Already a queue ? */
191 if (dev
->crq
.qsize
) {
192 hcall_dprintf("CRQ already registered\n");
195 dev
->crq
.qladdr
= queue_addr
;
196 dev
->crq
.qsize
= queue_len
;
199 dprintf("CRQ for dev 0x" TARGET_FMT_lx
" registered at 0x"
200 TARGET_FMT_lx
"/0x" TARGET_FMT_lx
"\n",
201 reg
, queue_addr
, queue_len
);
205 static target_ulong
free_crq(VIOsPAPRDevice
*dev
)
211 dprintf("CRQ for dev 0x%" PRIx32
" freed\n", dev
->reg
);
216 static target_ulong
h_free_crq(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
217 target_ulong opcode
, target_ulong
*args
)
219 target_ulong reg
= args
[0];
220 VIOsPAPRDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
223 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
227 return free_crq(dev
);
230 static target_ulong
h_send_crq(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
231 target_ulong opcode
, target_ulong
*args
)
233 target_ulong reg
= args
[0];
234 target_ulong msg_hi
= args
[1];
235 target_ulong msg_lo
= args
[2];
236 VIOsPAPRDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
237 uint64_t crq_mangle
[2];
240 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
243 crq_mangle
[0] = cpu_to_be64(msg_hi
);
244 crq_mangle
[1] = cpu_to_be64(msg_lo
);
246 if (dev
->crq
.SendFunc
) {
247 return dev
->crq
.SendFunc(dev
, (uint8_t *)crq_mangle
);
253 static target_ulong
h_enable_crq(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
254 target_ulong opcode
, target_ulong
*args
)
256 target_ulong reg
= args
[0];
257 VIOsPAPRDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
260 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
267 /* Returns negative error, 0 success, or positive: queue full */
268 int spapr_vio_send_crq(VIOsPAPRDevice
*dev
, uint8_t *crq
)
273 if (!dev
->crq
.qsize
) {
274 fprintf(stderr
, "spapr_vio_send_creq on uninitialized queue\n");
278 /* Maybe do a fast path for KVM just writing to the pages */
279 rc
= spapr_vio_dma_read(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
, &byte
, 1);
287 rc
= spapr_vio_dma_write(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
+ 8,
295 rc
= spapr_vio_dma_write(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
, crq
, 8);
300 dev
->crq
.qnext
= (dev
->crq
.qnext
+ 16) % dev
->crq
.qsize
;
302 if (dev
->signal_state
& 1) {
303 qemu_irq_pulse(spapr_vio_qirq(dev
));
309 /* "quiesce" handling */
311 static void spapr_vio_quiesce_one(VIOsPAPRDevice
*dev
)
314 spapr_tce_reset(dev
->dma
);
319 static void rtas_set_tce_bypass(sPAPREnvironment
*spapr
, uint32_t token
,
320 uint32_t nargs
, target_ulong args
,
321 uint32_t nret
, target_ulong rets
)
323 VIOsPAPRBus
*bus
= spapr
->vio_bus
;
325 uint32_t unit
, enable
;
328 rtas_st(rets
, 0, -3);
331 unit
= rtas_ld(args
, 0);
332 enable
= rtas_ld(args
, 1);
333 dev
= spapr_vio_find_by_reg(bus
, unit
);
335 rtas_st(rets
, 0, -3);
340 rtas_st(rets
, 0, -3);
344 spapr_tce_set_bypass(dev
->dma
, !!enable
);
349 static void rtas_quiesce(sPAPREnvironment
*spapr
, uint32_t token
,
350 uint32_t nargs
, target_ulong args
,
351 uint32_t nret
, target_ulong rets
)
353 VIOsPAPRBus
*bus
= spapr
->vio_bus
;
355 VIOsPAPRDevice
*dev
= NULL
;
358 rtas_st(rets
, 0, -3);
362 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
363 dev
= (VIOsPAPRDevice
*)kid
->child
;
364 spapr_vio_quiesce_one(dev
);
370 static VIOsPAPRDevice
*reg_conflict(VIOsPAPRDevice
*dev
)
372 VIOsPAPRBus
*bus
= DO_UPCAST(VIOsPAPRBus
, bus
, dev
->qdev
.parent_bus
);
374 VIOsPAPRDevice
*other
;
377 * Check for a device other than the given one which is already
378 * using the requested address. We have to open code this because
379 * the given dev might already be in the list.
381 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
382 other
= DO_UPCAST(VIOsPAPRDevice
, qdev
, kid
->child
);
384 if (other
!= dev
&& other
->reg
== dev
->reg
) {
392 static void spapr_vio_busdev_reset(DeviceState
*qdev
)
394 VIOsPAPRDevice
*dev
= DO_UPCAST(VIOsPAPRDevice
, qdev
, qdev
);
395 VIOsPAPRDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
397 /* Shut down the request queue and TCEs if necessary */
398 spapr_vio_quiesce_one(dev
);
400 dev
->signal_state
= 0;
407 static int spapr_vio_busdev_init(DeviceState
*qdev
)
409 VIOsPAPRDevice
*dev
= (VIOsPAPRDevice
*)qdev
;
410 VIOsPAPRDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
413 if (dev
->reg
!= -1) {
415 * Explicitly assigned address, just verify that no-one else
416 * is using it. other mechanism). We have to open code this
417 * rather than using spapr_vio_find_by_reg() because sdev
418 * itself is already in the list.
420 VIOsPAPRDevice
*other
= reg_conflict(dev
);
423 fprintf(stderr
, "vio: %s and %s devices conflict at address %#x\n",
424 object_get_typename(OBJECT(qdev
)),
425 object_get_typename(OBJECT(&other
->qdev
)),
430 /* Need to assign an address */
431 VIOsPAPRBus
*bus
= DO_UPCAST(VIOsPAPRBus
, bus
, dev
->qdev
.parent_bus
);
434 dev
->reg
= bus
->next_reg
++;
435 } while (reg_conflict(dev
));
438 /* Don't overwrite ids assigned on the command line */
440 id
= vio_format_dev_name(dev
);
444 dev
->irq
= spapr_allocate_msi(dev
->irq
);
449 if (pc
->rtce_window_size
) {
450 uint32_t liobn
= SPAPR_VIO_BASE_LIOBN
| dev
->reg
;
451 dev
->dma
= spapr_tce_new_dma_context(liobn
, pc
->rtce_window_size
);
454 return pc
->init(dev
);
457 static target_ulong
h_vio_signal(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
461 target_ulong reg
= args
[0];
462 target_ulong mode
= args
[1];
463 VIOsPAPRDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
464 VIOsPAPRDeviceClass
*pc
;
470 pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
472 if (mode
& ~pc
->signal_mask
) {
476 dev
->signal_state
= mode
;
481 VIOsPAPRBus
*spapr_vio_bus_init(void)
487 /* Create bridge device */
488 dev
= qdev_create(NULL
, "spapr-vio-bridge");
489 qdev_init_nofail(dev
);
491 /* Create bus on bridge device */
493 qbus
= qbus_create(TYPE_SPAPR_VIO_BUS
, dev
, "spapr-vio");
494 bus
= DO_UPCAST(VIOsPAPRBus
, bus
, qbus
);
495 bus
->next_reg
= 0x71000000;
498 spapr_register_hypercall(H_VIO_SIGNAL
, h_vio_signal
);
501 spapr_register_hypercall(H_REG_CRQ
, h_reg_crq
);
502 spapr_register_hypercall(H_FREE_CRQ
, h_free_crq
);
503 spapr_register_hypercall(H_SEND_CRQ
, h_send_crq
);
504 spapr_register_hypercall(H_ENABLE_CRQ
, h_enable_crq
);
507 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass
);
508 spapr_rtas_register("quiesce", rtas_quiesce
);
513 /* Represents sPAPR hcall VIO devices */
515 static int spapr_vio_bridge_init(SysBusDevice
*dev
)
521 static void spapr_vio_bridge_class_init(ObjectClass
*klass
, void *data
)
523 DeviceClass
*dc
= DEVICE_CLASS(klass
);
524 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
526 k
->init
= spapr_vio_bridge_init
;
530 static const TypeInfo spapr_vio_bridge_info
= {
531 .name
= "spapr-vio-bridge",
532 .parent
= TYPE_SYS_BUS_DEVICE
,
533 .instance_size
= sizeof(SysBusDevice
),
534 .class_init
= spapr_vio_bridge_class_init
,
537 static void vio_spapr_device_class_init(ObjectClass
*klass
, void *data
)
539 DeviceClass
*k
= DEVICE_CLASS(klass
);
540 k
->init
= spapr_vio_busdev_init
;
541 k
->reset
= spapr_vio_busdev_reset
;
542 k
->bus_type
= TYPE_SPAPR_VIO_BUS
;
543 k
->props
= spapr_vio_props
;
546 static const TypeInfo spapr_vio_type_info
= {
547 .name
= TYPE_VIO_SPAPR_DEVICE
,
548 .parent
= TYPE_DEVICE
,
549 .instance_size
= sizeof(VIOsPAPRDevice
),
551 .class_size
= sizeof(VIOsPAPRDeviceClass
),
552 .class_init
= vio_spapr_device_class_init
,
555 static void spapr_vio_register_types(void)
557 type_register_static(&spapr_vio_bus_info
);
558 type_register_static(&spapr_vio_bridge_info
);
559 type_register_static(&spapr_vio_type_info
);
562 type_init(spapr_vio_register_types
)
565 static int compare_reg(const void *p1
, const void *p2
)
567 VIOsPAPRDevice
const *dev1
, *dev2
;
569 dev1
= (VIOsPAPRDevice
*)*(DeviceState
**)p1
;
570 dev2
= (VIOsPAPRDevice
*)*(DeviceState
**)p2
;
572 if (dev1
->reg
< dev2
->reg
) {
575 if (dev1
->reg
== dev2
->reg
) {
579 /* dev1->reg > dev2->reg */
583 int spapr_populate_vdevice(VIOsPAPRBus
*bus
, void *fdt
)
585 DeviceState
*qdev
, **qdevs
;
589 /* Count qdevs on the bus list */
591 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
595 /* Copy out into an array of pointers */
596 qdevs
= g_malloc(sizeof(qdev
) * num
);
598 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
599 qdevs
[num
++] = kid
->child
;
603 qsort(qdevs
, num
, sizeof(qdev
), compare_reg
);
605 /* Hack alert. Give the devices to libfdt in reverse order, we happen
606 * to know that will mean they are in forward order in the tree. */
607 for (i
= num
- 1; i
>= 0; i
--) {
608 VIOsPAPRDevice
*dev
= (VIOsPAPRDevice
*)(qdevs
[i
]);
610 ret
= vio_make_devnode(dev
, fdt
);
624 int spapr_populate_chosen_stdout(void *fdt
, VIOsPAPRBus
*bus
)
630 dev
= spapr_vty_get_default(bus
);
634 offset
= fdt_path_offset(fdt
, "/chosen");
639 name
= vio_format_dev_name(dev
);
640 path
= g_strdup_printf("/vdevice/%s", name
);
642 ret
= fdt_setprop_string(fdt
, offset
, "linux,stdout-path", path
);
649 #endif /* CONFIG_FDT */