xref: /kvmtool/hw/serial.c (revision 206c41f433bbbb5763851a2f6df928a66f71193e)
1 #include "kvm/8250-serial.h"
2 
3 #include "kvm/read-write.h"
4 #include "kvm/ioport.h"
5 #include "kvm/mutex.h"
6 #include "kvm/util.h"
7 #include "kvm/term.h"
8 #include "kvm/kvm.h"
9 
10 #include <linux/types.h>
11 #include <linux/serial_reg.h>
12 
13 #include <pthread.h>
14 
15 /*
16  * This fakes a U6_16550A. The fifo len needs to be 64 as the kernel
17  * expects that for autodetection.
18  */
19 #define FIFO_LEN		64
20 #define FIFO_MASK		(FIFO_LEN - 1)
21 
22 #define UART_IIR_TYPE_BITS	0xc0
23 
24 struct serial8250_device {
25 	struct mutex		mutex;
26 	u8			id;
27 
28 	u16			iobase;
29 	u8			irq;
30 	u8			irq_state;
31 	int			txcnt;
32 	int			rxcnt;
33 	int			rxdone;
34 	char			txbuf[FIFO_LEN];
35 	char			rxbuf[FIFO_LEN];
36 
37 	u8			dll;
38 	u8			dlm;
39 	u8			iir;
40 	u8			ier;
41 	u8			fcr;
42 	u8			lcr;
43 	u8			mcr;
44 	u8			lsr;
45 	u8			msr;
46 	u8			scr;
47 };
48 
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, \
53 	.mcr			= UART_MCR_OUT2,
54 
55 static struct serial8250_device devices[] = {
56 	/* ttyS0 */
57 	[0]	= {
58 		.mutex			= MUTEX_INITIALIZER,
59 
60 		.id			= 0,
61 		.iobase			= 0x3f8,
62 		.irq			= 4,
63 
64 		SERIAL_REGS_SETTING
65 	},
66 	/* ttyS1 */
67 	[1]	= {
68 		.mutex			= MUTEX_INITIALIZER,
69 
70 		.id			= 1,
71 		.iobase			= 0x2f8,
72 		.irq			= 3,
73 
74 		SERIAL_REGS_SETTING
75 	},
76 	/* ttyS2 */
77 	[2]	= {
78 		.mutex			= MUTEX_INITIALIZER,
79 
80 		.id			= 2,
81 		.iobase			= 0x3e8,
82 		.irq			= 4,
83 
84 		SERIAL_REGS_SETTING
85 	},
86 	/* ttyS3 */
87 	[3]	= {
88 		.mutex			= MUTEX_INITIALIZER,
89 
90 		.id			= 3,
91 		.iobase			= 0x2e8,
92 		.irq			= 3,
93 
94 		SERIAL_REGS_SETTING
95 	},
96 };
97 
98 static void serial8250_flush_tx(struct kvm *kvm, struct serial8250_device *dev)
99 {
100 	dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
101 
102 	if (dev->txcnt) {
103 		term_putc(dev->txbuf, dev->txcnt, dev->id);
104 		dev->txcnt = 0;
105 	}
106 }
107 
108 static void serial8250_update_irq(struct kvm *kvm, struct serial8250_device *dev)
109 {
110 	u8 iir = 0;
111 
112 	/* Handle clear rx */
113 	if (dev->lcr & UART_FCR_CLEAR_RCVR) {
114 		dev->lcr &= ~UART_FCR_CLEAR_RCVR;
115 		dev->rxcnt = dev->rxdone = 0;
116 		dev->lsr &= ~UART_LSR_DR;
117 	}
118 
119 	/* Handle clear tx */
120 	if (dev->lcr & UART_FCR_CLEAR_XMIT) {
121 		dev->lcr &= ~UART_FCR_CLEAR_XMIT;
122 		dev->txcnt = 0;
123 		dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
124 	}
125 
126 	/* Data ready and rcv interrupt enabled ? */
127 	if ((dev->ier & UART_IER_RDI) && (dev->lsr & UART_LSR_DR))
128 		iir |= UART_IIR_RDI;
129 
130 	/* Transmitter empty and interrupt enabled ? */
131 	if ((dev->ier & UART_IER_THRI) && (dev->lsr & UART_LSR_TEMT))
132 		iir |= UART_IIR_THRI;
133 
134 	/* Now update the irq line, if necessary */
135 	if (!iir) {
136 		dev->iir = UART_IIR_NO_INT;
137 		if (dev->irq_state)
138 			kvm__irq_line(kvm, dev->irq, 0);
139 	} else {
140 		dev->iir = iir;
141 		if (!dev->irq_state)
142 			kvm__irq_line(kvm, dev->irq, 1);
143 	}
144 	dev->irq_state = iir;
145 
146 	/*
147 	 * If the kernel disabled the tx interrupt, we know that there
148 	 * is nothing more to transmit, so we can reset our tx logic
149 	 * here.
150 	 */
151 	if (!(dev->ier & UART_IER_THRI))
152 		serial8250_flush_tx(kvm, dev);
153 }
154 
155 #define SYSRQ_PENDING_NONE		0
156 
157 static int sysrq_pending;
158 
159 static void serial8250__sysrq(struct kvm *kvm, struct serial8250_device *dev)
160 {
161 	dev->lsr |= UART_LSR_DR | UART_LSR_BI;
162 	dev->rxbuf[dev->rxcnt++] = sysrq_pending;
163 	sysrq_pending	= SYSRQ_PENDING_NONE;
164 }
165 
166 static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,
167 				bool handle_sysrq)
168 {
169 	int c;
170 
171 	/*
172 	 * If the guest transmitted a full fifo, we clear the
173 	 * TEMT/THRE bits to let the kernel escape from the 8250
174 	 * interrupt handler. We come here only once a ms, so that
175 	 * should give the kernel the desired pause. That also flushes
176 	 * the tx fifo to the terminal.
177 	 */
178 	serial8250_flush_tx(kvm, dev);
179 
180 	if (dev->mcr & UART_MCR_LOOP)
181 		return;
182 
183 	if ((dev->lsr & UART_LSR_DR) || dev->rxcnt)
184 		return;
185 
186 	if (handle_sysrq && sysrq_pending) {
187 		serial8250__sysrq(kvm, dev);
188 		return;
189 	}
190 
191 	if (kvm->cfg.active_console != CONSOLE_8250)
192 		return;
193 
194 	while (term_readable(dev->id) &&
195 	       dev->rxcnt < FIFO_LEN) {
196 
197 		c = term_getc(kvm, dev->id);
198 
199 		if (c < 0)
200 			break;
201 		dev->rxbuf[dev->rxcnt++] = c;
202 		dev->lsr |= UART_LSR_DR;
203 	}
204 }
205 
206 void serial8250__update_consoles(struct kvm *kvm)
207 {
208 	unsigned int i;
209 
210 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
211 		struct serial8250_device *dev = &devices[i];
212 
213 		mutex_lock(&dev->mutex);
214 
215 		/* Restrict sysrq injection to the first port */
216 		serial8250__receive(kvm, dev, i == 0);
217 
218 		serial8250_update_irq(kvm, dev);
219 
220 		mutex_unlock(&dev->mutex);
221 	}
222 }
223 
224 void serial8250__inject_sysrq(struct kvm *kvm, char sysrq)
225 {
226 	sysrq_pending = sysrq;
227 }
228 
229 static struct serial8250_device *find_device(u16 port)
230 {
231 	unsigned int i;
232 
233 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
234 		struct serial8250_device *dev = &devices[i];
235 
236 		if (dev->iobase == (port & ~0x7))
237 			return dev;
238 	}
239 	return NULL;
240 }
241 
242 static bool serial8250_out(struct ioport *ioport, struct kvm *kvm, u16 port,
243 			   void *data, int size)
244 {
245 	struct serial8250_device *dev;
246 	u16 offset;
247 	bool ret = true;
248 	char *addr = data;
249 
250 	dev = find_device(port);
251 	if (!dev)
252 		return false;
253 
254 	mutex_lock(&dev->mutex);
255 
256 	offset = port - dev->iobase;
257 
258 	switch (offset) {
259 	case UART_TX:
260 		if (dev->lcr & UART_LCR_DLAB) {
261 			dev->dll = ioport__read8(data);
262 			break;
263 		}
264 
265 		/* Loopback mode */
266 		if (dev->mcr & UART_MCR_LOOP) {
267 			if (dev->rxcnt < FIFO_LEN) {
268 				dev->rxbuf[dev->rxcnt++] = *addr;
269 				dev->lsr |= UART_LSR_DR;
270 			}
271 			break;
272 		}
273 
274 		if (dev->txcnt < FIFO_LEN) {
275 			dev->txbuf[dev->txcnt++] = *addr;
276 			dev->lsr &= ~UART_LSR_TEMT;
277 			if (dev->txcnt == FIFO_LEN / 2)
278 				dev->lsr &= ~UART_LSR_THRE;
279 		} else {
280 			/* Should never happpen */
281 			dev->lsr &= ~(UART_LSR_TEMT | UART_LSR_THRE);
282 		}
283 		break;
284 	case UART_IER:
285 		if (!(dev->lcr & UART_LCR_DLAB))
286 			dev->ier = ioport__read8(data) & 0x0f;
287 		else
288 			dev->dlm = ioport__read8(data);
289 		break;
290 	case UART_FCR:
291 		dev->fcr = ioport__read8(data);
292 		break;
293 	case UART_LCR:
294 		dev->lcr = ioport__read8(data);
295 		break;
296 	case UART_MCR:
297 		dev->mcr = ioport__read8(data);
298 		break;
299 	case UART_LSR:
300 		/* Factory test */
301 		break;
302 	case UART_MSR:
303 		/* Not used */
304 		break;
305 	case UART_SCR:
306 		dev->scr = ioport__read8(data);
307 		break;
308 	default:
309 		ret = false;
310 		break;
311 	}
312 
313 	serial8250_update_irq(kvm, dev);
314 
315 	mutex_unlock(&dev->mutex);
316 
317 	return ret;
318 }
319 
320 static void serial8250_rx(struct serial8250_device *dev, void *data)
321 {
322 	if (dev->rxdone == dev->rxcnt)
323 		return;
324 
325 	/* Break issued ? */
326 	if (dev->lsr & UART_LSR_BI) {
327 		dev->lsr &= ~UART_LSR_BI;
328 		ioport__write8(data, 0);
329 		return;
330 	}
331 
332 	ioport__write8(data, dev->rxbuf[dev->rxdone++]);
333 	if (dev->rxcnt == dev->rxdone) {
334 		dev->lsr &= ~UART_LSR_DR;
335 		dev->rxcnt = dev->rxdone = 0;
336 	}
337 }
338 
339 static bool serial8250_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
340 {
341 	struct serial8250_device *dev;
342 	u16 offset;
343 	bool ret = true;
344 
345 	dev = find_device(port);
346 	if (!dev)
347 		return false;
348 
349 	mutex_lock(&dev->mutex);
350 
351 	offset = port - dev->iobase;
352 
353 	switch (offset) {
354 	case UART_RX:
355 		if (dev->lcr & UART_LCR_DLAB)
356 			ioport__write8(data, dev->dll);
357 		else
358 			serial8250_rx(dev, data);
359 		break;
360 	case UART_IER:
361 		if (dev->lcr & UART_LCR_DLAB)
362 			ioport__write8(data, dev->dlm);
363 		else
364 			ioport__write8(data, dev->ier);
365 		break;
366 	case UART_IIR:
367 		ioport__write8(data, dev->iir | UART_IIR_TYPE_BITS);
368 		break;
369 	case UART_LCR:
370 		ioport__write8(data, dev->lcr);
371 		break;
372 	case UART_MCR:
373 		ioport__write8(data, dev->mcr);
374 		break;
375 	case UART_LSR:
376 		ioport__write8(data, dev->lsr);
377 		break;
378 	case UART_MSR:
379 		ioport__write8(data, dev->msr);
380 		break;
381 	case UART_SCR:
382 		ioport__write8(data, dev->scr);
383 		break;
384 	default:
385 		ret = false;
386 		break;
387 	}
388 
389 	serial8250_update_irq(kvm, dev);
390 
391 	mutex_unlock(&dev->mutex);
392 
393 	return ret;
394 }
395 
396 static struct ioport_operations serial8250_ops = {
397 	.io_in		= serial8250_in,
398 	.io_out		= serial8250_out,
399 };
400 
401 static int serial8250__device_init(struct kvm *kvm, struct serial8250_device *dev)
402 {
403 	int r;
404 
405 	ioport__map_irq(&dev->irq);
406 	r = ioport__register(kvm, dev->iobase, &serial8250_ops, 8, NULL);
407 	kvm__irq_line(kvm, dev->irq, 0);
408 
409 	return r;
410 }
411 
412 int serial8250__init(struct kvm *kvm)
413 {
414 	unsigned int i, j;
415 	int r = 0;
416 
417 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
418 		struct serial8250_device *dev = &devices[i];
419 
420 		r = serial8250__device_init(kvm, dev);
421 		if (r < 0)
422 			goto cleanup;
423 	}
424 
425 	return r;
426 cleanup:
427 	for (j = 0; j <= i; j++) {
428 		struct serial8250_device *dev = &devices[j];
429 
430 		ioport__unregister(kvm, dev->iobase);
431 	}
432 
433 	return r;
434 }
435 dev_init(serial8250__init);
436 
437 int serial8250__exit(struct kvm *kvm)
438 {
439 	unsigned int i;
440 	int r;
441 
442 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
443 		struct serial8250_device *dev = &devices[i];
444 
445 		r = ioport__unregister(kvm, dev->iobase);
446 		if (r < 0)
447 			return r;
448 	}
449 
450 	return 0;
451 }
452 dev_exit(serial8250__exit);
453