blob: ce82b940195843827be4d22e710942b0683a4618 [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>
Yang Zhangc7c9c562013-01-25 10:18:51 +080038#include <linux/export.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030039#include <asm/processor.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030040#include <asm/page.h>
41#include <asm/current.h>
Gleb Natapov1000ff82009-07-07 16:00:57 +030042#include <trace/events/kvm.h>
Zhang Xiantao82470192007-12-17 13:59:56 +080043
44#include "ioapic.h"
45#include "lapic.h"
Marcelo Tosattif5244722008-07-26 17:01:00 -030046#include "irq.h"
Zhang Xiantao82470192007-12-17 13:59:56 +080047
Laurent Viviere25e3ed2007-10-12 11:01:59 +020048#if 0
49#define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
50#else
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030051#define ioapic_debug(fmt, arg...)
Laurent Viviere25e3ed2007-10-12 11:01:59 +020052#endif
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -030053static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030054
55static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
56 unsigned long addr,
57 unsigned long length)
58{
59 unsigned long result = 0;
60
61 switch (ioapic->ioregsel) {
62 case IOAPIC_REG_VERSION:
63 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
64 | (IOAPIC_VERSION_ID & 0xff));
65 break;
66
67 case IOAPIC_REG_APIC_ID:
68 case IOAPIC_REG_ARB_ID:
69 result = ((ioapic->id & 0xf) << 24);
70 break;
71
72 default:
73 {
74 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
75 u64 redir_content;
76
77 ASSERT(redir_index < IOAPIC_NUM_PINS);
78
79 redir_content = ioapic->redirtbl[redir_index].bits;
80 result = (ioapic->ioregsel & 0x1) ?
81 (redir_content >> 32) & 0xffffffff :
82 redir_content & 0xffffffff;
83 break;
84 }
85 }
86
87 return result;
88}
89
Gleb Natapov49256632009-02-04 17:28:14 +020090static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030091{
Sheng Yangcf9e4e12009-02-11 16:03:36 +080092 union kvm_ioapic_redirect_entry *pent;
Gleb Natapov49256632009-02-04 17:28:14 +020093 int injected = -1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030094
95 pent = &ioapic->redirtbl[idx];
96
97 if (!pent->fields.mask) {
Gleb Natapov49256632009-02-04 17:28:14 +020098 injected = ioapic_deliver(ioapic, idx);
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -030099 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300100 pent->fields.remote_irr = 1;
101 }
Gleb Natapov49256632009-02-04 17:28:14 +0200102
103 return injected;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300104}
105
Avi Kivity46a929b2009-12-28 14:08:30 +0200106static void update_handled_vectors(struct kvm_ioapic *ioapic)
107{
108 DECLARE_BITMAP(handled_vectors, 256);
109 int i;
110
111 memset(handled_vectors, 0, sizeof(handled_vectors));
112 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
113 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
114 memcpy(ioapic->handled_vectors, handled_vectors,
115 sizeof(handled_vectors));
116 smp_wmb();
117}
118
Yang Zhangc7c9c562013-01-25 10:18:51 +0800119void kvm_ioapic_calculate_eoi_exitmap(struct kvm_vcpu *vcpu,
120 u64 *eoi_exit_bitmap)
121{
122 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
123 union kvm_ioapic_redirect_entry *e;
124 struct kvm_lapic_irq irqe;
125 int index;
126
127 spin_lock(&ioapic->lock);
128 /* traverse ioapic entry to set eoi exit bitmap*/
129 for (index = 0; index < IOAPIC_NUM_PINS; index++) {
130 e = &ioapic->redirtbl[index];
131 if (!e->fields.mask &&
132 (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
133 kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC,
134 index))) {
135 irqe.dest_id = e->fields.dest_id;
136 irqe.vector = e->fields.vector;
137 irqe.dest_mode = e->fields.dest_mode;
138 irqe.delivery_mode = e->fields.delivery_mode << 8;
139 kvm_calculate_eoi_exitmap(vcpu, &irqe, eoi_exit_bitmap);
140 }
141 }
142 spin_unlock(&ioapic->lock);
143}
144EXPORT_SYMBOL_GPL(kvm_ioapic_calculate_eoi_exitmap);
145
146void kvm_ioapic_make_eoibitmap_request(struct kvm *kvm)
147{
148 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
149
150 if (!kvm_apic_vid_enabled(kvm) || !ioapic)
151 return;
152 kvm_make_update_eoibitmap_request(kvm);
153}
154
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300155static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
156{
157 unsigned index;
Avi Kivity75858a82009-01-04 17:10:50 +0200158 bool mask_before, mask_after;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300159 union kvm_ioapic_redirect_entry *e;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300160
161 switch (ioapic->ioregsel) {
162 case IOAPIC_REG_VERSION:
163 /* Writes are ignored. */
164 break;
165
166 case IOAPIC_REG_APIC_ID:
167 ioapic->id = (val >> 24) & 0xf;
168 break;
169
170 case IOAPIC_REG_ARB_ID:
171 break;
172
173 default:
174 index = (ioapic->ioregsel - 0x10) >> 1;
175
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200176 ioapic_debug("change redir index %x val %x\n", index, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300177 if (index >= IOAPIC_NUM_PINS)
178 return;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300179 e = &ioapic->redirtbl[index];
180 mask_before = e->fields.mask;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300181 if (ioapic->ioregsel & 1) {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300182 e->bits &= 0xffffffff;
183 e->bits |= (u64) val << 32;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300184 } else {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300185 e->bits &= ~0xffffffffULL;
186 e->bits |= (u32) val;
187 e->fields.remote_irr = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300188 }
Avi Kivity46a929b2009-12-28 14:08:30 +0200189 update_handled_vectors(ioapic);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300190 mask_after = e->fields.mask;
Avi Kivity75858a82009-01-04 17:10:50 +0200191 if (mask_before != mask_after)
Gleb Natapov4a994352010-07-11 15:32:23 +0300192 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300193 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300194 && ioapic->irr & (1 << index))
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300195 ioapic_service(ioapic, index);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800196 kvm_ioapic_make_eoibitmap_request(ioapic->kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300197 break;
198 }
199}
200
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300201static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300202{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200203 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
204 struct kvm_lapic_irq irqe;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300205
206 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200207 "vector=%x trig_mode=%x\n",
Liu Yuana38f84c2011-04-21 14:53:57 +0800208 entry->fields.dest_id, entry->fields.dest_mode,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200209 entry->fields.delivery_mode, entry->fields.vector,
210 entry->fields.trig_mode);
211
212 irqe.dest_id = entry->fields.dest_id;
213 irqe.vector = entry->fields.vector;
214 irqe.dest_mode = entry->fields.dest_mode;
215 irqe.trig_mode = entry->fields.trig_mode;
216 irqe.delivery_mode = entry->fields.delivery_mode << 8;
217 irqe.level = 1;
218 irqe.shorthand = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300219
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200220 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300221}
222
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300223int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
224 int level)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300225{
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300226 u32 old_irr;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300227 u32 mask = 1 << irq;
Sheng Yangcf9e4e12009-02-11 16:03:36 +0800228 union kvm_ioapic_redirect_entry entry;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300229 int ret, irq_level;
230
231 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300232
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300233 spin_lock(&ioapic->lock);
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300234 old_irr = ioapic->irr;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300235 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
236 irq_source_id, level);
237 entry = ioapic->redirtbl[irq];
238 irq_level ^= entry.fields.polarity;
239 if (!irq_level) {
240 ioapic->irr &= ~mask;
241 ret = 1;
242 } else {
243 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
244 ioapic->irr |= mask;
245 if ((edge && old_irr != ioapic->irr) ||
246 (!edge && !entry.fields.remote_irr))
247 ret = ioapic_service(ioapic, irq);
248 else
249 ret = 0; /* report coalesced interrupt */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300250 }
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300251 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300252 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300253
Gleb Natapov49256632009-02-04 17:28:14 +0200254 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300255}
256
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300257void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
258{
259 int i;
260
261 spin_lock(&ioapic->lock);
262 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
263 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
264 spin_unlock(&ioapic->lock);
265}
266
Gleb Natapoveba02262009-08-24 11:54:25 +0300267static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
268 int trigger_mode)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300269{
Gleb Natapoveba02262009-08-24 11:54:25 +0300270 int i;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300271
Gleb Natapoveba02262009-08-24 11:54:25 +0300272 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
273 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300274
Gleb Natapoveba02262009-08-24 11:54:25 +0300275 if (ent->fields.vector != vector)
276 continue;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300277
Gleb Natapoveba02262009-08-24 11:54:25 +0300278 /*
279 * We are dropping lock while calling ack notifiers because ack
280 * notifier callbacks for assigned devices call into IOAPIC
281 * recursively. Since remote_irr is cleared only after call
282 * to notifiers if the same vector will be delivered while lock
283 * is dropped it will be put into irr and will be delivered
284 * after ack notifier returns.
285 */
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300286 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300287 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300288 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300289
290 if (trigger_mode != IOAPIC_LEVEL_TRIG)
291 continue;
292
Marcelo Tosattif5244722008-07-26 17:01:00 -0300293 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
294 ent->fields.remote_irr = 0;
Gleb Natapoveba02262009-08-24 11:54:25 +0300295 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
296 ioapic_service(ioapic, i);
Marcelo Tosattif5244722008-07-26 17:01:00 -0300297 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300298}
299
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +0300300bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
301{
302 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
303 smp_rmb();
304 return test_bit(vector, ioapic->handled_vectors);
305}
306
Marcelo Tosattif5244722008-07-26 17:01:00 -0300307void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700308{
309 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700310
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300311 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300312 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300313 spin_unlock(&ioapic->lock);
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700314}
315
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400316static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
317{
318 return container_of(dev, struct kvm_ioapic, dev);
319}
320
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300321static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300322{
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300323 return ((addr >= ioapic->base_address &&
324 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
325}
326
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300327static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
328 void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300329{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400330 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300331 u32 result;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300332 if (!ioapic_in_range(ioapic, addr))
333 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300334
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200335 ioapic_debug("addr %lx\n", (unsigned long)addr);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300336 ASSERT(!(addr & 0xf)); /* check alignment */
337
338 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300339 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300340 switch (addr) {
341 case IOAPIC_REG_SELECT:
342 result = ioapic->ioregsel;
343 break;
344
345 case IOAPIC_REG_WINDOW:
346 result = ioapic_read_indirect(ioapic, addr, len);
347 break;
348
349 default:
350 result = 0;
351 break;
352 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300353 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300354
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300355 switch (len) {
356 case 8:
357 *(u64 *) val = result;
358 break;
359 case 1:
360 case 2:
361 case 4:
362 memcpy(val, (char *)&result, len);
363 break;
364 default:
365 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
366 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300367 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300368}
369
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300370static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
371 const void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300372{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400373 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300374 u32 data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300375 if (!ioapic_in_range(ioapic, addr))
376 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300377
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200378 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
379 (void*)addr, len, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300380 ASSERT(!(addr & 0xf)); /* check alignment */
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300381
Julian Stecklinad77fe632011-11-23 13:54:30 +0100382 switch (len) {
383 case 8:
384 case 4:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300385 data = *(u32 *) val;
Julian Stecklinad77fe632011-11-23 13:54:30 +0100386 break;
387 case 2:
388 data = *(u16 *) val;
389 break;
390 case 1:
391 data = *(u8 *) val;
392 break;
393 default:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300394 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
Gleb Natapoveba02262009-08-24 11:54:25 +0300395 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300396 }
397
398 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300399 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300400 switch (addr) {
401 case IOAPIC_REG_SELECT:
Julian Stecklinad77fe632011-11-23 13:54:30 +0100402 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300403 break;
404
405 case IOAPIC_REG_WINDOW:
406 ioapic_write_indirect(ioapic, data);
407 break;
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800408#ifdef CONFIG_IA64
409 case IOAPIC_REG_EOI:
Gleb Natapoveba02262009-08-24 11:54:25 +0300410 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800411 break;
412#endif
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300413
414 default:
415 break;
416 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300417 spin_unlock(&ioapic->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300418 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300419}
420
Eddie Dong8c392692007-10-10 12:15:54 +0200421void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
422{
423 int i;
424
425 for (i = 0; i < IOAPIC_NUM_PINS; i++)
426 ioapic->redirtbl[i].fields.mask = 1;
427 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
428 ioapic->ioregsel = 0;
429 ioapic->irr = 0;
430 ioapic->id = 0;
Avi Kivity46a929b2009-12-28 14:08:30 +0200431 update_handled_vectors(ioapic);
Eddie Dong8c392692007-10-10 12:15:54 +0200432}
433
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400434static const struct kvm_io_device_ops ioapic_mmio_ops = {
435 .read = ioapic_mmio_read,
436 .write = ioapic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400437};
438
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300439int kvm_ioapic_init(struct kvm *kvm)
440{
441 struct kvm_ioapic *ioapic;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400442 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300443
444 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
445 if (!ioapic)
446 return -ENOMEM;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300447 spin_lock_init(&ioapic->lock);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +0800448 kvm->arch.vioapic = ioapic;
Eddie Dong8c392692007-10-10 12:15:54 +0200449 kvm_ioapic_reset(ioapic);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400450 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300451 ioapic->kvm = kvm;
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200452 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300453 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
454 IOAPIC_MEM_LENGTH, &ioapic->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200455 mutex_unlock(&kvm->slots_lock);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800456 if (ret < 0) {
457 kvm->arch.vioapic = NULL;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400458 kfree(ioapic);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800459 }
Gregory Haskins090b7af2009-07-07 17:08:44 -0400460
461 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300462}
Avi Kivity75858a82009-01-04 17:10:50 +0200463
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800464void kvm_ioapic_destroy(struct kvm *kvm)
465{
466 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
467
468 if (ioapic) {
469 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
470 kvm->arch.vioapic = NULL;
471 kfree(ioapic);
472 }
473}
474
Gleb Natapoveba02262009-08-24 11:54:25 +0300475int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
476{
477 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
478 if (!ioapic)
479 return -EINVAL;
480
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300481 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300482 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300483 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300484 return 0;
485}
486
487int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
488{
489 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
490 if (!ioapic)
491 return -EINVAL;
492
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300493 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300494 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
Avi Kivity46a929b2009-12-28 14:08:30 +0200495 update_handled_vectors(ioapic);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800496 kvm_ioapic_make_eoibitmap_request(kvm);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300497 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300498 return 0;
499}