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.
32 #include <linux/kvm.h>
34 #include <linux/highmem.h>
35 #include <linux/smp.h>
36 #include <linux/hrtimer.h>
38 #include <asm/processor.h>
41 #include <asm/current.h>
42 #include <asm/apicdef.h>
43 #include <asm/io_apic.h>
46 #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
48 #define ioapic_debug(fmt, arg...)
50 static void ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
52 static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
56 unsigned long result = 0;
58 switch (ioapic->ioregsel) {
59 case IOAPIC_REG_VERSION:
60 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
61 | (IOAPIC_VERSION_ID & 0xff));
64 case IOAPIC_REG_APIC_ID:
65 case IOAPIC_REG_ARB_ID:
66 result = ((ioapic->id & 0xf) << 24);
71 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
74 ASSERT(redir_index < IOAPIC_NUM_PINS);
76 redir_content = ioapic->redirtbl[redir_index].bits;
77 result = (ioapic->ioregsel & 0x1) ?
78 (redir_content >> 32) & 0xffffffff :
79 redir_content & 0xffffffff;
87 static void ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
89 union ioapic_redir_entry *pent;
91 pent = &ioapic->redirtbl[idx];
93 if (!pent->fields.mask) {
94 ioapic_deliver(ioapic, idx);
95 if (pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
96 pent->fields.remote_irr = 1;
98 if (!pent->fields.trig_mode)
99 ioapic->irr &= ~(1 << idx);
102 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
106 switch (ioapic->ioregsel) {
107 case IOAPIC_REG_VERSION:
108 /* Writes are ignored. */
111 case IOAPIC_REG_APIC_ID:
112 ioapic->id = (val >> 24) & 0xf;
115 case IOAPIC_REG_ARB_ID:
119 index = (ioapic->ioregsel - 0x10) >> 1;
121 ioapic_debug("change redir index %x val %x\n", index, val);
122 if (index >= IOAPIC_NUM_PINS)
124 if (ioapic->ioregsel & 1) {
125 ioapic->redirtbl[index].bits &= 0xffffffff;
126 ioapic->redirtbl[index].bits |= (u64) val << 32;
128 ioapic->redirtbl[index].bits &= ~0xffffffffULL;
129 ioapic->redirtbl[index].bits |= (u32) val;
130 ioapic->redirtbl[index].fields.remote_irr = 0;
132 if (ioapic->irr & (1 << index))
133 ioapic_service(ioapic, index);
138 static void ioapic_inj_irq(struct kvm_ioapic *ioapic,
139 struct kvm_lapic *target,
140 u8 vector, u8 trig_mode, u8 delivery_mode)
142 ioapic_debug("irq %d trig %d deliv %d\n", vector, trig_mode,
145 ASSERT((delivery_mode == dest_Fixed) ||
146 (delivery_mode == dest_LowestPrio));
148 kvm_apic_set_irq(target, vector, trig_mode);
151 static u32 ioapic_get_delivery_bitmask(struct kvm_ioapic *ioapic, u8 dest,
156 struct kvm *kvm = ioapic->kvm;
157 struct kvm_vcpu *vcpu;
159 ioapic_debug("dest %d dest_mode %d\n", dest, dest_mode);
161 if (dest_mode == 0) { /* Physical mode. */
162 if (dest == 0xFF) { /* Broadcast. */
163 for (i = 0; i < KVM_MAX_VCPUS; ++i)
164 if (kvm->vcpus[i] && kvm->vcpus[i]->apic)
168 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
169 vcpu = kvm->vcpus[i];
172 if (kvm_apic_match_physical_addr(vcpu->apic, dest)) {
178 } else if (dest != 0) /* Logical mode, MDA non-zero. */
179 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
180 vcpu = kvm->vcpus[i];
184 kvm_apic_match_logical_addr(vcpu->apic, dest))
185 mask |= 1 << vcpu->vcpu_id;
187 ioapic_debug("mask %x\n", mask);
191 static void ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
193 u8 dest = ioapic->redirtbl[irq].fields.dest_id;
194 u8 dest_mode = ioapic->redirtbl[irq].fields.dest_mode;
195 u8 delivery_mode = ioapic->redirtbl[irq].fields.delivery_mode;
196 u8 vector = ioapic->redirtbl[irq].fields.vector;
197 u8 trig_mode = ioapic->redirtbl[irq].fields.trig_mode;
199 struct kvm_lapic *target;
200 struct kvm_vcpu *vcpu;
203 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
204 "vector=%x trig_mode=%x\n",
205 dest, dest_mode, delivery_mode, vector, trig_mode);
207 deliver_bitmask = ioapic_get_delivery_bitmask(ioapic, dest, dest_mode);
208 if (!deliver_bitmask) {
209 ioapic_debug("no target on destination\n");
213 switch (delivery_mode) {
214 case dest_LowestPrio:
216 kvm_apic_round_robin(ioapic->kvm, vector, deliver_bitmask);
218 ioapic_inj_irq(ioapic, target, vector,
219 trig_mode, delivery_mode);
221 ioapic_debug("null round robin: "
222 "mask=%x vector=%x delivery_mode=%x\n",
223 deliver_bitmask, vector, dest_LowestPrio);
226 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
227 if (!(deliver_bitmask & (1 << vcpu_id)))
229 deliver_bitmask &= ~(1 << vcpu_id);
230 vcpu = ioapic->kvm->vcpus[vcpu_id];
233 ioapic_inj_irq(ioapic, target, vector,
234 trig_mode, delivery_mode);
241 printk(KERN_WARNING "Unsupported delivery mode %d\n",
247 void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
249 u32 old_irr = ioapic->irr;
251 union ioapic_redir_entry entry;
253 if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
254 entry = ioapic->redirtbl[irq];
255 level ^= entry.fields.polarity;
257 ioapic->irr &= ~mask;
260 if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
261 || !entry.fields.remote_irr)
262 ioapic_service(ioapic, irq);
267 static int get_eoi_gsi(struct kvm_ioapic *ioapic, int vector)
271 for (i = 0; i < IOAPIC_NUM_PINS; i++)
272 if (ioapic->redirtbl[i].fields.vector == vector)
277 void kvm_ioapic_update_eoi(struct kvm *kvm, int vector)
279 struct kvm_ioapic *ioapic = kvm->vioapic;
280 union ioapic_redir_entry *ent;
283 gsi = get_eoi_gsi(ioapic, vector);
285 printk(KERN_WARNING "Can't find redir item for %d EOI\n",
290 ent = &ioapic->redirtbl[gsi];
291 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
293 ent->fields.remote_irr = 0;
294 if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
295 ioapic_deliver(ioapic, gsi);
298 static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr)
300 struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
302 return ((addr >= ioapic->base_address &&
303 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
306 static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
309 struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
312 ioapic_debug("addr %lx\n", (unsigned long)addr);
313 ASSERT(!(addr & 0xf)); /* check alignment */
317 case IOAPIC_REG_SELECT:
318 result = ioapic->ioregsel;
321 case IOAPIC_REG_WINDOW:
322 result = ioapic_read_indirect(ioapic, addr, len);
331 *(u64 *) val = result;
336 memcpy(val, (char *)&result, len);
339 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
343 static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
346 struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
349 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
350 (void*)addr, len, val);
351 ASSERT(!(addr & 0xf)); /* check alignment */
352 if (len == 4 || len == 8)
355 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
361 case IOAPIC_REG_SELECT:
362 ioapic->ioregsel = data;
365 case IOAPIC_REG_WINDOW:
366 ioapic_write_indirect(ioapic, data);
374 int kvm_ioapic_init(struct kvm *kvm)
376 struct kvm_ioapic *ioapic;
379 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
382 kvm->vioapic = ioapic;
383 for (i = 0; i < IOAPIC_NUM_PINS; i++)
384 ioapic->redirtbl[i].fields.mask = 1;
385 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
386 ioapic->dev.read = ioapic_mmio_read;
387 ioapic->dev.write = ioapic_mmio_write;
388 ioapic->dev.in_range = ioapic_in_range;
389 ioapic->dev.private = ioapic;
391 kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);