serial.c 8.94 KB
Newer Older
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
#include "kvm/fdt.h"
10

11
#include <linux/types.h>
12
13
#include <linux/serial_reg.h>

14
#include <pthread.h>
15

16
17
18
19
20
21
22
23
24
/*
 * This fakes a U6_16550A. The fifo len needs to be 64 as the kernel
 * expects that for autodetection.
 */
#define FIFO_LEN		64
#define FIFO_MASK		(FIFO_LEN - 1)

#define UART_IIR_TYPE_BITS	0xc0

25
struct serial8250_device {
26
	struct mutex		mutex;
27
	u8			id;
28

29
30
	u16			iobase;
	u8			irq;
31
	u8			irq_state;
32
	int			txcnt;
33
34
35
36
	int			rxcnt;
	int			rxdone;
	char			txbuf[FIFO_LEN];
	char			rxbuf[FIFO_LEN];
37
38
39
40
41
42
43
44
45
46
47

	u8			dll;
	u8			dlm;
	u8			iir;
	u8			ier;
	u8			fcr;
	u8			lcr;
	u8			mcr;
	u8			lsr;
	u8			msr;
	u8			scr;
48
49
};

50
51
52
53
54
55
#define SERIAL_REGS_SETTING \
	.iir			= UART_IIR_NO_INT, \
	.lsr			= UART_LSR_TEMT | UART_LSR_THRE, \
	.msr			= UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS, \
	.mcr			= UART_MCR_OUT2,

56
static struct serial8250_device devices[] = {
57
58
	/* ttyS0 */
	[0]	= {
59
		.mutex			= MUTEX_INITIALIZER,
60

61
		.id			= 0,
62
63
64
		.iobase			= 0x3f8,
		.irq			= 4,

65
		SERIAL_REGS_SETTING
66
	},
67
68
	/* ttyS1 */
	[1]	= {
69
		.mutex			= MUTEX_INITIALIZER,
70

71
		.id			= 1,
72
73
		.iobase			= 0x2f8,
		.irq			= 3,
74

75
		SERIAL_REGS_SETTING
76
77
78
	},
	/* ttyS2 */
	[2]	= {
79
		.mutex			= MUTEX_INITIALIZER,
80

81
		.id			= 2,
82
83
		.iobase			= 0x3e8,
		.irq			= 4,
84

85
		SERIAL_REGS_SETTING
86
87
88
	},
	/* ttyS3 */
	[3]	= {
89
		.mutex			= MUTEX_INITIALIZER,
90

91
		.id			= 3,
92
93
94
		.iobase			= 0x2e8,
		.irq			= 3,

95
		SERIAL_REGS_SETTING
96
	},
97
98
};

99
static void serial8250_flush_tx(struct kvm *kvm, struct serial8250_device *dev)
100
101
102
103
{
	dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;

	if (dev->txcnt) {
104
		term_putc(dev->txbuf, dev->txcnt, dev->id);
105
106
107
108
		dev->txcnt = 0;
	}
}

109
110
111
112
static void serial8250_update_irq(struct kvm *kvm, struct serial8250_device *dev)
{
	u8 iir = 0;

113
114
115
116
117
118
119
120
121
122
123
124
125
126
	/* Handle clear rx */
	if (dev->lcr & UART_FCR_CLEAR_RCVR) {
		dev->lcr &= ~UART_FCR_CLEAR_RCVR;
		dev->rxcnt = dev->rxdone = 0;
		dev->lsr &= ~UART_LSR_DR;
	}

	/* Handle clear tx */
	if (dev->lcr & UART_FCR_CLEAR_XMIT) {
		dev->lcr &= ~UART_FCR_CLEAR_XMIT;
		dev->txcnt = 0;
		dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE;
	}

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	/* Data ready and rcv interrupt enabled ? */
	if ((dev->ier & UART_IER_RDI) && (dev->lsr & UART_LSR_DR))
		iir |= UART_IIR_RDI;

	/* Transmitter empty and interrupt enabled ? */
	if ((dev->ier & UART_IER_THRI) && (dev->lsr & UART_LSR_TEMT))
		iir |= UART_IIR_THRI;

	/* Now update the irq line, if necessary */
	if (!iir) {
		dev->iir = UART_IIR_NO_INT;
		if (dev->irq_state)
			kvm__irq_line(kvm, dev->irq, 0);
	} else {
		dev->iir = iir;
		if (!dev->irq_state)
			kvm__irq_line(kvm, dev->irq, 1);
	}
	dev->irq_state = iir;
146
147
148
149
150
151

	/*
	 * If the kernel disabled the tx interrupt, we know that there
	 * is nothing more to transmit, so we can reset our tx logic
	 * here.
	 */
152
	if (!(dev->ier & UART_IER_THRI))
153
		serial8250_flush_tx(kvm, dev);
154
155
}

