1 #include "kvm/8250-serial.h"
3 #include "kvm/read-write.h"
4 #include "kvm/ioport.h"
10 #include <linux/types.h>
11 #include <linux/serial_reg.h>
15 struct serial8250_device
{
16 pthread_mutex_t mutex
;
21 u8 rbr
; /* receive buffer */
34 #define SERIAL_REGS_SETTING \
35 .iir = UART_IIR_NO_INT, \
36 .lsr = UART_LSR_TEMT | UART_LSR_THRE, \
37 .msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS, \
40 static struct serial8250_device devices
[] = {
43 .mutex
= PTHREAD_MUTEX_INITIALIZER
,
52 .mutex
= PTHREAD_MUTEX_INITIALIZER
,
61 .mutex
= PTHREAD_MUTEX_INITIALIZER
,
70 .mutex
= PTHREAD_MUTEX_INITIALIZER
,
79 #define SYSRQ_PENDING_NONE 0
80 #define SYSRQ_PENDING_BREAK 1
81 #define SYSRQ_PENDING_CMD 2
83 static int sysrq_pending
;
85 static void serial8250__sysrq(struct kvm
*kvm
, struct serial8250_device
*dev
)
87 switch (sysrq_pending
) {
88 case SYSRQ_PENDING_BREAK
:
89 dev
->lsr
|= UART_LSR_DR
| UART_LSR_BI
;
91 sysrq_pending
= SYSRQ_PENDING_CMD
;
93 case SYSRQ_PENDING_CMD
:
95 dev
->lsr
|= UART_LSR_DR
;
97 sysrq_pending
= SYSRQ_PENDING_NONE
;
102 static void serial8250__receive(struct kvm
*kvm
, struct serial8250_device
*dev
)
106 if (dev
->lsr
& UART_LSR_DR
)
110 serial8250__sysrq(kvm
, dev
);
114 if (!term_readable(CONSOLE_8250
))
117 c
= term_getc(CONSOLE_8250
);
123 dev
->lsr
|= UART_LSR_DR
;
127 * Interrupts are injected for ttyS0 only.
129 void serial8250__inject_interrupt(struct kvm
*kvm
)
131 struct serial8250_device
*dev
= &devices
[0];
133 mutex_lock(&dev
->mutex
);
135 serial8250__receive(kvm
, dev
);
137 if (dev
->ier
& UART_IER_RDI
&& dev
->lsr
& UART_LSR_DR
)
138 dev
->iir
= UART_IIR_RDI
;
139 else if (dev
->ier
& UART_IER_THRI
)
140 dev
->iir
= UART_IIR_THRI
;
142 dev
->iir
= UART_IIR_NO_INT
;
144 if (dev
->iir
!= UART_IIR_NO_INT
) {
145 kvm__irq_line(kvm
, dev
->irq
, 0);
146 kvm__irq_line(kvm
, dev
->irq
, 1);
149 mutex_unlock(&dev
->mutex
);
152 void serial8250__inject_sysrq(struct kvm
*kvm
)
154 sysrq_pending
= SYSRQ_PENDING_BREAK
;
157 static struct serial8250_device
*find_device(u16 port
)
161 for (i
= 0; i
< ARRAY_SIZE(devices
); i
++) {
162 struct serial8250_device
*dev
= &devices
[i
];
164 if (dev
->iobase
== (port
& ~0x7))
170 static bool serial8250_out(struct ioport
*ioport
, struct kvm
*kvm
, u16 port
, void *data
, int size
)
172 struct serial8250_device
*dev
;
176 dev
= find_device(port
);
180 mutex_lock(&dev
->mutex
);
182 offset
= port
- dev
->iobase
;
184 if (dev
->lcr
& UART_LCR_DLAB
) {
187 dev
->dll
= ioport__read8(data
);
190 dev
->dlm
= ioport__read8(data
);
193 dev
->fcr
= ioport__read8(data
);
196 dev
->lcr
= ioport__read8(data
);
199 dev
->mcr
= ioport__read8(data
);
208 dev
->scr
= ioport__read8(data
);
219 if (!(dev
->mcr
& UART_MCR_LOOP
))
220 term_putc(CONSOLE_8250
, addr
, size
);
222 dev
->iir
= UART_IIR_NO_INT
;
226 dev
->fcr
= ioport__read8(data
);
229 dev
->ier
= ioport__read8(data
) & 0x3f;
232 dev
->lcr
= ioport__read8(data
);
235 dev
->mcr
= ioport__read8(data
);
244 dev
->scr
= ioport__read8(data
);
253 mutex_unlock(&dev
->mutex
);
258 static bool serial8250_in(struct ioport
*ioport
, struct kvm
*kvm
, u16 port
, void *data
, int size
)
260 struct serial8250_device
*dev
;
264 dev
= find_device(port
);
268 mutex_lock(&dev
->mutex
);
270 offset
= port
- dev
->iobase
;
272 if (dev
->lcr
& UART_LCR_DLAB
) {
275 ioport__write8(data
, dev
->dll
);
279 ioport__write8(data
, dev
->dlm
);
288 ioport__write8(data
, dev
->rbr
);
289 dev
->lsr
&= ~UART_LSR_DR
;
290 dev
->iir
= UART_IIR_NO_INT
;
294 ioport__write8(data
, dev
->ier
);
306 if (dev
->fcr
& UART_FCR_ENABLE_FIFO
)
309 ioport__write8(data
, iir
);
313 ioport__write8(data
, dev
->lcr
);
316 ioport__write8(data
, dev
->mcr
);
319 ioport__write8(data
, dev
->lsr
);
320 dev
->lsr
&= ~(UART_LSR_OE
|UART_LSR_PE
|UART_LSR_FE
|UART_LSR_BI
);
323 ioport__write8(data
, dev
->msr
);
326 ioport__write8(data
, dev
->scr
);
333 mutex_unlock(&dev
->mutex
);
338 static struct ioport_operations serial8250_ops
= {
339 .io_in
= serial8250_in
,
340 .io_out
= serial8250_out
,
343 static void serial8250__device_init(struct kvm
*kvm
, struct serial8250_device
*dev
)
345 ioport__register(dev
->iobase
, &serial8250_ops
, 8, NULL
);
346 kvm__irq_line(kvm
, dev
->irq
, 0);
349 void serial8250__init(struct kvm
*kvm
)
353 for (i
= 0; i
< ARRAY_SIZE(devices
); i
++) {
354 struct serial8250_device
*dev
= &devices
[i
];
356 serial8250__device_init(kvm
, dev
);