2 * Copyright (C) 2001 MandrakeSoft S.A.
3 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
8 * http://www.linux-mandrake.com/
9 * http://www.mandrakesoft.com/
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Yunhong Jiang <yunhong.jiang@intel.com>
26 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
27 * Based on Xen 3.1 code.
30 #include <linux/kvm_host.h>
31 #include <linux/kvm.h>
33 #include <linux/highmem.h>
34 #include <linux/smp.h>
35 #include <linux/hrtimer.h>
37 #include <linux/slab.h>
38 #include <linux/export.h>
39 #include <asm/processor.h>
41 #include <asm/current.h>
42 #include <trace/events/kvm.h>
49 #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
51 #define ioapic_debug(fmt, arg...)
53 static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
55 static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
59 unsigned long result = 0;
61 switch (ioapic->ioregsel) {
62 case IOAPIC_REG_VERSION:
63 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
64 | (IOAPIC_VERSION_ID & 0xff));
67 case IOAPIC_REG_APIC_ID:
68 case IOAPIC_REG_ARB_ID:
69 result = ((ioapic->id & 0xf) << 24);
74 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
77 if (redir_index < IOAPIC_NUM_PINS)
79 ioapic->redirtbl[redir_index].bits;
81 redir_content = ~0ULL;
83 result = (ioapic->ioregsel & 0x1) ?
84 (redir_content >> 32) & 0xffffffff :
85 redir_content & 0xffffffff;
93 static void rtc_irq_eoi_tracking_reset(struct kvm_ioapic *ioapic)
95 ioapic->rtc_status.pending_eoi = 0;
96 bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
99 static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
101 bool new_val, old_val;
102 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
103 union kvm_ioapic_redirect_entry *e;
105 e = &ioapic->redirtbl[RTC_GSI];
106 if (!kvm_apic_match_dest(vcpu, NULL, 0, e->fields.dest_id,
107 e->fields.dest_mode))
110 new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector);
111 old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
113 if (new_val == old_val)
117 __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
118 ioapic->rtc_status.pending_eoi++;
120 __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
121 ioapic->rtc_status.pending_eoi--;
124 WARN_ON(ioapic->rtc_status.pending_eoi < 0);
127 void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
129 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
131 spin_lock(&ioapic->lock);
132 __rtc_irq_eoi_tracking_restore_one(vcpu);
133 spin_unlock(&ioapic->lock);
136 static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)
138 struct kvm_vcpu *vcpu;
141 if (RTC_GSI >= IOAPIC_NUM_PINS)
144 rtc_irq_eoi_tracking_reset(ioapic);
145 kvm_for_each_vcpu(i, vcpu, ioapic->kvm)
146 __rtc_irq_eoi_tracking_restore_one(vcpu);
149 static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
151 union kvm_ioapic_redirect_entry *pent;
154 pent = &ioapic->redirtbl[idx];
156 if (!pent->fields.mask) {
157 injected = ioapic_deliver(ioapic, idx);
158 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
159 pent->fields.remote_irr = 1;
165 static void update_handled_vectors(struct kvm_ioapic *ioapic)
167 DECLARE_BITMAP(handled_vectors, 256);
170 memset(handled_vectors, 0, sizeof(handled_vectors));
171 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
172 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
173 memcpy(ioapic->handled_vectors, handled_vectors,
174 sizeof(handled_vectors));
178 void kvm_ioapic_calculate_eoi_exitmap(struct kvm_vcpu *vcpu,
179 u64 *eoi_exit_bitmap)
181 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
182 union kvm_ioapic_redirect_entry *e;
185 spin_lock(&ioapic->lock);
186 /* traverse ioapic entry to set eoi exit bitmap*/
187 for (index = 0; index < IOAPIC_NUM_PINS; index++) {
188 e = &ioapic->redirtbl[index];
189 if (!e->fields.mask &&
190 (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
191 kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC,
193 if (kvm_apic_match_dest(vcpu, NULL, 0,
194 e->fields.dest_id, e->fields.dest_mode))
195 __set_bit(e->fields.vector, (unsigned long *)eoi_exit_bitmap);
198 spin_unlock(&ioapic->lock);
200 EXPORT_SYMBOL_GPL(kvm_ioapic_calculate_eoi_exitmap);
202 void kvm_ioapic_make_eoibitmap_request(struct kvm *kvm)
204 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
206 if (!kvm_apic_vid_enabled(kvm) || !ioapic)
208 kvm_make_update_eoibitmap_request(kvm);
211 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
214 bool mask_before, mask_after;
215 union kvm_ioapic_redirect_entry *e;
217 switch (ioapic->ioregsel) {
218 case IOAPIC_REG_VERSION:
219 /* Writes are ignored. */
222 case IOAPIC_REG_APIC_ID:
223 ioapic->id = (val >> 24) & 0xf;
226 case IOAPIC_REG_ARB_ID:
230 index = (ioapic->ioregsel - 0x10) >> 1;
232 ioapic_debug("change redir index %x val %x\n", index, val);
233 if (index >= IOAPIC_NUM_PINS)
235 e = &ioapic->redirtbl[index];
236 mask_before = e->fields.mask;
237 if (ioapic->ioregsel & 1) {
238 e->bits &= 0xffffffff;
239 e->bits |= (u64) val << 32;
241 e->bits &= ~0xffffffffULL;
242 e->bits |= (u32) val;
243 e->fields.remote_irr = 0;
245 update_handled_vectors(ioapic);
246 mask_after = e->fields.mask;
247 if (mask_before != mask_after)
248 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
249 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
250 && ioapic->irr & (1 << index))
251 ioapic_service(ioapic, index);
252 kvm_ioapic_make_eoibitmap_request(ioapic->kvm);
257 static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
259 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
260 struct kvm_lapic_irq irqe;
262 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
263 "vector=%x trig_mode=%x\n",
264 entry->fields.dest_id, entry->fields.dest_mode,
265 entry->fields.delivery_mode, entry->fields.vector,
266 entry->fields.trig_mode);
268 irqe.dest_id = entry->fields.dest_id;
269 irqe.vector = entry->fields.vector;
270 irqe.dest_mode = entry->fields.dest_mode;
271 irqe.trig_mode = entry->fields.trig_mode;
272 irqe.delivery_mode = entry->fields.delivery_mode << 8;
276 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
279 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
284 union kvm_ioapic_redirect_entry entry;
287 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
289 spin_lock(&ioapic->lock);
290 old_irr = ioapic->irr;
291 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
292 irq_source_id, level);
293 entry = ioapic->redirtbl[irq];
294 irq_level ^= entry.fields.polarity;
296 ioapic->irr &= ~mask;
299 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
301 if ((edge && old_irr != ioapic->irr) ||
302 (!edge && !entry.fields.remote_irr))
303 ret = ioapic_service(ioapic, irq);
305 ret = 0; /* report coalesced interrupt */
307 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
308 spin_unlock(&ioapic->lock);
313 void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
317 spin_lock(&ioapic->lock);
318 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
319 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
320 spin_unlock(&ioapic->lock);
323 static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
324 struct kvm_ioapic *ioapic, int vector, int trigger_mode)
328 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
329 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
331 if (ent->fields.vector != vector)
335 * We are dropping lock while calling ack notifiers because ack
336 * notifier callbacks for assigned devices call into IOAPIC
337 * recursively. Since remote_irr is cleared only after call
338 * to notifiers if the same vector will be delivered while lock
339 * is dropped it will be put into irr and will be delivered
340 * after ack notifier returns.
342 spin_unlock(&ioapic->lock);
343 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
344 spin_lock(&ioapic->lock);
346 if (trigger_mode != IOAPIC_LEVEL_TRIG)
349 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
350 ent->fields.remote_irr = 0;
351 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
352 ioapic_service(ioapic, i);
356 bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
358 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
360 return test_bit(vector, ioapic->handled_vectors);
363 void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
365 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
367 spin_lock(&ioapic->lock);
368 __kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode);
369 spin_unlock(&ioapic->lock);
372 static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
374 return container_of(dev, struct kvm_ioapic, dev);
377 static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
379 return ((addr >= ioapic->base_address &&
380 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
383 static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
386 struct kvm_ioapic *ioapic = to_ioapic(this);
388 if (!ioapic_in_range(ioapic, addr))
391 ioapic_debug("addr %lx\n", (unsigned long)addr);
392 ASSERT(!(addr & 0xf)); /* check alignment */
395 spin_lock(&ioapic->lock);
397 case IOAPIC_REG_SELECT:
398 result = ioapic->ioregsel;
401 case IOAPIC_REG_WINDOW:
402 result = ioapic_read_indirect(ioapic, addr, len);
409 spin_unlock(&ioapic->lock);
413 *(u64 *) val = result;
418 memcpy(val, (char *)&result, len);
421 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
426 static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
429 struct kvm_ioapic *ioapic = to_ioapic(this);
431 if (!ioapic_in_range(ioapic, addr))
434 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
435 (void*)addr, len, val);
436 ASSERT(!(addr & 0xf)); /* check alignment */
450 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
455 spin_lock(&ioapic->lock);
457 case IOAPIC_REG_SELECT:
458 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
461 case IOAPIC_REG_WINDOW:
462 ioapic_write_indirect(ioapic, data);
466 __kvm_ioapic_update_eoi(NULL, ioapic, data, IOAPIC_LEVEL_TRIG);
473 spin_unlock(&ioapic->lock);
477 void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
481 for (i = 0; i < IOAPIC_NUM_PINS; i++)
482 ioapic->redirtbl[i].fields.mask = 1;
483 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
484 ioapic->ioregsel = 0;
487 rtc_irq_eoi_tracking_reset(ioapic);
488 update_handled_vectors(ioapic);
491 static const struct kvm_io_device_ops ioapic_mmio_ops = {
492 .read = ioapic_mmio_read,
493 .write = ioapic_mmio_write,
496 int kvm_ioapic_init(struct kvm *kvm)
498 struct kvm_ioapic *ioapic;
501 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
504 spin_lock_init(&ioapic->lock);
505 kvm->arch.vioapic = ioapic;
506 kvm_ioapic_reset(ioapic);
507 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
509 mutex_lock(&kvm->slots_lock);
510 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
511 IOAPIC_MEM_LENGTH, &ioapic->dev);
512 mutex_unlock(&kvm->slots_lock);
514 kvm->arch.vioapic = NULL;
521 void kvm_ioapic_destroy(struct kvm *kvm)
523 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
526 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
527 kvm->arch.vioapic = NULL;
532 int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
534 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
538 spin_lock(&ioapic->lock);
539 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
540 spin_unlock(&ioapic->lock);
544 int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
546 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
550 spin_lock(&ioapic->lock);
551 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
552 update_handled_vectors(ioapic);
553 kvm_ioapic_make_eoibitmap_request(kvm);
554 kvm_rtc_eoi_tracking_restore_all(ioapic);
555 spin_unlock(&ioapic->lock);