xref: /kvmtool/hw/serial.c (revision d4b02a378f6bd6e22b18e754a19f3ce3fa2f3e2e)
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 	pthread_mutex_t		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			= PTHREAD_MUTEX_INITIALIZER,
59 
60 		.id			= 0,
61 		.iobase			= 0x3f8,
62 		.irq			= 4,
63 
64 		SERIAL_REGS_SETTING
65 	},
66 	/* ttyS1 */
67 	[1]	= {
68 		.mutex			= PTHREAD_MUTEX_INITIALIZER,
69 
70 		.id			= 1,
71 		.iobase			= 0x2f8,
72 		.irq			= 3,
73 
74 		SERIAL_REGS_SETTING
75 	},
76 	/* ttyS2 */
77 	[2]	= {
78 		.mutex			= PTHREAD_MUTEX_INITIALIZER,
79 
80 		.id			= 2,
81 		.iobase			= 0x3e8,
82 		.irq			= 4,
83 
84 		SERIAL_REGS_SETTING
85 	},
86 	/* ttyS3 */
87 	[3]	= {
88 		.mutex			= PTHREAD_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 serial8250_device *dev)
99 {
100 	dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
101 
102 	if (dev->txcnt) {
103 		term_putc(CONSOLE_8250, 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(dev);
153 }
154 
155 #define SYSRQ_PENDING_NONE		0
156 #define SYSRQ_PENDING_BREAK		1
157 
158 static int sysrq_pending;
159 
160 static void serial8250__sysrq(struct kvm *kvm, struct serial8250_device *dev)
161 {
162 	dev->lsr |= UART_LSR_DR | UART_LSR_BI;
163 	dev->rxbuf[dev->rxcnt++] = 'p';
164 	sysrq_pending	= SYSRQ_PENDING_NONE;
165 }
166 
167 static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,
168 				bool handle_sysrq)
169 {
170 	int c;
171 
172 	/*
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.
178 	 */
179 	serial8250_flush_tx(dev);
180 
181 	if (dev->mcr & UART_MCR_LOOP)
182 		return;
183 
184 	if ((dev->lsr & UART_LSR_DR) || dev->rxcnt)
185 		return;
186 
187 	if (handle_sysrq && sysrq_pending) {
188 		serial8250__sysrq(kvm, dev);
189 		return;
190 	}
191 
192 	while (term_readable(CONSOLE_8250, dev->id) &&
193 	       dev->rxcnt < FIFO_LEN) {
194 
195 		c = term_getc(CONSOLE_8250, dev->id);
196 
197 		if (c < 0)
198 			break;
199 		dev->rxbuf[dev->rxcnt++] = c;
200 		dev->lsr |= UART_LSR_DR;
201 	}
202 }
203 
204 void serial8250__update_consoles(struct kvm *kvm)
205 {
206 	unsigned int i;
207 
208 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
209 		struct serial8250_device *dev = &devices[i];
210 
211 		mutex_lock(&dev->mutex);
212 
213 		/* Restrict sysrq injection to the first port */
214 		serial8250__receive(kvm, dev, i == 0);
215 
216 		serial8250_update_irq(kvm, dev);
217 
218 		mutex_unlock(&dev->mutex);
219 	}
220 }
221 
222 void serial8250__inject_sysrq(struct kvm *kvm)
223 {
224 	sysrq_pending	= SYSRQ_PENDING_BREAK;
225 }
226 
227 static struct serial8250_device *find_device(u16 port)
228 {
229 	unsigned int i;
230 
231 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
232 		struct serial8250_device *dev = &devices[i];
233 
234 		if (dev->iobase == (port & ~0x7))
235 			return dev;
236 	}
237 	return NULL;
238 }
239 
240 static bool serial8250_out(struct ioport *ioport, struct kvm *kvm, u16 port,
241 			   void *data, int size)
242 {
243 	struct serial8250_device *dev;
244 	u16 offset;
245 	bool ret = true;
246 	char *addr = data;
247 
248 	dev = find_device(port);
249 	if (!dev)
250 		return false;
251 
252 	mutex_lock(&dev->mutex);
253 
254 	offset = port - dev->iobase;
255 
256 	switch (offset) {
257 	case UART_TX:
258 		if (dev->lcr & UART_LCR_DLAB) {
259 			dev->dll = ioport__read8(data);
260 			break;
261 		}
262 
263 		/* Loopback mode */
264 		if (dev->mcr & UART_MCR_LOOP) {
265 			if (dev->rxcnt < FIFO_LEN) {
266 				dev->rxbuf[dev->rxcnt++] = *addr;
267 				dev->lsr |= UART_LSR_DR;
268 			}
269 			break;
270 		}
271 
272 		if (dev->txcnt < FIFO_LEN) {
273 			dev->txbuf[dev->txcnt++] = *addr;
274 			dev->lsr &= ~UART_LSR_TEMT;
275 			if (dev->txcnt == FIFO_LEN / 2)
276 				dev->lsr &= ~UART_LSR_THRE;
277 		} else {
278 			/* Should never happpen */
279 			dev->lsr &= ~(UART_LSR_TEMT | UART_LSR_THRE);
280 		}
281 		break;
282 	case UART_IER:
283 		if (!(dev->lcr & UART_LCR_DLAB))
284 			dev->ier = ioport__read8(data) & 0x0f;
285 		else
286 			dev->dlm = ioport__read8(data);
287 		break;
288 	case UART_FCR:
289 		dev->fcr = ioport__read8(data);
290 		break;
291 	case UART_LCR:
292 		dev->lcr = ioport__read8(data);
293 		break;
294 	case UART_MCR:
295 		dev->mcr = ioport__read8(data);
296 		break;
297 	case UART_LSR:
298 		/* Factory test */
299 		break;
300 	case UART_MSR:
301 		/* Not used */
302 		break;
303 	case UART_SCR:
304 		dev->scr = ioport__read8(data);
305 		break;
306 	default:
307 		ret = false;
308 		break;
309 	}
310 
311 	serial8250_update_irq(kvm, dev);
312 
313 	mutex_unlock(&dev->mutex);
314 
315 	return ret;
316 }
317 
318 static void serial8250_rx(struct serial8250_device *dev, void *data)
319 {
320 	if (dev->rxdone == dev->rxcnt)
321 		return;
322 
323 	/* Break issued ? */
324 	if (dev->lsr & UART_LSR_BI) {
325 		dev->lsr &= ~UART_LSR_BI;
326 		ioport__write8(data, 0);
327 		return;
328 	}
329 
330 	ioport__write8(data, dev->rxbuf[dev->rxdone++]);
331 	if (dev->rxcnt == dev->rxdone) {
332 		dev->lsr &= ~UART_LSR_DR;
333 		dev->rxcnt = dev->rxdone = 0;
334 	}
335 }
336 
337 static bool serial8250_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
338 {
339 	struct serial8250_device *dev;
340 	u16 offset;
341 	bool ret = true;
342 
343 	dev = find_device(port);
344 	if (!dev)
345 		return false;
346 
347 	mutex_lock(&dev->mutex);
348 
349 	offset = port - dev->iobase;
350 
351 	switch (offset) {
352 	case UART_RX:
353 		if (dev->lcr & UART_LCR_DLAB)
354 			ioport__write8(data, dev->dll);
355 		else
356 			serial8250_rx(dev, data);
357 		break;
358 	case UART_IER:
359 		if (dev->lcr & UART_LCR_DLAB)
360 			ioport__write8(data, dev->dlm);
361 		else
362 			ioport__write8(data, dev->ier);
363 		break;
364 	case UART_IIR:
365 		ioport__write8(data, dev->iir | UART_IIR_TYPE_BITS);
366 		break;
367 	case UART_LCR:
368 		ioport__write8(data, dev->lcr);
369 		break;
370 	case UART_MCR:
371 		ioport__write8(data, dev->mcr);
372 		break;
373 	case UART_LSR:
374 		ioport__write8(data, dev->lsr);
375 		break;
376 	case UART_MSR:
377 		ioport__write8(data, dev->msr);
378 		break;
379 	case UART_SCR:
380 		ioport__write8(data, dev->scr);
381 		break;
382 	default:
383 		ret = false;
384 		break;
385 	}
386 
387 	serial8250_update_irq(kvm, dev);
388 
389 	mutex_unlock(&dev->mutex);
390 
391 	return ret;
392 }
393 
394 static struct ioport_operations serial8250_ops = {
395 	.io_in		= serial8250_in,
396 	.io_out		= serial8250_out,
397 };
398 
399 static void serial8250__device_init(struct kvm *kvm, struct serial8250_device *dev)
400 {
401 	ioport__register(dev->iobase, &serial8250_ops, 8, NULL);
402 	kvm__irq_line(kvm, dev->irq, 0);
403 }
404 
405 void serial8250__init(struct kvm *kvm)
406 {
407 	unsigned int i;
408 
409 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
410 		struct serial8250_device *dev = &devices[i];
411 
412 		serial8250__device_init(kvm, dev);
413 	}
414 }
415