diff options
author | Avi Kivity <avi@qumranet.com> | 2007-12-16 04:02:48 -0500 |
---|---|---|
committer | Avi Kivity <avi@qumranet.com> | 2008-01-30 11:01:18 -0500 |
commit | edf884172e9828c6234b254208af04655855038d (patch) | |
tree | f5e5d1eecaed9737eced6ba60d09fe93149751c1 /arch/x86/kvm/ioapic.c | |
parent | 9584bf2c93f56656dba0de8f6c75b54ca7995143 (diff) |
KVM: Move arch dependent files to new directory arch/x86/kvm/
This paves the way for multiple architecture support. Note that while
ioapic.c could potentially be shared with ia64, it is also moved.
Signed-off-by: Avi Kivity <avi@qumranet.com>
Diffstat (limited to 'arch/x86/kvm/ioapic.c')
-rw-r--r-- | arch/x86/kvm/ioapic.c | 400 |
1 files changed, 400 insertions, 0 deletions
diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c new file mode 100644 index 000000000000..72f12f75495d --- /dev/null +++ b/arch/x86/kvm/ioapic.c | |||
@@ -0,0 +1,400 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2001 MandrakeSoft S.A. | ||
3 | * | ||
4 | * MandrakeSoft S.A. | ||
5 | * 43, rue d'Aboukir | ||
6 | * 75002 Paris - France | ||
7 | * http://www.linux-mandrake.com/ | ||
8 | * http://www.mandrakesoft.com/ | ||
9 | * | ||
10 | * This library is free software; you can redistribute it and/or | ||
11 | * modify it under the terms of the GNU Lesser General Public | ||
12 | * License as published by the Free Software Foundation; either | ||
13 | * version 2 of the License, or (at your option) any later version. | ||
14 | * | ||
15 | * This library is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * Lesser General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU Lesser General Public | ||
21 | * License along with this library; if not, write to the Free Software | ||
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
23 | * | ||
24 | * Yunhong Jiang <yunhong.jiang@intel.com> | ||
25 | * Yaozu (Eddie) Dong <eddie.dong@intel.com> | ||
26 | * Based on Xen 3.1 code. | ||
27 | */ | ||
28 | |||
29 | #include <linux/kvm_host.h> | ||
30 | #include <linux/kvm.h> | ||
31 | #include <linux/mm.h> | ||
32 | #include <linux/highmem.h> | ||
33 | #include <linux/smp.h> | ||
34 | #include <linux/hrtimer.h> | ||
35 | #include <linux/io.h> | ||
36 | #include <asm/processor.h> | ||
37 | #include <asm/page.h> | ||
38 | #include <asm/current.h> | ||
39 | #include "irq.h" | ||
40 | #if 0 | ||
41 | #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) | ||
42 | #else | ||
43 | #define ioapic_debug(fmt, arg...) | ||
44 | #endif | ||
45 | static void ioapic_deliver(struct kvm_ioapic *vioapic, int irq); | ||
46 | |||
47 | static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, | ||
48 | unsigned long addr, | ||
49 | unsigned long length) | ||
50 | { | ||
51 | unsigned long result = 0; | ||
52 | |||
53 | switch (ioapic->ioregsel) { | ||
54 | case IOAPIC_REG_VERSION: | ||
55 | result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16) | ||
56 | | (IOAPIC_VERSION_ID & 0xff)); | ||
57 | break; | ||
58 | |||
59 | case IOAPIC_REG_APIC_ID: | ||
60 | case IOAPIC_REG_ARB_ID: | ||
61 | result = ((ioapic->id & 0xf) << 24); | ||
62 | break; | ||
63 | |||
64 | default: | ||
65 | { | ||
66 | u32 redir_index = (ioapic->ioregsel - 0x10) >> 1; | ||
67 | u64 redir_content; | ||
68 | |||
69 | ASSERT(redir_index < IOAPIC_NUM_PINS); | ||
70 | |||
71 | redir_content = ioapic->redirtbl[redir_index].bits; | ||
72 | result = (ioapic->ioregsel & 0x1) ? | ||
73 | (redir_content >> 32) & 0xffffffff : | ||
74 | redir_content & 0xffffffff; | ||
75 | break; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | return result; | ||
80 | } | ||
81 | |||
82 | static void ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx) | ||
83 | { | ||
84 | union ioapic_redir_entry *pent; | ||
85 | |||
86 | pent = &ioapic->redirtbl[idx]; | ||
87 | |||
88 | if (!pent->fields.mask) { | ||
89 | ioapic_deliver(ioapic, idx); | ||
90 | if (pent->fields.trig_mode == IOAPIC_LEVEL_TRIG) | ||
91 | pent->fields.remote_irr = 1; | ||
92 | } | ||
93 | if (!pent->fields.trig_mode) | ||
94 | ioapic->irr &= ~(1 << idx); | ||
95 | } | ||
96 | |||
97 | static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val) | ||
98 | { | ||
99 | unsigned index; | ||
100 | |||
101 | switch (ioapic->ioregsel) { | ||
102 | case IOAPIC_REG_VERSION: | ||
103 | /* Writes are ignored. */ | ||
104 | break; | ||
105 | |||
106 | case IOAPIC_REG_APIC_ID: | ||
107 | ioapic->id = (val >> 24) & 0xf; | ||
108 | break; | ||
109 | |||
110 | case IOAPIC_REG_ARB_ID: | ||
111 | break; | ||
112 | |||
113 | default: | ||
114 | index = (ioapic->ioregsel - 0x10) >> 1; | ||
115 | |||
116 | ioapic_debug("change redir index %x val %x\n", index, val); | ||
117 | if (index >= IOAPIC_NUM_PINS) | ||
118 | return; | ||
119 | if (ioapic->ioregsel & 1) { | ||
120 | ioapic->redirtbl[index].bits &= 0xffffffff; | ||
121 | ioapic->redirtbl[index].bits |= (u64) val << 32; | ||
122 | } else { | ||
123 | ioapic->redirtbl[index].bits &= ~0xffffffffULL; | ||
124 | ioapic->redirtbl[index].bits |= (u32) val; | ||
125 | ioapic->redirtbl[index].fields.remote_irr = 0; | ||
126 | } | ||
127 | if (ioapic->irr & (1 << index)) | ||
128 | ioapic_service(ioapic, index); | ||
129 | break; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | static void ioapic_inj_irq(struct kvm_ioapic *ioapic, | ||
134 | struct kvm_vcpu *vcpu, | ||
135 | u8 vector, u8 trig_mode, u8 delivery_mode) | ||
136 | { | ||
137 | ioapic_debug("irq %d trig %d deliv %d\n", vector, trig_mode, | ||
138 | delivery_mode); | ||
139 | |||
140 | ASSERT((delivery_mode == IOAPIC_FIXED) || | ||
141 | (delivery_mode == IOAPIC_LOWEST_PRIORITY)); | ||
142 | |||
143 | kvm_apic_set_irq(vcpu, vector, trig_mode); | ||
144 | } | ||
145 | |||
146 | static u32 ioapic_get_delivery_bitmask(struct kvm_ioapic *ioapic, u8 dest, | ||
147 | u8 dest_mode) | ||
148 | { | ||
149 | u32 mask = 0; | ||
150 | int i; | ||
151 | struct kvm *kvm = ioapic->kvm; | ||
152 | struct kvm_vcpu *vcpu; | ||
153 | |||
154 | ioapic_debug("dest %d dest_mode %d\n", dest, dest_mode); | ||
155 | |||
156 | if (dest_mode == 0) { /* Physical mode. */ | ||
157 | if (dest == 0xFF) { /* Broadcast. */ | ||
158 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | ||
159 | if (kvm->vcpus[i] && kvm->vcpus[i]->arch.apic) | ||
160 | mask |= 1 << i; | ||
161 | return mask; | ||
162 | } | ||
163 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | ||
164 | vcpu = kvm->vcpus[i]; | ||
165 | if (!vcpu) | ||
166 | continue; | ||
167 | if (kvm_apic_match_physical_addr(vcpu->arch.apic, dest)) { | ||
168 | if (vcpu->arch.apic) | ||
169 | mask = 1 << i; | ||
170 | break; | ||
171 | } | ||
172 | } | ||
173 | } else if (dest != 0) /* Logical mode, MDA non-zero. */ | ||
174 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | ||
175 | vcpu = kvm->vcpus[i]; | ||
176 | if (!vcpu) | ||
177 | continue; | ||
178 | if (vcpu->arch.apic && | ||
179 | kvm_apic_match_logical_addr(vcpu->arch.apic, dest)) | ||
180 | mask |= 1 << vcpu->vcpu_id; | ||
181 | } | ||
182 | ioapic_debug("mask %x\n", mask); | ||
183 | return mask; | ||
184 | } | ||
185 | |||
186 | static void ioapic_deliver(struct kvm_ioapic *ioapic, int irq) | ||
187 | { | ||
188 | u8 dest = ioapic->redirtbl[irq].fields.dest_id; | ||
189 | u8 dest_mode = ioapic->redirtbl[irq].fields.dest_mode; | ||
190 | u8 delivery_mode = ioapic->redirtbl[irq].fields.delivery_mode; | ||
191 | u8 vector = ioapic->redirtbl[irq].fields.vector; | ||
192 | u8 trig_mode = ioapic->redirtbl[irq].fields.trig_mode; | ||
193 | u32 deliver_bitmask; | ||
194 | struct kvm_vcpu *vcpu; | ||
195 | int vcpu_id; | ||
196 | |||
197 | ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x " | ||
198 | "vector=%x trig_mode=%x\n", | ||
199 | dest, dest_mode, delivery_mode, vector, trig_mode); | ||
200 | |||
201 | deliver_bitmask = ioapic_get_delivery_bitmask(ioapic, dest, dest_mode); | ||
202 | if (!deliver_bitmask) { | ||
203 | ioapic_debug("no target on destination\n"); | ||
204 | return; | ||
205 | } | ||
206 | |||
207 | switch (delivery_mode) { | ||
208 | case IOAPIC_LOWEST_PRIORITY: | ||
209 | vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector, | ||
210 | deliver_bitmask); | ||
211 | if (vcpu != NULL) | ||
212 | ioapic_inj_irq(ioapic, vcpu, vector, | ||
213 | trig_mode, delivery_mode); | ||
214 | else | ||
215 | ioapic_debug("null lowest prio vcpu: " | ||
216 | "mask=%x vector=%x delivery_mode=%x\n", | ||
217 | deliver_bitmask, vector, IOAPIC_LOWEST_PRIORITY); | ||
218 | break; | ||
219 | case IOAPIC_FIXED: | ||
220 | for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) { | ||
221 | if (!(deliver_bitmask & (1 << vcpu_id))) | ||
222 | continue; | ||
223 | deliver_bitmask &= ~(1 << vcpu_id); | ||
224 | vcpu = ioapic->kvm->vcpus[vcpu_id]; | ||
225 | if (vcpu) { | ||
226 | ioapic_inj_irq(ioapic, vcpu, vector, | ||
227 | trig_mode, delivery_mode); | ||
228 | } | ||
229 | } | ||
230 | break; | ||
231 | |||
232 | /* TODO: NMI */ | ||
233 | default: | ||
234 | printk(KERN_WARNING "Unsupported delivery mode %d\n", | ||
235 | delivery_mode); | ||
236 | break; | ||
237 | } | ||
238 | } | ||
239 | |||
240 | void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level) | ||
241 | { | ||
242 | u32 old_irr = ioapic->irr; | ||
243 | u32 mask = 1 << irq; | ||
244 | union ioapic_redir_entry entry; | ||
245 | |||
246 | if (irq >= 0 && irq < IOAPIC_NUM_PINS) { | ||
247 | entry = ioapic->redirtbl[irq]; | ||
248 | level ^= entry.fields.polarity; | ||
249 | if (!level) | ||
250 | ioapic->irr &= ~mask; | ||
251 | else { | ||
252 | ioapic->irr |= mask; | ||
253 | if ((!entry.fields.trig_mode && old_irr != ioapic->irr) | ||
254 | || !entry.fields.remote_irr) | ||
255 | ioapic_service(ioapic, irq); | ||
256 | } | ||
257 | } | ||
258 | } | ||
259 | |||
260 | static int get_eoi_gsi(struct kvm_ioapic *ioapic, int vector) | ||
261 | { | ||
262 | int i; | ||
263 | |||
264 | for (i = 0; i < IOAPIC_NUM_PINS; i++) | ||
265 | if (ioapic->redirtbl[i].fields.vector == vector) | ||
266 | return i; | ||
267 | return -1; | ||
268 | } | ||
269 | |||
270 | void kvm_ioapic_update_eoi(struct kvm *kvm, int vector) | ||
271 | { | ||
272 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; | ||
273 | union ioapic_redir_entry *ent; | ||
274 | int gsi; | ||
275 | |||
276 | gsi = get_eoi_gsi(ioapic, vector); | ||
277 | if (gsi == -1) { | ||
278 | printk(KERN_WARNING "Can't find redir item for %d EOI\n", | ||
279 | vector); | ||
280 | return; | ||
281 | } | ||
282 | |||
283 | ent = &ioapic->redirtbl[gsi]; | ||
284 | ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG); | ||
285 | |||
286 | ent->fields.remote_irr = 0; | ||
287 | if (!ent->fields.mask && (ioapic->irr & (1 << gsi))) | ||
288 | ioapic_deliver(ioapic, gsi); | ||
289 | } | ||
290 | |||
291 | static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr) | ||
292 | { | ||
293 | struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private; | ||
294 | |||
295 | return ((addr >= ioapic->base_address && | ||
296 | (addr < ioapic->base_address + IOAPIC_MEM_LENGTH))); | ||
297 | } | ||
298 | |||
299 | static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len, | ||
300 | void *val) | ||
301 | { | ||
302 | struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private; | ||
303 | u32 result; | ||
304 | |||
305 | ioapic_debug("addr %lx\n", (unsigned long)addr); | ||
306 | ASSERT(!(addr & 0xf)); /* check alignment */ | ||
307 | |||
308 | addr &= 0xff; | ||
309 | switch (addr) { | ||
310 | case IOAPIC_REG_SELECT: | ||
311 | result = ioapic->ioregsel; | ||
312 | break; | ||
313 | |||
314 | case IOAPIC_REG_WINDOW: | ||
315 | result = ioapic_read_indirect(ioapic, addr, len); | ||
316 | break; | ||
317 | |||
318 | default: | ||
319 | result = 0; | ||
320 | break; | ||
321 | } | ||
322 | switch (len) { | ||
323 | case 8: | ||
324 | *(u64 *) val = result; | ||
325 | break; | ||
326 | case 1: | ||
327 | case 2: | ||
328 | case 4: | ||
329 | memcpy(val, (char *)&result, len); | ||
330 | break; | ||
331 | default: | ||
332 | printk(KERN_WARNING "ioapic: wrong length %d\n", len); | ||
333 | } | ||
334 | } | ||
335 | |||
336 | static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len, | ||
337 | const void *val) | ||
338 | { | ||
339 | struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private; | ||
340 | u32 data; | ||
341 | |||
342 | ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n", | ||
343 | (void*)addr, len, val); | ||
344 | ASSERT(!(addr & 0xf)); /* check alignment */ | ||
345 | if (len == 4 || len == 8) | ||
346 | data = *(u32 *) val; | ||
347 | else { | ||
348 | printk(KERN_WARNING "ioapic: Unsupported size %d\n", len); | ||
349 | return; | ||
350 | } | ||
351 | |||
352 | addr &= 0xff; | ||
353 | switch (addr) { | ||
354 | case IOAPIC_REG_SELECT: | ||
355 | ioapic->ioregsel = data; | ||
356 | break; | ||
357 | |||
358 | case IOAPIC_REG_WINDOW: | ||
359 | ioapic_write_indirect(ioapic, data); | ||
360 | break; | ||
361 | #ifdef CONFIG_IA64 | ||
362 | case IOAPIC_REG_EOI: | ||
363 | kvm_ioapic_update_eoi(ioapic, data); | ||
364 | break; | ||
365 | #endif | ||
366 | |||
367 | default: | ||
368 | break; | ||
369 | } | ||
370 | } | ||
371 | |||
372 | void kvm_ioapic_reset(struct kvm_ioapic *ioapic) | ||
373 | { | ||
374 | int i; | ||
375 | |||
376 | for (i = 0; i < IOAPIC_NUM_PINS; i++) | ||
377 | ioapic->redirtbl[i].fields.mask = 1; | ||
378 | ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS; | ||
379 | ioapic->ioregsel = 0; | ||
380 | ioapic->irr = 0; | ||
381 | ioapic->id = 0; | ||
382 | } | ||
383 | |||
384 | int kvm_ioapic_init(struct kvm *kvm) | ||
385 | { | ||
386 | struct kvm_ioapic *ioapic; | ||
387 | |||
388 | ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL); | ||
389 | if (!ioapic) | ||
390 | return -ENOMEM; | ||
391 | kvm->arch.vioapic = ioapic; | ||
392 | kvm_ioapic_reset(ioapic); | ||
393 | ioapic->dev.read = ioapic_mmio_read; | ||
394 | ioapic->dev.write = ioapic_mmio_write; | ||
395 | ioapic->dev.in_range = ioapic_in_range; | ||
396 | ioapic->dev.private = ioapic; | ||
397 | ioapic->kvm = kvm; | ||
398 | kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev); | ||
399 | return 0; | ||
400 | } | ||