xref: /openbsd-src/sys/dev/pv/xenvar.h (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: xenvar.h,v 1.38 2016/09/14 17:48:28 mikeb Exp $	*/
2 
3 /*
4  * Copyright (c) 2015 Mike Belopuhov
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _DEV_PV_XENVAR_H_
20 #define _DEV_PV_XENVAR_H_
21 
22 /* #define XEN_DEBUG */
23 
24 #ifdef XEN_DEBUG
25 #define DPRINTF(x...)		printf(x)
26 #else
27 #define DPRINTF(x...)
28 #endif
29 
30 struct xen_intsrc {
31 	SLIST_ENTRY(xen_intsrc)	 xi_entry;
32 	struct evcount		 xi_evcnt;
33 	evtchn_port_t		 xi_port;
34 	short			 xi_noclose;
35 	short			 xi_masked;
36 	struct task		 xi_task;
37 	struct taskq		*xi_taskq;
38 };
39 
40 struct xen_gntent {
41 	grant_entry_t		*ge_table;
42 	grant_ref_t		 ge_start;
43 	short			 ge_reserved;
44 	short			 ge_next;
45 	short			 ge_free;
46 	struct mutex		 ge_mtx;
47 };
48 
49 struct xen_gntmap {
50 	grant_ref_t		 gm_ref;
51 	paddr_t			 gm_paddr;
52 };
53 
54 struct xen_softc {
55 	struct device		 sc_dev;
56 	uint32_t		 sc_base;
57 	void			*sc_hc;
58 	uint32_t		 sc_features;
59 #define  XENFEAT_CBVEC		(1<<8)
60 
61 	struct shared_info	*sc_ipg;	/* HYPERVISOR_shared_info */
62 
63 	uint32_t		 sc_flags;
64 #define  XSF_CBVEC		 0x0001
65 #define  XSF_UNPLUG_NIC		 0x0002		/* disable emul. NICs */
66 #define  XSF_UNPLUG_IDE		 0x0004		/* disable emul. primary IDE */
67 #define  XSF_UNPLUG_IDESEC	 0x0008		/* disable emul. sec. IDE */
68 
69 	uint64_t		 sc_irq;	/* IDT vector number */
70 	SLIST_HEAD(, xen_intsrc) sc_intrs;
71 
72 	struct xen_gntent	*sc_gnt;	/* grant table entries */
73 	struct mutex		 sc_gntmtx;
74 	int			 sc_gntcnt;	/* number of allocated frames */
75 	int			 sc_gntmax;	/* number of allotted frames */
76 
77 	/*
78 	 * Xenstore
79 	 */
80 	struct xs_softc		*sc_xs;		/* xenstore softc */
81 
82 	struct task		 sc_ctltsk;	/* control task */
83 };
84 
85 extern struct xen_softc *xen_sc;
86 
87 struct xen_attach_args {
88 	void			*xa_parent;
89 	char			 xa_name[16];
90 	char			 xa_node[64];
91 	char			 xa_backend[128];
92 	int			 xa_domid;
93 	bus_dma_tag_t		 xa_dmat;
94 };
95 
96 /*
97  * Grant table references don't convey the information about an actual
98  * offset of the data within the page, however Xen needs to know it.
99  * We (ab)use bus_dma_segment's _ds_boundary member to store it.  Please
100  * note that we don't save or restore it's original value atm because
101  * neither i386 nor amd64 bus_dmamap_unload(9) code needs it.
102  */
103 #define ds_offset		 _ds_boundary
104 
105 /*
106  *  Hypercalls
107  */
108 #define XC_MEMORY		12
109 #define XC_OEVTCHN		16
110 #define XC_VERSION		17
111 #define XC_GNTTAB		20
112 #define XC_EVTCHN		32
113 #define XC_HVM			34
114 
115 int	xen_hypercall(struct xen_softc *, int, int, ...);
116 int	xen_hypercallv(struct xen_softc *, int, int, ulong *);
117 
118 /*
119  *  Interrupts
120  */
121 typedef uint32_t xen_intr_handle_t;
122 
123 void	xen_intr(void);
124 void	xen_intr_ack(void);
125 void	xen_intr_signal(xen_intr_handle_t);
126 void	xen_intr_schedule(xen_intr_handle_t);
127 int	xen_intr_establish(evtchn_port_t, xen_intr_handle_t *, int,
128 	    void (*)(void *), void *, char *);
129 int	xen_intr_disestablish(xen_intr_handle_t);
130 void	xen_intr_enable(void);
131 void	xen_intr_mask(xen_intr_handle_t);
132 int	xen_intr_unmask(xen_intr_handle_t);
133 
134 /*
135  *  XenStore
136  */
137 #define XS_LIST			0x01
138 #define XS_READ			0x02
139 #define XS_WATCH		0x04
140 #define XS_TOPEN		0x06
141 #define XS_TCLOSE		0x07
142 #define XS_WRITE		0x0b
143 #define XS_RM			0x0d
144 #define XS_EVENT		0x0f
145 #define XS_ERROR		0x10
146 #define XS_MAX			0x16
147 
148 struct xs_transaction {
149 	uint32_t		 xst_id;
150 	uint32_t		 xst_flags;
151 #define XST_POLL		0x0001
152 	struct xs_softc		*xst_sc;
153 };
154 
155 static __inline void
156 clear_bit(u_int b, volatile void *p)
157 {
158 	atomic_clearbits_int(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f));
159 }
160 
161 static __inline void
162 set_bit(u_int b, volatile void *p)
163 {
164 	atomic_setbits_int(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f));
165 }
166 
167 static __inline int
168 test_bit(u_int b, volatile void *p)
169 {
170 	return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f)));
171 }
172 
173 int	xs_cmd(struct xs_transaction *, int, const char *, struct iovec **,
174 	    int *);
175 void	xs_resfree(struct xs_transaction *, struct iovec *, int);
176 int	xs_watch(struct xen_softc *, const char *, const char *, struct task *,
177 	    void (*)(void *), void *);
178 int	xs_getprop(struct xen_softc *, const char *, const char *, char *, int);
179 int	xs_setprop(struct xen_softc *, const char *, const char *, char *, int);
180 int	xs_kvop(void *, int, char *, char *, size_t);
181 
182 #endif	/* _DEV_PV_XENVAR_H_ */
183