blob: 7c79c1d76d0c13e72463fcddc00cf845ad63ca87 [file] [log] [blame]
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03001/*
2 * Copyright (C) 2001 MandrakeSoft S.A.
3 *
4 * MandrakeSoft S.A.
5 * 43, rue d'Aboukir
6 * 75002 Paris - France
7 * http://www.linux-mandrake.com/
8 * http://www.mandrakesoft.com/
9 *
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.
14 *
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.
19 *
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
23 *
24 * Yunhong Jiang <yunhong.jiang@intel.com>
25 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
26 * Based on Xen 3.1 code.
27 */
28
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030030#include <linux/kvm.h>
31#include <linux/mm.h>
32#include <linux/highmem.h>
33#include <linux/smp.h>
34#include <linux/hrtimer.h>
35#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030037#include <asm/processor.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030038#include <asm/page.h>
39#include <asm/current.h>
Gleb Natapov1000ff82009-07-07 16:00:57 +030040#include <trace/events/kvm.h>
Zhang Xiantao82470192007-12-17 13:59:56 +080041
42#include "ioapic.h"
43#include "lapic.h"
Marcelo Tosattif5244722008-07-26 17:01:00 -030044#include "irq.h"
Zhang Xiantao82470192007-12-17 13:59:56 +080045
Laurent Viviere25e3ed2007-10-12 11:01:59 +020046#if 0
47#define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
48#else
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030049#define ioapic_debug(fmt, arg...)
Laurent Viviere25e3ed2007-10-12 11:01:59 +020050#endif
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -030051static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030052
53static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
54 unsigned long addr,
55 unsigned long length)
56{
57 unsigned long result = 0;
58
59 switch (ioapic->ioregsel) {
60 case IOAPIC_REG_VERSION:
61 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
62 | (IOAPIC_VERSION_ID & 0xff));
63 break;
64
65 case IOAPIC_REG_APIC_ID:
66 case IOAPIC_REG_ARB_ID:
67 result = ((ioapic->id & 0xf) << 24);
68 break;
69
70 default:
71 {
72 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
73 u64 redir_content;
74
75 ASSERT(redir_index < IOAPIC_NUM_PINS);
76
77 redir_content = ioapic->redirtbl[redir_index].bits;
78 result = (ioapic->ioregsel & 0x1) ?
79 (redir_content >> 32) & 0xffffffff :
80 redir_content & 0xffffffff;
81 break;
82 }
83 }
84
85 return result;
86}
87
Gleb Natapov49256632009-02-04 17:28:14 +020088static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030089{
Sheng Yangcf9e4e12009-02-11 16:03:36 +080090 union kvm_ioapic_redirect_entry *pent;
Gleb Natapov49256632009-02-04 17:28:14 +020091 int injected = -1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030092
93 pent = &ioapic->redirtbl[idx];
94
95 if (!pent->fields.mask) {
Gleb Natapov49256632009-02-04 17:28:14 +020096 injected = ioapic_deliver(ioapic, idx);
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -030097 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030098 pent->fields.remote_irr = 1;
99 }
Gleb Natapov49256632009-02-04 17:28:14 +0200100
101 return injected;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300102}
103
Avi Kivity46a929b2009-12-28 14:08:30 +0200104static void update_handled_vectors(struct kvm_ioapic *ioapic)
105{
106 DECLARE_BITMAP(handled_vectors, 256);
107 int i;
108
109 memset(handled_vectors, 0, sizeof(handled_vectors));
110 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
111 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
112 memcpy(ioapic->handled_vectors, handled_vectors,
113 sizeof(handled_vectors));
114 smp_wmb();
115}
116
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300117static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
118{
119 unsigned index;
Avi Kivity75858a82009-01-04 17:10:50 +0200120 bool mask_before, mask_after;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300121 union kvm_ioapic_redirect_entry *e;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300122
123 switch (ioapic->ioregsel) {
124 case IOAPIC_REG_VERSION:
125 /* Writes are ignored. */
126 break;
127
128 case IOAPIC_REG_APIC_ID:
129 ioapic->id = (val >> 24) & 0xf;
130 break;
131
132 case IOAPIC_REG_ARB_ID:
133 break;
134
135 default:
136 index = (ioapic->ioregsel - 0x10) >> 1;
137
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200138 ioapic_debug("change redir index %x val %x\n", index, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300139 if (index >= IOAPIC_NUM_PINS)
140 return;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300141 e = &ioapic->redirtbl[index];
142 mask_before = e->fields.mask;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300143 if (ioapic->ioregsel & 1) {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300144 e->bits &= 0xffffffff;
145 e->bits |= (u64) val << 32;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300146 } else {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300147 e->bits &= ~0xffffffffULL;
148 e->bits |= (u32) val;
149 e->fields.remote_irr = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300150 }
Avi Kivity46a929b2009-12-28 14:08:30 +0200151 update_handled_vectors(ioapic);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300152 mask_after = e->fields.mask;
Avi Kivity75858a82009-01-04 17:10:50 +0200153 if (mask_before != mask_after)
154 kvm_fire_mask_notifiers(ioapic->kvm, index, mask_after);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300155 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300156 && ioapic->irr & (1 << index))
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300157 ioapic_service(ioapic, index);
158 break;
159 }
160}
161
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300162static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300163{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200164 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
165 struct kvm_lapic_irq irqe;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300166
167 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200168 "vector=%x trig_mode=%x\n",
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200169 entry->fields.dest, entry->fields.dest_mode,
170 entry->fields.delivery_mode, entry->fields.vector,
171 entry->fields.trig_mode);
172
173 irqe.dest_id = entry->fields.dest_id;
174 irqe.vector = entry->fields.vector;
175 irqe.dest_mode = entry->fields.dest_mode;
176 irqe.trig_mode = entry->fields.trig_mode;
177 irqe.delivery_mode = entry->fields.delivery_mode << 8;
178 irqe.level = 1;
179 irqe.shorthand = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300180
Avi Kivity8c35f232008-02-25 10:28:31 +0200181#ifdef CONFIG_X86
Gleb Natapova53c17d2009-03-05 16:34:49 +0200182 /* Always delivery PIT interrupt to vcpu 0 */
Sheng Yang74a3a8f2009-03-04 13:33:02 +0800183 if (irq == 0) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200184 irqe.dest_mode = 0; /* Physical mode. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300185 /* need to read apic_id from apic regiest since
186 * it can be rewritten */
187 irqe.dest_id = ioapic->kvm->bsp_vcpu->vcpu_id;
Gleb Natapova53c17d2009-03-05 16:34:49 +0200188 }
Avi Kivity8c35f232008-02-25 10:28:31 +0200189#endif
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200190 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300191}
192
Gleb Natapov49256632009-02-04 17:28:14 +0200193int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300194{
195 u32 old_irr = ioapic->irr;
196 u32 mask = 1 << irq;
Sheng Yangcf9e4e12009-02-11 16:03:36 +0800197 union kvm_ioapic_redirect_entry entry;
Gleb Natapov49256632009-02-04 17:28:14 +0200198 int ret = 1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300199
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300200 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300201 if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
202 entry = ioapic->redirtbl[irq];
203 level ^= entry.fields.polarity;
204 if (!level)
205 ioapic->irr &= ~mask;
206 else {
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300207 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300208 ioapic->irr |= mask;
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300209 if ((edge && old_irr != ioapic->irr) ||
210 (!edge && !entry.fields.remote_irr))
Gleb Natapov49256632009-02-04 17:28:14 +0200211 ret = ioapic_service(ioapic, irq);
Gleb Natapov65a82212009-09-03 12:10:34 +0300212 else
213 ret = 0; /* report coalesced interrupt */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300214 }
Gleb Natapov1000ff82009-07-07 16:00:57 +0300215 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300216 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300217 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300218
Gleb Natapov49256632009-02-04 17:28:14 +0200219 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300220}
221
Gleb Natapoveba02262009-08-24 11:54:25 +0300222static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
223 int trigger_mode)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300224{
Gleb Natapoveba02262009-08-24 11:54:25 +0300225 int i;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300226
Gleb Natapoveba02262009-08-24 11:54:25 +0300227 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
228 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300229
Gleb Natapoveba02262009-08-24 11:54:25 +0300230 if (ent->fields.vector != vector)
231 continue;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300232
Gleb Natapoveba02262009-08-24 11:54:25 +0300233 /*
234 * We are dropping lock while calling ack notifiers because ack
235 * notifier callbacks for assigned devices call into IOAPIC
236 * recursively. Since remote_irr is cleared only after call
237 * to notifiers if the same vector will be delivered while lock
238 * is dropped it will be put into irr and will be delivered
239 * after ack notifier returns.
240 */
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300241 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300242 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300243 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300244
245 if (trigger_mode != IOAPIC_LEVEL_TRIG)
246 continue;
247
Marcelo Tosattif5244722008-07-26 17:01:00 -0300248 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
249 ent->fields.remote_irr = 0;
Gleb Natapoveba02262009-08-24 11:54:25 +0300250 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
251 ioapic_service(ioapic, i);
Marcelo Tosattif5244722008-07-26 17:01:00 -0300252 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300253}
254
Marcelo Tosattif5244722008-07-26 17:01:00 -0300255void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700256{
257 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700258
Avi Kivity46a929b2009-12-28 14:08:30 +0200259 smp_rmb();
260 if (!test_bit(vector, ioapic->handled_vectors))
261 return;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300262 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300263 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300264 spin_unlock(&ioapic->lock);
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700265}
266
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400267static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
268{
269 return container_of(dev, struct kvm_ioapic, dev);
270}
271
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300272static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300273{
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300274 return ((addr >= ioapic->base_address &&
275 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
276}
277
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300278static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
279 void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300280{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400281 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300282 u32 result;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300283 if (!ioapic_in_range(ioapic, addr))
284 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300285
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200286 ioapic_debug("addr %lx\n", (unsigned long)addr);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300287 ASSERT(!(addr & 0xf)); /* check alignment */
288
289 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300290 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300291 switch (addr) {
292 case IOAPIC_REG_SELECT:
293 result = ioapic->ioregsel;
294 break;
295
296 case IOAPIC_REG_WINDOW:
297 result = ioapic_read_indirect(ioapic, addr, len);
298 break;
299
300 default:
301 result = 0;
302 break;
303 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300304 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300305
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300306 switch (len) {
307 case 8:
308 *(u64 *) val = result;
309 break;
310 case 1:
311 case 2:
312 case 4:
313 memcpy(val, (char *)&result, len);
314 break;
315 default:
316 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
317 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300318 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300319}
320
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300321static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
322 const void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300323{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400324 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300325 u32 data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300326 if (!ioapic_in_range(ioapic, addr))
327 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300328
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200329 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
330 (void*)addr, len, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300331 ASSERT(!(addr & 0xf)); /* check alignment */
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300332
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300333 if (len == 4 || len == 8)
334 data = *(u32 *) val;
335 else {
336 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
Gleb Natapoveba02262009-08-24 11:54:25 +0300337 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300338 }
339
340 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300341 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300342 switch (addr) {
343 case IOAPIC_REG_SELECT:
344 ioapic->ioregsel = data;
345 break;
346
347 case IOAPIC_REG_WINDOW:
348 ioapic_write_indirect(ioapic, data);
349 break;
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800350#ifdef CONFIG_IA64
351 case IOAPIC_REG_EOI:
Gleb Natapoveba02262009-08-24 11:54:25 +0300352 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800353 break;
354#endif
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300355
356 default:
357 break;
358 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300359 spin_unlock(&ioapic->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300360 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300361}
362
Eddie Dong8c392692007-10-10 12:15:54 +0200363void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
364{
365 int i;
366
367 for (i = 0; i < IOAPIC_NUM_PINS; i++)
368 ioapic->redirtbl[i].fields.mask = 1;
369 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
370 ioapic->ioregsel = 0;
371 ioapic->irr = 0;
372 ioapic->id = 0;
Avi Kivity46a929b2009-12-28 14:08:30 +0200373 update_handled_vectors(ioapic);
Eddie Dong8c392692007-10-10 12:15:54 +0200374}
375
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400376static const struct kvm_io_device_ops ioapic_mmio_ops = {
377 .read = ioapic_mmio_read,
378 .write = ioapic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400379};
380
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300381int kvm_ioapic_init(struct kvm *kvm)
382{
383 struct kvm_ioapic *ioapic;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400384 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300385
386 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
387 if (!ioapic)
388 return -ENOMEM;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300389 spin_lock_init(&ioapic->lock);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +0800390 kvm->arch.vioapic = ioapic;
Eddie Dong8c392692007-10-10 12:15:54 +0200391 kvm_ioapic_reset(ioapic);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400392 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300393 ioapic->kvm = kvm;
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200394 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200395 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200396 mutex_unlock(&kvm->slots_lock);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800397 if (ret < 0) {
398 kvm->arch.vioapic = NULL;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400399 kfree(ioapic);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800400 }
Gregory Haskins090b7af2009-07-07 17:08:44 -0400401
402 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300403}
Avi Kivity75858a82009-01-04 17:10:50 +0200404
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800405void kvm_ioapic_destroy(struct kvm *kvm)
406{
407 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
408
409 if (ioapic) {
410 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
411 kvm->arch.vioapic = NULL;
412 kfree(ioapic);
413 }
414}
415
Gleb Natapoveba02262009-08-24 11:54:25 +0300416int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
417{
418 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
419 if (!ioapic)
420 return -EINVAL;
421
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300422 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300423 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300424 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300425 return 0;
426}
427
428int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
429{
430 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
431 if (!ioapic)
432 return -EINVAL;
433
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300434 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300435 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
Avi Kivity46a929b2009-12-28 14:08:30 +0200436 update_handled_vectors(ioapic);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300437 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300438 return 0;
439}