Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 MandrakeSoft S.A. |
| 3 | * |
| 4 | * MandrakeSoft S.A. |
| 5 | * 43, rue d'Aboukir |
| 6 | * 75002 Paris - France |
| 7 | * http://www.linux-mandrake.com/ |
| 8 | * http://www.mandrakesoft.com/ |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | * Yunhong Jiang <yunhong.jiang@intel.com> |
| 25 | * Yaozu (Eddie) Dong <eddie.dong@intel.com> |
| 26 | * Based on Xen 3.1 code. |
| 27 | */ |
| 28 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 29 | #include <linux/kvm_host.h> |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 30 | #include <linux/kvm.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/highmem.h> |
| 33 | #include <linux/smp.h> |
| 34 | #include <linux/hrtimer.h> |
| 35 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 37 | #include <asm/processor.h> |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 38 | #include <asm/page.h> |
| 39 | #include <asm/current.h> |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 40 | #include <trace/events/kvm.h> |
Zhang Xiantao | 8247019 | 2007-12-17 13:59:56 +0800 | [diff] [blame] | 41 | |
| 42 | #include "ioapic.h" |
| 43 | #include "lapic.h" |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 44 | #include "irq.h" |
Zhang Xiantao | 8247019 | 2007-12-17 13:59:56 +0800 | [diff] [blame] | 45 | |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 46 | #if 0 |
| 47 | #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) |
| 48 | #else |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 49 | #define ioapic_debug(fmt, arg...) |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 50 | #endif |
Marcelo Tosatti | ff4b9df | 2008-06-05 00:08:11 -0300 | [diff] [blame] | 51 | static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 52 | |
| 53 | static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, |
| 54 | unsigned long addr, |
| 55 | unsigned long length) |
| 56 | { |
| 57 | unsigned long result = 0; |
| 58 | |
| 59 | switch (ioapic->ioregsel) { |
| 60 | case IOAPIC_REG_VERSION: |
| 61 | result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16) |
| 62 | | (IOAPIC_VERSION_ID & 0xff)); |
| 63 | break; |
| 64 | |
| 65 | case IOAPIC_REG_APIC_ID: |
| 66 | case IOAPIC_REG_ARB_ID: |
| 67 | result = ((ioapic->id & 0xf) << 24); |
| 68 | break; |
| 69 | |
| 70 | default: |
| 71 | { |
| 72 | u32 redir_index = (ioapic->ioregsel - 0x10) >> 1; |
| 73 | u64 redir_content; |
| 74 | |
| 75 | ASSERT(redir_index < IOAPIC_NUM_PINS); |
| 76 | |
| 77 | redir_content = ioapic->redirtbl[redir_index].bits; |
| 78 | result = (ioapic->ioregsel & 0x1) ? |
| 79 | (redir_content >> 32) & 0xffffffff : |
| 80 | redir_content & 0xffffffff; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 88 | static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 89 | { |
Sheng Yang | cf9e4e1 | 2009-02-11 16:03:36 +0800 | [diff] [blame] | 90 | union kvm_ioapic_redirect_entry *pent; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 91 | int injected = -1; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 92 | |
| 93 | pent = &ioapic->redirtbl[idx]; |
| 94 | |
| 95 | if (!pent->fields.mask) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 96 | injected = ioapic_deliver(ioapic, idx); |
Marcelo Tosatti | ff4b9df | 2008-06-05 00:08:11 -0300 | [diff] [blame] | 97 | if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 98 | pent->fields.remote_irr = 1; |
| 99 | } |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 100 | |
| 101 | return injected; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 102 | } |
| 103 | |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 104 | static void update_handled_vectors(struct kvm_ioapic *ioapic) |
| 105 | { |
| 106 | DECLARE_BITMAP(handled_vectors, 256); |
| 107 | int i; |
| 108 | |
| 109 | memset(handled_vectors, 0, sizeof(handled_vectors)); |
| 110 | for (i = 0; i < IOAPIC_NUM_PINS; ++i) |
| 111 | __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors); |
| 112 | memcpy(ioapic->handled_vectors, handled_vectors, |
| 113 | sizeof(handled_vectors)); |
| 114 | smp_wmb(); |
| 115 | } |
| 116 | |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 117 | static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val) |
| 118 | { |
| 119 | unsigned index; |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 120 | bool mask_before, mask_after; |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 121 | union kvm_ioapic_redirect_entry *e; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 122 | |
| 123 | switch (ioapic->ioregsel) { |
| 124 | case IOAPIC_REG_VERSION: |
| 125 | /* Writes are ignored. */ |
| 126 | break; |
| 127 | |
| 128 | case IOAPIC_REG_APIC_ID: |
| 129 | ioapic->id = (val >> 24) & 0xf; |
| 130 | break; |
| 131 | |
| 132 | case IOAPIC_REG_ARB_ID: |
| 133 | break; |
| 134 | |
| 135 | default: |
| 136 | index = (ioapic->ioregsel - 0x10) >> 1; |
| 137 | |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 138 | ioapic_debug("change redir index %x val %x\n", index, val); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 139 | if (index >= IOAPIC_NUM_PINS) |
| 140 | return; |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 141 | e = &ioapic->redirtbl[index]; |
| 142 | mask_before = e->fields.mask; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 143 | if (ioapic->ioregsel & 1) { |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 144 | e->bits &= 0xffffffff; |
| 145 | e->bits |= (u64) val << 32; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 146 | } else { |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 147 | e->bits &= ~0xffffffffULL; |
| 148 | e->bits |= (u32) val; |
| 149 | e->fields.remote_irr = 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 150 | } |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 151 | update_handled_vectors(ioapic); |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 152 | mask_after = e->fields.mask; |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 153 | if (mask_before != mask_after) |
| 154 | kvm_fire_mask_notifiers(ioapic->kvm, index, mask_after); |
Gleb Natapov | 70f93da | 2009-07-05 18:48:12 +0300 | [diff] [blame] | 155 | if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG |
Gleb Natapov | b4a2f5e | 2009-07-05 18:48:11 +0300 | [diff] [blame] | 156 | && ioapic->irr & (1 << index)) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 157 | ioapic_service(ioapic, index); |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
Marcelo Tosatti | ff4b9df | 2008-06-05 00:08:11 -0300 | [diff] [blame] | 162 | static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 163 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 164 | union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq]; |
| 165 | struct kvm_lapic_irq irqe; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 166 | |
| 167 | ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x " |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 168 | "vector=%x trig_mode=%x\n", |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 169 | entry->fields.dest, entry->fields.dest_mode, |
| 170 | entry->fields.delivery_mode, entry->fields.vector, |
| 171 | entry->fields.trig_mode); |
| 172 | |
| 173 | irqe.dest_id = entry->fields.dest_id; |
| 174 | irqe.vector = entry->fields.vector; |
| 175 | irqe.dest_mode = entry->fields.dest_mode; |
| 176 | irqe.trig_mode = entry->fields.trig_mode; |
| 177 | irqe.delivery_mode = entry->fields.delivery_mode << 8; |
| 178 | irqe.level = 1; |
| 179 | irqe.shorthand = 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 180 | |
Avi Kivity | 8c35f23 | 2008-02-25 10:28:31 +0200 | [diff] [blame] | 181 | #ifdef CONFIG_X86 |
Gleb Natapov | a53c17d | 2009-03-05 16:34:49 +0200 | [diff] [blame] | 182 | /* Always delivery PIT interrupt to vcpu 0 */ |
Sheng Yang | 74a3a8f | 2009-03-04 13:33:02 +0800 | [diff] [blame] | 183 | if (irq == 0) { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 184 | irqe.dest_mode = 0; /* Physical mode. */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 185 | /* need to read apic_id from apic regiest since |
| 186 | * it can be rewritten */ |
| 187 | irqe.dest_id = ioapic->kvm->bsp_vcpu->vcpu_id; |
Gleb Natapov | a53c17d | 2009-03-05 16:34:49 +0200 | [diff] [blame] | 188 | } |
Avi Kivity | 8c35f23 | 2008-02-25 10:28:31 +0200 | [diff] [blame] | 189 | #endif |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 190 | return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 191 | } |
| 192 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 193 | int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 194 | { |
Marcelo Tosatti | 07dc726 | 2010-06-02 11:26:26 -0300 | [diff] [blame] | 195 | u32 old_irr; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 196 | u32 mask = 1 << irq; |
Sheng Yang | cf9e4e1 | 2009-02-11 16:03:36 +0800 | [diff] [blame] | 197 | union kvm_ioapic_redirect_entry entry; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 198 | int ret = 1; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 199 | |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 200 | spin_lock(&ioapic->lock); |
Marcelo Tosatti | 07dc726 | 2010-06-02 11:26:26 -0300 | [diff] [blame] | 201 | old_irr = ioapic->irr; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 202 | if (irq >= 0 && irq < IOAPIC_NUM_PINS) { |
| 203 | entry = ioapic->redirtbl[irq]; |
| 204 | level ^= entry.fields.polarity; |
| 205 | if (!level) |
| 206 | ioapic->irr &= ~mask; |
| 207 | else { |
Gleb Natapov | b4a2f5e | 2009-07-05 18:48:11 +0300 | [diff] [blame] | 208 | int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 209 | ioapic->irr |= mask; |
Gleb Natapov | b4a2f5e | 2009-07-05 18:48:11 +0300 | [diff] [blame] | 210 | if ((edge && old_irr != ioapic->irr) || |
| 211 | (!edge && !entry.fields.remote_irr)) |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 212 | ret = ioapic_service(ioapic, irq); |
Gleb Natapov | 65a8221 | 2009-09-03 12:10:34 +0300 | [diff] [blame] | 213 | else |
| 214 | ret = 0; /* report coalesced interrupt */ |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 215 | } |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 216 | trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 217 | } |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 218 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 219 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 220 | return ret; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 221 | } |
| 222 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 223 | static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector, |
| 224 | int trigger_mode) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 225 | { |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 226 | int i; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 227 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 228 | for (i = 0; i < IOAPIC_NUM_PINS; i++) { |
| 229 | union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i]; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 230 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 231 | if (ent->fields.vector != vector) |
| 232 | continue; |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 233 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 234 | /* |
| 235 | * We are dropping lock while calling ack notifiers because ack |
| 236 | * notifier callbacks for assigned devices call into IOAPIC |
| 237 | * recursively. Since remote_irr is cleared only after call |
| 238 | * to notifiers if the same vector will be delivered while lock |
| 239 | * is dropped it will be put into irr and will be delivered |
| 240 | * after ack notifier returns. |
| 241 | */ |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 242 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 243 | kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 244 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 245 | |
| 246 | if (trigger_mode != IOAPIC_LEVEL_TRIG) |
| 247 | continue; |
| 248 | |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 249 | ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG); |
| 250 | ent->fields.remote_irr = 0; |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 251 | if (!ent->fields.mask && (ioapic->irr & (1 << i))) |
| 252 | ioapic_service(ioapic, i); |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 253 | } |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 254 | } |
| 255 | |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 256 | 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] | 257 | { |
| 258 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; |
Avi Kivity | 4fa6b9c | 2008-06-17 15:36:36 -0700 | [diff] [blame] | 259 | |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 260 | smp_rmb(); |
| 261 | if (!test_bit(vector, ioapic->handled_vectors)) |
| 262 | return; |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 263 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 264 | __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 265 | spin_unlock(&ioapic->lock); |
Avi Kivity | 4fa6b9c | 2008-06-17 15:36:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 268 | static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev) |
| 269 | { |
| 270 | return container_of(dev, struct kvm_ioapic, dev); |
| 271 | } |
| 272 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 273 | 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] | 274 | { |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 275 | return ((addr >= ioapic->base_address && |
| 276 | (addr < ioapic->base_address + IOAPIC_MEM_LENGTH))); |
| 277 | } |
| 278 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 279 | static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len, |
| 280 | void *val) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 281 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 282 | struct kvm_ioapic *ioapic = to_ioapic(this); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 283 | u32 result; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 284 | if (!ioapic_in_range(ioapic, addr)) |
| 285 | return -EOPNOTSUPP; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 286 | |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 287 | ioapic_debug("addr %lx\n", (unsigned long)addr); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 288 | ASSERT(!(addr & 0xf)); /* check alignment */ |
| 289 | |
| 290 | addr &= 0xff; |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 291 | spin_lock(&ioapic->lock); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 292 | switch (addr) { |
| 293 | case IOAPIC_REG_SELECT: |
| 294 | result = ioapic->ioregsel; |
| 295 | break; |
| 296 | |
| 297 | case IOAPIC_REG_WINDOW: |
| 298 | result = ioapic_read_indirect(ioapic, addr, len); |
| 299 | break; |
| 300 | |
| 301 | default: |
| 302 | result = 0; |
| 303 | break; |
| 304 | } |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 305 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 306 | |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 307 | switch (len) { |
| 308 | case 8: |
| 309 | *(u64 *) val = result; |
| 310 | break; |
| 311 | case 1: |
| 312 | case 2: |
| 313 | case 4: |
| 314 | memcpy(val, (char *)&result, len); |
| 315 | break; |
| 316 | default: |
| 317 | printk(KERN_WARNING "ioapic: wrong length %d\n", len); |
| 318 | } |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 319 | return 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 320 | } |
| 321 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 322 | static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len, |
| 323 | const void *val) |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 324 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 325 | struct kvm_ioapic *ioapic = to_ioapic(this); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 326 | u32 data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 327 | if (!ioapic_in_range(ioapic, addr)) |
| 328 | return -EOPNOTSUPP; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 329 | |
Laurent Vivier | e25e3ed | 2007-10-12 11:01:59 +0200 | [diff] [blame] | 330 | ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n", |
| 331 | (void*)addr, len, val); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 332 | ASSERT(!(addr & 0xf)); /* check alignment */ |
Marcelo Tosatti | 60eead7 | 2009-06-04 15:08:23 -0300 | [diff] [blame] | 333 | |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 334 | if (len == 4 || len == 8) |
| 335 | data = *(u32 *) val; |
| 336 | else { |
| 337 | printk(KERN_WARNING "ioapic: Unsupported size %d\n", len); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 338 | return 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | addr &= 0xff; |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 342 | spin_lock(&ioapic->lock); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 343 | switch (addr) { |
| 344 | case IOAPIC_REG_SELECT: |
| 345 | ioapic->ioregsel = data; |
| 346 | break; |
| 347 | |
| 348 | case IOAPIC_REG_WINDOW: |
| 349 | ioapic_write_indirect(ioapic, data); |
| 350 | break; |
Zhang Xiantao | b1fd3d3 | 2007-12-02 22:53:07 +0800 | [diff] [blame] | 351 | #ifdef CONFIG_IA64 |
| 352 | case IOAPIC_REG_EOI: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 353 | __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG); |
Zhang Xiantao | b1fd3d3 | 2007-12-02 22:53:07 +0800 | [diff] [blame] | 354 | break; |
| 355 | #endif |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 356 | |
| 357 | default: |
| 358 | break; |
| 359 | } |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 360 | spin_unlock(&ioapic->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 361 | return 0; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 362 | } |
| 363 | |
Eddie Dong | 8c39269 | 2007-10-10 12:15:54 +0200 | [diff] [blame] | 364 | void kvm_ioapic_reset(struct kvm_ioapic *ioapic) |
| 365 | { |
| 366 | int i; |
| 367 | |
| 368 | for (i = 0; i < IOAPIC_NUM_PINS; i++) |
| 369 | ioapic->redirtbl[i].fields.mask = 1; |
| 370 | ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS; |
| 371 | ioapic->ioregsel = 0; |
| 372 | ioapic->irr = 0; |
| 373 | ioapic->id = 0; |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 374 | update_handled_vectors(ioapic); |
Eddie Dong | 8c39269 | 2007-10-10 12:15:54 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 377 | static const struct kvm_io_device_ops ioapic_mmio_ops = { |
| 378 | .read = ioapic_mmio_read, |
| 379 | .write = ioapic_mmio_write, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 380 | }; |
| 381 | |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 382 | int kvm_ioapic_init(struct kvm *kvm) |
| 383 | { |
| 384 | struct kvm_ioapic *ioapic; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 385 | int ret; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 386 | |
| 387 | ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL); |
| 388 | if (!ioapic) |
| 389 | return -ENOMEM; |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 390 | spin_lock_init(&ioapic->lock); |
Zhang Xiantao | d7deeeb | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 391 | kvm->arch.vioapic = ioapic; |
Eddie Dong | 8c39269 | 2007-10-10 12:15:54 +0200 | [diff] [blame] | 392 | kvm_ioapic_reset(ioapic); |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 393 | kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 394 | ioapic->kvm = kvm; |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 395 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 396 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &ioapic->dev); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 397 | mutex_unlock(&kvm->slots_lock); |
Wei Yongjun | 1ae77ba | 2010-02-09 10:31:09 +0800 | [diff] [blame] | 398 | if (ret < 0) { |
| 399 | kvm->arch.vioapic = NULL; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 400 | kfree(ioapic); |
Wei Yongjun | 1ae77ba | 2010-02-09 10:31:09 +0800 | [diff] [blame] | 401 | } |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 402 | |
| 403 | return ret; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 404 | } |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 405 | |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 406 | void kvm_ioapic_destroy(struct kvm *kvm) |
| 407 | { |
| 408 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; |
| 409 | |
| 410 | if (ioapic) { |
| 411 | kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev); |
| 412 | kvm->arch.vioapic = NULL; |
| 413 | kfree(ioapic); |
| 414 | } |
| 415 | } |
| 416 | |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 417 | int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state) |
| 418 | { |
| 419 | struct kvm_ioapic *ioapic = ioapic_irqchip(kvm); |
| 420 | if (!ioapic) |
| 421 | return -EINVAL; |
| 422 | |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 423 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 424 | memcpy(state, ioapic, sizeof(struct kvm_ioapic_state)); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 425 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state) |
| 430 | { |
| 431 | struct kvm_ioapic *ioapic = ioapic_irqchip(kvm); |
| 432 | if (!ioapic) |
| 433 | return -EINVAL; |
| 434 | |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 435 | spin_lock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 436 | memcpy(ioapic, state, sizeof(struct kvm_ioapic_state)); |
Avi Kivity | 46a929b | 2009-12-28 14:08:30 +0200 | [diff] [blame] | 437 | update_handled_vectors(ioapic); |
Marcelo Tosatti | 46a47b1 | 2010-04-23 14:03:38 -0300 | [diff] [blame] | 438 | spin_unlock(&ioapic->lock); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 439 | return 0; |
| 440 | } |