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 ASSERT(redir_index < IOAPIC_NUM_PINS);
78 redir_content = ioapic->redirtbl[redir_index].bits;
79 result = (ioapic->ioregsel & 0x1) ?
80 (redir_content >> 32) & 0xffffffff :
81 redir_content & 0xffffffff;
89 static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
91 union kvm_ioapic_redirect_entry *pent;
94 pent = &ioapic->redirtbl[idx];
96 if (!pent->fields.mask) {
97 injected = ioapic_deliver(ioapic, idx);
98 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
99 pent->fields.remote_irr = 1;
105 static void update_handled_vectors(struct kvm_ioapic *ioapic)
107 DECLARE_BITMAP(handled_vectors, 256);
110 memset(handled_vectors, 0, sizeof(handled_vectors));
111 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
112 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
113 memcpy(ioapic->handled_vectors, handled_vectors,
114 sizeof(handled_vectors));
118 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
121 bool mask_before, mask_after;
122 union kvm_ioapic_redirect_entry *e;
124 switch (ioapic->ioregsel) {
125 case IOAPIC_REG_VERSION:
126 /* Writes are ignored. */
129 case IOAPIC_REG_APIC_ID:
130 ioapic->id = (val >> 24) & 0xf;
133 case IOAPIC_REG_ARB_ID:
137 index = (ioapic->ioregsel - 0x10) >> 1;
139 ioapic_debug("change redir index %x val %x\n", index, val);
140 if (index >= IOAPIC_NUM_PINS)
142 e = &ioapic->redirtbl[index];
143 mask_before = e->fields.mask;
144 if (ioapic->ioregsel & 1) {
145 e->bits &= 0xffffffff;
146 e->bits |= (u64) val << 32;
148 e->bits &= ~0xffffffffULL;
149 e->bits |= (u32) val;
150 e->fields.remote_irr = 0;
152 update_handled_vectors(ioapic);
153 mask_after = e->fields.mask;
154 if (mask_before != mask_after)
155 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
156 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
157 && ioapic->irr & (1 << index))
158 ioapic_service(ioapic, index);
163 static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
165 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
166 struct kvm_lapic_irq irqe;
168 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
169 "vector=%x trig_mode=%x\n",
170 entry->fields.dest_id, entry->fields.dest_mode,
171 entry->fields.delivery_mode, entry->fields.vector,
172 entry->fields.trig_mode);
174 irqe.dest_id = entry->fields.dest_id;
175 irqe.vector = entry->fields.vector;
176 irqe.dest_mode = entry->fields.dest_mode;
177 irqe.trig_mode = entry->fields.trig_mode;
178 irqe.delivery_mode = entry->fields.delivery_mode << 8;
182 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
185 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
190 union kvm_ioapic_redirect_entry entry;
193 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
195 spin_lock(&ioapic->lock);
196 old_irr = ioapic->irr;
197 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
198 irq_source_id, level);
199 entry = ioapic->redirtbl[irq];
200 irq_level ^= entry.fields.polarity;
202 ioapic->irr &= ~mask;
205 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
207 if ((edge && old_irr != ioapic->irr) ||
208 (!edge && !entry.fields.remote_irr))
209 ret = ioapic_service(ioapic, irq);
211 ret = 0; /* report coalesced interrupt */
213 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
214 spin_unlock(&ioapic->lock);
219 void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
223 spin_lock(&ioapic->lock);
224 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
225 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
226 spin_unlock(&ioapic->lock);
229 static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
234 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
235 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
237 if (ent->fields.vector != vector)
241 * We are dropping lock while calling ack notifiers because ack
242 * notifier callbacks for assigned devices call into IOAPIC
243 * recursively. Since remote_irr is cleared only after call
244 * to notifiers if the same vector will be delivered while lock
245 * is dropped it will be put into irr and will be delivered
246 * after ack notifier returns.
248 spin_unlock(&ioapic->lock);
249 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
250 spin_lock(&ioapic->lock);
252 if (trigger_mode != IOAPIC_LEVEL_TRIG)
255 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
256 ent->fields.remote_irr = 0;
257 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
258 ioapic_service(ioapic, i);
262 bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
264 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
266 return test_bit(vector, ioapic->handled_vectors);
269 void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
271 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
273 spin_lock(&ioapic->lock);
274 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
275 spin_unlock(&ioapic->lock);
278 static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
280 return container_of(dev, struct kvm_ioapic, dev);
283 static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
285 return ((addr >= ioapic->base_address &&
286 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
289 static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
292 struct kvm_ioapic *ioapic = to_ioapic(this);
294 if (!ioapic_in_range(ioapic, addr))
297 ioapic_debug("addr %lx\n", (unsigned long)addr);
298 ASSERT(!(addr & 0xf)); /* check alignment */
301 spin_lock(&ioapic->lock);
303 case IOAPIC_REG_SELECT:
304 result = ioapic->ioregsel;
307 case IOAPIC_REG_WINDOW:
308 result = ioapic_read_indirect(ioapic, addr, len);
315 spin_unlock(&ioapic->lock);
319 *(u64 *) val = result;
324 memcpy(val, (char *)&result, len);
327 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
332 static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
335 struct kvm_ioapic *ioapic = to_ioapic(this);
337 if (!ioapic_in_range(ioapic, addr))
340 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
341 (void*)addr, len, val);
342 ASSERT(!(addr & 0xf)); /* check alignment */
356 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
361 spin_lock(&ioapic->lock);
363 case IOAPIC_REG_SELECT:
364 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
367 case IOAPIC_REG_WINDOW:
368 ioapic_write_indirect(ioapic, data);
372 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
379 spin_unlock(&ioapic->lock);
383 void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
387 for (i = 0; i < IOAPIC_NUM_PINS; i++)
388 ioapic->redirtbl[i].fields.mask = 1;
389 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
390 ioapic->ioregsel = 0;
393 update_handled_vectors(ioapic);
396 static const struct kvm_io_device_ops ioapic_mmio_ops = {
397 .read = ioapic_mmio_read,
398 .write = ioapic_mmio_write,
401 int kvm_ioapic_init(struct kvm *kvm)
403 struct kvm_ioapic *ioapic;
406 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
409 spin_lock_init(&ioapic->lock);
410 kvm->arch.vioapic = ioapic;
411 kvm_ioapic_reset(ioapic);
412 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
414 mutex_lock(&kvm->slots_lock);
415 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
416 IOAPIC_MEM_LENGTH, &ioapic->dev);
417 mutex_unlock(&kvm->slots_lock);
419 kvm->arch.vioapic = NULL;
426 void kvm_ioapic_destroy(struct kvm *kvm)
428 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
431 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
432 kvm->arch.vioapic = NULL;
437 int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
439 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
443 spin_lock(&ioapic->lock);
444 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
445 spin_unlock(&ioapic->lock);
449 int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
451 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
455 spin_lock(&ioapic->lock);
456 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
457 update_handled_vectors(ioapic);
458 spin_unlock(&ioapic->lock);