blob: c5776febb517cc208dc2fe75a63c030ecd634c3f [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>
Marios Pomonis8c864052019-12-11 12:47:44 -080039#include <linux/nospec.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030040#include <asm/processor.h>
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030041#include <asm/page.h>
42#include <asm/current.h>
Gleb Natapov1000ff82009-07-07 16:00:57 +030043#include <trace/events/kvm.h>
Zhang Xiantao82470192007-12-17 13:59:56 +080044
45#include "ioapic.h"
46#include "lapic.h"
Marcelo Tosattif5244722008-07-26 17:01:00 -030047#include "irq.h"
Zhang Xiantao82470192007-12-17 13:59:56 +080048
Paolo Bonzini0b10a1c2014-03-18 11:51:29 +010049static int ioapic_service(struct kvm_ioapic *vioapic, int irq,
Yang Zhangaa2fbe62013-04-11 19:21:40 +080050 bool line_status);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030051
52static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
53 unsigned long addr,
54 unsigned long length)
55{
56 unsigned long result = 0;
57
58 switch (ioapic->ioregsel) {
59 case IOAPIC_REG_VERSION:
60 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
61 | (IOAPIC_VERSION_ID & 0xff));
62 break;
63
64 case IOAPIC_REG_APIC_ID:
65 case IOAPIC_REG_ARB_ID:
66 result = ((ioapic->id & 0xf) << 24);
67 break;
68
69 default:
70 {
71 u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
Marios Pomonis8c864052019-12-11 12:47:44 -080072 u64 redir_content = ~0ULL;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030073
Marios Pomonis8c864052019-12-11 12:47:44 -080074 if (redir_index < IOAPIC_NUM_PINS) {
75 u32 index = array_index_nospec(
76 redir_index, IOAPIC_NUM_PINS);
77
78 redir_content = ioapic->redirtbl[index].bits;
79 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030080
Eddie Dong1fd4f2a2007-07-18 12:03:39 +030081 result = (ioapic->ioregsel & 0x1) ?
82 (redir_content >> 32) & 0xffffffff :
83 redir_content & 0xffffffff;
84 break;
85 }
86 }
87
88 return result;
89}
90
Yang Zhang10606912013-04-11 19:21:38 +080091static void rtc_irq_eoi_tracking_reset(struct kvm_ioapic *ioapic)
92{
93 ioapic->rtc_status.pending_eoi = 0;
Radim Krčmář81cdb252016-11-23 21:15:27 +010094 bitmap_zero(ioapic->rtc_status.dest_map.map, KVM_MAX_VCPU_ID);
Yang Zhang10606912013-04-11 19:21:38 +080095}
96
Paolo Bonzini4009b242014-03-28 20:41:51 +010097static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic);
98
99static void rtc_status_pending_eoi_check_valid(struct kvm_ioapic *ioapic)
100{
101 if (WARN_ON(ioapic->rtc_status.pending_eoi < 0))
102 kvm_rtc_eoi_tracking_restore_all(ioapic);
103}
104
Yang Zhang10606912013-04-11 19:21:38 +0800105static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
106{
107 bool new_val, old_val;
108 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
Paolo Bonzinib0eaf452016-09-14 23:39:12 +0200109 struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
Yang Zhang10606912013-04-11 19:21:38 +0800110 union kvm_ioapic_redirect_entry *e;
111
112 e = &ioapic->redirtbl[RTC_GSI];
Peter Xu5c69d5c2019-12-04 20:07:20 +0100113 if (!kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT,
114 e->fields.dest_id,
115 kvm_lapic_irq_dest_mode(!!e->fields.dest_mode)))
Yang Zhang10606912013-04-11 19:21:38 +0800116 return;
117
118 new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector);
Paolo Bonzinib0eaf452016-09-14 23:39:12 +0200119 old_val = test_bit(vcpu->vcpu_id, dest_map->map);
Yang Zhang10606912013-04-11 19:21:38 +0800120
121 if (new_val == old_val)
122 return;
123
124 if (new_val) {
Paolo Bonzinib0eaf452016-09-14 23:39:12 +0200125 __set_bit(vcpu->vcpu_id, dest_map->map);
126 dest_map->vectors[vcpu->vcpu_id] = e->fields.vector;
Yang Zhang10606912013-04-11 19:21:38 +0800127 ioapic->rtc_status.pending_eoi++;
128 } else {
Paolo Bonzinib0eaf452016-09-14 23:39:12 +0200129 __clear_bit(vcpu->vcpu_id, dest_map->map);
Yang Zhang10606912013-04-11 19:21:38 +0800130 ioapic->rtc_status.pending_eoi--;
Paolo Bonzini4009b242014-03-28 20:41:51 +0100131 rtc_status_pending_eoi_check_valid(ioapic);
Yang Zhang10606912013-04-11 19:21:38 +0800132 }
Yang Zhang10606912013-04-11 19:21:38 +0800133}
134
135void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
136{
137 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
138
139 spin_lock(&ioapic->lock);
140 __rtc_irq_eoi_tracking_restore_one(vcpu);
141 spin_unlock(&ioapic->lock);
142}
143
144static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)
145{
146 struct kvm_vcpu *vcpu;
147 int i;
148
149 if (RTC_GSI >= IOAPIC_NUM_PINS)
150 return;
151
152 rtc_irq_eoi_tracking_reset(ioapic);
153 kvm_for_each_vcpu(i, vcpu, ioapic->kvm)
154 __rtc_irq_eoi_tracking_restore_one(vcpu);
155}
156
Yang Zhang2c2bf012013-04-11 19:21:41 +0800157static void rtc_irq_eoi(struct kvm_ioapic *ioapic, struct kvm_vcpu *vcpu)
158{
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100159 if (test_and_clear_bit(vcpu->vcpu_id,
160 ioapic->rtc_status.dest_map.map)) {
Yang Zhang2c2bf012013-04-11 19:21:41 +0800161 --ioapic->rtc_status.pending_eoi;
Paolo Bonzini4009b242014-03-28 20:41:51 +0100162 rtc_status_pending_eoi_check_valid(ioapic);
163 }
Yang Zhang2c2bf012013-04-11 19:21:41 +0800164}
165
166static bool rtc_irq_check_coalesced(struct kvm_ioapic *ioapic)
167{
168 if (ioapic->rtc_status.pending_eoi > 0)
169 return true; /* coalesced */
170
171 return false;
172}
173
Paolo Bonzini44847de2014-03-18 12:00:14 +0100174static int ioapic_set_irq(struct kvm_ioapic *ioapic, unsigned int irq,
175 int irq_level, bool line_status)
176{
177 union kvm_ioapic_redirect_entry entry;
178 u32 mask = 1 << irq;
179 u32 old_irr;
180 int edge, ret;
181
182 entry = ioapic->redirtbl[irq];
183 edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
184
185 if (!irq_level) {
186 ioapic->irr &= ~mask;
187 ret = 1;
188 goto out;
189 }
190
191 /*
192 * Return 0 for coalesced interrupts; for edge-triggered interrupts,
193 * this only happens if a previous edge has not been delivered due
Miaohe Lin00116792019-12-11 14:26:23 +0800194 * to masking. For level interrupts, the remote_irr field tells
Paolo Bonzini44847de2014-03-18 12:00:14 +0100195 * us if the interrupt is waiting for an EOI.
196 *
197 * RTC is special: it is edge-triggered, but userspace likes to know
198 * if it has been already ack-ed via EOI because coalesced RTC
199 * interrupts lead to time drift in Windows guests. So we track
200 * EOI manually for the RTC interrupt.
201 */
202 if (irq == RTC_GSI && line_status &&
203 rtc_irq_check_coalesced(ioapic)) {
204 ret = 0;
205 goto out;
206 }
207
208 old_irr = ioapic->irr;
209 ioapic->irr |= mask;
Nikita Leshenko7d225362017-11-05 15:52:31 +0200210 if (edge) {
Wincy Van5bda6ee2014-12-24 11:14:29 +0800211 ioapic->irr_delivered &= ~mask;
Nikita Leshenko7d225362017-11-05 15:52:31 +0200212 if (old_irr == ioapic->irr) {
213 ret = 0;
214 goto out;
215 }
Paolo Bonzini44847de2014-03-18 12:00:14 +0100216 }
217
218 ret = ioapic_service(ioapic, irq, line_status);
219
220out:
221 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
222 return ret;
223}
224
Paolo Bonzini673f7b42014-03-18 11:39:23 +0100225static void kvm_ioapic_inject_all(struct kvm_ioapic *ioapic, unsigned long irr)
226{
227 u32 idx;
228
229 rtc_irq_eoi_tracking_reset(ioapic);
230 for_each_set_bit(idx, &irr, IOAPIC_NUM_PINS)
231 ioapic_set_irq(ioapic, idx, 1, true);
232
233 kvm_rtc_eoi_tracking_restore_all(ioapic);
234}
235
236
Andrey Smetanin63086302015-11-10 15:36:32 +0300237void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, ulong *ioapic_handled_vectors)
Yang Zhangc7c9c562013-01-25 10:18:51 +0800238{
239 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
Joerg Roedel4d99ba82016-02-29 16:04:45 +0100240 struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
Yang Zhangc7c9c562013-01-25 10:18:51 +0800241 union kvm_ioapic_redirect_entry *e;
Yang Zhangc7c9c562013-01-25 10:18:51 +0800242 int index;
243
244 spin_lock(&ioapic->lock);
Joerg Roedel4d99ba82016-02-29 16:04:45 +0100245
246 /* Make sure we see any missing RTC EOI */
247 if (test_bit(vcpu->vcpu_id, dest_map->map))
248 __set_bit(dest_map->vectors[vcpu->vcpu_id],
249 ioapic_handled_vectors);
250
Yang Zhangc7c9c562013-01-25 10:18:51 +0800251 for (index = 0; index < IOAPIC_NUM_PINS; index++) {
252 e = &ioapic->redirtbl[index];
Paolo Bonzini0f6c0a72014-07-30 18:07:24 +0200253 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
254 kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index) ||
255 index == RTC_GSI) {
Peter Xu5c69d5c2019-12-04 20:07:20 +0100256 u16 dm = kvm_lapic_irq_dest_mode(!!e->fields.dest_mode);
257
258 if (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT,
259 e->fields.dest_id, dm) ||
Nikita Leshenko0fc5a362017-11-05 15:52:29 +0200260 kvm_apic_pending_eoi(vcpu, e->fields.vector))
Yang Zhangcf9e65b2013-04-11 19:25:14 +0800261 __set_bit(e->fields.vector,
Andrey Smetanin63086302015-11-10 15:36:32 +0300262 ioapic_handled_vectors);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800263 }
264 }
265 spin_unlock(&ioapic->lock);
266}
Yang Zhangc7c9c562013-01-25 10:18:51 +0800267
David Hildenbrand993225a2017-04-07 10:50:33 +0200268void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm)
Yang Zhangc7c9c562013-01-25 10:18:51 +0800269{
David Hildenbrand0bceb152017-04-07 10:50:25 +0200270 if (!ioapic_in_kernel(kvm))
Yang Zhangc7c9c562013-01-25 10:18:51 +0800271 return;
Yang Zhang3d81bc72013-04-11 19:25:13 +0800272 kvm_make_scan_ioapic_request(kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800273}
274
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300275static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
276{
277 unsigned index;
Avi Kivity75858a82009-01-04 17:10:50 +0200278 bool mask_before, mask_after;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300279 union kvm_ioapic_redirect_entry *e;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -0500280 unsigned long vcpu_bitmap;
281 int old_remote_irr, old_delivery_status, old_dest_id, old_dest_mode;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300282
283 switch (ioapic->ioregsel) {
284 case IOAPIC_REG_VERSION:
285 /* Writes are ignored. */
286 break;
287
288 case IOAPIC_REG_APIC_ID:
289 ioapic->id = (val >> 24) & 0xf;
290 break;
291
292 case IOAPIC_REG_ARB_ID:
293 break;
294
295 default:
296 index = (ioapic->ioregsel - 0x10) >> 1;
297
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300298 if (index >= IOAPIC_NUM_PINS)
299 return;
Gleb Natapov70f93da2009-07-05 18:48:12 +0300300 e = &ioapic->redirtbl[index];
301 mask_before = e->fields.mask;
Nikita Leshenkob200dded2017-11-05 15:52:33 +0200302 /* Preserve read-only fields */
303 old_remote_irr = e->fields.remote_irr;
304 old_delivery_status = e->fields.delivery_status;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -0500305 old_dest_id = e->fields.dest_id;
306 old_dest_mode = e->fields.dest_mode;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300307 if (ioapic->ioregsel & 1) {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300308 e->bits &= 0xffffffff;
309 e->bits |= (u64) val << 32;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300310 } else {
Gleb Natapov70f93da2009-07-05 18:48:12 +0300311 e->bits &= ~0xffffffffULL;
312 e->bits |= (u32) val;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300313 }
Nikita Leshenkob200dded2017-11-05 15:52:33 +0200314 e->fields.remote_irr = old_remote_irr;
315 e->fields.delivery_status = old_delivery_status;
Nikita Leshenkoa8bfec22017-11-05 15:52:32 +0200316
317 /*
318 * Some OSes (Linux, Xen) assume that Remote IRR bit will
319 * be cleared by IOAPIC hardware when the entry is configured
320 * as edge-triggered. This behavior is used to simulate an
321 * explicit EOI on IOAPICs that don't have the EOI register.
322 */
323 if (e->fields.trig_mode == IOAPIC_EDGE_TRIG)
324 e->fields.remote_irr = 0;
325
Gleb Natapov70f93da2009-07-05 18:48:12 +0300326 mask_after = e->fields.mask;
Avi Kivity75858a82009-01-04 17:10:50 +0200327 if (mask_before != mask_after)
Gleb Natapov4a994352010-07-11 15:32:23 +0300328 kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
Gleb Natapov70f93da2009-07-05 18:48:12 +0300329 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
Gleb Natapovb4a2f5e2009-07-05 18:48:11 +0300330 && ioapic->irr & (1 << index))
Yang Zhangaa2fbe62013-04-11 19:21:40 +0800331 ioapic_service(ioapic, index, false);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -0500332 if (e->fields.delivery_mode == APIC_DM_FIXED) {
333 struct kvm_lapic_irq irq;
334
Peter Xu150a84f2019-12-04 20:07:21 +0100335 irq.shorthand = APIC_DEST_NOSHORT;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -0500336 irq.vector = e->fields.vector;
337 irq.delivery_mode = e->fields.delivery_mode << 8;
338 irq.dest_id = e->fields.dest_id;
Peter Xuc96001c2019-12-04 20:07:18 +0100339 irq.dest_mode =
340 kvm_lapic_irq_dest_mode(!!e->fields.dest_mode);
Nitesh Narayan Lal9a2ae9f2019-11-20 07:12:24 -0500341 bitmap_zero(&vcpu_bitmap, 16);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -0500342 kvm_bitmap_or_dest_vcpus(ioapic->kvm, &irq,
343 &vcpu_bitmap);
344 if (old_dest_mode != e->fields.dest_mode ||
345 old_dest_id != e->fields.dest_id) {
346 /*
347 * Update vcpu_bitmap with vcpus specified in
348 * the previous request as well. This is done to
349 * keep ioapic_handled_vectors synchronized.
350 */
351 irq.dest_id = old_dest_id;
Peter Xuc96001c2019-12-04 20:07:18 +0100352 irq.dest_mode =
353 kvm_lapic_irq_dest_mode(
354 !!e->fields.dest_mode);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -0500355 kvm_bitmap_or_dest_vcpus(ioapic->kvm, &irq,
356 &vcpu_bitmap);
357 }
358 kvm_make_scan_ioapic_request_mask(ioapic->kvm,
359 &vcpu_bitmap);
360 } else {
361 kvm_make_scan_ioapic_request(ioapic->kvm);
362 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300363 break;
364 }
365}
366
Paolo Bonzini0b10a1c2014-03-18 11:51:29 +0100367static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300368{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200369 union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
370 struct kvm_lapic_irq irqe;
Yang Zhang2c2bf012013-04-11 19:21:41 +0800371 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300372
Nikita Leshenkoda3fe7b2017-11-05 15:52:30 +0200373 if (entry->fields.mask ||
374 (entry->fields.trig_mode == IOAPIC_LEVEL_TRIG &&
375 entry->fields.remote_irr))
Paolo Bonzini0b10a1c2014-03-18 11:51:29 +0100376 return -1;
377
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200378 irqe.dest_id = entry->fields.dest_id;
379 irqe.vector = entry->fields.vector;
Peter Xuc96001c2019-12-04 20:07:18 +0100380 irqe.dest_mode = kvm_lapic_irq_dest_mode(!!entry->fields.dest_mode);
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200381 irqe.trig_mode = entry->fields.trig_mode;
382 irqe.delivery_mode = entry->fields.delivery_mode << 8;
383 irqe.level = 1;
Peter Xu150a84f2019-12-04 20:07:21 +0100384 irqe.shorthand = APIC_DEST_NOSHORT;
James Sullivan93bbf0b2015-03-18 19:26:03 -0600385 irqe.msi_redir_hint = false;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300386
Paolo Bonzini0bc830b2014-03-18 10:47:17 +0100387 if (irqe.trig_mode == IOAPIC_EDGE_TRIG)
Wincy Van5bda6ee2014-12-24 11:14:29 +0800388 ioapic->irr_delivered |= 1 << irq;
Paolo Bonzini0bc830b2014-03-18 10:47:17 +0100389
Yang Zhang2c2bf012013-04-11 19:21:41 +0800390 if (irq == RTC_GSI && line_status) {
Paolo Bonzini4009b242014-03-28 20:41:51 +0100391 /*
392 * pending_eoi cannot ever become negative (see
393 * rtc_status_pending_eoi_check_valid) and the caller
394 * ensures that it is only called if it is >= zero, namely
395 * if rtc_irq_check_coalesced returns false).
396 */
Yang Zhang2c2bf012013-04-11 19:21:41 +0800397 BUG_ON(ioapic->rtc_status.pending_eoi != 0);
398 ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +0100399 &ioapic->rtc_status.dest_map);
Paolo Bonzini5678de3f2014-03-28 20:41:50 +0100400 ioapic->rtc_status.pending_eoi = (ret < 0 ? 0 : ret);
Yang Zhang2c2bf012013-04-11 19:21:41 +0800401 } else
402 ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
403
Paolo Bonzini0b10a1c2014-03-18 11:51:29 +0100404 if (ret && irqe.trig_mode == IOAPIC_LEVEL_TRIG)
405 entry->fields.remote_irr = 1;
406
Yang Zhang2c2bf012013-04-11 19:21:41 +0800407 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300408}
409
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300410int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
Yang Zhangaa2fbe62013-04-11 19:21:40 +0800411 int level, bool line_status)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300412{
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300413 int ret, irq_level;
414
415 BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300416
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300417 spin_lock(&ioapic->lock);
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300418 irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
419 irq_source_id, level);
Paolo Bonzini44847de2014-03-18 12:00:14 +0100420 ret = ioapic_set_irq(ioapic, irq, irq_level, line_status);
Yang Zhang2c2bf012013-04-11 19:21:41 +0800421
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300422 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300423
Gleb Natapov49256632009-02-04 17:28:14 +0200424 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300425}
426
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300427void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
428{
429 int i;
430
431 spin_lock(&ioapic->lock);
432 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
433 __clear_bit(irq_source_id, &ioapic->irq_states[i]);
434 spin_unlock(&ioapic->lock);
435}
436
Zhang Haoyu184564e2014-09-11 16:47:04 +0800437static void kvm_ioapic_eoi_inject_work(struct work_struct *work)
438{
439 int i;
440 struct kvm_ioapic *ioapic = container_of(work, struct kvm_ioapic,
441 eoi_inject.work);
442 spin_lock(&ioapic->lock);
443 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
444 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
445
446 if (ent->fields.trig_mode != IOAPIC_LEVEL_TRIG)
447 continue;
448
449 if (ioapic->irr & (1 << i) && !ent->fields.remote_irr)
450 ioapic_service(ioapic, i, false);
451 }
452 spin_unlock(&ioapic->lock);
453}
454
455#define IOAPIC_SUCCESSIVE_IRQ_MAX_COUNT 10000
456
Yang Zhang1fcc7892013-04-11 19:21:35 +0800457static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
458 struct kvm_ioapic *ioapic, int vector, int trigger_mode)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300459{
Joerg Roedel4d99ba82016-02-29 16:04:45 +0100460 struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
Radim Krčmářc806a6a2015-03-18 19:38:22 +0100461 struct kvm_lapic *apic = vcpu->arch.apic;
Joerg Roedel4d99ba82016-02-29 16:04:45 +0100462 int i;
463
464 /* RTC special handling */
465 if (test_bit(vcpu->vcpu_id, dest_map->map) &&
466 vector == dest_map->vectors[vcpu->vcpu_id])
467 rtc_irq_eoi(ioapic, vcpu);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300468
Gleb Natapoveba02262009-08-24 11:54:25 +0300469 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
470 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300471
Gleb Natapoveba02262009-08-24 11:54:25 +0300472 if (ent->fields.vector != vector)
473 continue;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300474
Gleb Natapoveba02262009-08-24 11:54:25 +0300475 /*
476 * We are dropping lock while calling ack notifiers because ack
477 * notifier callbacks for assigned devices call into IOAPIC
478 * recursively. Since remote_irr is cleared only after call
479 * to notifiers if the same vector will be delivered while lock
480 * is dropped it will be put into irr and will be delivered
481 * after ack notifier returns.
482 */
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300483 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300484 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300485 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300486
Radim Krčmářc806a6a2015-03-18 19:38:22 +0100487 if (trigger_mode != IOAPIC_LEVEL_TRIG ||
Suravee Suthikulpanitdfb95952016-05-04 14:09:41 -0500488 kvm_lapic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)
Gleb Natapoveba02262009-08-24 11:54:25 +0300489 continue;
490
Marcelo Tosattif5244722008-07-26 17:01:00 -0300491 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
492 ent->fields.remote_irr = 0;
Zhang Haoyu184564e2014-09-11 16:47:04 +0800493 if (!ent->fields.mask && (ioapic->irr & (1 << i))) {
494 ++ioapic->irq_eoi[i];
495 if (ioapic->irq_eoi[i] == IOAPIC_SUCCESSIVE_IRQ_MAX_COUNT) {
496 /*
497 * Real hardware does not deliver the interrupt
498 * immediately during eoi broadcast, and this
499 * lets a buggy guest make slow progress
500 * even if it does not correctly handle a
501 * level-triggered interrupt. Emulate this
502 * behavior if we detect an interrupt storm.
503 */
504 schedule_delayed_work(&ioapic->eoi_inject, HZ / 100);
505 ioapic->irq_eoi[i] = 0;
506 trace_kvm_ioapic_delayed_eoi_inj(ent->bits);
507 } else {
508 ioapic_service(ioapic, i, false);
509 }
510 } else {
511 ioapic->irq_eoi[i] = 0;
512 }
Marcelo Tosattif5244722008-07-26 17:01:00 -0300513 }
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300514}
515
Yang Zhang1fcc7892013-04-11 19:21:35 +0800516void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700517{
Yang Zhang1fcc7892013-04-11 19:21:35 +0800518 struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700519
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300520 spin_lock(&ioapic->lock);
Yang Zhang1fcc7892013-04-11 19:21:35 +0800521 __kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300522 spin_unlock(&ioapic->lock);
Avi Kivity4fa6b9c2008-06-17 15:36:36 -0700523}
524
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400525static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
526{
527 return container_of(dev, struct kvm_ioapic, dev);
528}
529
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300530static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300531{
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300532 return ((addr >= ioapic->base_address &&
533 (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
534}
535
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000536static int ioapic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
537 gpa_t addr, int len, void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300538{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400539 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300540 u32 result;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300541 if (!ioapic_in_range(ioapic, addr))
542 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300543
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300544 ASSERT(!(addr & 0xf)); /* check alignment */
545
546 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300547 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300548 switch (addr) {
549 case IOAPIC_REG_SELECT:
550 result = ioapic->ioregsel;
551 break;
552
553 case IOAPIC_REG_WINDOW:
554 result = ioapic_read_indirect(ioapic, addr, len);
555 break;
556
557 default:
558 result = 0;
559 break;
560 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300561 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300562
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300563 switch (len) {
564 case 8:
565 *(u64 *) val = result;
566 break;
567 case 1:
568 case 2:
569 case 4:
570 memcpy(val, (char *)&result, len);
571 break;
572 default:
573 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
574 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300575 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300576}
577
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000578static int ioapic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
579 gpa_t addr, int len, const void *val)
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300580{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400581 struct kvm_ioapic *ioapic = to_ioapic(this);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300582 u32 data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300583 if (!ioapic_in_range(ioapic, addr))
584 return -EOPNOTSUPP;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300585
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300586 ASSERT(!(addr & 0xf)); /* check alignment */
Marcelo Tosatti60eead72009-06-04 15:08:23 -0300587
Julian Stecklinad77fe632011-11-23 13:54:30 +0100588 switch (len) {
589 case 8:
590 case 4:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300591 data = *(u32 *) val;
Julian Stecklinad77fe632011-11-23 13:54:30 +0100592 break;
593 case 2:
594 data = *(u16 *) val;
595 break;
596 case 1:
597 data = *(u8 *) val;
598 break;
599 default:
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300600 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
Gleb Natapoveba02262009-08-24 11:54:25 +0300601 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300602 }
603
604 addr &= 0xff;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300605 spin_lock(&ioapic->lock);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300606 switch (addr) {
607 case IOAPIC_REG_SELECT:
Julian Stecklinad77fe632011-11-23 13:54:30 +0100608 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300609 break;
610
611 case IOAPIC_REG_WINDOW:
612 ioapic_write_indirect(ioapic, data);
613 break;
614
615 default:
616 break;
617 }
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300618 spin_unlock(&ioapic->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300619 return 0;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300620}
621
Stephen Hemminger79408762013-12-29 12:12:29 -0800622static void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
Eddie Dong8c392692007-10-10 12:15:54 +0200623{
624 int i;
625
Zhang Haoyu184564e2014-09-11 16:47:04 +0800626 cancel_delayed_work_sync(&ioapic->eoi_inject);
Eddie Dong8c392692007-10-10 12:15:54 +0200627 for (i = 0; i < IOAPIC_NUM_PINS; i++)
628 ioapic->redirtbl[i].fields.mask = 1;
629 ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
630 ioapic->ioregsel = 0;
631 ioapic->irr = 0;
Wincy Van5bda6ee2014-12-24 11:14:29 +0800632 ioapic->irr_delivered = 0;
Eddie Dong8c392692007-10-10 12:15:54 +0200633 ioapic->id = 0;
Jiri Slaby86786542016-10-13 17:45:20 +0200634 memset(ioapic->irq_eoi, 0x00, sizeof(ioapic->irq_eoi));
Yang Zhang10606912013-04-11 19:21:38 +0800635 rtc_irq_eoi_tracking_reset(ioapic);
Eddie Dong8c392692007-10-10 12:15:54 +0200636}
637
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400638static const struct kvm_io_device_ops ioapic_mmio_ops = {
639 .read = ioapic_mmio_read,
640 .write = ioapic_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400641};
642
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300643int kvm_ioapic_init(struct kvm *kvm)
644{
645 struct kvm_ioapic *ioapic;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400646 int ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300647
Ben Gardon254272c2019-02-11 11:02:50 -0800648 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL_ACCOUNT);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300649 if (!ioapic)
650 return -ENOMEM;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300651 spin_lock_init(&ioapic->lock);
Zhang Haoyu184564e2014-09-11 16:47:04 +0800652 INIT_DELAYED_WORK(&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +0800653 kvm->arch.vioapic = ioapic;
Eddie Dong8c392692007-10-10 12:15:54 +0200654 kvm_ioapic_reset(ioapic);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400655 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300656 ioapic->kvm = kvm;
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200657 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300658 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
659 IOAPIC_MEM_LENGTH, &ioapic->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200660 mutex_unlock(&kvm->slots_lock);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800661 if (ret < 0) {
662 kvm->arch.vioapic = NULL;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400663 kfree(ioapic);
Wei Yongjun1ae77ba2010-02-09 10:31:09 +0800664 }
Gregory Haskins090b7af2009-07-07 17:08:44 -0400665
666 return ret;
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300667}
Avi Kivity75858a82009-01-04 17:10:50 +0200668
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800669void kvm_ioapic_destroy(struct kvm *kvm)
670{
671 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
672
Peter Xu950712e2017-03-15 16:01:18 +0800673 if (!ioapic)
674 return;
675
Zhang Haoyu184564e2014-09-11 16:47:04 +0800676 cancel_delayed_work_sync(&ioapic->eoi_inject);
David Hildenbrand49f520b2017-04-07 10:50:29 +0200677 mutex_lock(&kvm->slots_lock);
Julia Lawalld90e3a32015-04-27 22:35:34 +0200678 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
David Hildenbrand49f520b2017-04-07 10:50:29 +0200679 mutex_unlock(&kvm->slots_lock);
Julia Lawalld90e3a32015-04-27 22:35:34 +0200680 kvm->arch.vioapic = NULL;
681 kfree(ioapic);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800682}
683
David Hildenbrand33392b42017-04-07 10:50:27 +0200684void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
Gleb Natapoveba02262009-08-24 11:54:25 +0300685{
David Hildenbrand0191e922017-04-07 10:50:24 +0200686 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Gleb Natapoveba02262009-08-24 11:54:25 +0300687
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300688 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300689 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
Wincy Van5bda6ee2014-12-24 11:14:29 +0800690 state->irr &= ~ioapic->irr_delivered;
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300691 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300692}
693
David Hildenbrand33392b42017-04-07 10:50:27 +0200694void kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
Gleb Natapoveba02262009-08-24 11:54:25 +0300695{
David Hildenbrand0191e922017-04-07 10:50:24 +0200696 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Gleb Natapoveba02262009-08-24 11:54:25 +0300697
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300698 spin_lock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300699 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
Paolo Bonzini673f7b42014-03-18 11:39:23 +0100700 ioapic->irr = 0;
Wincy Van5bda6ee2014-12-24 11:14:29 +0800701 ioapic->irr_delivered = 0;
David Hildenbrandca8ab3f2017-04-07 10:50:32 +0200702 kvm_make_scan_ioapic_request(kvm);
Paolo Bonzini673f7b42014-03-18 11:39:23 +0100703 kvm_ioapic_inject_all(ioapic, state->irr);
Marcelo Tosatti46a47b12010-04-23 14:03:38 -0300704 spin_unlock(&ioapic->lock);
Gleb Natapoveba02262009-08-24 11:54:25 +0300705}