2 * Copyright (C) 2001 MandrakeSoft S.A.
7 * http://www.linux-mandrake.com/
8 * http://www.mandrakesoft.com/
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.
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.
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
24 * Yunhong Jiang <yunhong.jiang@intel.com>
25 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
26 * Based on Xen 3.1 code.
30 #include <linux/kvm.h>
32 #include <linux/highmem.h>
33 #include <linux/smp.h>
34 #include <linux/hrtimer.h>
36 #include <asm/processor.h>
39 #include <asm/current.h>
40 #include <asm/apicdef.h>
41 #include <asm/io_apic.h>
43 /* #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
44 #define ioapic_debug(fmt, arg...)
45 static void ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
47 static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
51 unsigned long result = 0;
53 switch (ioapic->ioregsel) {
54 case IOAPIC_REG_VERSION:
55 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
56 | (IOAPIC_VERSION_ID & 0xff));
59 case IOAPIC_REG_APIC_ID:
60 case IOAPIC_REG_ARB_ID:
61 result = ((ioapic->id & 0xf) << 24);
66 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
69 ASSERT(redir_index < IOAPIC_NUM_PINS);
71 redir_content = ioapic->redirtbl[redir_index].bits;
72 result = (ioapic->ioregsel & 0x1) ?
73 (redir_content >> 32) & 0xffffffff :
74 redir_content & 0xffffffff;
82 static void ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
84 union ioapic_redir_entry *pent;
86 pent = &ioapic->redirtbl[idx];
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;
93 if (!pent->fields.trig_mode)
94 ioapic->irr &= ~(1 << idx);
97 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
101 switch (ioapic->ioregsel) {
102 case IOAPIC_REG_VERSION:
103 /* Writes are ignored. */
106 case IOAPIC_REG_APIC_ID:
107 ioapic->id = (val >> 24) & 0xf;
110 case IOAPIC_REG_ARB_ID:
114 index = (ioapic->ioregsel - 0x10) >> 1;
116 ioapic_debug("change redir index %x val %x", index, val);
117 if (index >= IOAPIC_NUM_PINS)
119 if (ioapic->ioregsel & 1) {
120 ioapic->redirtbl[index].bits &= 0xffffffff;
121 ioapic->redirtbl[index].bits |= (u64) val << 32;
123 ioapic->redirtbl[index].bits &= ~0xffffffffULL;
124 ioapic->redirtbl[index].bits |= (u32) val;
125 ioapic->redirtbl[index].fields.remote_irr = 0;
127 if (ioapic->irr & (1 << index))
128 ioapic_service(ioapic, index);
133 static void ioapic_inj_irq(struct kvm_ioapic *ioapic,
134 struct kvm_lapic *target,
135 u8 vector, u8 trig_mode, u8 delivery_mode)
137 ioapic_debug("irq %d trig %d deliv %d", vector, trig_mode,
140 ASSERT((delivery_mode == dest_Fixed) ||
141 (delivery_mode == dest_LowestPrio));
143 kvm_apic_set_irq(target, vector, trig_mode);
146 static u32 ioapic_get_delivery_bitmask(struct kvm_ioapic *ioapic, u8 dest,
151 struct kvm *kvm = ioapic->kvm;
152 struct kvm_vcpu *vcpu;
154 ioapic_debug("dest %d dest_mode %d", dest, dest_mode);
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]->apic)
163 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
164 vcpu = kvm->vcpus[i];
167 if (kvm_apic_match_physical_addr(vcpu->apic, dest)) {
173 } else if (dest != 0) /* Logical mode, MDA non-zero. */
174 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
175 vcpu = kvm->vcpus[i];
179 kvm_apic_match_logical_addr(vcpu->apic, dest))
180 mask |= 1 << vcpu->vcpu_id;
182 ioapic_debug("mask %x", mask);
186 static void ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
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;
194 struct kvm_lapic *target;
195 struct kvm_vcpu *vcpu;
198 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
199 "vector=%x trig_mode=%x",
200 dest, dest_mode, delivery_mode, vector, trig_mode);
202 deliver_bitmask = ioapic_get_delivery_bitmask(ioapic, dest, dest_mode);
203 if (!deliver_bitmask) {
204 ioapic_debug("no target on destination");
208 switch (delivery_mode) {
209 case dest_LowestPrio:
211 kvm_apic_round_robin(ioapic->kvm, vector, deliver_bitmask);
213 ioapic_inj_irq(ioapic, target, vector,
214 trig_mode, delivery_mode);
216 ioapic_debug("null round robin: "
217 "mask=%x vector=%x delivery_mode=%x",
218 deliver_bitmask, vector, dest_LowestPrio);
221 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
222 if (!(deliver_bitmask & (1 << vcpu_id)))
224 deliver_bitmask &= ~(1 << vcpu_id);
225 vcpu = ioapic->kvm->vcpus[vcpu_id];
228 ioapic_inj_irq(ioapic, target, vector,
229 trig_mode, delivery_mode);
236 printk(KERN_WARNING "Unsupported delivery mode %d\n",
242 void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
244 u32 old_irr = ioapic->irr;
246 union ioapic_redir_entry entry;
248 if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
249 entry = ioapic->redirtbl[irq];
250 level ^= entry.fields.polarity;
252 ioapic->irr &= ~mask;
255 if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
256 || !entry.fields.remote_irr)
257 ioapic_service(ioapic, irq);
262 static int get_eoi_gsi(struct kvm_ioapic *ioapic, int vector)
266 for (i = 0; i < IOAPIC_NUM_PINS; i++)
267 if (ioapic->redirtbl[i].fields.vector == vector)
272 void kvm_ioapic_update_eoi(struct kvm *kvm, int vector)
274 struct kvm_ioapic *ioapic = kvm->vioapic;
275 union ioapic_redir_entry *ent;
278 gsi = get_eoi_gsi(ioapic, vector);
280 printk(KERN_WARNING "Can't find redir item for %d EOI\n",
285 ent = &ioapic->redirtbl[gsi];
286 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
288 ent->fields.remote_irr = 0;
289 if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
290 ioapic_deliver(ioapic, gsi);
293 static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr)
295 struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
297 return ((addr >= ioapic->base_address &&
298 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
301 static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
304 struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
307 ioapic_debug("addr %lx", (unsigned long)addr);
308 ASSERT(!(addr & 0xf)); /* check alignment */
312 case IOAPIC_REG_SELECT:
313 result = ioapic->ioregsel;
316 case IOAPIC_REG_WINDOW:
317 result = ioapic_read_indirect(ioapic, addr, len);
326 *(u64 *) val = result;
331 memcpy(val, (char *)&result, len);
334 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
338 static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
341 struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
344 ioapic_debug("ioapic_mmio_write addr=%lx len=%d val=%p\n",
346 ASSERT(!(addr & 0xf)); /* check alignment */
347 if (len == 4 || len == 8)
350 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
356 case IOAPIC_REG_SELECT:
357 ioapic->ioregsel = data;
360 case IOAPIC_REG_WINDOW:
361 ioapic_write_indirect(ioapic, data);
369 int kvm_ioapic_init(struct kvm *kvm)
371 struct kvm_ioapic *ioapic;
374 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
377 kvm->vioapic = ioapic;
378 for (i = 0; i < IOAPIC_NUM_PINS; i++)
379 ioapic->redirtbl[i].fields.mask = 1;
380 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
381 ioapic->dev.read = ioapic_mmio_read;
382 ioapic->dev.write = ioapic_mmio_write;
383 ioapic->dev.in_range = ioapic_in_range;
384 ioapic->dev.private = ioapic;
386 kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);