156
157
158
159
#define SYSRQ_PENDING_NONE		0

static int sysrq_pending;

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++] = sysrq_pending;
164
	sysrq_pending	= SYSRQ_PENDING_NONE;
165
166
}

167
168
static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev,
				bool handle_sysrq)
169
170
171
{
	int c;

172
	if (dev->mcr & UART_MCR_LOOP)
173
174
		return;

175
	if ((dev->lsr & UART_LSR_DR) || dev->rxcnt)
176
177
		return;

178
179
	if (handle_sysrq && sysrq_pending) {
		serial8250__sysrq(kvm, dev);
180
		return;
181
	}
182

183
184
185
186
	if (kvm->cfg.active_console != CONSOLE_8250)
		return;

	while (term_readable(dev->id) &&
187
	       dev->rxcnt < FIFO_LEN) {
188

189
		c = term_getc(kvm, dev->id);
190

191
192
193
194
195
		if (c < 0)
			break;
		dev->rxbuf[dev->rxcnt++] = c;
		dev->lsr |= UART_LSR_DR;
	}
196
197
}

198
void serial8250__update_consoles(struct kvm *kvm)
199
{
200
	unsigned int i;
201

202
	for (i = 0; i < ARRAY_SIZE(devices); i++) {
203
		struct serial8250_device *dev = &devices[i];
204

205
		mutex_lock(&dev->mutex);
206

207
208
		/* Restrict sysrq injection to the first port */
		serial8250__receive(kvm, dev, i == 0);
209

210
		serial8250_update_irq(kvm, dev);
211
212
213

		mutex_unlock(&dev->mutex);
	}
214
215
}

216
void serial8250__inject_sysrq(struct kvm *kvm, char sysrq)
217
{
218
	sysrq_pending = sysrq;
219
220
}

221
static bool serial8250_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port,
222
			   void *data, int size)
223
{
224
	struct serial8250_device *dev = ioport->priv;
225
	u16 offset;
226
	bool ret = true;
227
	char *addr = data;
228

229
	mutex_lock(&dev->mutex);
230

231
	offset = port - dev->iobase;
232

233
234
	switch (offset) {
	case UART_TX:
235
236
237
238
		if (dev->lcr & UART_LCR_DLAB) {
			dev->dll = ioport__read8(data);
			break;
		}
239

240
241
242
243
244
245
		/* Loopback mode */
		if (dev->mcr & UART_MCR_LOOP) {
			if (dev->rxcnt < FIFO_LEN) {
				dev->rxbuf[dev->rxcnt++] = *addr;
				dev->lsr |= UART_LSR_DR;
			}
246
			break;
247
248
249
250
251
252
253
		}

		if (dev->txcnt < FIFO_LEN) {
			dev->txbuf[dev->txcnt++] = *addr;
			dev->lsr &= ~UART_LSR_TEMT;
			if (dev->txcnt == FIFO_LEN / 2)
				dev->lsr &= ~UART_LSR_THRE;
254
			serial8250_flush_tx(vcpu->kvm, dev);
255
		} else {
256
257
			/* Should never happpen */
			dev->lsr &= ~(UART_LSR_TEMT | UART_LSR_THRE);
258
		}
259
260
		break;
	case UART_IER:
261
		if (!(dev->lcr & UART_LCR_DLAB))
262
			dev->ier = ioport__read8(data) & 0x0f;
263
		else
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
			dev->dlm = ioport__read8(data);
		break;
	case UART_FCR:
		dev->fcr = ioport__read8(data);
		break;
	case UART_LCR:
		dev->lcr = ioport__read8(data);
		break;
	case UART_MCR:
		dev->mcr = ioport__read8(data);
		break;
	case UART_LSR:
		/* Factory test */
		break;
	case UART_MSR:
		/* Not used */
		break;
	case UART_SCR:
		dev->scr = ioport__read8(data);
		break;
	default:
		ret = false;
		break;
287
	}
288

289
	serial8250_update_irq(vcpu->kvm, dev);
290

291
	mutex_unlock(&dev->mutex);
292
293

	return ret;
294
295
}

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
static void serial8250_rx(struct serial8250_device *dev, void *data)
{
	if (dev->rxdone == dev->rxcnt)
		return;

	/* Break issued ? */
	if (dev->lsr & UART_LSR_BI) {
		dev->lsr &= ~UART_LSR_BI;
		ioport__write8(data, 0);
		return;
	}

	ioport__write8(data, dev->rxbuf[dev->rxdone++]);
	if (dev->rxcnt == dev->rxdone) {
		dev->lsr &= ~UART_LSR_DR;
		dev->rxcnt = dev->rxdone = 0;
	}
}

