4 #include "hw/spapr_vio.h"
6 #define VTERM_BUFSIZE 16
8 typedef struct VIOsPAPRVTYDevice
{
10 CharDriverState
*chardev
;
12 uint8_t buf
[VTERM_BUFSIZE
];
15 static int vty_can_receive(void *opaque
)
17 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)opaque
;
19 return (dev
->in
- dev
->out
) < VTERM_BUFSIZE
;
22 static void vty_receive(void *opaque
, const uint8_t *buf
, int size
)
24 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)opaque
;
27 if ((dev
->in
== dev
->out
) && size
) {
28 /* toggle line to simulate edge interrupt */
29 qemu_irq_pulse(dev
->sdev
.qirq
);
31 for (i
= 0; i
< size
; i
++) {
32 assert((dev
->in
- dev
->out
) < VTERM_BUFSIZE
);
33 dev
->buf
[dev
->in
++ % VTERM_BUFSIZE
] = buf
[i
];
37 static int vty_getchars(VIOsPAPRDevice
*sdev
, uint8_t *buf
, int max
)
39 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)sdev
;
42 while ((n
< max
) && (dev
->out
!= dev
->in
)) {
43 buf
[n
++] = dev
->buf
[dev
->out
++ % VTERM_BUFSIZE
];
49 void vty_putchars(VIOsPAPRDevice
*sdev
, uint8_t *buf
, int len
)
51 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)sdev
;
53 /* FIXME: should check the qemu_chr_fe_write() return value */
54 qemu_chr_fe_write(dev
->chardev
, buf
, len
);
57 static int spapr_vty_init(VIOsPAPRDevice
*sdev
)
59 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)sdev
;
62 fprintf(stderr
, "spapr-vty: Can't create vty without a chardev!\n");
66 qemu_chr_add_handlers(dev
->chardev
, vty_can_receive
,
67 vty_receive
, NULL
, dev
);
72 /* Forward declaration */
73 static VIOsPAPRDevice
*vty_lookup(sPAPREnvironment
*spapr
, target_ulong reg
);
75 static target_ulong
h_put_term_char(CPUState
*env
, sPAPREnvironment
*spapr
,
76 target_ulong opcode
, target_ulong
*args
)
78 target_ulong reg
= args
[0];
79 target_ulong len
= args
[1];
80 target_ulong char0_7
= args
[2];
81 target_ulong char8_15
= args
[3];
85 sdev
= vty_lookup(spapr
, reg
);
94 *((uint64_t *)buf
) = cpu_to_be64(char0_7
);
95 *((uint64_t *)buf
+ 1) = cpu_to_be64(char8_15
);
97 vty_putchars(sdev
, buf
, len
);
102 static target_ulong
h_get_term_char(CPUState
*env
, sPAPREnvironment
*spapr
,
103 target_ulong opcode
, target_ulong
*args
)
105 target_ulong reg
= args
[0];
106 target_ulong
*len
= args
+ 0;
107 target_ulong
*char0_7
= args
+ 1;
108 target_ulong
*char8_15
= args
+ 2;
109 VIOsPAPRDevice
*sdev
;
112 sdev
= vty_lookup(spapr
, reg
);
117 *len
= vty_getchars(sdev
, buf
, sizeof(buf
));
119 memset(buf
+ *len
, 0, 16 - *len
);
122 *char0_7
= be64_to_cpu(*((uint64_t *)buf
));
123 *char8_15
= be64_to_cpu(*((uint64_t *)buf
+ 1));
128 void spapr_vty_create(VIOsPAPRBus
*bus
, uint32_t reg
, CharDriverState
*chardev
)
132 dev
= qdev_create(&bus
->bus
, "spapr-vty");
133 qdev_prop_set_uint32(dev
, "reg", reg
);
134 qdev_prop_set_chr(dev
, "chardev", chardev
);
135 qdev_init_nofail(dev
);
138 static VIOsPAPRDeviceInfo spapr_vty
= {
139 .init
= spapr_vty_init
,
142 .dt_compatible
= "hvterm1",
143 .qdev
.name
= "spapr-vty",
144 .qdev
.size
= sizeof(VIOsPAPRVTYDevice
),
145 .qdev
.props
= (Property
[]) {
146 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVTYDevice
, sdev
, SPAPR_VTY_BASE_ADDRESS
, 0),
147 DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice
, chardev
),
148 DEFINE_PROP_END_OF_LIST(),
152 VIOsPAPRDevice
*spapr_vty_get_default(VIOsPAPRBus
*bus
)
154 VIOsPAPRDevice
*sdev
, *selected
;
158 * To avoid the console bouncing around we want one VTY to be
159 * the "default". We haven't really got anything to go on, so
160 * arbitrarily choose the one with the lowest reg value.
164 QTAILQ_FOREACH(iter
, &bus
->bus
.children
, sibling
) {
165 /* Only look at VTY devices */
166 if (iter
->info
!= &spapr_vty
.qdev
) {
170 sdev
= DO_UPCAST(VIOsPAPRDevice
, qdev
, iter
);
172 /* First VTY we've found, so it is selected for now */
178 /* Choose VTY with lowest reg value */
179 if (sdev
->reg
< selected
->reg
) {
187 static VIOsPAPRDevice
*vty_lookup(sPAPREnvironment
*spapr
, target_ulong reg
)
189 VIOsPAPRDevice
*sdev
;
191 sdev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
192 if (!sdev
&& reg
== 0) {
193 /* Hack for kernel early debug, which always specifies reg==0.
194 * We search all VIO devices, and grab the vty with the lowest
195 * reg. This attempts to mimic existing PowerVM behaviour
196 * (early debug does work there, despite having no vty with
198 return spapr_vty_get_default(spapr
->vio_bus
);
204 static void spapr_vty_register(void)
206 spapr_vio_bus_register_withprop(&spapr_vty
);
207 spapr_register_hypercall(H_PUT_TERM_CHAR
, h_put_term_char
);
208 spapr_register_hypercall(H_GET_TERM_CHAR
, h_get_term_char
);
210 device_init(spapr_vty_register
);