Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 MandrakeSoft S.A. |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 3 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 4 | * |
| 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 Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 30 | #include <linux/kvm_host.h> |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 31 | #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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 38 | #include <linux/export.h> |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 39 | #include <asm/processor.h> |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 40 | #include <asm/page.h> |
| 41 | #include <asm/current.h> |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 42 | #include <trace/events/kvm.h> |
Zhang Xiantao | 8247019 | 2007-12-17 13:59:56 +0800 | [diff] [blame] | 43 | |
| 44 | #include "ioapic.h" |
| 45 | #include "lapic.h" |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 46 | #include "irq.h" |
Zhang Xiantao | 8247019 | 2007-12-17 13:59:56 +0800 | [diff] [blame] | 47 | |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 48 | #if 0 |
| 49 | #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) |
| 50 | #else |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 51 | #define ioapic_debug(fmt, arg...) |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 52 | #endif |
Marcelo Tosatti | ff4b9df | 2008-06-05 00:08:11 -0300 | [diff] [blame] | 53 | static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 54 | |
| 55 | static 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 Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 90 | static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 91 | { |
Sheng Yang | cf9e4e1 | 2009-02-11 16:03:36 +0800 | [diff] [blame] | 92 | union kvm_ioapic_redirect_entry *pent; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 93 | int injected = -1; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 94 | |
| 95 | pent = &ioapic->redirtbl[idx]; |
| 96 | |
| 97 | if (!pent->fields.mask) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 98 | injected = ioapic_deliver(ioapic, idx); |
Marcelo Tosatti | ff4b9df | 2008-06-05 00:08:11 -0300 | [diff] [blame] | 99 | if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 100 | pent->fields.remote_irr = 1; |
| 101 | } |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 102 | |
| 103 | return injected; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 104 | } |
| 105 | |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 106 | static 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 Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 119 | void 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 | } |
| 144 | EXPORT_SYMBOL_GPL(kvm_ioapic_calculate_eoi_exitmap); |
| 145 | |
| 146 | void 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 Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 155 | static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val) |
| 156 | { |
| 157 | unsigned index; |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 158 | bool mask_before, mask_after; |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 159 | union kvm_ioapic_redirect_entry *e; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 160 | |
| 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 Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 176 | ioapic_debug("change redir index %x val %x\n", index, val); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 177 | if (index >= IOAPIC_NUM_PINS) |
| 178 | return; |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 179 | e = &ioapic->redirtbl[index]; |
| 180 | mask_before = e->fields.mask; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 181 | if (ioapic->ioregsel & 1) { |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 182 | e->bits &= 0xffffffff; |
| 183 | e->bits |= (u64) val << 32; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 184 | } else { |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 185 | e->bits &= ~0xffffffffULL; |
| 186 | e->bits |= (u32) val; |
| 187 | e->fields.remote_irr = 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 188 | } |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 189 | update_handled_vectors(ioapic); |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 190 | mask_after = e->fields.mask; |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 191 | if (mask_before != mask_after) |
Gleb Natapov | 4a99435 | 2010-07-11 15:32:23 +0300 | [diff] [blame] | 192 | kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after); |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 193 | if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG |
Gleb Natapov | b4a2f5e | 2009-07-05 18:48:11 +0300 | [diff] [blame] | 194 | && ioapic->irr & (1 << index)) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 195 | ioapic_service(ioapic, index); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 196 | kvm_ioapic_make_eoibitmap_request(ioapic->kvm); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
Marcelo Tosatti | ff4b9df | 2008-06-05 00:08:11 -0300 | [diff] [blame] | 201 | static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 202 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 203 | union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq]; |
| 204 | struct kvm_lapic_irq irqe; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 205 | |
| 206 | ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x " |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 207 | "vector=%x trig_mode=%x\n", |
Liu Yuan | a38f84c | 2011-04-21 14:53:57 +0800 | [diff] [blame] | 208 | entry->fields.dest_id, entry->fields.dest_mode, |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 209 | 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 Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 219 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 220 | return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 221 | } |
| 222 | |
Michael S. Tsirkin | 1a577b7 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 223 | int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id, |
| 224 | int level) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 225 | { |
Marcelo Tosatti | 07dc726 | 2010-06-02 11:26:26 -0300 | [diff] [blame] | 226 | u32 old_irr; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 227 | u32 mask = 1 << irq; |
Sheng Yang | cf9e4e1 | 2009-02-11 16:03:36 +0800 | [diff] [blame] | 228 | union kvm_ioapic_redirect_entry entry; |
Michael S. Tsirkin | 28a6fda | 2012-08-14 19:20:28 +0300 | [diff] [blame] | 229 | int ret, irq_level; |
| 230 | |
| 231 | BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 232 | |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 233 | spin_lock(&ioapic->lock); |
Marcelo Tosatti | 07dc726 | 2010-06-02 11:26:26 -0300 | [diff] [blame] | 234 | old_irr = ioapic->irr; |
Michael S. Tsirkin | 28a6fda | 2012-08-14 19:20:28 +0300 | [diff] [blame] | 235 | 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 Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 250 | } |
Michael S. Tsirkin | 28a6fda | 2012-08-14 19:20:28 +0300 | [diff] [blame] | 251 | trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 252 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 253 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 254 | return ret; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 255 | } |
| 256 | |
Michael S. Tsirkin | 1a577b7 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 257 | void 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 Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 267 | static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector, |
| 268 | int trigger_mode) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 269 | { |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 270 | int i; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 271 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 272 | for (i = 0; i < IOAPIC_NUM_PINS; i++) { |
| 273 | union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i]; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 274 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 275 | if (ent->fields.vector != vector) |
| 276 | continue; |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 277 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 278 | /* |
| 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 Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 286 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 287 | kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 288 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 289 | |
| 290 | if (trigger_mode != IOAPIC_LEVEL_TRIG) |
| 291 | continue; |
| 292 | |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 293 | ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG); |
| 294 | ent->fields.remote_irr = 0; |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 295 | if (!ent->fields.mask && (ioapic->irr & (1 << i))) |
| 296 | ioapic_service(ioapic, i); |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 297 | } |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 298 | } |
| 299 | |
Michael S. Tsirkin | a0c9a822 | 2012-04-11 18:49:55 +0300 | [diff] [blame] | 300 | bool 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 Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 307 | void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode) |
Avi Kivity | 4fa6b9c | 2008-06-17 15:36:36 -0700 | [diff] [blame] | 308 | { |
| 309 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; |
Avi Kivity | 4fa6b9c | 2008-06-17 15:36:36 -0700 | [diff] [blame] | 310 | |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 311 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 312 | __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 313 | spin_unlock(&ioapic->lock); |
Avi Kivity | 4fa6b9c | 2008-06-17 15:36:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 316 | static 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. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 321 | static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 322 | { |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 323 | return ((addr >= ioapic->base_address && |
| 324 | (addr < ioapic->base_address + IOAPIC_MEM_LENGTH))); |
| 325 | } |
| 326 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 327 | static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len, |
| 328 | void *val) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 329 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 330 | struct kvm_ioapic *ioapic = to_ioapic(this); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 331 | u32 result; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 332 | if (!ioapic_in_range(ioapic, addr)) |
| 333 | return -EOPNOTSUPP; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 334 | |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 335 | ioapic_debug("addr %lx\n", (unsigned long)addr); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 336 | ASSERT(!(addr & 0xf)); /* check alignment */ |
| 337 | |
| 338 | addr &= 0xff; |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 339 | spin_lock(&ioapic->lock); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 340 | 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 Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 353 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 354 | |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 355 | 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. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 367 | return 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 368 | } |
| 369 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 370 | static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len, |
| 371 | const void *val) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 372 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 373 | struct kvm_ioapic *ioapic = to_ioapic(this); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 374 | u32 data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 375 | if (!ioapic_in_range(ioapic, addr)) |
| 376 | return -EOPNOTSUPP; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 377 | |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 378 | ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n", |
| 379 | (void*)addr, len, val); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 380 | ASSERT(!(addr & 0xf)); /* check alignment */ |
Marcelo Tosatti | 60eead7 | 2009-06-04 15:08:23 -0300 | [diff] [blame] | 381 | |
Julian Stecklina | d77fe63 | 2011-11-23 13:54:30 +0100 | [diff] [blame] | 382 | switch (len) { |
| 383 | case 8: |
| 384 | case 4: |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 385 | data = *(u32 *) val; |
Julian Stecklina | d77fe63 | 2011-11-23 13:54:30 +0100 | [diff] [blame] | 386 | break; |
| 387 | case 2: |
| 388 | data = *(u16 *) val; |
| 389 | break; |
| 390 | case 1: |
| 391 | data = *(u8 *) val; |
| 392 | break; |
| 393 | default: |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 394 | printk(KERN_WARNING "ioapic: Unsupported size %d\n", len); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 395 | return 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | addr &= 0xff; |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 399 | spin_lock(&ioapic->lock); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 400 | switch (addr) { |
| 401 | case IOAPIC_REG_SELECT: |
Julian Stecklina | d77fe63 | 2011-11-23 13:54:30 +0100 | [diff] [blame] | 402 | ioapic->ioregsel = data & 0xFF; /* 8-bit register */ |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 403 | break; |
| 404 | |
| 405 | case IOAPIC_REG_WINDOW: |
| 406 | ioapic_write_indirect(ioapic, data); |
| 407 | break; |
Zhang Xiantao | b1fd3d3 | 2007-12-02 22:53:07 +0800 | [diff] [blame] | 408 | #ifdef CONFIG_IA64 |
| 409 | case IOAPIC_REG_EOI: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 410 | __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG); |
Zhang Xiantao | b1fd3d3 | 2007-12-02 22:53:07 +0800 | [diff] [blame] | 411 | break; |
| 412 | #endif |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 413 | |
| 414 | default: |
| 415 | break; |
| 416 | } |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 417 | spin_unlock(&ioapic->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 418 | return 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 419 | } |
| 420 | |
Eddie Dong | 8c39269 | 2007-10-10 12:15:54 +0200 | [diff] [blame] | 421 | void 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 Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 431 | update_handled_vectors(ioapic); |
Eddie Dong | 8c39269 | 2007-10-10 12:15:54 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 434 | static const struct kvm_io_device_ops ioapic_mmio_ops = { |
| 435 | .read = ioapic_mmio_read, |
| 436 | .write = ioapic_mmio_write, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 437 | }; |
| 438 | |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 439 | int kvm_ioapic_init(struct kvm *kvm) |
| 440 | { |
| 441 | struct kvm_ioapic *ioapic; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 442 | int ret; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 443 | |
| 444 | ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL); |
| 445 | if (!ioapic) |
| 446 | return -ENOMEM; |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 447 | spin_lock_init(&ioapic->lock); |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 448 | kvm->arch.vioapic = ioapic; |
Eddie Dong | 8c39269 | 2007-10-10 12:15:54 +0200 | [diff] [blame] | 449 | kvm_ioapic_reset(ioapic); |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 450 | kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 451 | ioapic->kvm = kvm; |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 452 | mutex_lock(&kvm->slots_lock); |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 453 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address, |
| 454 | IOAPIC_MEM_LENGTH, &ioapic->dev); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 455 | mutex_unlock(&kvm->slots_lock); |
Wei Yongjun | 1ae77ba | 2010-02-09 10:31:09 +0800 | [diff] [blame] | 456 | if (ret < 0) { |
| 457 | kvm->arch.vioapic = NULL; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 458 | kfree(ioapic); |
Wei Yongjun | 1ae77ba | 2010-02-09 10:31:09 +0800 | [diff] [blame] | 459 | } |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 460 | |
| 461 | return ret; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 462 | } |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 463 | |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 464 | void 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 Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 475 | int 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 Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 481 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 482 | memcpy(state, ioapic, sizeof(struct kvm_ioapic_state)); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 483 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int 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 Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 493 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 494 | memcpy(ioapic, state, sizeof(struct kvm_ioapic_state)); |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 495 | update_handled_vectors(ioapic); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 496 | kvm_ioapic_make_eoibitmap_request(kvm); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 497 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 498 | return 0; |
| 499 | } |