315
static bool serial8250_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
316
{
317
	struct serial8250_device *dev = ioport->priv;
318
	u16 offset;
319
	bool ret = true;
320

321
	mutex_lock(&dev->mutex);
322

323
	offset = port - dev->iobase;
324

325
326
	switch (offset) {
	case UART_RX:
327
		if (dev->lcr & UART_LCR_DLAB)
328
			ioport__write8(data, dev->dll);
329
330
		else
			serial8250_rx(dev, data);
331
332
333
334
335
336
337
		break;
	case UART_IER:
		if (dev->lcr & UART_LCR_DLAB)
			ioport__write8(data, dev->dlm);
		else
			ioport__write8(data, dev->ier);
		break;
338
	case UART_IIR:
339
		ioport__write8(data, dev->iir | UART_IIR_TYPE_BITS);
340
		break;
341
	case UART_LCR:
342
		ioport__write8(data, dev->lcr);
343
		break;
344
	case UART_MCR:
345
		ioport__write8(data, dev->mcr);
346
		break;
347
	case UART_LSR:
348
		ioport__write8(data, dev->lsr);
349
		break;
350
	case UART_MSR:
351
		ioport__write8(data, dev->msr);
352
		break;
353
	case UART_SCR:
354
		ioport__write8(data, dev->scr);
355
		break;
356
	default:
357
		ret = false;
358
		break;
359
	}
360

361
	serial8250_update_irq(vcpu->kvm, dev);
362

363
	mutex_unlock(&dev->mutex);
364
365

	return ret;
366
367
}

368
#ifdef CONFIG_HAS_LIBFDT
369
370
371

char *fdt_stdout_path = NULL;

372
#define DEVICE_NAME_MAX_LEN 32
373
374
375
376
377
static
void serial8250_generate_fdt_node(struct ioport *ioport, void *fdt,
				  void (*generate_irq_prop)(void *fdt,
							    u8 irq,
							    enum irq_type))
378
{
379
	char dev_name[DEVICE_NAME_MAX_LEN];
380
	struct serial8250_device *dev = ioport->priv;
381
	u64 addr = KVM_IOPORT_AREA + dev->iobase;
382
	u64 reg_prop[] = {
383
		cpu_to_fdt64(addr),
384
385
386
		cpu_to_fdt64(8),
	};

387
388
	snprintf(dev_name, DEVICE_NAME_MAX_LEN, "U6_16550A@%llx", addr);

389
390
391
392
393
394
	if (!fdt_stdout_path) {
		fdt_stdout_path = malloc(strlen(dev_name) + 2);
		/* Assumes that this node is a child of the root node. */
		sprintf(fdt_stdout_path, "/%s", dev_name);
	}

395
	_FDT(fdt_begin_node(fdt, dev_name));
396
397
	_FDT(fdt_property_string(fdt, "compatible", "ns16550a"));
	_FDT(fdt_property(fdt, "reg", reg_prop, sizeof(reg_prop)));
398
	generate_irq_prop(fdt, dev->irq, IRQ_TYPE_LEVEL_HIGH);
399
400
401
402
403
404
405
	_FDT(fdt_property_cell(fdt, "clock-frequency", 1843200));
	_FDT(fdt_end_node(fdt));
}
#else
#define serial8250_generate_fdt_node	NULL
#endif

406
static struct ioport_operations serial8250_ops = {
407
408
409
	.io_in			= serial8250_in,
	.io_out			= serial8250_out,
	.generate_fdt_node	= serial8250_generate_fdt_node,
410
411
};

412
static int serial8250__device_init(struct kvm *kvm, struct serial8250_device *dev)
413
{
414
415
	int r;

416
	ioport__map_irq(&dev->irq);
417
	r = ioport__register(kvm, dev->iobase, &serial8250_ops, 8, dev);
418
419

	return r;
420
421
}

422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
int serial8250__init(struct kvm *kvm)
{
	unsigned int i, j;
	int r = 0;

	for (i = 0; i < ARRAY_SIZE(devices); i++) {
		struct serial8250_device *dev = &devices[i];

		r = serial8250__device_init(kvm, dev);
		if (r < 0)
			goto cleanup;
	}

	return r;
cleanup:
	for (j = 0; j <= i; j++) {
		struct serial8250_device *dev = &devices[j];

440
		ioport__unregister(kvm, dev->iobase);
441
442
443
444
	}

	return r;
}
445
dev_init(serial8250__init);
446
447

int serial8250__exit(struct kvm *kvm)
448
{
449
	unsigned int i;
450
	int r;
451
452
453
454

	for (i = 0; i < ARRAY_SIZE(devices); i++) {
		struct serial8250_device *dev = &devices[i];

455
		r = ioport__unregister(kvm, dev->iobase);
456
457
		if (r < 0)
			return r;
458
	}
459
460

	return 0;
461
}
462
dev_exit(serial8250__exit);