xref: /kvmtool/hw/serial.c (revision 8dfae8be1660114bbf5b7726ba52d7abe9239d17)
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 struct serial8250_device {
16 	pthread_mutex_t		mutex;
17 	u8			id;
18 
19 	u16			iobase;
20 	u8			irq;
21 	u8			irq_state;
22 	int			txcnt;
23 
24 	u8			rbr;		/* receive buffer */
25 	u8			dll;
26 	u8			dlm;
27 	u8			iir;
28 	u8			ier;
29 	u8			fcr;
30 	u8			lcr;
31 	u8			mcr;
32 	u8			lsr;
33 	u8			msr;
34 	u8			scr;
35 };
36 
37 #define SERIAL_REGS_SETTING \
38 	.iir			= UART_IIR_NO_INT, \
39 	.lsr			= UART_LSR_TEMT | UART_LSR_THRE, \
40 	.msr			= UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS, \
41 	.mcr			= UART_MCR_OUT2,
42 
43 static struct serial8250_device devices[] = {
44 	/* ttyS0 */
45 	[0]	= {
46 		.mutex			= PTHREAD_MUTEX_INITIALIZER,
47 
48 		.id			= 0,
49 		.iobase			= 0x3f8,
50 		.irq			= 4,
51 
52 		SERIAL_REGS_SETTING
53 	},
54 	/* ttyS1 */
55 	[1]	= {
56 		.mutex			= PTHREAD_MUTEX_INITIALIZER,
57 
58 		.id			= 1,
59 		.iobase			= 0x2f8,
60 		.irq			= 3,
61 
62 		SERIAL_REGS_SETTING
63 	},
64 	/* ttyS2 */
65 	[2]	= {
66 		.mutex			= PTHREAD_MUTEX_INITIALIZER,
67 
68 		.id			= 2,
69 		.iobase			= 0x3e8,
70 		.irq			= 4,
71 
72 		SERIAL_REGS_SETTING
73 	},
74 	/* ttyS3 */
75 	[3]	= {
76 		.mutex			= PTHREAD_MUTEX_INITIALIZER,
77 
78 		.id			= 3,
79 		.iobase			= 0x2e8,
80 		.irq			= 3,
81 
82 		SERIAL_REGS_SETTING
83 	},
84 };
85 
86 static void serial8250_update_irq(struct kvm *kvm, struct serial8250_device *dev)
87 {
88 	u8 iir = 0;
89 
90 	/* Data ready and rcv interrupt enabled ? */
91 	if ((dev->ier & UART_IER_RDI) && (dev->lsr & UART_LSR_DR))
92 		iir |= UART_IIR_RDI;
93 
94 	/* Transmitter empty and interrupt enabled ? */
95 	if ((dev->ier & UART_IER_THRI) && (dev->lsr & UART_LSR_TEMT))
96 		iir |= UART_IIR_THRI;
97 
98 	/* Now update the irq line, if necessary */
99 	if (!iir) {
100 		dev->iir = UART_IIR_NO_INT;
101 		if (dev->irq_state)
102 			kvm__irq_line(kvm, dev->irq, 0);
103 	} else {
104 		dev->iir = iir;
105 		if (!dev->irq_state)
106 			kvm__irq_line(kvm, dev->irq, 1);
107 	}
108 	dev->irq_state = iir;
109 
110 	/*
111 	 * If the kernel disabled the tx interrupt, we know that there
112 	 * is nothing more to transmit, so we can reset our tx logic
113 	 * here.
114 	 */
115 	if (!(dev->ier & UART_IER_THRI)) {
116 		dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
117 		dev->txcnt = 0;
118 	}
119 }
120 
121 #define SYSRQ_PENDING_NONE		0
122 #define SYSRQ_PENDING_BREAK		1
123 #define SYSRQ_PENDING_CMD		2
124 
125 static int sysrq_pending;
126 
127 static void serial8250__sysrq(struct kvm *kvm, struct serial8250_device *dev)
128 {
129 	switch (sysrq_pending) {
130 	case SYSRQ_PENDING_BREAK:
131 		dev->lsr |= UART_LSR_DR | UART_LSR_BI;
132 
133 		sysrq_pending = SYSRQ_PENDING_CMD;
134 		break;
135 	case SYSRQ_PENDING_CMD:
136 		dev->rbr = 'p';
137 		dev->lsr |= UART_LSR_DR;
138 
139 		sysrq_pending	= SYSRQ_PENDING_NONE;
140 		break;
141 	}
142 }
143 
144 static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev)
145 {
146 	int c;
147 
148 	/*
149 	 * If the guest transmitted 16 chars in a row, we clear the
150 	 * TEMT/THRE bits to let the kernel escape from the 8250
151 	 * interrupt handler. We come here only once a ms, so that
152 	 * should give the kernel the desired pause.
153 	 */
154 	dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
155 	dev->txcnt = 0;
156 
157 	if (dev->lsr & UART_LSR_DR)
158 		return;
159 
160 	if (sysrq_pending) {
161 		serial8250__sysrq(kvm, dev);
162 		return;
163 	}
164 
165 	if (!term_readable(CONSOLE_8250, dev->id))
166 		return;
167 
168 	c = term_getc(CONSOLE_8250, dev->id);
169 
170 	if (c < 0)
171 		return;
172 
173 	dev->rbr = c;
174 	dev->lsr |= UART_LSR_DR;
175 }
176 
177 void serial8250__update_consoles(struct kvm *kvm)
178 {
179 	unsigned int i;
180 
181 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
182 		struct serial8250_device *dev = &devices[i];
183 
184 		mutex_lock(&dev->mutex);
185 
186 		serial8250__receive(kvm, dev);
187 
188 		serial8250_update_irq(kvm, dev);
189 
190 		mutex_unlock(&dev->mutex);
191 	}
192 }
193 
194 void serial8250__inject_sysrq(struct kvm *kvm)
195 {
196 	sysrq_pending	= SYSRQ_PENDING_BREAK;
197 }
198 
199 static struct serial8250_device *find_device(u16 port)
200 {
201 	unsigned int i;
202 
203 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
204 		struct serial8250_device *dev = &devices[i];
205 
206 		if (dev->iobase == (port & ~0x7))
207 			return dev;
208 	}
209 	return NULL;
210 }
211 
212 static bool serial8250_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
213 {
214 	struct serial8250_device *dev;
215 	u16 offset;
216 	bool ret = true;
217 
218 	dev = find_device(port);
219 	if (!dev)
220 		return false;
221 
222 	mutex_lock(&dev->mutex);
223 
224 	offset = port - dev->iobase;
225 
226 	switch (offset) {
227 	case UART_TX:
228 		if (!(dev->lcr & UART_LCR_DLAB)) {
229 			char *addr = data;
230 
231 			if (!(dev->mcr & UART_MCR_LOOP))
232 				term_putc(CONSOLE_8250, addr, size, dev->id);
233 			/* else FIXME: Inject data into rcv path for LOOP */
234 
235 			if (++dev->txcnt == 16)
236 				dev->lsr &= ~(UART_LSR_TEMT | UART_LSR_THRE);
237 			break;
238 		} else {
239 			dev->dll = ioport__read8(data);
240 		}
241 		break;
242 	case UART_IER:
243 		if (!(dev->lcr & UART_LCR_DLAB))
244 			dev->ier = ioport__read8(data) & 0x3f;
245 		else
246 			dev->dlm = ioport__read8(data);
247 		break;
248 	case UART_FCR:
249 		dev->fcr = ioport__read8(data);
250 		break;
251 	case UART_LCR:
252 		dev->lcr = ioport__read8(data);
253 		break;
254 	case UART_MCR:
255 		dev->mcr = ioport__read8(data);
256 		break;
257 	case UART_LSR:
258 		/* Factory test */
259 		break;
260 	case UART_MSR:
261 		/* Not used */
262 		break;
263 	case UART_SCR:
264 		dev->scr = ioport__read8(data);
265 		break;
266 	default:
267 		ret = false;
268 		break;
269 	}
270 
271 	serial8250_update_irq(kvm, dev);
272 
273 	mutex_unlock(&dev->mutex);
274 
275 	return ret;
276 }
277 
278 static bool serial8250_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
279 {
280 	struct serial8250_device *dev;
281 	u16 offset;
282 	bool ret = true;
283 
284 	dev = find_device(port);
285 	if (!dev)
286 		return false;
287 
288 	mutex_lock(&dev->mutex);
289 
290 	offset = port - dev->iobase;
291 
292 	switch (offset) {
293 	case UART_RX:
294 		if (dev->lcr & UART_LCR_DLAB) {
295 			ioport__write8(data, dev->dll);
296 		} else {
297 			ioport__write8(data, dev->rbr);
298 			dev->lsr &= ~UART_LSR_DR;
299 		}
300 		break;
301 	case UART_IER:
302 		if (dev->lcr & UART_LCR_DLAB)
303 			ioport__write8(data, dev->dlm);
304 		else
305 			ioport__write8(data, dev->ier);
306 		break;
307 	case UART_IIR:
308 		ioport__write8(data, dev->iir);
309 		break;
310 	case UART_LCR:
311 		ioport__write8(data, dev->lcr);
312 		break;
313 	case UART_MCR:
314 		ioport__write8(data, dev->mcr);
315 		break;
316 	case UART_LSR:
317 		ioport__write8(data, dev->lsr);
318 		break;
319 	case UART_MSR:
320 		ioport__write8(data, dev->msr);
321 		break;
322 	case UART_SCR:
323 		ioport__write8(data, dev->scr);
324 		break;
325 	default:
326 		ret = false;
327 		break;
328 	}
329 
330 	serial8250_update_irq(kvm, dev);
331 
332 	mutex_unlock(&dev->mutex);
333 
334 	return ret;
335 }
336 
337 static struct ioport_operations serial8250_ops = {
338 	.io_in		= serial8250_in,
339 	.io_out		= serial8250_out,
340 };
341 
342 static void serial8250__device_init(struct kvm *kvm, struct serial8250_device *dev)
343 {
344 	ioport__register(dev->iobase, &serial8250_ops, 8, NULL);
345 	kvm__irq_line(kvm, dev->irq, 0);
346 }
347 
348 void serial8250__init(struct kvm *kvm)
349 {
350 	unsigned int i;
351 
352 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
353 		struct serial8250_device *dev = &devices[i];
354 
355 		serial8250__device_init(kvm, dev);
356 	}
357 }
358