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 #if !defined(__XEN_INTERFACE_VERSION__)
32 #define __XEN_INTERFACE_VERSION__ 0x00030208
33 #endif
34
35 #define GRANT_REF_INVALID 0xffffffff
36
37 #ifdef LOCORE
38 #define __ASSEMBLY__
39 #endif
40
41 #include <xen/interface/xen.h>
42
43 #ifndef __ASSEMBLY__
44 #include <xen/interface/event_channel.h>
45
46 struct hypervisor_info {
47 vm_paddr_t (*get_xenstore_mfn)(void);
48 evtchn_port_t (*get_xenstore_evtchn)(void);
49 vm_paddr_t (*get_console_mfn)(void);
50 evtchn_port_t (*get_console_evtchn)(void);
51 uint32_t (*get_start_flags)(void);
52 };
53 extern struct hypervisor_info hypervisor_info;
54
55 static inline vm_paddr_t
xen_get_xenstore_mfn(void)56 xen_get_xenstore_mfn(void)
57 {
58
59 return (hypervisor_info.get_xenstore_mfn());
60 }
61
62 static inline evtchn_port_t
xen_get_xenstore_evtchn(void)63 xen_get_xenstore_evtchn(void)
64 {
65
66 return (hypervisor_info.get_xenstore_evtchn());
67 }
68
69 static inline vm_paddr_t
xen_get_console_mfn(void)70 xen_get_console_mfn(void)
71 {
72
73 return (hypervisor_info.get_console_mfn());
74 }
75
76 static inline evtchn_port_t
xen_get_console_evtchn(void)77 xen_get_console_evtchn(void)
78 {
79
80 return (hypervisor_info.get_console_evtchn());
81 }
82
83 static inline uint32_t
xen_get_start_flags(void)84 xen_get_start_flags(void)
85 {
86
87 return (hypervisor_info.get_start_flags());
88 }
89 #endif
90
91 #include <machine/xen/xen-os.h>
92
93 /* Everything below this point is not included by assembler (.S) files. */
94 #ifndef __ASSEMBLY__
95
96 extern shared_info_t *HYPERVISOR_shared_info;
97
98 extern int xen_disable_pv_disks;
99 extern int xen_disable_pv_nics;
100
101 extern bool xen_suspend_cancelled;
102
103 enum xen_domain_type {
104 XEN_NATIVE, /* running on bare hardware */
105 XEN_PV_DOMAIN, /* running in a PV domain */
106 XEN_HVM_DOMAIN, /* running in a Xen hvm domain */
107 };
108
109 extern enum xen_domain_type xen_domain_type;
110
111 static inline int
xen_domain(void)112 xen_domain(void)
113 {
114 return (xen_domain_type != XEN_NATIVE);
115 }
116
117 static inline int
xen_pv_domain(void)118 xen_pv_domain(void)
119 {
120 return (xen_domain_type == XEN_PV_DOMAIN);
121 }
122
123 static inline int
xen_hvm_domain(void)124 xen_hvm_domain(void)
125 {
126 return (xen_domain_type == XEN_HVM_DOMAIN);
127 }
128
129 static inline bool
xen_initial_domain(void)130 xen_initial_domain(void)
131 {
132
133 return (xen_domain() && (xen_get_start_flags() & SIF_INITDOMAIN) != 0);
134 }
135
136 /*
137 * Based on ofed/include/linux/bitops.h
138 *
139 * Those helpers are prefixed by xen_ because xen-os.h is widely included
140 * and we don't want the other drivers using them.
141 *
142 */
143 #define NBPL (NBBY * sizeof(long))
144
145 static inline bool
xen_test_bit(int bit,volatile long * addr)146 xen_test_bit(int bit, volatile long *addr)
147 {
148 unsigned long mask = 1UL << (bit % NBPL);
149
150 return !!(atomic_load_acq_long(&addr[bit / NBPL]) & mask);
151 }
152
153 static inline void
xen_set_bit(int bit,volatile long * addr)154 xen_set_bit(int bit, volatile long *addr)
155 {
156 atomic_set_long(&addr[bit / NBPL], 1UL << (bit % NBPL));
157 }
158
159 static inline void
xen_clear_bit(int bit,volatile long * addr)160 xen_clear_bit(int bit, volatile long *addr)
161 {
162 atomic_clear_long(&addr[bit / NBPL], 1UL << (bit % NBPL));
163 }
164
165 #undef NBPL
166
167 /*
168 * Functions to allocate/free unused memory in order
169 * to map memory from other domains.
170 */
171 struct resource *xenmem_alloc(device_t dev, int *res_id, size_t size);
172 int xenmem_free(device_t dev, int res_id, struct resource *res);
173
174 /* Debug/emergency function, prints directly to hypervisor console */
175 void xc_printf(const char *, ...) __printflike(1, 2);
176
177 #ifndef xen_mb
178 #define xen_mb() mb()
179 #endif
180 #ifndef xen_rmb
181 #define xen_rmb() rmb()
182 #endif
183 #ifndef xen_wmb
184 #define xen_wmb() wmb()
185 #endif
186
187 #endif /* !__ASSEMBLY__ */
188
189 #endif /* _XEN_XEN_OS_H_ */
190