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