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 <asm/processor.h>
40 #include <asm/current.h>
41 #include <trace/events/kvm.h>
48 #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
50 #define ioapic_debug(fmt, arg...)
52 static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
54 static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
58 unsigned long result = 0;
60 switch (ioapic->ioregsel) {
61 case IOAPIC_REG_VERSION:
62 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
63 | (IOAPIC_VERSION_ID & 0xff));
66 case IOAPIC_REG_APIC_ID:
67 case IOAPIC_REG_ARB_ID:
68 result = ((ioapic->id & 0xf) << 24);
73 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
76 if (redir_index < IOAPIC_NUM_PINS)
78 ioapic->redirtbl[redir_index].bits;
80 redir_content = ~0ULL;
82 result = (ioapic->ioregsel & 0x1) ?
83 (redir_content >> 32) & 0xffffffff :
84 redir_content & 0xffffffff;
92 static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
94 union kvm_ioapic_redirect_entry *pent;
97 pent = &ioapic->redirtbl[idx];
99 if (!pent->fields.mask) {
100 injected = ioapic_deliver(ioapic, idx);
101 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
102 pent->fields.remote_irr = 1;
108 static void update_handled_vectors(struct kvm_ioapic *ioapic)
110 DECLARE_BITMAP(handled_vectors, 256);
113 memset(handled_vectors, 0, sizeof(handled_vectors));
114 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
115 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
116 memcpy(ioapic->handled_vectors, handled_vectors,
117 sizeof(handled_vectors));
121 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
124 bool mask_before, mask_after;
125 union kvm_ioapic_redirect_entry *e;
127 switch (ioapic->ioregsel) {
128 case IOAPIC_REG_VERSION:
129 /* Writes are ignored. */
132 case IOAPIC_REG_APIC_ID:
133 ioapic->id = (val >> 24) & 0xf;
136 case IOAPIC_REG_ARB_ID:
140 index = (ioapic->ioregsel - 0x10) >> 1;
142 ioapic_debug("change redir index %x val %x\n", index, val);
143 if (index >= IOAPIC_NUM_PINS)
145 e = &ioapic->redirtbl[index];
146 mask_before = e->fields.mask;
147 if (ioapic->ioregsel & 1) {
148 e->bits &= 0xffffffff;
149 e->bits |= (u64) val << 32;
151 e->bits &= ~0xffffffffULL;
152 e->bits |= (u32) val;
153 e->fields.remote_irr = 0;
155 update_handled_vectors(ioapic);
156 mask_after = e->fields.mask;
157 if (mask_before != mask_after)
158 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
159 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
160 && ioapic->irr & (1 << index))
161 ioapic_service(ioapic, index);
166 static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
168 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
169 struct kvm_lapic_irq irqe;
171 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
172 "vector=%x trig_mode=%x\n",
173 entry->fields.dest_id, entry->fields.dest_mode,
174 entry->fields.delivery_mode, entry->fields.vector,
175 entry->fields.trig_mode);
177 irqe.dest_id = entry->fields.dest_id;
178 irqe.vector = entry->fields.vector;
179 irqe.dest_mode = entry->fields.dest_mode;
180 irqe.trig_mode = entry->fields.trig_mode;
181 irqe.delivery_mode = entry->fields.delivery_mode << 8;
186 /* Always delivery PIT interrupt to vcpu 0 */
188 irqe.dest_mode = 0; /* Physical mode. */
189 /* need to read apic_id from apic regiest since
190 * it can be rewritten */
191 irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
194 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
197 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
201 union kvm_ioapic_redirect_entry entry;
204 spin_lock(&ioapic->lock);
205 old_irr = ioapic->irr;
206 if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
207 entry = ioapic->redirtbl[irq];
208 level ^= entry.fields.polarity;
210 ioapic->irr &= ~mask;
212 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
214 if ((edge && old_irr != ioapic->irr) ||
215 (!edge && !entry.fields.remote_irr))
216 ret = ioapic_service(ioapic, irq);
218 ret = 0; /* report coalesced interrupt */
220 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
222 spin_unlock(&ioapic->lock);
227 static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
232 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
233 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
235 if (ent->fields.vector != vector)
239 * We are dropping lock while calling ack notifiers because ack
240 * notifier callbacks for assigned devices call into IOAPIC
241 * recursively. Since remote_irr is cleared only after call
242 * to notifiers if the same vector will be delivered while lock
243 * is dropped it will be put into irr and will be delivered
244 * after ack notifier returns.
246 spin_unlock(&ioapic->lock);
247 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
248 spin_lock(&ioapic->lock);
250 if (trigger_mode != IOAPIC_LEVEL_TRIG)
253 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
254 ent->fields.remote_irr = 0;
255 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
256 ioapic_service(ioapic, i);
260 void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
262 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
265 if (!test_bit(vector, ioapic->handled_vectors))
267 spin_lock(&ioapic->lock);
268 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
269 spin_unlock(&ioapic->lock);
272 static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
274 return container_of(dev, struct kvm_ioapic, dev);
277 static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
279 return ((addr >= ioapic->base_address &&
280 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
283 static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
286 struct kvm_ioapic *ioapic = to_ioapic(this);
288 if (!ioapic_in_range(ioapic, addr))
291 ioapic_debug("addr %lx\n", (unsigned long)addr);
292 ASSERT(!(addr & 0xf)); /* check alignment */
295 spin_lock(&ioapic->lock);
297 case IOAPIC_REG_SELECT:
298 result = ioapic->ioregsel;
301 case IOAPIC_REG_WINDOW:
302 result = ioapic_read_indirect(ioapic, addr, len);
309 spin_unlock(&ioapic->lock);
313 *(u64 *) val = result;
318 memcpy(val, (char *)&result, len);
321 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
326 static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
329 struct kvm_ioapic *ioapic = to_ioapic(this);
331 if (!ioapic_in_range(ioapic, addr))
334 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
335 (void*)addr, len, val);
336 ASSERT(!(addr & 0xf)); /* check alignment */
350 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
355 spin_lock(&ioapic->lock);
357 case IOAPIC_REG_SELECT:
358 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
361 case IOAPIC_REG_WINDOW:
362 ioapic_write_indirect(ioapic, data);
366 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
373 spin_unlock(&ioapic->lock);
377 void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
381 for (i = 0; i < IOAPIC_NUM_PINS; i++)
382 ioapic->redirtbl[i].fields.mask = 1;
383 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
384 ioapic->ioregsel = 0;
387 update_handled_vectors(ioapic);
390 static const struct kvm_io_device_ops ioapic_mmio_ops = {
391 .read = ioapic_mmio_read,
392 .write = ioapic_mmio_write,
395 int kvm_ioapic_init(struct kvm *kvm)
397 struct kvm_ioapic *ioapic;
400 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
403 spin_lock_init(&ioapic->lock);
404 kvm->arch.vioapic = ioapic;
405 kvm_ioapic_reset(ioapic);
406 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
408 mutex_lock(&kvm->slots_lock);
409 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
410 IOAPIC_MEM_LENGTH, &ioapic->dev);
411 mutex_unlock(&kvm->slots_lock);
413 kvm->arch.vioapic = NULL;
420 void kvm_ioapic_destroy(struct kvm *kvm)
422 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
425 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
426 kvm->arch.vioapic = NULL;
431 int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
433 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
437 spin_lock(&ioapic->lock);
438 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
439 spin_unlock(&ioapic->lock);
443 int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
445 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
449 spin_lock(&ioapic->lock);
450 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
451 update_handled_vectors(ioapic);
452 spin_unlock(&ioapic->lock);