xref: /freebsd/sys/xen/xen-os.h (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
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 <sys/rman.h>
43 
44 #include <xen/hvm.h>
45 #include <contrib/xen/event_channel.h>
46 
47 /*
48  * Setup function which needs to be called on each processor by architecture
49  */
50 extern void xen_setup_vcpu_info(void);
51 
52 static inline vm_paddr_t
53 xen_get_xenstore_mfn(void)
54 {
55 
56 	return (hvm_get_parameter(HVM_PARAM_STORE_PFN));
57 }
58 
59 static inline evtchn_port_t
60 xen_get_xenstore_evtchn(void)
61 {
62 
63 	return (hvm_get_parameter(HVM_PARAM_STORE_EVTCHN));
64 }
65 
66 static inline vm_paddr_t
67 xen_get_console_mfn(void)
68 {
69 
70 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_PFN));
71 }
72 
73 static inline evtchn_port_t
74 xen_get_console_evtchn(void)
75 {
76 
77 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN));
78 }
79 
80 extern shared_info_t *HYPERVISOR_shared_info;
81 
82 extern bool xen_suspend_cancelled;
83 
84 static inline bool
85 xen_domain(void)
86 {
87 	return (vm_guest == VM_GUEST_XEN);
88 }
89 
90 static inline bool
91 xen_pv_domain(void)
92 {
93 	return (false);
94 }
95 
96 static inline bool
97 xen_hvm_domain(void)
98 {
99 	return (vm_guest == VM_GUEST_XEN);
100 }
101 
102 static inline bool
103 xen_initial_domain(void)
104 {
105 
106 	return (xen_domain() && (hvm_start_flags & SIF_INITDOMAIN) != 0);
107 }
108 #endif
109 
110 #include <machine/xen/xen-os.h>
111 
112 /* Everything below this point is not included by assembler (.S) files. */
113 #ifndef __ASSEMBLY__
114 
115 /*
116  * Based on ofed/include/linux/bitops.h
117  *
118  * Those helpers are prefixed by xen_ because xen-os.h is widely included
119  * and we don't want the other drivers using them.
120  *
121  */
122 #define NBPL (NBBY * sizeof(long))
123 
124 static inline bool
125 xen_test_bit(int bit, volatile xen_ulong_t *addr)
126 {
127 	unsigned long mask = 1UL << (bit % NBPL);
128 
129 	return !!(atomic_load_acq_xen_ulong(&addr[bit / NBPL]) & mask);
130 }
131 
132 static inline void
133 xen_set_bit(int bit, volatile xen_ulong_t *addr)
134 {
135 	atomic_set_xen_ulong(&addr[bit / NBPL], 1UL << (bit % NBPL));
136 }
137 
138 static inline void
139 xen_clear_bit(int bit, volatile xen_ulong_t *addr)
140 {
141 	atomic_clear_xen_ulong(&addr[bit / NBPL], 1UL << (bit % NBPL));
142 }
143 
144 #undef NBPL
145 
146 /*
147  * Functions to allocate/free unused memory in order
148  * to map memory from other domains.
149  */
150 struct resource *xenmem_alloc(device_t dev, int *res_id, size_t size);
151 int xenmem_free(device_t dev, int res_id, struct resource *res);
152 
153 /* Debug/emergency function, prints directly to hypervisor console */
154 void xc_printf(const char *, ...) __printflike(1, 2);
155 
156 /*
157  * Emergency print function, can be defined per-arch, otherwise defaults to
158  * HYPERVISOR_console_write.  Should not be called directly, use xc_printf
159  * instead.
160  */
161 void xen_emergency_print(const char *str, size_t size);
162 
163 /* Arch-specific helper to init scratch mapping space. */
164 int xen_arch_init_physmem(device_t dev, struct rman *mem);
165 
166 #ifndef xen_mb
167 #define xen_mb() mb()
168 #endif
169 #ifndef xen_rmb
170 #define xen_rmb() rmb()
171 #endif
172 #ifndef xen_wmb
173 #define xen_wmb() wmb()
174 #endif
175 
176 #endif /* !__ASSEMBLY__ */
177 
178 #endif /* _XEN_XEN_OS_H_ */
179