blob: 5ba005c00e2f76998694d202eca68452600ee5bf [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
Andy Honiga2c118b2013-02-20 14:49:16 -080077 if (redir_index < IOAPIC_NUM_PINS)
78 redir_content =
79 ioapic->redirtbl[redir_index].bits;
80 else
81 redir_content = ~0ULL;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030082
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030083 result = (ioapic->ioregsel & 0x1) ?
84 (redir_content >> 32) & 0xffffffff :
85 redir_content & 0xffffffff;
86 break;
87 }
88 }
89
90 return result;
91}
92
Gleb Natapov49256632009-02-04 17:28:14 +020093static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030094{
Sheng Yangcf9e4e12009-02-11 16:03:36 +080095 union kvm_ioapic_redirect_entry *pent;
Gleb Natapov49256632009-02-04 17:28:14 +020096 int injected = -1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030097
98 pent = &ioapic->redirtbl[idx];
99
100 if (!pent->fields.mask) {
Gleb Natapov49256632009-02-04 17:28:14 +0200101 injected = ioapic_deliver(ioapic, idx);
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300102 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300103 pent->fields.remote_irr = 1;
104 }
Gleb Natapov49256632009-02-04 17:28:14 +0200105
106 return injected;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300107}
108
Avi Kivity46a929b2009-12-28 14:08:30 +0200109static void update_handled_vectors(struct kvm_ioapic *ioapic)
110{
111 DECLARE_BITMAP(handled_vectors, 256);
112 int i;
113
114 memset(handled_vectors, 0, sizeof(handled_vectors));
115 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
116 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
117 memcpy(ioapic->handled_vectors, handled_vectors,
118 sizeof(handled_vectors));
119 smp_wmb();
120}
121
Yang Zhangc7c9c562013-01-25 10:18:51 +0800122void kvm_ioapic_calculate_eoi_exitmap(struct kvm_vcpu *vcpu,
123 u64 *eoi_exit_bitmap)
124{
125 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
126 union kvm_ioapic_redirect_entry *e;
127 struct kvm_lapic_irq irqe;
128 int index;
129
130 spin_lock(&ioapic->lock);
131 /* traverse ioapic entry to set eoi exit bitmap*/
132 for (index = 0; index < IOAPIC_NUM_PINS; index++) {
133 e = &ioapic->redirtbl[index];
134 if (!e->fields.mask &&
135 (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
136 kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC,
137 index))) {
138 irqe.dest_id = e->fields.dest_id;
139 irqe.vector = e->fields.vector;
140 irqe.dest_mode = e->fields.dest_mode;
141 irqe.delivery_mode = e->fields.delivery_mode << 8;
142 kvm_calculate_eoi_exitmap(vcpu, &irqe, eoi_exit_bitmap);
143 }
144 }
145 spin_unlock(&ioapic->lock);
146}
147EXPORT_SYMBOL_GPL(kvm_ioapic_calculate_eoi_exitmap);
148
149void kvm_ioapic_make_eoibitmap_request(struct kvm *kvm)
150{
151 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
152
153 if (!kvm_apic_vid_enabled(kvm) || !ioapic)
154 return;
155 kvm_make_update_eoibitmap_request(kvm);
156}
157
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300158static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
159{
160 unsigned index;
Avi Kivity75858a82009-01-04 17:10:50 +0200161 bool mask_before, mask_after;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300162 union kvm_ioapic_redirect_entry *e;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300163
164 switch (ioapic->ioregsel) {
165 case IOAPIC_REG_VERSION:
166 /* Writes are ignored. */
167 break;
168
169 case IOAPIC_REG_APIC_ID:
170 ioapic->id = (val >> 24) & 0xf;
171 break;
172
173 case IOAPIC_REG_ARB_ID:
174 break;
175
176 default:
177 index = (ioapic->ioregsel - 0x10) >> 1;
178
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200179 ioapic_debug("change redir index %x val %x\n", index, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300180 if (index >= IOAPIC_NUM_PINS)
181 return;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300182 e = &ioapic->redirtbl[index];
183 mask_before = e->fields.mask;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300184 if (ioapic->ioregsel & 1) {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300185 e->bits &= 0xffffffff;
186 e->bits |= (u64) val << 32;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300187 } else {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300188 e->bits &= ~0xffffffffULL;
189 e->bits |= (u32) val;
190 e->fields.remote_irr = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300191 }
Avi Kivity46a929b2009-12-28 14:08:30 +0200192 update_handled_vectors(ioapic);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300193 mask_after = e->fields.mask;
Avi Kivity75858a82009-01-04 17:10:50 +0200194 if (mask_before != mask_after)
Gleb Natapov4a994352010-07-11 15:32:23 +0300195 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300196 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300197 && ioapic->irr & (1 << index))
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300198 ioapic_service(ioapic, index);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800199 kvm_ioapic_make_eoibitmap_request(ioapic->kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300200 break;
201 }
202}
203
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300204static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300205{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200206 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
207 struct kvm_lapic_irq irqe;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300208
209 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200210 "vector=%x trig_mode=%x\n",
Liu Yuana38f84c2011-04-21 14:53:57 +0800211 entry->fields.dest_id, entry->fields.dest_mode,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200212 entry->fields.delivery_mode, entry->fields.vector,
213 entry->fields.trig_mode);
214
215 irqe.dest_id = entry->fields.dest_id;
216 irqe.vector = entry->fields.vector;
217 irqe.dest_mode = entry->fields.dest_mode;
218 irqe.trig_mode = entry->fields.trig_mode;
219 irqe.delivery_mode = entry->fields.delivery_mode << 8;
220 irqe.level = 1;
221 irqe.shorthand = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300222
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200223 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300224}
225
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300226int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
227 int level)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300228{
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300229 u32 old_irr;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300230 u32 mask = 1 << irq;
Sheng Yangcf9e4e12009-02-11 16:03:36 +0800231 union kvm_ioapic_redirect_entry entry;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300232 int ret, irq_level;
233
234 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300235
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300236 spin_lock(&ioapic->lock);
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300237 old_irr = ioapic->irr;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300238 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
239 irq_source_id, level);
240 entry = ioapic->redirtbl[irq];
241 irq_level ^= entry.fields.polarity;
242 if (!irq_level) {
243 ioapic->irr &= ~mask;
244 ret = 1;
245 } else {
246 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
247 ioapic->irr |= mask;
248 if ((edge && old_irr != ioapic->irr) ||
249 (!edge && !entry.fields.remote_irr))
250 ret = ioapic_service(ioapic, irq);
251 else
252 ret = 0; /* report coalesced interrupt */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300253 }
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300254 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300255 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300256
Gleb Natapov49256632009-02-04 17:28:14 +0200257 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300258}
259
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300260void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
261{
262 int i;
263
264 spin_lock(&ioapic->lock);
265 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
266 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
267 spin_unlock(&ioapic->lock);
268}
269
Gleb Natapoveba02262009-08-24 11:54:25 +0300270static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
271 int trigger_mode)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300272{
Gleb Natapoveba02262009-08-24 11:54:25 +0300273 int i;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300274
Gleb Natapoveba02262009-08-24 11:54:25 +0300275 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
276 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300277
Gleb Natapoveba02262009-08-24 11:54:25 +0300278 if (ent->fields.vector != vector)
279 continue;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300280
Gleb Natapoveba02262009-08-24 11:54:25 +0300281 /*
282 * We are dropping lock while calling ack notifiers because ack
283 * notifier callbacks for assigned devices call into IOAPIC
284 * recursively. Since remote_irr is cleared only after call
285 * to notifiers if the same vector will be delivered while lock
286 * is dropped it will be put into irr and will be delivered
287 * after ack notifier returns.
288 */
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300289 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300290 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300291 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300292
293 if (trigger_mode != IOAPIC_LEVEL_TRIG)
294 continue;
295
Marcelo Tosattif5244722008-07-26 17:01:00 -0300296 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
297 ent->fields.remote_irr = 0;
Gleb Natapoveba02262009-08-24 11:54:25 +0300298 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
299 ioapic_service(ioapic, i);
Marcelo Tosattif5244722008-07-26 17:01:00 -0300300 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300301}
302
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +0300303bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
304{
305 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
306 smp_rmb();
307 return test_bit(vector, ioapic->handled_vectors);
308}
309
Marcelo Tosattif5244722008-07-26 17:01:00 -0300310void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700311{
312 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700313
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300314 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300315 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300316 spin_unlock(&ioapic->lock);
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700317}
318
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400319static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
320{
321 return container_of(dev, struct kvm_ioapic, dev);
322}
323
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300324static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300325{
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300326 return ((addr >= ioapic->base_address &&
327 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
328}
329
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300330static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
331 void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300332{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400333 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300334 u32 result;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300335 if (!ioapic_in_range(ioapic, addr))
336 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300337
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200338 ioapic_debug("addr %lx\n", (unsigned long)addr);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300339 ASSERT(!(addr & 0xf)); /* check alignment */
340
341 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300342 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300343 switch (addr) {
344 case IOAPIC_REG_SELECT:
345 result = ioapic->ioregsel;
346 break;
347
348 case IOAPIC_REG_WINDOW:
349 result = ioapic_read_indirect(ioapic, addr, len);
350 break;
351
352 default:
353 result = 0;
354 break;
355 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300356 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300357
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300358 switch (len) {
359 case 8:
360 *(u64 *) val = result;
361 break;
362 case 1:
363 case 2:
364 case 4:
365 memcpy(val, (char *)&result, len);
366 break;
367 default:
368 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
369 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300370 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300371}
372
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300373static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
374 const void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300375{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400376 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300377 u32 data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300378 if (!ioapic_in_range(ioapic, addr))
379 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300380
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200381 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
382 (void*)addr, len, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300383 ASSERT(!(addr & 0xf)); /* check alignment */
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300384
Julian Stecklinad77fe632011-11-23 13:54:30 +0100385 switch (len) {
386 case 8:
387 case 4:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300388 data = *(u32 *) val;
Julian Stecklinad77fe632011-11-23 13:54:30 +0100389 break;
390 case 2:
391 data = *(u16 *) val;
392 break;
393 case 1:
394 data = *(u8 *) val;
395 break;
396 default:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300397 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
Gleb Natapoveba02262009-08-24 11:54:25 +0300398 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300399 }
400
401 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300402 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300403 switch (addr) {
404 case IOAPIC_REG_SELECT:
Julian Stecklinad77fe632011-11-23 13:54:30 +0100405 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300406 break;
407
408 case IOAPIC_REG_WINDOW:
409 ioapic_write_indirect(ioapic, data);
410 break;
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800411#ifdef CONFIG_IA64
412 case IOAPIC_REG_EOI:
Gleb Natapoveba02262009-08-24 11:54:25 +0300413 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800414 break;
415#endif
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300416
417 default:
418 break;
419 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300420 spin_unlock(&ioapic->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300421 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300422}
423
Eddie Dong8c392692007-10-10 12:15:54 +0200424void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
425{
426 int i;
427
428 for (i = 0; i < IOAPIC_NUM_PINS; i++)
429 ioapic->redirtbl[i].fields.mask = 1;
430 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
431 ioapic->ioregsel = 0;
432 ioapic->irr = 0;
433 ioapic->id = 0;
Avi Kivity46a929b2009-12-28 14:08:30 +0200434 update_handled_vectors(ioapic);
Eddie Dong8c392692007-10-10 12:15:54 +0200435}
436
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400437static const struct kvm_io_device_ops ioapic_mmio_ops = {
438 .read = ioapic_mmio_read,
439 .write = ioapic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400440};
441
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300442int kvm_ioapic_init(struct kvm *kvm)
443{
444 struct kvm_ioapic *ioapic;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400445 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300446
447 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
448 if (!ioapic)
449 return -ENOMEM;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300450 spin_lock_init(&ioapic->lock);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +0800451 kvm->arch.vioapic = ioapic;
Eddie Dong8c392692007-10-10 12:15:54 +0200452 kvm_ioapic_reset(ioapic);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400453 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300454 ioapic->kvm = kvm;
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200455 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300456 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
457 IOAPIC_MEM_LENGTH, &ioapic->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200458 mutex_unlock(&kvm->slots_lock);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800459 if (ret < 0) {
460 kvm->arch.vioapic = NULL;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400461 kfree(ioapic);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800462 }
Gregory Haskins090b7af2009-07-07 17:08:44 -0400463
464 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300465}
Avi Kivity75858a82009-01-04 17:10:50 +0200466
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800467void kvm_ioapic_destroy(struct kvm *kvm)
468{
469 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
470
471 if (ioapic) {
472 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
473 kvm->arch.vioapic = NULL;
474 kfree(ioapic);
475 }
476}
477
Gleb Natapoveba02262009-08-24 11:54:25 +0300478int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
479{
480 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
481 if (!ioapic)
482 return -EINVAL;
483
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300484 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300485 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300486 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300487 return 0;
488}
489
490int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
491{
492 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
493 if (!ioapic)
494 return -EINVAL;
495
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300496 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300497 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
Avi Kivity46a929b2009-12-28 14:08:30 +0200498 update_handled_vectors(ioapic);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800499 kvm_ioapic_make_eoibitmap_request(kvm);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300500 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300501 return 0;
502}