blob: 9d76baa78e89728187901de884e977b0bf8ffaca [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
Yang Zhang10606912013-04-11 19:21:38 +080093static void rtc_irq_eoi_tracking_reset(struct kvm_ioapic *ioapic)
94{
95 ioapic->rtc_status.pending_eoi = 0;
96 bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
97}
98
99static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
100{
101 bool new_val, old_val;
102 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
103 union kvm_ioapic_redirect_entry *e;
104
105 e = &ioapic->redirtbl[RTC_GSI];
106 if (!kvm_apic_match_dest(vcpu, NULL, 0, e->fields.dest_id,
107 e->fields.dest_mode))
108 return;
109
110 new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector);
111 old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
112
113 if (new_val == old_val)
114 return;
115
116 if (new_val) {
117 __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
118 ioapic->rtc_status.pending_eoi++;
119 } else {
120 __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
121 ioapic->rtc_status.pending_eoi--;
122 }
123
124 WARN_ON(ioapic->rtc_status.pending_eoi < 0);
125}
126
127void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
128{
129 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
130
131 spin_lock(&ioapic->lock);
132 __rtc_irq_eoi_tracking_restore_one(vcpu);
133 spin_unlock(&ioapic->lock);
134}
135
136static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)
137{
138 struct kvm_vcpu *vcpu;
139 int i;
140
141 if (RTC_GSI >= IOAPIC_NUM_PINS)
142 return;
143
144 rtc_irq_eoi_tracking_reset(ioapic);
145 kvm_for_each_vcpu(i, vcpu, ioapic->kvm)
146 __rtc_irq_eoi_tracking_restore_one(vcpu);
147}
148
Gleb Natapov49256632009-02-04 17:28:14 +0200149static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300150{
Sheng Yangcf9e4e12009-02-11 16:03:36 +0800151 union kvm_ioapic_redirect_entry *pent;
Gleb Natapov49256632009-02-04 17:28:14 +0200152 int injected = -1;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300153
154 pent = &ioapic->redirtbl[idx];
155
156 if (!pent->fields.mask) {
Gleb Natapov49256632009-02-04 17:28:14 +0200157 injected = ioapic_deliver(ioapic, idx);
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300158 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300159 pent->fields.remote_irr = 1;
160 }
Gleb Natapov49256632009-02-04 17:28:14 +0200161
162 return injected;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300163}
164
Avi Kivity46a929b2009-12-28 14:08:30 +0200165static void update_handled_vectors(struct kvm_ioapic *ioapic)
166{
167 DECLARE_BITMAP(handled_vectors, 256);
168 int i;
169
170 memset(handled_vectors, 0, sizeof(handled_vectors));
171 for (i = 0; i < IOAPIC_NUM_PINS; ++i)
172 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
173 memcpy(ioapic->handled_vectors, handled_vectors,
174 sizeof(handled_vectors));
175 smp_wmb();
176}
177
Yang Zhangc7c9c562013-01-25 10:18:51 +0800178void kvm_ioapic_calculate_eoi_exitmap(struct kvm_vcpu *vcpu,
179 u64 *eoi_exit_bitmap)
180{
181 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
182 union kvm_ioapic_redirect_entry *e;
Yang Zhangc7c9c562013-01-25 10:18:51 +0800183 int index;
184
185 spin_lock(&ioapic->lock);
186 /* traverse ioapic entry to set eoi exit bitmap*/
187 for (index = 0; index < IOAPIC_NUM_PINS; index++) {
188 e = &ioapic->redirtbl[index];
189 if (!e->fields.mask &&
190 (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
191 kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC,
192 index))) {
Yang Zhang44944d42013-04-07 08:25:18 +0800193 if (kvm_apic_match_dest(vcpu, NULL, 0,
194 e->fields.dest_id, e->fields.dest_mode))
195 __set_bit(e->fields.vector, (unsigned long *)eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800196 }
197 }
198 spin_unlock(&ioapic->lock);
199}
200EXPORT_SYMBOL_GPL(kvm_ioapic_calculate_eoi_exitmap);
201
202void kvm_ioapic_make_eoibitmap_request(struct kvm *kvm)
203{
204 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
205
206 if (!kvm_apic_vid_enabled(kvm) || !ioapic)
207 return;
208 kvm_make_update_eoibitmap_request(kvm);
209}
210
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300211static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
212{
213 unsigned index;
Avi Kivity75858a82009-01-04 17:10:50 +0200214 bool mask_before, mask_after;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300215 union kvm_ioapic_redirect_entry *e;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300216
217 switch (ioapic->ioregsel) {
218 case IOAPIC_REG_VERSION:
219 /* Writes are ignored. */
220 break;
221
222 case IOAPIC_REG_APIC_ID:
223 ioapic->id = (val >> 24) & 0xf;
224 break;
225
226 case IOAPIC_REG_ARB_ID:
227 break;
228
229 default:
230 index = (ioapic->ioregsel - 0x10) >> 1;
231
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200232 ioapic_debug("change redir index %x val %x\n", index, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300233 if (index >= IOAPIC_NUM_PINS)
234 return;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300235 e = &ioapic->redirtbl[index];
236 mask_before = e->fields.mask;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300237 if (ioapic->ioregsel & 1) {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300238 e->bits &= 0xffffffff;
239 e->bits |= (u64) val << 32;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300240 } else {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300241 e->bits &= ~0xffffffffULL;
242 e->bits |= (u32) val;
243 e->fields.remote_irr = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300244 }
Avi Kivity46a929b2009-12-28 14:08:30 +0200245 update_handled_vectors(ioapic);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300246 mask_after = e->fields.mask;
Avi Kivity75858a82009-01-04 17:10:50 +0200247 if (mask_before != mask_after)
Gleb Natapov4a994352010-07-11 15:32:23 +0300248 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300249 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300250 && ioapic->irr & (1 << index))
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300251 ioapic_service(ioapic, index);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800252 kvm_ioapic_make_eoibitmap_request(ioapic->kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300253 break;
254 }
255}
256
Marcelo Tosattiff4b9df2008-06-05 00:08:11 -0300257static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300258{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200259 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
260 struct kvm_lapic_irq irqe;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300261
262 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200263 "vector=%x trig_mode=%x\n",
Liu Yuana38f84c2011-04-21 14:53:57 +0800264 entry->fields.dest_id, entry->fields.dest_mode,
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200265 entry->fields.delivery_mode, entry->fields.vector,
266 entry->fields.trig_mode);
267
268 irqe.dest_id = entry->fields.dest_id;
269 irqe.vector = entry->fields.vector;
270 irqe.dest_mode = entry->fields.dest_mode;
271 irqe.trig_mode = entry->fields.trig_mode;
272 irqe.delivery_mode = entry->fields.delivery_mode << 8;
273 irqe.level = 1;
274 irqe.shorthand = 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300275
Yang Zhangb4f22252013-04-11 19:21:37 +0800276 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300277}
278
Michael S. Tsirkin1a577b72012-07-19 13:45:20 +0300279int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
280 int level)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300281{
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300282 u32 old_irr;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300283 u32 mask = 1 << irq;
Sheng Yangcf9e4e12009-02-11 16:03:36 +0800284 union kvm_ioapic_redirect_entry entry;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300285 int ret, irq_level;
286
287 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300288
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300289 spin_lock(&ioapic->lock);
Marcelo Tosatti07dc7262010-06-02 11:26:26 -0300290 old_irr = ioapic->irr;
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300291 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
292 irq_source_id, level);
293 entry = ioapic->redirtbl[irq];
294 irq_level ^= entry.fields.polarity;
295 if (!irq_level) {
296 ioapic->irr &= ~mask;
297 ret = 1;
298 } else {
299 int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
300 ioapic->irr |= mask;
301 if ((edge && old_irr != ioapic->irr) ||
302 (!edge && !entry.fields.remote_irr))
303 ret = ioapic_service(ioapic, irq);
304 else
305 ret = 0; /* report coalesced interrupt */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300306 }
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300307 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300308 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300309
Gleb Natapov49256632009-02-04 17:28:14 +0200310 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300311}
312
Michael S. Tsirkin1a577b72012-07-19 13:45:20 +0300313void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
314{
315 int i;
316
317 spin_lock(&ioapic->lock);
318 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
319 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
320 spin_unlock(&ioapic->lock);
321}
322
Yang Zhang1fcc7892013-04-11 19:21:35 +0800323static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
324 struct kvm_ioapic *ioapic, int vector, int trigger_mode)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300325{
Gleb Natapoveba02262009-08-24 11:54:25 +0300326 int i;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300327
Gleb Natapoveba02262009-08-24 11:54:25 +0300328 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
329 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300330
Gleb Natapoveba02262009-08-24 11:54:25 +0300331 if (ent->fields.vector != vector)
332 continue;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300333
Gleb Natapoveba02262009-08-24 11:54:25 +0300334 /*
335 * We are dropping lock while calling ack notifiers because ack
336 * notifier callbacks for assigned devices call into IOAPIC
337 * recursively. Since remote_irr is cleared only after call
338 * to notifiers if the same vector will be delivered while lock
339 * is dropped it will be put into irr and will be delivered
340 * after ack notifier returns.
341 */
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300342 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300343 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300344 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300345
346 if (trigger_mode != IOAPIC_LEVEL_TRIG)
347 continue;
348
Marcelo Tosattif5244722008-07-26 17:01:00 -0300349 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
350 ent->fields.remote_irr = 0;
Gleb Natapoveba02262009-08-24 11:54:25 +0300351 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
352 ioapic_service(ioapic, i);
Marcelo Tosattif5244722008-07-26 17:01:00 -0300353 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300354}
355
Michael S. Tsirkina0c9a8222012-04-11 18:49:55 +0300356bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
357{
358 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
359 smp_rmb();
360 return test_bit(vector, ioapic->handled_vectors);
361}
362
Yang Zhang1fcc7892013-04-11 19:21:35 +0800363void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700364{
Yang Zhang1fcc7892013-04-11 19:21:35 +0800365 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700366
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300367 spin_lock(&ioapic->lock);
Yang Zhang1fcc7892013-04-11 19:21:35 +0800368 __kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300369 spin_unlock(&ioapic->lock);
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700370}
371
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400372static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
373{
374 return container_of(dev, struct kvm_ioapic, dev);
375}
376
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300377static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300378{
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300379 return ((addr >= ioapic->base_address &&
380 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
381}
382
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300383static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
384 void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300385{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400386 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300387 u32 result;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300388 if (!ioapic_in_range(ioapic, addr))
389 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300390
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200391 ioapic_debug("addr %lx\n", (unsigned long)addr);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300392 ASSERT(!(addr & 0xf)); /* check alignment */
393
394 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300395 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300396 switch (addr) {
397 case IOAPIC_REG_SELECT:
398 result = ioapic->ioregsel;
399 break;
400
401 case IOAPIC_REG_WINDOW:
402 result = ioapic_read_indirect(ioapic, addr, len);
403 break;
404
405 default:
406 result = 0;
407 break;
408 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300409 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300410
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300411 switch (len) {
412 case 8:
413 *(u64 *) val = result;
414 break;
415 case 1:
416 case 2:
417 case 4:
418 memcpy(val, (char *)&result, len);
419 break;
420 default:
421 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
422 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300423 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300424}
425
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300426static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
427 const void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300428{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400429 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300430 u32 data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300431 if (!ioapic_in_range(ioapic, addr))
432 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300433
Laurent Viviere25e3ed2007-10-12 11:01:59 +0200434 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
435 (void*)addr, len, val);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300436 ASSERT(!(addr & 0xf)); /* check alignment */
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300437
Julian Stecklinad77fe632011-11-23 13:54:30 +0100438 switch (len) {
439 case 8:
440 case 4:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300441 data = *(u32 *) val;
Julian Stecklinad77fe632011-11-23 13:54:30 +0100442 break;
443 case 2:
444 data = *(u16 *) val;
445 break;
446 case 1:
447 data = *(u8 *) val;
448 break;
449 default:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300450 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
Gleb Natapoveba02262009-08-24 11:54:25 +0300451 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300452 }
453
454 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300455 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300456 switch (addr) {
457 case IOAPIC_REG_SELECT:
Julian Stecklinad77fe632011-11-23 13:54:30 +0100458 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300459 break;
460
461 case IOAPIC_REG_WINDOW:
462 ioapic_write_indirect(ioapic, data);
463 break;
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800464#ifdef CONFIG_IA64
465 case IOAPIC_REG_EOI:
Yang Zhang1fcc7892013-04-11 19:21:35 +0800466 __kvm_ioapic_update_eoi(NULL, ioapic, data, IOAPIC_LEVEL_TRIG);
Zhang Xiantaob1fd3d32007-12-02 22:53:07 +0800467 break;
468#endif
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300469
470 default:
471 break;
472 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300473 spin_unlock(&ioapic->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300474 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300475}
476
Eddie Dong8c392692007-10-10 12:15:54 +0200477void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
478{
479 int i;
480
481 for (i = 0; i < IOAPIC_NUM_PINS; i++)
482 ioapic->redirtbl[i].fields.mask = 1;
483 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
484 ioapic->ioregsel = 0;
485 ioapic->irr = 0;
486 ioapic->id = 0;
Yang Zhang10606912013-04-11 19:21:38 +0800487 rtc_irq_eoi_tracking_reset(ioapic);
Avi Kivity46a929b2009-12-28 14:08:30 +0200488 update_handled_vectors(ioapic);
Eddie Dong8c392692007-10-10 12:15:54 +0200489}
490
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400491static const struct kvm_io_device_ops ioapic_mmio_ops = {
492 .read = ioapic_mmio_read,
493 .write = ioapic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400494};
495
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300496int kvm_ioapic_init(struct kvm *kvm)
497{
498 struct kvm_ioapic *ioapic;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400499 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300500
501 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
502 if (!ioapic)
503 return -ENOMEM;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300504 spin_lock_init(&ioapic->lock);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +0800505 kvm->arch.vioapic = ioapic;
Eddie Dong8c392692007-10-10 12:15:54 +0200506 kvm_ioapic_reset(ioapic);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400507 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300508 ioapic->kvm = kvm;
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200509 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300510 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
511 IOAPIC_MEM_LENGTH, &ioapic->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200512 mutex_unlock(&kvm->slots_lock);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800513 if (ret < 0) {
514 kvm->arch.vioapic = NULL;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400515 kfree(ioapic);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800516 }
Gregory Haskins090b7af2009-07-07 17:08:44 -0400517
518 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300519}
Avi Kivity75858a82009-01-04 17:10:50 +0200520
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800521void kvm_ioapic_destroy(struct kvm *kvm)
522{
523 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
524
525 if (ioapic) {
526 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
527 kvm->arch.vioapic = NULL;
528 kfree(ioapic);
529 }
530}
531
Gleb Natapoveba02262009-08-24 11:54:25 +0300532int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
533{
534 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
535 if (!ioapic)
536 return -EINVAL;
537
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300538 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300539 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300540 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300541 return 0;
542}
543
544int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
545{
546 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
547 if (!ioapic)
548 return -EINVAL;
549
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300550 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300551 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
Avi Kivity46a929b2009-12-28 14:08:30 +0200552 update_handled_vectors(ioapic);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800553 kvm_ioapic_make_eoibitmap_request(kvm);
Yang Zhang10606912013-04-11 19:21:38 +0800554 kvm_rtc_eoi_tracking_restore_all(ioapic);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300555 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300556 return 0;
557}