xref: /freebsd/sys/xen/xen-os.h (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /******************************************************************************
2  * xen/xen-os.h
3  *
4  * Random collection of macros and definition
5  *
6  * Copyright (c) 2003, 2004 Keir Fraser (on behalf of the Xen team)
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to
11  * deal in the Software without restriction, including without limitation the
12  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13  * sell copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
28 #ifndef _XEN_XEN_OS_H_
29 #define _XEN_XEN_OS_H_
30 
31 #define  __XEN_INTERFACE_VERSION__ 0x00040d00
32 
33 #define GRANT_REF_INVALID   0xffffffff
34 
35 #ifdef LOCORE
36 #define __ASSEMBLY__
37 #endif
38 
39 #include <contrib/xen/xen.h>
40 
41 #ifndef __ASSEMBLY__
42 #include <xen/hvm.h>
43 #include <contrib/xen/event_channel.h>
44 
45 /*
46  * Setup function which needs to be called on each processor by architecture
47  */
48 extern void xen_setup_vcpu_info(void);
49 
50 static inline vm_paddr_t
51 xen_get_xenstore_mfn(void)
52 {
53 
54 	return (hvm_get_parameter(HVM_PARAM_STORE_PFN));
55 }
56 
57 static inline evtchn_port_t
58 xen_get_xenstore_evtchn(void)
59 {
60 
61 	return (hvm_get_parameter(HVM_PARAM_STORE_EVTCHN));
62 }
63 
64 static inline vm_paddr_t
65 xen_get_console_mfn(void)
66 {
67 
68 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_PFN));
69 }
70 
71 static inline evtchn_port_t
72 xen_get_console_evtchn(void)
73 {
74 
75 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN));
76 }
77 
78 extern shared_info_t *HYPERVISOR_shared_info;
79 
80 extern bool xen_suspend_cancelled;
81 
82 enum xen_domain_type {
83 	XEN_NATIVE,             /* running on bare hardware    */
84 	XEN_PV_DOMAIN,          /* running in a PV domain      */
85 	XEN_HVM_DOMAIN,         /* running in a Xen hvm domain */
86 };
87 
88 extern enum xen_domain_type xen_domain_type;
89 
90 static inline int
91 xen_domain(void)
92 {
93 	return (xen_domain_type != XEN_NATIVE);
94 }
95 
96 static inline int
97 xen_pv_domain(void)
98 {
99 	return (xen_domain_type == XEN_PV_DOMAIN);
100 }
101 
102 static inline int
103 xen_hvm_domain(void)
104 {
105 	return (xen_domain_type == XEN_HVM_DOMAIN);
106 }
107 
108 static inline bool
109 xen_initial_domain(void)
110 {
111 
112 	return (xen_domain() && (hvm_start_flags & SIF_INITDOMAIN) != 0);
113 }
114 #endif
115 
116 #include <machine/xen/xen-os.h>
117 
118 /* Everything below this point is not included by assembler (.S) files. */
119 #ifndef __ASSEMBLY__
120 
121 /*
122  * Based on ofed/include/linux/bitops.h
123  *
124  * Those helpers are prefixed by xen_ because xen-os.h is widely included
125  * and we don't want the other drivers using them.
126  *
127  */
128 #define NBPL (NBBY * sizeof(long))
129 
130 static inline bool
131 xen_test_bit(int bit, volatile long *addr)
132 {
133 	unsigned long mask = 1UL << (bit % NBPL);
134 
135 	return !!(atomic_load_acq_long(&addr[bit / NBPL]) & mask);
136 }
137 
138 static inline void
139 xen_set_bit(int bit, volatile long *addr)
140 {
141 	atomic_set_long(&addr[bit / NBPL], 1UL << (bit % NBPL));
142 }
143 
144 static inline void
145 xen_clear_bit(int bit, volatile long *addr)
146 {
147 	atomic_clear_long(&addr[bit / NBPL], 1UL << (bit % NBPL));
148 }
149 
150 #undef NBPL
151 
152 /*
153  * Functions to allocate/free unused memory in order
154  * to map memory from other domains.
155  */
156 struct resource *xenmem_alloc(device_t dev, int *res_id, size_t size);
157 int xenmem_free(device_t dev, int res_id, struct resource *res);
158 
159 /* Debug/emergency function, prints directly to hypervisor console */
160 void xc_printf(const char *, ...) __printflike(1, 2);
161 
162 #ifndef xen_mb
163 #define xen_mb() mb()
164 #endif
165 #ifndef xen_rmb
166 #define xen_rmb() rmb()
167 #endif
168 #ifndef xen_wmb
169 #define xen_wmb() wmb()
170 #endif
171 
172 #endif /* !__ASSEMBLY__ */
173 
174 #endif /* _XEN_XEN_OS_H_ */
175