blob: 9f477f67163940949a169a5f6daa41d7cd441146 [file] [log] [blame]
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03001/*
2 * Copyright (C) 2001 MandrakeSoft S.A.
Avi Kivity221d0592010-05-23 18:37:00 +03003 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03004 *
5 * MandrakeSoft S.A.
6 * 43, rue d'Aboukir
7 * 75002 Paris - France
8 * http://www.linux-mandrake.com/
9 * http://www.mandrakesoft.com/
10 *
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.
15 *
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.
20 *
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
24 *
25 * Yunhong Jiang <yunhong.jiang@intel.com>
26 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
27 * Based on Xen 3.1 code.
28 */
29
Avi Kivityedf88412007-12-16 11:02:48 +020030#include <linux/kvm_host.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030031#include <linux/kvm.h>
32#include <linux/mm.h>
33#include <linux/highmem.h>
34#include <linux/smp.h>
35#include <linux/hrtimer.h>
36#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030038#include <asm/processor.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030039#include <asm/page.h>
40#include <asm/current.h>
Gleb Natapov1000ff82009-07-07 16:00:57 +030041#include <trace/events/kvm.h>
Zhang Xiantao82470192007-12-17 13:59:56 +080042
43#include "ioapic.h"
44#include "lapic.h"
Marcelo Tosattif5244722008-07-26 17:01:00 -030045#include "irq.h"
Zhang Xiantao82470192007-12-17 13:59:56 +080046
Laurent Viviere25e3ed2007-10-12 11:01:59 +020047#if 0
48#define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
49#else
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030050#define ioapic_debug(fmt, arg...)
Laurent Viviere25e3ed2007-10-12 11:01:59 +020051#endif
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -030052static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030053
54static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
55 unsigned long addr,
56 unsigned long length)
57{
58 unsigned long result = 0;
59
60 switch (ioapic->ioregsel) {
61 case IOAPIC_REG_VERSION:
62 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
63 | (IOAPIC_VERSION_ID & 0xff));
64 break;
65
66 case IOAPIC_REG_APIC_ID:
67 case IOAPIC_REG_ARB_ID:
68 result = ((ioapic->id & 0xf) << 24);
69 break;
70
71 default:
72 {
73 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
74 u64 redir_content;
75
Andy Honige7d7f0a2013-02-20 14:49:16 -080076 if (redir_index < IOAPIC_NUM_PINS)
77 redir_content =
78 ioapic->redirtbl[redir_index].bits;
79 else
80 redir_content = ~0ULL;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030081
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030082 result = (ioapic->ioregsel & 0x1) ?
83 (redir_content >> 32) & 0xffffffff :
84 redir_content & 0xffffffff;
85 break;
86 }
87 }
88
89 return result;
90}
91
Gleb Natapov49256632009-02-04 17:28:14 +020092static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030093{
Sheng Yangcf9e4e12009-02-11 16:03:36 +080094 union kvm_ioapic_redirect_entry *pent;
Gleb Natapov49256632009-02-04 17:28:14 +020095 int injected = -1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030096
97 pent = &ioapic->redirtbl[idx];
98
99 if (!pent->fields.mask) {
Gleb Natapov49256632009-02-04 17:28:14 +0200100 injected = ioapic_deliver(ioapic, idx);
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300101 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300102 pent->fields.remote_irr = 1;
103 }
Gleb Natapov49256632009-02-04 17:28:14 +0200104
105 return injected;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300106}
107
Avi Kivity46a929b2009-12-28 14:08:30 +0200108static void update_handled_vectors(struct kvm_ioapic *ioapic)
109{
110 DECLARE_BITMAP(handled_vectors, 256);
111 int i;
112
113 memset(handled_vectors, 0, sizeof(handled_vectors));
114 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
115 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
116 memcpy(ioapic->handled_vectors, handled_vectors,
117 sizeof(handled_vectors));
118 smp_wmb();
119}
120
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300121static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
122{
123 unsigned index;
Avi Kivity75858a82009-01-04 17:10:50 +0200124 bool mask_before, mask_after;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300125 union kvm_ioapic_redirect_entry *e;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300126
127 switch (ioapic->ioregsel) {
128 case IOAPIC_REG_VERSION:
129 /* Writes are ignored. */
130 break;
131
132 case IOAPIC_REG_APIC_ID:
133 ioapic->id = (val >> 24) & 0xf;
134 break;
135
136 case IOAPIC_REG_ARB_ID:
137 break;
138
139 default:
140 index = (ioapic->ioregsel - 0x10) >> 1;
141
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200142 ioapic_debug("change redir index %x val %x\n", index, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300143 if (index >= IOAPIC_NUM_PINS)
144 return;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300145 e = &ioapic->redirtbl[index];
146 mask_before = e->fields.mask;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300147 if (ioapic->ioregsel & 1) {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300148 e->bits &= 0xffffffff;
149 e->bits |= (u64) val << 32;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300150 } else {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300151 e->bits &= ~0xffffffffULL;
152 e->bits |= (u32) val;
153 e->fields.remote_irr = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300154 }
Avi Kivity46a929b2009-12-28 14:08:30 +0200155 update_handled_vectors(ioapic);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300156 mask_after = e->fields.mask;
Avi Kivity75858a82009-01-04 17:10:50 +0200157 if (mask_before != mask_after)
Gleb Natapov4a994352010-07-11 15:32:23 +0300158 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300159 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300160 && ioapic->irr & (1 << index))
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300161 ioapic_service(ioapic, index);
162 break;
163 }
164}
165
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300166static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300167{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200168 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
169 struct kvm_lapic_irq irqe;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300170
171 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200172 "vector=%x trig_mode=%x\n",
Liu Yuana38f84c2011-04-21 14:53:57 +0800173 entry->fields.dest_id, entry->fields.dest_mode,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200174 entry->fields.delivery_mode, entry->fields.vector,
175 entry->fields.trig_mode);
176
177 irqe.dest_id = entry->fields.dest_id;
178 irqe.vector = entry->fields.vector;
179 irqe.dest_mode = entry->fields.dest_mode;
180 irqe.trig_mode = entry->fields.trig_mode;
181 irqe.delivery_mode = entry->fields.delivery_mode << 8;
182 irqe.level = 1;
183 irqe.shorthand = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300184
Avi Kivity8c35f232008-02-25 10:28:31 +0200185#ifdef CONFIG_X86
Gleb Natapova53c17d2009-03-05 16:34:49 +0200186 /* Always delivery PIT interrupt to vcpu 0 */
Sheng Yang74a3a8f2009-03-04 13:33:02 +0800187 if (irq == 0) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200188 irqe.dest_mode = 0; /* Physical mode. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300189 /* need to read apic_id from apic regiest since
190 * it can be rewritten */
Gleb Natapovd546cb42011-12-15 12:38:40 +0200191 irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
Gleb Natapova53c17d2009-03-05 16:34:49 +0200192 }
Avi Kivity8c35f232008-02-25 10:28:31 +0200193#endif
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200194 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300195}
196
Gleb Natapov49256632009-02-04 17:28:14 +0200197int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300198{
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300199 u32 old_irr;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300200 u32 mask = 1 << irq;
Sheng Yangcf9e4e12009-02-11 16:03:36 +0800201 union kvm_ioapic_redirect_entry entry;
Gleb Natapov49256632009-02-04 17:28:14 +0200202 int ret = 1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300203
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300204 spin_lock(&ioapic->lock);
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300205 old_irr = ioapic->irr;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300206 if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
207 entry = ioapic->redirtbl[irq];
208 level ^= entry.fields.polarity;
209 if (!level)
210 ioapic->irr &= ~mask;
211 else {
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300212 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300213 ioapic->irr |= mask;
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300214 if ((edge && old_irr != ioapic->irr) ||
215 (!edge && !entry.fields.remote_irr))
Gleb Natapov49256632009-02-04 17:28:14 +0200216 ret = ioapic_service(ioapic, irq);
Gleb Natapov65a82212009-09-03 12:10:34 +0300217 else
218 ret = 0; /* report coalesced interrupt */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300219 }
Gleb Natapov1000ff82009-07-07 16:00:57 +0300220 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300221 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300222 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300223
Gleb Natapov49256632009-02-04 17:28:14 +0200224 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300225}
226
Gleb Natapoveba02262009-08-24 11:54:25 +0300227static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
228 int trigger_mode)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300229{
Gleb Natapoveba02262009-08-24 11:54:25 +0300230 int i;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300231
Gleb Natapoveba02262009-08-24 11:54:25 +0300232 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
233 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300234
Gleb Natapoveba02262009-08-24 11:54:25 +0300235 if (ent->fields.vector != vector)
236 continue;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300237
Gleb Natapoveba02262009-08-24 11:54:25 +0300238 /*
239 * We are dropping lock while calling ack notifiers because ack
240 * notifier callbacks for assigned devices call into IOAPIC
241 * recursively. Since remote_irr is cleared only after call
242 * to notifiers if the same vector will be delivered while lock
243 * is dropped it will be put into irr and will be delivered
244 * after ack notifier returns.
245 */
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300246 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300247 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300248 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300249
250 if (trigger_mode != IOAPIC_LEVEL_TRIG)
251 continue;
252
Marcelo Tosattif5244722008-07-26 17:01:00 -0300253 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
254 ent->fields.remote_irr = 0;
Gleb Natapoveba02262009-08-24 11:54:25 +0300255 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
256 ioapic_service(ioapic, i);
Marcelo Tosattif5244722008-07-26 17:01:00 -0300257 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300258}
259
Marcelo Tosattif5244722008-07-26 17:01:00 -0300260void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700261{
262 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700263
Avi Kivity46a929b2009-12-28 14:08:30 +0200264 smp_rmb();
265 if (!test_bit(vector, ioapic->handled_vectors))
266 return;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300267 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300268 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300269 spin_unlock(&ioapic->lock);
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700270}
271
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400272static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
273{
274 return container_of(dev, struct kvm_ioapic, dev);
275}
276
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300277static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300278{
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300279 return ((addr >= ioapic->base_address &&
280 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
281}
282
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300283static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
284 void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300285{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400286 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300287 u32 result;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300288 if (!ioapic_in_range(ioapic, addr))
289 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300290
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200291 ioapic_debug("addr %lx\n", (unsigned long)addr);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300292 ASSERT(!(addr & 0xf)); /* check alignment */
293
294 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300295 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300296 switch (addr) {
297 case IOAPIC_REG_SELECT:
298 result = ioapic->ioregsel;
299 break;
300
301 case IOAPIC_REG_WINDOW:
302 result = ioapic_read_indirect(ioapic, addr, len);
303 break;
304
305 default:
306 result = 0;
307 break;
308 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300309 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300310
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300311 switch (len) {
312 case 8:
313 *(u64 *) val = result;
314 break;
315 case 1:
316 case 2:
317 case 4:
318 memcpy(val, (char *)&result, len);
319 break;
320 default:
321 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
322 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300323 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300324}
325
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300326static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
327 const void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300328{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400329 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300330 u32 data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300331 if (!ioapic_in_range(ioapic, addr))
332 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300333
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200334 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
335 (void*)addr, len, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300336 ASSERT(!(addr & 0xf)); /* check alignment */
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300337
Julian Stecklinad77fe632011-11-23 13:54:30 +0100338 switch (len) {
339 case 8:
340 case 4:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300341 data = *(u32 *) val;
Julian Stecklinad77fe632011-11-23 13:54:30 +0100342 break;
343 case 2:
344 data = *(u16 *) val;
345 break;
346 case 1:
347 data = *(u8 *) val;
348 break;
349 default:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300350 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
Gleb Natapoveba02262009-08-24 11:54:25 +0300351 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300352 }
353
354 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300355 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300356 switch (addr) {
357 case IOAPIC_REG_SELECT:
Julian Stecklinad77fe632011-11-23 13:54:30 +0100358 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300359 break;
360
361 case IOAPIC_REG_WINDOW:
362 ioapic_write_indirect(ioapic, data);
363 break;
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800364#ifdef CONFIG_IA64
365 case IOAPIC_REG_EOI:
Gleb Natapoveba02262009-08-24 11:54:25 +0300366 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800367 break;
368#endif
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300369
370 default:
371 break;
372 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300373 spin_unlock(&ioapic->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300374 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300375}
376
Eddie Dong8c392692007-10-10 12:15:54 +0200377void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
378{
379 int i;
380
381 for (i = 0; i < IOAPIC_NUM_PINS; i++)
382 ioapic->redirtbl[i].fields.mask = 1;
383 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
384 ioapic->ioregsel = 0;
385 ioapic->irr = 0;
386 ioapic->id = 0;
Avi Kivity46a929b2009-12-28 14:08:30 +0200387 update_handled_vectors(ioapic);
Eddie Dong8c392692007-10-10 12:15:54 +0200388}
389
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400390static const struct kvm_io_device_ops ioapic_mmio_ops = {
391 .read = ioapic_mmio_read,
392 .write = ioapic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400393};
394
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300395int kvm_ioapic_init(struct kvm *kvm)
396{
397 struct kvm_ioapic *ioapic;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400398 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300399
400 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
401 if (!ioapic)
402 return -ENOMEM;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300403 spin_lock_init(&ioapic->lock);
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +0800404 kvm->arch.vioapic = ioapic;
Eddie Dong8c392692007-10-10 12:15:54 +0200405 kvm_ioapic_reset(ioapic);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400406 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300407 ioapic->kvm = kvm;
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200408 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300409 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
410 IOAPIC_MEM_LENGTH, &ioapic->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200411 mutex_unlock(&kvm->slots_lock);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800412 if (ret < 0) {
413 kvm->arch.vioapic = NULL;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400414 kfree(ioapic);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800415 }
Gregory Haskins090b7af2009-07-07 17:08:44 -0400416
417 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300418}
Avi Kivity75858a82009-01-04 17:10:50 +0200419
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800420void kvm_ioapic_destroy(struct kvm *kvm)
421{
422 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
423
424 if (ioapic) {
425 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
426 kvm->arch.vioapic = NULL;
427 kfree(ioapic);
428 }
429}
430
Gleb Natapoveba02262009-08-24 11:54:25 +0300431int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
432{
433 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
434 if (!ioapic)
435 return -EINVAL;
436
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300437 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300438 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300439 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300440 return 0;
441}
442
443int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
444{
445 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
446 if (!ioapic)
447 return -EINVAL;
448
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300449 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300450 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
Avi Kivity46a929b2009-12-28 14:08:30 +0200451 update_handled_vectors(ioapic);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300452 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300453 return 0;
454}