]> rtime.felk.cvut.cz Git - jailhouse.git/blob - inmates/lib/x86/inmate.h
inmates: Avoid jailhouse/types.h
[jailhouse.git] / inmates / lib / x86 / inmate.h
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2013-2016
5  *
6  * Authors:
7  *  Jan Kiszka <jan.kiszka@siemens.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
13 #ifndef _JAILHOUSE_INMATE_H
14 #define _JAILHOUSE_INMATE_H
15
16 #define HEAP_BASE               0x000000
17 #define FSEGMENT_BASE           0x0f0000
18 #define COMM_REGION_BASE        0x100000
19
20 #define INMATE_CS32             0x8
21 #define INMATE_CS64             0x10
22 #define INMATE_DS32             0x18
23
24 #define PAGE_SIZE               (4 * 1024ULL)
25 #ifdef __x86_64__
26 #define BITS_PER_LONG           64
27 #define HUGE_PAGE_SIZE          (2 * 1024 * 1024ULL)
28 #else
29 #define BITS_PER_LONG           32
30 #define HUGE_PAGE_SIZE          (4 * 1024 * 1024ULL)
31 #endif
32 #define PAGE_MASK               (~(PAGE_SIZE - 1))
33 #define HUGE_PAGE_MASK          (~(HUGE_PAGE_SIZE - 1))
34
35 #define X2APIC_ID               0x802
36 #define X2APIC_ICR              0x830
37
38 #define APIC_LVL_ASSERT         (1 << 14)
39
40 #define PCI_CFG_VENDOR_ID       0x000
41 #define PCI_CFG_DEVICE_ID       0x002
42 #define PCI_CFG_COMMAND         0x004
43 # define PCI_CMD_IO             (1 << 0)
44 # define PCI_CMD_MEM            (1 << 1)
45 # define PCI_CMD_MASTER         (1 << 2)
46 # define PCI_CMD_INTX_OFF       (1 << 10)
47 #define PCI_CFG_STATUS          0x006
48 # define PCI_STS_INT            (1 << 3)
49 # define PCI_STS_CAPS           (1 << 4)
50 #define PCI_CFG_BAR             0x010
51 # define PCI_BAR_64BIT          0x4
52 #define PCI_CFG_CAP_PTR         0x034
53
54 #define PCI_ID_ANY              0xffff
55
56 #define PCI_CAP_MSI             0x05
57 #define PCI_CAP_MSIX            0x11
58
59 #define MSIX_CTRL_ENABLE        0x8000
60 #define MSIX_CTRL_FMASK         0x4000
61
62 #define SMP_MAX_CPUS            255
63
64 #ifndef __ASSEMBLY__
65 typedef signed char s8;
66 typedef unsigned char u8;
67
68 typedef signed short s16;
69 typedef unsigned short u16;
70
71 typedef signed int s32;
72 typedef unsigned int u32;
73
74 typedef signed long long s64;
75 typedef unsigned long long u64;
76
77 static inline void cpu_relax(void)
78 {
79         asm volatile("rep; nop" : : : "memory");
80 }
81
82 static inline void outb(u8 v, u16 port)
83 {
84         asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
85 }
86
87 static inline void outw(u16 v, u16 port)
88 {
89         asm volatile("outw %0,%1" : : "a" (v), "dN" (port));
90 }
91
92 static inline void outl(u32 v, u16 port)
93 {
94         asm volatile("outl %0,%1" : : "a" (v), "dN" (port));
95 }
96
97 static inline u8 inb(u16 port)
98 {
99         u8 v;
100         asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
101         return v;
102 }
103
104 static inline u16 inw(u16 port)
105 {
106         u16 v;
107         asm volatile("inw %1,%0" : "=a" (v) : "dN" (port));
108         return v;
109 }
110
111 static inline u32 inl(u16 port)
112 {
113         u32 v;
114         asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
115         return v;
116 }
117
118 static inline u8 mmio_read8(void *address)
119 {
120         return *(volatile u8 *)address;
121 }
122
123 static inline u16 mmio_read16(void *address)
124 {
125         return *(volatile u16 *)address;
126 }
127
128 static inline u32 mmio_read32(void *address)
129 {
130         u32 value;
131
132         /* assembly-encoded to match the hypervisor MMIO parser support */
133         asm volatile("movl (%1),%0" : "=r" (value) : "r" (address));
134         return value;
135 }
136
137 static inline u64 mmio_read64(void *address)
138 {
139         return *(volatile u64 *)address;
140 }
141
142 static inline void mmio_write8(void *address, u8 value)
143 {
144         *(volatile u8 *)address = value;
145 }
146
147 static inline void mmio_write16(void *address, u16 value)
148 {
149         *(volatile u16 *)address = value;
150 }
151
152 static inline void mmio_write32(void *address, u32 value)
153 {
154         /* assembly-encoded to match the hypervisor MMIO parser support */
155         asm volatile("movl %0,(%1)" : : "r" (value), "r" (address));
156 }
157
158 static inline void mmio_write64(void *address, u64 value)
159 {
160         *(volatile u64 *)address = value;
161 }
162
163 static inline u64 read_msr(unsigned int msr)
164 {
165         u32 low, high;
166
167         asm volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
168         return low | ((u64)high << 32);
169 }
170
171 static inline void write_msr(unsigned int msr, u64 val)
172 {
173         asm volatile("wrmsr"
174                 : /* no output */
175                 : "c" (msr), "a" ((u32)val), "d" ((u32)(val >> 32))
176                 : "memory");
177 }
178
179 static inline unsigned int cpu_id(void)
180 {
181         return read_msr(X2APIC_ID);
182 }
183
184 typedef void(*int_handler_t)(void);
185
186 void int_init(void);
187 void int_set_handler(unsigned int vector, int_handler_t handler);
188 void int_send_ipi(unsigned int cpu_id, unsigned int vector);
189
190 enum ioapic_trigger_mode {
191         TRIGGER_EDGE = 0,
192         TRIGGER_LEVEL_ACTIVE_HIGH = 1 << 15,
193         TRIGGER_LEVEL_ACTIVE_LOW = (1 << 15) | (1 << 13),
194 };
195
196 void ioapic_init(void);
197 void ioapic_pin_set_vector(unsigned int pin,
198                            enum ioapic_trigger_mode trigger_mode,
199                            unsigned int vector);
200
201 void hypercall_init(void);
202
203 unsigned long pm_timer_read(void);
204
205 unsigned long tsc_read(void);
206 unsigned long tsc_init(void);
207
208 void delay_us(unsigned long microsecs);
209
210 unsigned long apic_timer_init(unsigned int vector);
211 void apic_timer_set(unsigned long timeout_ns);
212
213 enum map_type { MAP_CACHED, MAP_UNCACHED };
214
215 void *alloc(unsigned long size, unsigned long align);
216 void map_range(void *start, unsigned long size, enum map_type map_type);
217
218 u32 pci_read_config(u16 bdf, unsigned int addr, unsigned int size);
219 void pci_write_config(u16 bdf, unsigned int addr, u32 value,
220                       unsigned int size);
221 int pci_find_device(u16 vendor, u16 device, u16 start_bdf);
222 int pci_find_cap(u16 bdf, u16 cap);
223 void pci_msi_set_vector(u16 bdf, unsigned int vector);
224 void pci_msix_set_vector(u16 bdf, unsigned int vector, u32 index);
225
226 extern volatile u32 smp_num_cpus;
227 extern u8 smp_cpu_ids[SMP_MAX_CPUS];
228 void smp_wait_for_all_cpus(void);
229 void smp_start_cpu(unsigned int cpu_id, void (*entry)(void));
230 #endif
231
232 #include "../inmate_common.h"
233
234 #endif /* !_JAILHOUSE_INMATE_H */