xref: /openbsd/usr.sbin/vmd/ns8250.h (revision d415bd75)
1 /* $OpenBSD: ns8250.h,v 1.10 2021/07/16 16:21:22 dv Exp $ */
2 /*
3  * Copyright (c) 2016 Mike Larkin <mlarkin@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /*
19  * Emulated 8250 UART
20  */
21 #define COM1_BASE       0x3f8
22 #define COM1_DATA	COM1_BASE+COM_OFFSET_DATA
23 #define COM1_IER	COM1_BASE+COM_OFFSET_IER
24 #define COM1_IIR	COM1_BASE+COM_OFFSET_IIR
25 #define COM1_LCR	COM1_BASE+COM_OFFSET_LCR
26 #define COM1_MCR	COM1_BASE+COM_OFFSET_MCR
27 #define COM1_LSR	COM1_BASE+COM_OFFSET_LSR
28 #define COM1_MSR	COM1_BASE+COM_OFFSET_MSR
29 #define COM1_SCR	COM1_BASE+COM_OFFSET_SCR
30 
31 #define COM_OFFSET_DATA 0
32 #define COM_OFFSET_IER  1
33 #define COM_OFFSET_IIR  2
34 #define COM_OFFSET_LCR  3
35 #define COM_OFFSET_MCR  4
36 #define COM_OFFSET_LSR  5
37 #define COM_OFFSET_MSR  6
38 #define COM_OFFSET_SCR  7
39 
40 /* ns8250 port identifier */
41 enum ns8250_portid {
42 	NS8250_COM1,
43 	NS8250_COM2,
44 };
45 
46 /* ns8250 UART registers */
47 struct ns8250_regs {
48 	uint8_t lcr;	/* Line Control Register */
49 	uint8_t fcr;	/* FIFO Control Register */
50 	uint8_t iir;	/* Interrupt ID Register */
51 	uint8_t ier;	/* Interrupt Enable Register */
52 	uint8_t divlo;	/* Baud rate divisor low byte */
53 	uint8_t divhi;	/* Baud rate divisor high byte */
54 	uint8_t msr;	/* Modem Status Register */
55 	uint8_t lsr;	/* Line Status Register */
56 	uint8_t mcr;	/* Modem Control Register */
57 	uint8_t scr;	/* Scratch Register */
58 	uint8_t data;	/* Unread input data */
59 };
60 
61 /* ns8250 UART device state */
62 struct ns8250_dev {
63 	pthread_mutex_t mutex;
64 	struct ns8250_regs regs;
65 	struct event event;
66 	struct event rate;
67 	struct event wake;
68 	struct timeval rate_tv;
69 	enum ns8250_portid portid;
70 	int fd;
71 	int irq;
72 	uint32_t vmid;
73 	uint64_t byte_out;
74 	uint32_t baudrate;
75 	uint32_t pause_ct;
76 };
77 
78 void ns8250_init(int, uint32_t);
79 uint8_t vcpu_exit_com(struct vm_run_params *);
80 uint8_t vcpu_process_com_data(struct vm_exit *, uint32_t, uint32_t);
81 void vcpu_process_com_lcr(struct vm_exit *);
82 void vcpu_process_com_lsr(struct vm_exit *);
83 void vcpu_process_com_ier(struct vm_exit *);
84 void vcpu_process_com_mcr(struct vm_exit *);
85 void vcpu_process_com_iir(struct vm_exit *);
86 void vcpu_process_com_msr(struct vm_exit *);
87 void vcpu_process_com_scr(struct vm_exit *);
88 int ns8250_dump(int);
89 int ns8250_restore(int, int, uint32_t);
90 void ns8250_stop(void);
91 void ns8250_start(void);
92