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>
47 #define ioapic_debug(fmt, arg...)
48 static int ioapic_deliver(struct kvm_ioapic
*vioapic
, int irq
);
50 static unsigned long ioapic_read_indirect(struct kvm_ioapic
*ioapic
,
54 unsigned long result
= 0;
56 switch (ioapic
->ioregsel
) {
57 case IOAPIC_REG_VERSION
:
58 result
= ((((IOAPIC_NUM_PINS
- 1) & 0xff) << 16)
59 | (IOAPIC_VERSION_ID
& 0xff));
62 case IOAPIC_REG_APIC_ID
:
63 case IOAPIC_REG_ARB_ID
:
64 result
= ((ioapic
->id
& 0xf) << 24);
69 u32 redir_index
= (ioapic
->ioregsel
- 0x10) >> 1;
72 ASSERT(redir_index
< IOAPIC_NUM_PINS
);
74 redir_content
= ioapic
->redirtbl
[redir_index
].bits
;
75 result
= (ioapic
->ioregsel
& 0x1) ?
76 (redir_content
>> 32) & 0xffffffff :
77 redir_content
& 0xffffffff;
85 static int ioapic_service(struct kvm_ioapic
*ioapic
, unsigned int idx
)
87 union kvm_ioapic_redirect_entry
*pent
;
90 pent
= &ioapic
->redirtbl
[idx
];
92 if (!pent
->fields
.mask
) {
93 injected
= ioapic_deliver(ioapic
, idx
);
94 if (injected
&& pent
->fields
.trig_mode
== IOAPIC_LEVEL_TRIG
)
95 pent
->fields
.remote_irr
= 1;
101 static void update_handled_vectors(struct kvm_ioapic
*ioapic
)
103 DECLARE_BITMAP(handled_vectors
, 256);
106 memset(handled_vectors
, 0, sizeof(handled_vectors
));
107 for (i
= 0; i
< IOAPIC_NUM_PINS
; ++i
)
108 __set_bit(ioapic
->redirtbl
[i
].fields
.vector
, handled_vectors
);
109 memcpy(ioapic
->handled_vectors
, handled_vectors
,
110 sizeof(handled_vectors
));
114 static void ioapic_write_indirect(struct kvm_ioapic
*ioapic
, u32 val
)
117 bool mask_before
, mask_after
;
118 union kvm_ioapic_redirect_entry
*e
;
120 switch (ioapic
->ioregsel
) {
121 case IOAPIC_REG_VERSION
:
122 /* Writes are ignored. */
125 case IOAPIC_REG_APIC_ID
:
126 ioapic
->id
= (val
>> 24) & 0xf;
129 case IOAPIC_REG_ARB_ID
:
133 index
= (ioapic
->ioregsel
- 0x10) >> 1;
135 ioapic_debug("change redir index %x val %x\n", index
, val
);
136 if (index
>= IOAPIC_NUM_PINS
)
138 e
= &ioapic
->redirtbl
[index
];
139 mask_before
= e
->fields
.mask
;
140 if (ioapic
->ioregsel
& 1) {
141 e
->bits
&= 0xffffffff;
142 e
->bits
|= (u64
) val
<< 32;
144 e
->bits
&= ~0xffffffffULL
;
145 e
->bits
|= (u32
) val
;
146 e
->fields
.remote_irr
= 0;
148 update_handled_vectors(ioapic
);
149 mask_after
= e
->fields
.mask
;
150 if (mask_before
!= mask_after
)
151 kvm_fire_mask_notifiers(ioapic
->kvm
, KVM_IRQCHIP_IOAPIC
, index
, mask_after
);
152 if (e
->fields
.trig_mode
== IOAPIC_LEVEL_TRIG
153 && ioapic
->irr
& (1 << index
))
154 ioapic_service(ioapic
, index
);
159 static int ioapic_deliver(struct kvm_ioapic
*ioapic
, int irq
)
161 union kvm_ioapic_redirect_entry
*entry
= &ioapic
->redirtbl
[irq
];
162 struct kvm_lapic_irq irqe
;
164 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
165 "vector=%x trig_mode=%x\n",
166 entry
->fields
.dest
, entry
->fields
.dest_mode
,
167 entry
->fields
.delivery_mode
, entry
->fields
.vector
,
168 entry
->fields
.trig_mode
);
170 irqe
.dest_id
= entry
->fields
.dest_id
;
171 irqe
.vector
= entry
->fields
.vector
;
172 irqe
.dest_mode
= entry
->fields
.dest_mode
;
173 irqe
.trig_mode
= entry
->fields
.trig_mode
;
174 irqe
.delivery_mode
= entry
->fields
.delivery_mode
<< 8;
179 /* Always delivery PIT interrupt to vcpu 0 */
181 irqe
.dest_mode
= 0; /* Physical mode. */
182 /* need to read apic_id from apic regiest since
183 * it can be rewritten */
184 irqe
.dest_id
= ioapic
->kvm
->bsp_vcpu
->vcpu_id
;
187 return kvm_irq_delivery_to_apic(ioapic
->kvm
, NULL
, &irqe
);
190 int kvm_ioapic_set_irq(struct kvm_ioapic
*ioapic
, int irq
, int level
)
194 union kvm_ioapic_redirect_entry entry
;
197 spin_lock(&ioapic
->lock
);
198 old_irr
= ioapic
->irr
;
199 if (irq
>= 0 && irq
< IOAPIC_NUM_PINS
) {
200 entry
= ioapic
->redirtbl
[irq
];
201 level
^= entry
.fields
.polarity
;
203 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);
215 spin_unlock(&ioapic
->lock
);
220 static void __kvm_ioapic_update_eoi(struct kvm_ioapic
*ioapic
, int vector
,
225 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
226 union kvm_ioapic_redirect_entry
*ent
= &ioapic
->redirtbl
[i
];
228 if (ent
->fields
.vector
!= vector
)
232 * We are dropping lock while calling ack notifiers because ack
233 * notifier callbacks for assigned devices call into IOAPIC
234 * recursively. Since remote_irr is cleared only after call
235 * to notifiers if the same vector will be delivered while lock
236 * is dropped it will be put into irr and will be delivered
237 * after ack notifier returns.
239 spin_unlock(&ioapic
->lock
);
240 kvm_notify_acked_irq(ioapic
->kvm
, KVM_IRQCHIP_IOAPIC
, i
);
241 spin_lock(&ioapic
->lock
);
243 if (trigger_mode
!= IOAPIC_LEVEL_TRIG
)
246 ASSERT(ent
->fields
.trig_mode
== IOAPIC_LEVEL_TRIG
);
247 ent
->fields
.remote_irr
= 0;
248 if (!ent
->fields
.mask
&& (ioapic
->irr
& (1 << i
)))
249 ioapic_service(ioapic
, i
);
253 void kvm_ioapic_update_eoi(struct kvm
*kvm
, int vector
, int trigger_mode
)
255 struct kvm_ioapic
*ioapic
= kvm
->arch
.vioapic
;
258 if (!test_bit(vector
, ioapic
->handled_vectors
))
260 spin_lock(&ioapic
->lock
);
261 __kvm_ioapic_update_eoi(ioapic
, vector
, trigger_mode
);
262 spin_unlock(&ioapic
->lock
);
265 static inline struct kvm_ioapic
*to_ioapic(struct kvm_io_device
*dev
)
267 return container_of(dev
, struct kvm_ioapic
, dev
);
270 static inline int ioapic_in_range(struct kvm_ioapic
*ioapic
, gpa_t addr
)
272 return ((addr
>= ioapic
->base_address
&&
273 (addr
< ioapic
->base_address
+ IOAPIC_MEM_LENGTH
)));
276 static int ioapic_mmio_read(struct kvm_io_device
*this, gpa_t addr
, int len
,
279 struct kvm_ioapic
*ioapic
= to_ioapic(this);
281 if (!ioapic_in_range(ioapic
, addr
))
284 ioapic_debug("addr %lx\n", (unsigned long)addr
);
285 ASSERT(!(addr
& 0xf)); /* check alignment */
288 spin_lock(&ioapic
->lock
);
290 case IOAPIC_REG_SELECT
:
291 result
= ioapic
->ioregsel
;
294 case IOAPIC_REG_WINDOW
:
295 result
= ioapic_read_indirect(ioapic
, addr
, len
);
302 spin_unlock(&ioapic
->lock
);
306 *(u64
*) val
= result
;
311 memcpy(val
, (char *)&result
, len
);
314 printk(KERN_WARNING
"ioapic: wrong length %d\n", len
);
319 static int ioapic_mmio_write(struct kvm_io_device
*this, gpa_t addr
, int len
,
322 struct kvm_ioapic
*ioapic
= to_ioapic(this);
324 if (!ioapic_in_range(ioapic
, addr
))
327 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
328 (void*)addr
, len
, val
);
329 ASSERT(!(addr
& 0xf)); /* check alignment */
331 if (len
== 4 || len
== 8)
334 printk(KERN_WARNING
"ioapic: Unsupported size %d\n", len
);
339 spin_lock(&ioapic
->lock
);
341 case IOAPIC_REG_SELECT
:
342 ioapic
->ioregsel
= data
;
345 case IOAPIC_REG_WINDOW
:
346 ioapic_write_indirect(ioapic
, data
);
350 __kvm_ioapic_update_eoi(ioapic
, data
, IOAPIC_LEVEL_TRIG
);
357 spin_unlock(&ioapic
->lock
);
361 void kvm_ioapic_reset(struct kvm_ioapic
*ioapic
)
365 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
366 ioapic
->redirtbl
[i
].fields
.mask
= 1;
367 ioapic
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
368 ioapic
->ioregsel
= 0;
371 update_handled_vectors(ioapic
);
374 static const struct kvm_io_device_ops ioapic_mmio_ops
= {
375 .read
= ioapic_mmio_read
,
376 .write
= ioapic_mmio_write
,
379 int kvm_ioapic_init(struct kvm
*kvm
)
381 struct kvm_ioapic
*ioapic
;
384 ioapic
= kzalloc(sizeof(struct kvm_ioapic
), GFP_KERNEL
);
387 spin_lock_init(&ioapic
->lock
);
388 kvm
->arch
.vioapic
= ioapic
;
389 kvm_ioapic_reset(ioapic
);
390 kvm_iodevice_init(&ioapic
->dev
, &ioapic_mmio_ops
);
392 mutex_lock(&kvm
->slots_lock
);
393 ret
= kvm_io_bus_register_dev(kvm
, KVM_MMIO_BUS
, &ioapic
->dev
);
394 mutex_unlock(&kvm
->slots_lock
);
396 kvm
->arch
.vioapic
= NULL
;
403 void kvm_ioapic_destroy(struct kvm
*kvm
)
405 struct kvm_ioapic
*ioapic
= kvm
->arch
.vioapic
;
408 kvm_io_bus_unregister_dev(kvm
, KVM_MMIO_BUS
, &ioapic
->dev
);
409 kvm
->arch
.vioapic
= NULL
;
414 int kvm_get_ioapic(struct kvm
*kvm
, struct kvm_ioapic_state
*state
)
416 struct kvm_ioapic
*ioapic
= ioapic_irqchip(kvm
);
420 spin_lock(&ioapic
->lock
);
421 memcpy(state
, ioapic
, sizeof(struct kvm_ioapic_state
));
422 spin_unlock(&ioapic
->lock
);
426 int kvm_set_ioapic(struct kvm
*kvm
, struct kvm_ioapic_state
*state
)
428 struct kvm_ioapic
*ioapic
= ioapic_irqchip(kvm
);
432 spin_lock(&ioapic
->lock
);
433 memcpy(ioapic
, state
, sizeof(struct kvm_ioapic_state
));
434 update_handled_vectors(ioapic
);
435 spin_unlock(&ioapic
->lock
);