blob: cfb7e4d52dc26d1c832eb2a554d8a8ed1d9d23c3 [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
76 ASSERT(redir_index < IOAPIC_NUM_PINS);
77
78 redir_content = ioapic->redirtbl[redir_index].bits;
79 result = (ioapic->ioregsel & 0x1) ?
80 (redir_content >> 32) & 0xffffffff :
81 redir_content & 0xffffffff;
82 break;
83 }
84 }
85
86 return result;
87}
88
Gleb Natapov49256632009-02-04 17:28:14 +020089static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030090{
Sheng Yangcf9e4e12009-02-11 16:03:36 +080091 union kvm_ioapic_redirect_entry *pent;
Gleb Natapov49256632009-02-04 17:28:14 +020092 int injected = -1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030093
94 pent = &ioapic->redirtbl[idx];
95
96 if (!pent->fields.mask) {
Gleb Natapov49256632009-02-04 17:28:14 +020097 injected = ioapic_deliver(ioapic, idx);
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -030098 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030099 pent->fields.remote_irr = 1;
100 }
Gleb Natapov49256632009-02-04 17:28:14 +0200101
102 return injected;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300103}
104
Avi Kivity46a929b2009-12-28 14:08:30 +0200105static void update_handled_vectors(struct kvm_ioapic *ioapic)
106{
107 DECLARE_BITMAP(handled_vectors, 256);
108 int i;
109
110 memset(handled_vectors, 0, sizeof(handled_vectors));
111 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
112 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
113 memcpy(ioapic->handled_vectors, handled_vectors,
114 sizeof(handled_vectors));
115 smp_wmb();
116}
117
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300118static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
119{
120 unsigned index;
Avi Kivity75858a82009-01-04 17:10:50 +0200121 bool mask_before, mask_after;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300122 union kvm_ioapic_redirect_entry *e;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300123
124 switch (ioapic->ioregsel) {
125 case IOAPIC_REG_VERSION:
126 /* Writes are ignored. */
127 break;
128
129 case IOAPIC_REG_APIC_ID:
130 ioapic->id = (val >> 24) & 0xf;
131 break;
132
133 case IOAPIC_REG_ARB_ID:
134 break;
135
136 default:
137 index = (ioapic->ioregsel - 0x10) >> 1;
138
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200139 ioapic_debug("change redir index %x val %x\n", index, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300140 if (index >= IOAPIC_NUM_PINS)
141 return;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300142 e = &ioapic->redirtbl[index];
143 mask_before = e->fields.mask;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300144 if (ioapic->ioregsel & 1) {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300145 e->bits &= 0xffffffff;
146 e->bits |= (u64) val << 32;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300147 } else {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300148 e->bits &= ~0xffffffffULL;
149 e->bits |= (u32) val;
150 e->fields.remote_irr = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300151 }
Avi Kivity46a929b2009-12-28 14:08:30 +0200152 update_handled_vectors(ioapic);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300153 mask_after = e->fields.mask;
Avi Kivity75858a82009-01-04 17:10:50 +0200154 if (mask_before != mask_after)
Gleb Natapov4a994352010-07-11 15:32:23 +0300155 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300156 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300157 && ioapic->irr & (1 << index))
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300158 ioapic_service(ioapic, index);
159 break;
160 }
161}
162
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300163static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300164{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200165 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
166 struct kvm_lapic_irq irqe;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300167
168 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200169 "vector=%x trig_mode=%x\n",
Liu Yuana38f84c2011-04-21 14:53:57 +0800170 entry->fields.dest_id, entry->fields.dest_mode,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200171 entry->fields.delivery_mode, entry->fields.vector,
172 entry->fields.trig_mode);
173
174 irqe.dest_id = entry->fields.dest_id;
175 irqe.vector = entry->fields.vector;
176 irqe.dest_mode = entry->fields.dest_mode;
177 irqe.trig_mode = entry->fields.trig_mode;
178 irqe.delivery_mode = entry->fields.delivery_mode << 8;
179 irqe.level = 1;
180 irqe.shorthand = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300181
Avi Kivity8c35f232008-02-25 10:28:31 +0200182#ifdef CONFIG_X86
Gleb Natapova53c17d2009-03-05 16:34:49 +0200183 /* Always delivery PIT interrupt to vcpu 0 */
Sheng Yang74a3a8f2009-03-04 13:33:02 +0800184 if (irq == 0) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200185 irqe.dest_mode = 0; /* Physical mode. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300186 /* need to read apic_id from apic regiest since
187 * it can be rewritten */
Gleb Natapovd546cb42011-12-15 12:38:40 +0200188 irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
Gleb Natapova53c17d2009-03-05 16:34:49 +0200189 }
Avi Kivity8c35f232008-02-25 10:28:31 +0200190#endif
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200191 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300192}
193
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300194int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
195 int level)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300196{
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300197 u32 old_irr;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300198 u32 mask = 1 << irq;
Sheng Yangcf9e4e12009-02-11 16:03:36 +0800199 union kvm_ioapic_redirect_entry entry;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300200 int ret, irq_level;
201
202 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
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;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300206 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
207 irq_source_id, level);
208 entry = ioapic->redirtbl[irq];
209 irq_level ^= entry.fields.polarity;
210 if (!irq_level) {
211 ioapic->irr &= ~mask;
212 ret = 1;
213 } else {
214 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
215 ioapic->irr |= mask;
216 if ((edge && old_irr != ioapic->irr) ||
217 (!edge && !entry.fields.remote_irr))
218 ret = ioapic_service(ioapic, irq);
219 else
220 ret = 0; /* report coalesced interrupt */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300221 }
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300222 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300223 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300224
Gleb Natapov49256632009-02-04 17:28:14 +0200225 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300226}
227
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300228void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
229{
230 int i;
231
232 spin_lock(&ioapic->lock);
233 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
234 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
235 spin_unlock(&ioapic->lock);
236}
237
Gleb Natapoveba02262009-08-24 11:54:25 +0300238static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
239 int trigger_mode)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300240{
Gleb Natapoveba02262009-08-24 11:54:25 +0300241 int i;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300242
Gleb Natapoveba02262009-08-24 11:54:25 +0300243 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
244 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300245
Gleb Natapoveba02262009-08-24 11:54:25 +0300246 if (ent->fields.vector != vector)
247 continue;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300248
Gleb Natapoveba02262009-08-24 11:54:25 +0300249 /*
250 * We are dropping lock while calling ack notifiers because ack
251 * notifier callbacks for assigned devices call into IOAPIC
252 * recursively. Since remote_irr is cleared only after call
253 * to notifiers if the same vector will be delivered while lock
254 * is dropped it will be put into irr and will be delivered
255 * after ack notifier returns.
256 */
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300257 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300258 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300259 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300260
261 if (trigger_mode != IOAPIC_LEVEL_TRIG)
262 continue;
263
Marcelo Tosattif5244722008-07-26 17:01:00 -0300264 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
265 ent->fields.remote_irr = 0;
Gleb Natapoveba02262009-08-24 11:54:25 +0300266 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
267 ioapic_service(ioapic, i);
Marcelo Tosattif5244722008-07-26 17:01:00 -0300268 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300269}
270
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +0300271bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
272{
273 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
274 smp_rmb();
275 return test_bit(vector, ioapic->handled_vectors);
276}
277
Marcelo Tosattif5244722008-07-26 17:01:00 -0300278void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700279{
280 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700281
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300282 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300283 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300284 spin_unlock(&ioapic->lock);
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700285}
286
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400287static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
288{
289 return container_of(dev, struct kvm_ioapic, dev);
290}
291
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300292static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300293{
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300294 return ((addr >= ioapic->base_address &&
295 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
296}
297
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300298static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
299 void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300300{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400301 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300302 u32 result;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300303 if (!ioapic_in_range(ioapic, addr))
304 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300305
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200306 ioapic_debug("addr %lx\n", (unsigned long)addr);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300307 ASSERT(!(addr & 0xf)); /* check alignment */
308
309 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300310 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300311 switch (addr) {
312 case IOAPIC_REG_SELECT:
313 result = ioapic->ioregsel;
314 break;
315
316 case IOAPIC_REG_WINDOW:
317 result = ioapic_read_indirect(ioapic, addr, len);
318 break;
319
320 default:
321 result = 0;
322 break;
323 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300324 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300325
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300326 switch (len) {
327 case 8:
328 *(u64 *) val = result;
329 break;
330 case 1:
331 case 2:
332 case 4:
333 memcpy(val, (char *)&result, len);
334 break;
335 default:
336 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
337 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300338 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300339}
340
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300341static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
342 const void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300343{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400344 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300345 u32 data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300346 if (!ioapic_in_range(ioapic, addr))
347 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300348
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200349 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
350 (void*)addr, len, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300351 ASSERT(!(addr & 0xf)); /* check alignment */
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300352
Julian Stecklinad77fe632011-11-23 13:54:30 +0100353 switch (len) {
354 case 8:
355 case 4:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300356 data = *(u32 *) val;
Julian Stecklinad77fe632011-11-23 13:54:30 +0100357 break;
358 case 2:
359 data = *(u16 *) val;
360 break;
361 case 1:
362 data = *(u8 *) val;
363 break;
364 default:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300365 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
Gleb Natapoveba02262009-08-24 11:54:25 +0300366 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300367 }
368
369 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300370 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300371 switch (addr) {
372 case IOAPIC_REG_SELECT:
Julian Stecklinad77fe632011-11-23 13:54:30 +0100373 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300374 break;
375
376 case IOAPIC_REG_WINDOW:
377 ioapic_write_indirect(ioapic, data);
378 break;
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800379#ifdef CONFIG_IA64
380 case IOAPIC_REG_EOI:
Gleb Natapoveba02262009-08-24 11:54:25 +0300381 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800382 break;
383#endif
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300384
385 default:
386 break;
387 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300388 spin_unlock(&ioapic->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300389 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300390}
391
Eddie Dong8c392692007-10-10 12:15:54 +0200392void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
393{
394 int i;
395
396 for (i = 0; i < IOAPIC_NUM_PINS; i++)
397 ioapic->redirtbl[i].fields.mask = 1;
398 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
399 ioapic->ioregsel = 0;
400 ioapic->irr = 0;
401 ioapic->id = 0;
Avi Kivity46a929b2009-12-28 14:08:30 +0200402 update_handled_vectors(ioapic);
Eddie Dong8c392692007-10-10 12:15:54 +0200403}
404
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400405static const struct kvm_io_device_ops ioapic_mmio_ops = {
406 .read = ioapic_mmio_read,
407 .write = ioapic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400408};
409
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300410int kvm_ioapic_init(struct kvm *kvm)
411{
412 struct kvm_ioapic *ioapic;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400413 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300414
415 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
416 if (!ioapic)
417 return -ENOMEM;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300418 spin_lock_init(&ioapic->lock);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +0800419 kvm->arch.vioapic = ioapic;
Eddie Dong8c392692007-10-10 12:15:54 +0200420 kvm_ioapic_reset(ioapic);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400421 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300422 ioapic->kvm = kvm;
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200423 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300424 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
425 IOAPIC_MEM_LENGTH, &ioapic->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200426 mutex_unlock(&kvm->slots_lock);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800427 if (ret < 0) {
428 kvm->arch.vioapic = NULL;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400429 kfree(ioapic);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800430 }
Gregory Haskins090b7af2009-07-07 17:08:44 -0400431
432 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300433}
Avi Kivity75858a82009-01-04 17:10:50 +0200434
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800435void kvm_ioapic_destroy(struct kvm *kvm)
436{
437 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
438
439 if (ioapic) {
440 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
441 kvm->arch.vioapic = NULL;
442 kfree(ioapic);
443 }
444}
445
Gleb Natapoveba02262009-08-24 11:54:25 +0300446int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
447{
448 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
449 if (!ioapic)
450 return -EINVAL;
451
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300452 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300453 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300454 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300455 return 0;
456}
457
458int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
459{
460 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
461 if (!ioapic)
462 return -EINVAL;
463
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300464 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300465 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
Avi Kivity46a929b2009-12-28 14:08:30 +0200466 update_handled_vectors(ioapic);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300467 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300468 return 0;
469}