xref: /kvmtool/hw/serial.c (revision 251cf9a60d724ab0e69fdac297dc6c3c2554c551)
1 #include "kvm/8250-serial.h"
2 
3 #include "kvm/ioport.h"
4 #include "kvm/util.h"
5 #include "kvm/kvm.h"
6 
7 #include <linux/serial_reg.h>
8 
9 #include <stdbool.h>
10 #include <poll.h>
11 
12 struct serial8250_device {
13 	uint16_t		iobase;
14 	uint8_t			irq;
15 
16 	uint8_t			rbr;		/* receive buffer */
17 	uint8_t			dll;
18 	uint8_t			dlm;
19 	uint8_t			iir;
20 	uint8_t			ier;
21 	uint8_t			fcr;
22 	uint8_t			lcr;
23 	uint8_t			mcr;
24 	uint8_t			lsr;
25 	uint8_t			scr;
26 
27 	uint8_t			counter;
28 };
29 
30 static struct serial8250_device devices[] = {
31 	/* ttyS0 */
32 	[0]	= {
33 		.iobase			= 0x3f8,
34 		.irq			= 4,
35 
36 		.lsr			= UART_LSR_TEMT | UART_LSR_THRE,
37 	},
38 	/* ttyS1 */
39 	[1]	= {
40 		.iobase			= 0x2f8,
41 		.irq			= 3,
42 
43 		.iir			= UART_IIR_NO_INT,
44 	},
45 	/* ttyS2 */
46 	[2]	= {
47 		.iobase			= 0x3e8,
48 		.irq			= 4,
49 
50 		.iir			= UART_IIR_NO_INT,
51 	},
52 };
53 
54 static int read_char(int fd)
55 {
56 	int c;
57 
58 	if (read(fd, &c, 1) < 0)
59 		return -1;
60 
61 	return c;
62 }
63 
64 static bool is_readable(int fd)
65 {
66 	struct pollfd pollfd = (struct pollfd) {
67 		.fd	= fd,
68 		.events	= POLLIN,
69 	};
70 
71 	return poll(&pollfd, 1, 0) > 0;
72 }
73 
74 static void serial8250__receive(struct kvm *self, struct serial8250_device *dev)
75 {
76 	int c;
77 
78 	if (dev->lsr & UART_LSR_DR)
79 		return;
80 
81 	if (!is_readable(fileno(stdin)))
82 		return;
83 
84 	c		= read_char(fileno(stdin));
85 	if (c < 0)
86 		return;
87 
88 	dev->rbr	= c;
89 	dev->lsr	|= UART_LSR_DR;
90 }
91 
92 /*
93  * Interrupts are injected for ttyS0 only.
94  */
95 void serial8250__interrupt(struct kvm *self)
96 {
97 	struct serial8250_device *dev = &devices[0];
98 	uint8_t new_iir;
99 
100 	dev->iir	= UART_IIR_NO_INT;
101 
102 	/* No interrupts enabled. Exit... */
103 	if (!(dev->ier & (UART_IER_THRI|UART_IER_RDI)))
104 		return;
105 
106 	serial8250__receive(self, dev);
107 
108 	new_iir		= 0;
109 
110 	if (dev->lsr & UART_LSR_DR)
111 		new_iir			|= UART_IIR_RDI;
112 	else if (dev->ier & UART_IER_THRI)
113 		new_iir			|= UART_IIR_THRI;
114 
115 	/* Only send an IRQ if there's work to do. */
116 	if (new_iir) {
117 		dev->counter		= 0;
118 		dev->iir		= new_iir;
119 		kvm__irq_line(self, dev->irq, 0);
120 		kvm__irq_line(self, dev->irq, 1);
121 	}
122 }
123 
124 static struct serial8250_device *find_device(uint16_t port)
125 {
126 	unsigned int i;
127 
128 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
129 		struct serial8250_device *dev = &devices[i];
130 
131 		if (dev->iobase == (port & ~0x7))
132 			return dev;
133 	}
134 	return NULL;
135 }
136 
137 static bool serial8250_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
138 {
139 	struct serial8250_device *dev;
140 	uint16_t offset;
141 
142 	dev		= find_device(port);
143 	if (!dev)
144 		return false;
145 
146 	offset		= port - dev->iobase;
147 
148 	if (dev->lcr & UART_LCR_DLAB) {
149 		switch (offset) {
150 		case UART_DLL:
151 			dev->dll		= ioport__read8(data);
152 			break;
153 		case UART_DLM:
154 			dev->dlm		= ioport__read8(data);
155 			break;
156 		case UART_FCR:
157 			dev->fcr		= ioport__read8(data);
158 			break;
159 		case UART_LCR:
160 			dev->lcr		= ioport__read8(data);
161 			break;
162 		default:
163 			return false;
164 		}
165 	} else {
166 		switch (offset) {
167 		case UART_TX: {
168 			char *p = data;
169 			int i;
170 
171 			while (count--) {
172 				for (i = 0; i < size; i++)
173 					fprintf(stdout, "%c", *p++);
174 			}
175 			fflush(stdout);
176 
177 			if (dev->counter++ > 10)
178 				dev->iir		= UART_IIR_NO_INT;
179 
180 			break;
181 		}
182 		case UART_IER:
183 			dev->ier		= ioport__read8(data);
184 			break;
185 		case UART_FCR:
186 			dev->fcr		= ioport__read8(data);
187 			break;
188 		case UART_LCR:
189 			dev->lcr		= ioport__read8(data);
190 			break;
191 		case UART_MCR:
192 			dev->mcr		= ioport__read8(data);
193 			break;
194 		case UART_SCR:
195 			dev->scr		= ioport__read8(data);
196 			break;
197 		default:
198 			return false;
199 		}
200 	}
201 
202 	return true;
203 }
204 
205 static bool serial8250_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
206 {
207 	struct serial8250_device *dev;
208 	uint16_t offset;
209 
210 	dev		= find_device(port);
211 	if (!dev)
212 		return false;
213 
214 	offset		= port - dev->iobase;
215 
216 	if (dev->lcr & UART_LCR_DLAB)
217 		return false;
218 
219 	switch (offset) {
220 	case UART_RX:
221 		if (dev->lsr & UART_LSR_DR) {
222 			dev->iir		= UART_IIR_NO_INT;
223 
224 			dev->lsr		&= ~UART_LSR_DR;
225 			ioport__write8(data, dev->rbr);
226 		}
227 		break;
228 	case UART_IER:
229 		ioport__write8(data, dev->ier);
230 		break;
231 	case UART_IIR:
232 		ioport__write8(data, dev->iir);
233 		break;
234 	case UART_LCR:
235 		ioport__write8(data, dev->lcr);
236 		break;
237 	case UART_MCR:
238 		ioport__write8(data, dev->mcr);
239 		break;
240 	case UART_LSR:
241 		ioport__write8(data, dev->lsr);
242 		break;
243 	case UART_MSR:
244 		ioport__write8(data, UART_MSR_CTS);
245 		break;
246 	case UART_SCR:
247 		ioport__write8(data, dev->scr);
248 		break;
249 	default:
250 		return false;
251 	}
252 
253 	return true;
254 }
255 
256 static struct ioport_operations serial8250_ops = {
257 	.io_in		= serial8250_in,
258 	.io_out		= serial8250_out,
259 };
260 
261 void serial8250__init(void)
262 {
263 	unsigned int i;
264 
265 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
266 		struct serial8250_device *dev = &devices[i];
267 
268 		ioport__register(dev->iobase, &serial8250_ops, 8);
269 	}
270 }
271