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>
16 * This fakes a U6_16550A. The fifo len needs to be 64 as the kernel
17 * expects that for autodetection.
20 #define FIFO_MASK (FIFO_LEN - 1)
22 #define UART_IIR_TYPE_BITS 0xc0
24 struct serial8250_device {
25 pthread_mutex_t mutex;
49 #define SERIAL_REGS_SETTING \
50 .iir = UART_IIR_NO_INT, \
51 .lsr = UART_LSR_TEMT | UART_LSR_THRE, \
52 .msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS, \
55 static struct serial8250_device devices[] = {
58 .mutex = PTHREAD_MUTEX_INITIALIZER,
68 .mutex = PTHREAD_MUTEX_INITIALIZER,
78 .mutex = PTHREAD_MUTEX_INITIALIZER,
88 .mutex = PTHREAD_MUTEX_INITIALIZER,
98 static void serial8250_flush_tx(struct kvm *kvm, struct serial8250_device *dev)
100 dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
103 if (kvm->cfg.active_console == CONSOLE_8250)
104 term_putc(dev->txbuf, dev->txcnt, dev->id);
109 static void serial8250_update_irq(struct kvm *kvm, struct serial8250_device *dev)
113 /* Handle clear rx */
114 if (dev->lcr & UART_FCR_CLEAR_RCVR) {
115 dev->lcr &= ~UART_FCR_CLEAR_RCVR;
116 dev->rxcnt = dev->rxdone = 0;
117 dev->lsr &= ~UART_LSR_DR;
120 /* Handle clear tx */
121 if (dev->lcr & UART_FCR_CLEAR_XMIT) {
122 dev->lcr &= ~UART_FCR_CLEAR_XMIT;
124 dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
127 /* Data ready and rcv interrupt enabled ? */
128 if ((dev->ier & UART_IER_RDI) && (dev->lsr & UART_LSR_DR))
131 /* Transmitter empty and interrupt enabled ? */
132 if ((dev->ier & UART_IER_THRI) && (dev->lsr & UART_LSR_TEMT))
133 iir |= UART_IIR_THRI;
135 /* Now update the irq line, if necessary */
137 dev->iir = UART_IIR_NO_INT;
139 kvm__irq_line(kvm, dev->irq, 0);
143 kvm__irq_line(kvm, dev->irq, 1);
145 dev->irq_state = iir;
148 * If the kernel disabled the tx interrupt, we know that there
149 * is nothing more to transmit, so we can reset our tx logic
152 if (!(dev->ier & UART_IER_THRI))
153 serial8250_flush_tx(kvm, dev);
156 #define SYSRQ_PENDING_NONE 0
158 static int sysrq_pending;
160 static void serial8250__sysrq(struct kvm *kvm, struct serial8250_device *dev)
162 dev->lsr |= UART_LSR_DR | UART_LSR_BI;
163 dev->rxbuf[dev->rxcnt++] = sysrq_pending;
164 sysrq_pending = SYSRQ_PENDING_NONE;
167 static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,
173 * If the guest transmitted a full fifo, we clear the
174 * TEMT/THRE bits to let the kernel escape from the 8250
175 * interrupt handler. We come here only once a ms, so that
176 * should give the kernel the desired pause. That also flushes
177 * the tx fifo to the terminal.
179 serial8250_flush_tx(kvm, dev);
181 if (dev->mcr & UART_MCR_LOOP)
184 if ((dev->lsr & UART_LSR_DR) || dev->rxcnt)
187 if (handle_sysrq && sysrq_pending) {
188 serial8250__sysrq(kvm, dev);
192 if (kvm->cfg.active_console != CONSOLE_8250)
195 while (term_readable(dev->id) &&
196 dev->rxcnt < FIFO_LEN) {
198 c = term_getc(kvm, dev->id);
202 dev->rxbuf[dev->rxcnt++] = c;
203 dev->lsr |= UART_LSR_DR;
207 void serial8250__update_consoles(struct kvm *kvm)
211 for (i = 0; i < ARRAY_SIZE(devices); i++) {
212 struct serial8250_device *dev = &devices[i];
214 mutex_lock(&dev->mutex);
216 /* Restrict sysrq injection to the first port */
217 serial8250__receive(kvm, dev, i == 0);
219 serial8250_update_irq(kvm, dev);
221 mutex_unlock(&dev->mutex);
225 void serial8250__inject_sysrq(struct kvm *kvm, char sysrq)
227 sysrq_pending = sysrq;
230 static struct serial8250_device *find_device(u16 port)
234 for (i = 0; i < ARRAY_SIZE(devices); i++) {
235 struct serial8250_device *dev = &devices[i];
237 if (dev->iobase == (port & ~0x7))
243 static bool serial8250_out(struct ioport *ioport, struct kvm *kvm, u16 port,
244 void *data, int size)
246 struct serial8250_device *dev;
251 dev = find_device(port);
255 mutex_lock(&dev->mutex);
257 offset = port - dev->iobase;
261 if (dev->lcr & UART_LCR_DLAB) {
262 dev->dll = ioport__read8(data);
267 if (dev->mcr & UART_MCR_LOOP) {
268 if (dev->rxcnt < FIFO_LEN) {
269 dev->rxbuf[dev->rxcnt++] = *addr;
270 dev->lsr |= UART_LSR_DR;
275 if (dev->txcnt < FIFO_LEN) {
276 dev->txbuf[dev->txcnt++] = *addr;
277 dev->lsr &= ~UART_LSR_TEMT;
278 if (dev->txcnt == FIFO_LEN / 2)
279 dev->lsr &= ~UART_LSR_THRE;
281 /* Should never happpen */
282 dev->lsr &= ~(UART_LSR_TEMT | UART_LSR_THRE);
286 if (!(dev->lcr & UART_LCR_DLAB))
287 dev->ier = ioport__read8(data) & 0x0f;
289 dev->dlm = ioport__read8(data);
292 dev->fcr = ioport__read8(data);
295 dev->lcr = ioport__read8(data);
298 dev->mcr = ioport__read8(data);
307 dev->scr = ioport__read8(data);
314 serial8250_update_irq(kvm, dev);
316 mutex_unlock(&dev->mutex);
321 static void serial8250_rx(struct serial8250_device *dev, void *data)
323 if (dev->rxdone == dev->rxcnt)
327 if (dev->lsr & UART_LSR_BI) {
328 dev->lsr &= ~UART_LSR_BI;
329 ioport__write8(data, 0);
333 ioport__write8(data, dev->rxbuf[dev->rxdone++]);
334 if (dev->rxcnt == dev->rxdone) {
335 dev->lsr &= ~UART_LSR_DR;
336 dev->rxcnt = dev->rxdone = 0;
340 static bool serial8250_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
342 struct serial8250_device *dev;
346 dev = find_device(port);
350 mutex_lock(&dev->mutex);
352 offset = port - dev->iobase;
356 if (dev->lcr & UART_LCR_DLAB)
357 ioport__write8(data, dev->dll);
359 serial8250_rx(dev, data);
362 if (dev->lcr & UART_LCR_DLAB)
363 ioport__write8(data, dev->dlm);
365 ioport__write8(data, dev->ier);
368 ioport__write8(data, dev->iir | UART_IIR_TYPE_BITS);
371 ioport__write8(data, dev->lcr);
374 ioport__write8(data, dev->mcr);
377 ioport__write8(data, dev->lsr);
380 ioport__write8(data, dev->msr);
383 ioport__write8(data, dev->scr);
390 serial8250_update_irq(kvm, dev);
392 mutex_unlock(&dev->mutex);
397 static struct ioport_operations serial8250_ops = {
398 .io_in = serial8250_in,
399 .io_out = serial8250_out,
402 static int serial8250__device_init(struct kvm *kvm, struct serial8250_device *dev)
406 r = ioport__register(kvm, dev->iobase, &serial8250_ops, 8, NULL);
407 kvm__irq_line(kvm, dev->irq, 0);
412 int serial8250__init(struct kvm *kvm)
417 for (i = 0; i < ARRAY_SIZE(devices); i++) {
418 struct serial8250_device *dev = &devices[i];
420 r = serial8250__device_init(kvm, dev);
427 for (j = 0; j <= i; j++) {
428 struct serial8250_device *dev = &devices[j];
430 ioport__unregister(kvm, dev->iobase);
435 dev_init(serial8250__init);
437 int serial8250__exit(struct kvm *kvm)
442 for (i = 0; i < ARRAY_SIZE(devices); i++) {
443 struct serial8250_device *dev = &devices[i];
445 r = ioport__unregister(kvm, dev->iobase);
452 dev_exit(serial8250__exit);