]> rtime.felk.cvut.cz Git - jailhouse.git/blob - inmates/lib/x86/inmate.h
inmates: Add PCI services to inmates framework
[jailhouse.git] / inmates / lib / x86 / inmate.h
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2013
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 #define HEAP_BASE               0x000000
14 #define FSEGMENT_BASE           0x0f0000
15 #define COMM_REGION_BASE        0x100000
16
17 #define INMATE_CS32             0x8
18 #define INMATE_CS64             0x10
19 #define INMATE_DS32             0x18
20
21 #define NS_PER_MSEC             1000000UL
22 #define NS_PER_SEC              1000000000UL
23
24 #define PAGE_SIZE               (4 * 1024ULL)
25 #ifdef __x86_64__
26 #define HUGE_PAGE_SIZE          (2 * 1024 * 1024ULL)
27 #else
28 #define HUGE_PAGE_SIZE          (4 * 1024 * 1024ULL)
29 #endif
30 #define PAGE_MASK               (~(PAGE_SIZE - 1))
31 #define HUGE_PAGE_MASK          (~(HUGE_PAGE_SIZE - 1))
32
33 #define X2APIC_ID               0x802
34
35 #define PCI_CFG_VENDOR_ID       0x000
36 #define PCI_CFG_DEVICE_ID       0x002
37 #define PCI_CFG_COMMAND         0x004
38 # define PCI_CMD_IO             (1 << 0)
39 # define PCI_CMD_MEM            (1 << 1)
40 # define PCI_CMD_MASTER         (1 << 2)
41 # define PCI_CMD_INTX_OFF       (1 << 10)
42 #define PCI_CFG_STATUS          0x006
43 # define PCI_STS_INT            (1 << 3)
44 #define PCI_CFG_BAR             0x010
45 #define PCI_CFG_CAP_PTR         0x034
46
47 #define PCI_ID_ANY              0xffff
48
49 #define PCI_CAP_MSI             0x05
50
51 #ifndef __ASSEMBLY__
52 typedef signed char s8;
53 typedef unsigned char u8;
54
55 typedef signed short s16;
56 typedef unsigned short u16;
57
58 typedef signed int s32;
59 typedef unsigned int u32;
60
61 typedef signed long long s64;
62 typedef unsigned long long u64;
63
64 typedef s8 __s8;
65 typedef u8 __u8;
66
67 typedef s16 __s16;
68 typedef u16 __u16;
69
70 typedef s32 __s32;
71 typedef u32 __u32;
72
73 typedef s64 __s64;
74 typedef u64 __u64;
75
76 typedef enum { true=1, false=0 } bool;
77
78 static inline void cpu_relax(void)
79 {
80         asm volatile("rep; nop" : : : "memory");
81 }
82
83 static inline void outb(u8 v, u16 port)
84 {
85         asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
86 }
87
88 static inline void outw(u16 v, u16 port)
89 {
90         asm volatile("outw %0,%1" : : "a" (v), "dN" (port));
91 }
92
93 static inline void outl(u32 v, u16 port)
94 {
95         asm volatile("outl %0,%1" : : "a" (v), "dN" (port));
96 }
97
98 static inline u8 inb(u16 port)
99 {
100         u8 v;
101         asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
102         return v;
103 }
104
105 static inline u16 inw(u16 port)
106 {
107         u16 v;
108         asm volatile("inw %1,%0" : "=a" (v) : "dN" (port));
109         return v;
110 }
111
112 static inline u32 inl(u16 port)
113 {
114         u32 v;
115         asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
116         return v;
117 }
118
119 static inline u8 mmio_read8(void *address)
120 {
121         return *(volatile u8 *)address;
122 }
123
124 static inline u16 mmio_read16(void *address)
125 {
126         return *(volatile u16 *)address;
127 }
128
129 static inline u32 mmio_read32(void *address)
130 {
131         u32 value;
132
133         /* assembly-encoded to match the hypervisor MMIO parser support */
134         asm volatile("movl (%1),%0" : "=r" (value) : "r" (address));
135         return value;
136 }
137
138 static inline u64 mmio_read64(void *address)
139 {
140         return *(volatile u64 *)address;
141 }
142
143 static inline void mmio_write8(void *address, u8 value)
144 {
145         *(volatile u8 *)address = value;
146 }
147
148 static inline void mmio_write16(void *address, u16 value)
149 {
150         *(volatile u16 *)address = value;
151 }
152
153 static inline void mmio_write32(void *address, u32 value)
154 {
155         /* assembly-encoded to match the hypervisor MMIO parser support */
156         asm volatile("movl %0,(%1)" : : "r" (value), "r" (address));
157 }
158
159 static inline void mmio_write64(void *address, u64 value)
160 {
161         *(volatile u64 *)address = value;
162 }
163
164 static inline u64 read_msr(unsigned int msr)
165 {
166         u32 low, high;
167
168         asm volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
169         return low | ((u64)high << 32);
170 }
171
172 static inline void write_msr(unsigned int msr, u64 val)
173 {
174         asm volatile("wrmsr"
175                 : /* no output */
176                 : "c" (msr), "a" (val), "d" (val >> 32)
177                 : "memory");
178 }
179
180 static inline unsigned int cpu_id(void)
181 {
182         return read_msr(X2APIC_ID);
183 }
184
185 #include <jailhouse/hypercall.h>
186
187 #define comm_region     ((struct jailhouse_comm_region *)COMM_REGION_BASE)
188
189 extern unsigned int printk_uart_base;
190 void printk(const char *fmt, ...);
191
192 void *memset(void *s, int c, unsigned long n);
193
194 typedef void(*int_handler_t)(void);
195
196 void int_init(void);
197 void int_set_handler(unsigned int vector, int_handler_t handler);
198
199 enum ioapic_trigger_mode {
200         TRIGGER_EDGE = 0,
201         TRIGGER_LEVEL_ACTIVE_HIGH = 1 << 15,
202         TRIGGER_LEVEL_ACTIVE_LOW = (1 << 15) | (1 << 13),
203 };
204
205 void ioapic_init(void);
206 void ioapic_pin_set_vector(unsigned int pin,
207                            enum ioapic_trigger_mode trigger_mode,
208                            unsigned int vector);
209
210 void inmate_main(void);
211
212 unsigned long pm_timer_read(void);
213 unsigned long apic_timer_init(unsigned int vector);
214 void apic_timer_set(unsigned long timeout_ns);
215
216 enum map_type { MAP_CACHED, MAP_UNCACHED };
217
218 void *alloc(unsigned long size, unsigned long align);
219 void map_range(void *start, unsigned long size, enum map_type map_type);
220
221 u32 pci_read_config(u16 bdf, unsigned int addr, unsigned int size);
222 void pci_write_config(u16 bdf, unsigned int addr, u32 value,
223                       unsigned int size);
224 int pci_find_device(u16 vendor, u16 device);
225 int pci_find_cap(u16 bdf, u16 cap);
226 void pci_msi_set_vector(u16 bdf, unsigned int vector);
227 #endif