Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1 | /* |
| 2 | * 8259 interrupt controller emulation |
| 3 | * |
| 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
| 5 | * Copyright (c) 2007 Intel Corporation |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 6 | * Copyright 2009 Red Hat, Inc. and/or its affiliates. |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | * Authors: |
| 26 | * Yaozu (Eddie) Dong <Eddie.dong@intel.com> |
| 27 | * Port from Qemu. |
| 28 | */ |
| 29 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Avi Kivity | 3f35385 | 2008-12-21 22:48:32 +0200 | [diff] [blame] | 31 | #include <linux/bitops.h> |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 32 | #include "irq.h" |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 33 | |
| 34 | #include <linux/kvm_host.h> |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 35 | #include "trace.h" |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 36 | |
Avi Kivity | 073d461 | 2010-05-03 17:34:34 +0300 | [diff] [blame] | 37 | static void pic_irq_request(struct kvm *kvm, int level); |
| 38 | |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 39 | static void pic_lock(struct kvm_pic *s) |
| 40 | __acquires(&s->lock) |
| 41 | { |
Avi Kivity | f4f5105 | 2010-09-19 18:44:07 +0200 | [diff] [blame] | 42 | spin_lock(&s->lock); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void pic_unlock(struct kvm_pic *s) |
| 46 | __releases(&s->lock) |
| 47 | { |
| 48 | bool wakeup = s->wakeup_needed; |
Chris Lalancette | 529df65 | 2010-06-21 11:29:40 -0400 | [diff] [blame] | 49 | struct kvm_vcpu *vcpu, *found = NULL; |
| 50 | int i; |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 51 | |
| 52 | s->wakeup_needed = false; |
| 53 | |
Avi Kivity | f4f5105 | 2010-09-19 18:44:07 +0200 | [diff] [blame] | 54 | spin_unlock(&s->lock); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 55 | |
| 56 | if (wakeup) { |
Chris Lalancette | 529df65 | 2010-06-21 11:29:40 -0400 | [diff] [blame] | 57 | kvm_for_each_vcpu(i, vcpu, s->kvm) { |
| 58 | if (kvm_apic_accept_pic_intr(vcpu)) { |
| 59 | found = vcpu; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (!found) |
Avi Kivity | ae0635b | 2010-07-27 14:51:44 +0300 | [diff] [blame] | 65 | return; |
| 66 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 67 | kvm_make_request(KVM_REQ_EVENT, found); |
Chris Lalancette | 529df65 | 2010-06-21 11:29:40 -0400 | [diff] [blame] | 68 | kvm_vcpu_kick(found); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 72 | static void pic_clear_isr(struct kvm_kpic_state *s, int irq) |
| 73 | { |
| 74 | s->isr &= ~(1 << irq); |
Gleb Natapov | 938396a | 2009-08-04 15:30:28 +0300 | [diff] [blame] | 75 | if (s != &s->pics_state->pics[0]) |
| 76 | irq += 8; |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 77 | /* |
| 78 | * We are dropping lock while calling ack notifiers since ack |
| 79 | * notifier callbacks for assigned devices call into PIC recursively. |
| 80 | * Other interrupt may be delivered to PIC while lock is dropped but |
| 81 | * it should be safe since PIC state is already updated at this stage. |
| 82 | */ |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 83 | pic_unlock(s->pics_state); |
Gleb Natapov | 938396a | 2009-08-04 15:30:28 +0300 | [diff] [blame] | 84 | kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 85 | pic_lock(s->pics_state); |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 86 | } |
| 87 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 88 | /* |
| 89 | * set irq level. If an edge is detected, then the IRR is set to 1 |
| 90 | */ |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 91 | static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 92 | { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 93 | int mask, ret = 1; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 94 | mask = 1 << irq; |
| 95 | if (s->elcr & mask) /* level triggered */ |
| 96 | if (level) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 97 | ret = !(s->irr & mask); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 98 | s->irr |= mask; |
| 99 | s->last_irr |= mask; |
| 100 | } else { |
| 101 | s->irr &= ~mask; |
| 102 | s->last_irr &= ~mask; |
| 103 | } |
| 104 | else /* edge triggered */ |
| 105 | if (level) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 106 | if ((s->last_irr & mask) == 0) { |
| 107 | ret = !(s->irr & mask); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 108 | s->irr |= mask; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 109 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 110 | s->last_irr |= mask; |
| 111 | } else |
| 112 | s->last_irr &= ~mask; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 113 | |
| 114 | return (s->imr & mask) ? -1 : ret; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * return the highest priority found in mask (highest = smallest |
| 119 | * number). Return 8 if no irq |
| 120 | */ |
| 121 | static inline int get_priority(struct kvm_kpic_state *s, int mask) |
| 122 | { |
| 123 | int priority; |
| 124 | if (mask == 0) |
| 125 | return 8; |
| 126 | priority = 0; |
| 127 | while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) |
| 128 | priority++; |
| 129 | return priority; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * return the pic wanted interrupt. return -1 if none |
| 134 | */ |
| 135 | static int pic_get_irq(struct kvm_kpic_state *s) |
| 136 | { |
| 137 | int mask, cur_priority, priority; |
| 138 | |
| 139 | mask = s->irr & ~s->imr; |
| 140 | priority = get_priority(s, mask); |
| 141 | if (priority == 8) |
| 142 | return -1; |
| 143 | /* |
| 144 | * compute current priority. If special fully nested mode on the |
| 145 | * master, the IRQ coming from the slave is not taken into account |
| 146 | * for the priority computation. |
| 147 | */ |
| 148 | mask = s->isr; |
| 149 | if (s->special_fully_nested_mode && s == &s->pics_state->pics[0]) |
| 150 | mask &= ~(1 << 2); |
| 151 | cur_priority = get_priority(s, mask); |
| 152 | if (priority < cur_priority) |
| 153 | /* |
| 154 | * higher priority found: an irq should be generated |
| 155 | */ |
| 156 | return (priority + s->priority_add) & 7; |
| 157 | else |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * raise irq to CPU if necessary. must be called every time the active |
| 163 | * irq may change |
| 164 | */ |
| 165 | static void pic_update_irq(struct kvm_pic *s) |
| 166 | { |
| 167 | int irq2, irq; |
| 168 | |
| 169 | irq2 = pic_get_irq(&s->pics[1]); |
| 170 | if (irq2 >= 0) { |
| 171 | /* |
| 172 | * if irq request by slave pic, signal master PIC |
| 173 | */ |
| 174 | pic_set_irq1(&s->pics[0], 2, 1); |
| 175 | pic_set_irq1(&s->pics[0], 2, 0); |
| 176 | } |
| 177 | irq = pic_get_irq(&s->pics[0]); |
Avi Kivity | 36633f3 | 2010-05-03 17:38:06 +0300 | [diff] [blame] | 178 | pic_irq_request(s->kvm, irq >= 0); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 179 | } |
| 180 | |
He, Qing | 6ceb9d7 | 2007-07-26 11:05:18 +0300 | [diff] [blame] | 181 | void kvm_pic_update_irq(struct kvm_pic *s) |
| 182 | { |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 183 | pic_lock(s); |
He, Qing | 6ceb9d7 | 2007-07-26 11:05:18 +0300 | [diff] [blame] | 184 | pic_update_irq(s); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 185 | pic_unlock(s); |
He, Qing | 6ceb9d7 | 2007-07-26 11:05:18 +0300 | [diff] [blame] | 186 | } |
| 187 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 188 | int kvm_pic_set_irq(void *opaque, int irq, int level) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 189 | { |
| 190 | struct kvm_pic *s = opaque; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 191 | int ret = -1; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 192 | |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 193 | pic_lock(s); |
Ben-Ami Yassour | c65bbfa | 2008-07-06 17:15:07 +0300 | [diff] [blame] | 194 | if (irq >= 0 && irq < PIC_NUM_PINS) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 195 | ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level); |
Ben-Ami Yassour | c65bbfa | 2008-07-06 17:15:07 +0300 | [diff] [blame] | 196 | pic_update_irq(s); |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 197 | trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr, |
| 198 | s->pics[irq >> 3].imr, ret == 0); |
Ben-Ami Yassour | c65bbfa | 2008-07-06 17:15:07 +0300 | [diff] [blame] | 199 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 200 | pic_unlock(s); |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 201 | |
| 202 | return ret; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * acknowledge interrupt 'irq' |
| 207 | */ |
| 208 | static inline void pic_intack(struct kvm_kpic_state *s, int irq) |
| 209 | { |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 210 | s->isr |= 1 << irq; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 211 | /* |
| 212 | * We don't clear a level sensitive interrupt here |
| 213 | */ |
| 214 | if (!(s->elcr & (1 << irq))) |
| 215 | s->irr &= ~(1 << irq); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 216 | |
| 217 | if (s->auto_eoi) { |
| 218 | if (s->rotate_on_auto_eoi) |
| 219 | s->priority_add = (irq + 1) & 7; |
| 220 | pic_clear_isr(s, irq); |
| 221 | } |
| 222 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 223 | } |
| 224 | |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 225 | int kvm_pic_read_irq(struct kvm *kvm) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 226 | { |
| 227 | int irq, irq2, intno; |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 228 | struct kvm_pic *s = pic_irqchip(kvm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 229 | |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 230 | pic_lock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 231 | irq = pic_get_irq(&s->pics[0]); |
| 232 | if (irq >= 0) { |
| 233 | pic_intack(&s->pics[0], irq); |
| 234 | if (irq == 2) { |
| 235 | irq2 = pic_get_irq(&s->pics[1]); |
| 236 | if (irq2 >= 0) |
| 237 | pic_intack(&s->pics[1], irq2); |
| 238 | else |
| 239 | /* |
| 240 | * spurious IRQ on slave controller |
| 241 | */ |
| 242 | irq2 = 7; |
| 243 | intno = s->pics[1].irq_base + irq2; |
| 244 | irq = irq2 + 8; |
| 245 | } else |
| 246 | intno = s->pics[0].irq_base + irq; |
| 247 | } else { |
| 248 | /* |
| 249 | * spurious IRQ on host controller |
| 250 | */ |
| 251 | irq = 7; |
| 252 | intno = s->pics[0].irq_base + irq; |
| 253 | } |
| 254 | pic_update_irq(s); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 255 | pic_unlock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 256 | |
| 257 | return intno; |
| 258 | } |
| 259 | |
Eddie Dong | 2fcceae | 2007-10-10 12:14:25 +0200 | [diff] [blame] | 260 | void kvm_pic_reset(struct kvm_kpic_state *s) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 261 | { |
Gleb Natapov | 79c727d | 2009-08-24 11:54:18 +0300 | [diff] [blame] | 262 | int irq; |
Avi Kivity | 073d461 | 2010-05-03 17:34:34 +0300 | [diff] [blame] | 263 | struct kvm_vcpu *vcpu0 = s->pics_state->kvm->bsp_vcpu; |
Gleb Natapov | 79c727d | 2009-08-24 11:54:18 +0300 | [diff] [blame] | 264 | u8 irr = s->irr, isr = s->imr; |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 265 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 266 | s->last_irr = 0; |
| 267 | s->irr = 0; |
| 268 | s->imr = 0; |
| 269 | s->isr = 0; |
| 270 | s->priority_add = 0; |
| 271 | s->irq_base = 0; |
| 272 | s->read_reg_select = 0; |
| 273 | s->poll = 0; |
| 274 | s->special_mask = 0; |
| 275 | s->init_state = 0; |
| 276 | s->auto_eoi = 0; |
| 277 | s->rotate_on_auto_eoi = 0; |
| 278 | s->special_fully_nested_mode = 0; |
| 279 | s->init4 = 0; |
Gleb Natapov | 79c727d | 2009-08-24 11:54:18 +0300 | [diff] [blame] | 280 | |
| 281 | for (irq = 0; irq < PIC_NUM_PINS/2; irq++) { |
| 282 | if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0)) |
| 283 | if (irr & (1 << irq) || isr & (1 << irq)) { |
| 284 | pic_clear_isr(s, irq); |
| 285 | } |
| 286 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static void pic_ioport_write(void *opaque, u32 addr, u32 val) |
| 290 | { |
| 291 | struct kvm_kpic_state *s = opaque; |
| 292 | int priority, cmd, irq; |
| 293 | |
| 294 | addr &= 1; |
| 295 | if (addr == 0) { |
| 296 | if (val & 0x10) { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 297 | s->init4 = val & 1; |
Avi Kivity | 9ed049c | 2010-08-30 12:18:24 +0300 | [diff] [blame] | 298 | s->last_irr = 0; |
| 299 | s->imr = 0; |
| 300 | s->priority_add = 0; |
| 301 | s->special_mask = 0; |
| 302 | s->read_reg_select = 0; |
| 303 | if (!s->init4) { |
| 304 | s->special_fully_nested_mode = 0; |
| 305 | s->auto_eoi = 0; |
| 306 | } |
| 307 | s->init_state = 1; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 308 | if (val & 0x02) |
| 309 | printk(KERN_ERR "single mode not supported"); |
| 310 | if (val & 0x08) |
| 311 | printk(KERN_ERR |
| 312 | "level sensitive irq not supported"); |
| 313 | } else if (val & 0x08) { |
| 314 | if (val & 0x04) |
| 315 | s->poll = 1; |
| 316 | if (val & 0x02) |
| 317 | s->read_reg_select = val & 1; |
| 318 | if (val & 0x40) |
| 319 | s->special_mask = (val >> 5) & 1; |
| 320 | } else { |
| 321 | cmd = val >> 5; |
| 322 | switch (cmd) { |
| 323 | case 0: |
| 324 | case 4: |
| 325 | s->rotate_on_auto_eoi = cmd >> 2; |
| 326 | break; |
| 327 | case 1: /* end of interrupt */ |
| 328 | case 5: |
| 329 | priority = get_priority(s, s->isr); |
| 330 | if (priority != 8) { |
| 331 | irq = (priority + s->priority_add) & 7; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 332 | if (cmd == 5) |
| 333 | s->priority_add = (irq + 1) & 7; |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 334 | pic_clear_isr(s, irq); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 335 | pic_update_irq(s->pics_state); |
| 336 | } |
| 337 | break; |
| 338 | case 3: |
| 339 | irq = val & 7; |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 340 | pic_clear_isr(s, irq); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 341 | pic_update_irq(s->pics_state); |
| 342 | break; |
| 343 | case 6: |
| 344 | s->priority_add = (val + 1) & 7; |
| 345 | pic_update_irq(s->pics_state); |
| 346 | break; |
| 347 | case 7: |
| 348 | irq = val & 7; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 349 | s->priority_add = (irq + 1) & 7; |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 350 | pic_clear_isr(s, irq); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 351 | pic_update_irq(s->pics_state); |
| 352 | break; |
| 353 | default: |
| 354 | break; /* no operation */ |
| 355 | } |
| 356 | } |
| 357 | } else |
| 358 | switch (s->init_state) { |
Gleb Natapov | 9195c4d | 2010-07-15 12:24:37 +0300 | [diff] [blame] | 359 | case 0: { /* normal mode */ |
| 360 | u8 imr_diff = s->imr ^ val, |
| 361 | off = (s == &s->pics_state->pics[0]) ? 0 : 8; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 362 | s->imr = val; |
Gleb Natapov | 9195c4d | 2010-07-15 12:24:37 +0300 | [diff] [blame] | 363 | for (irq = 0; irq < PIC_NUM_PINS/2; irq++) |
| 364 | if (imr_diff & (1 << irq)) |
| 365 | kvm_fire_mask_notifiers( |
| 366 | s->pics_state->kvm, |
| 367 | SELECT_PIC(irq + off), |
| 368 | irq + off, |
| 369 | !!(s->imr & (1 << irq))); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 370 | pic_update_irq(s->pics_state); |
| 371 | break; |
Gleb Natapov | 9195c4d | 2010-07-15 12:24:37 +0300 | [diff] [blame] | 372 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 373 | case 1: |
| 374 | s->irq_base = val & 0xf8; |
| 375 | s->init_state = 2; |
| 376 | break; |
| 377 | case 2: |
| 378 | if (s->init4) |
| 379 | s->init_state = 3; |
| 380 | else |
| 381 | s->init_state = 0; |
| 382 | break; |
| 383 | case 3: |
| 384 | s->special_fully_nested_mode = (val >> 4) & 1; |
| 385 | s->auto_eoi = (val >> 1) & 1; |
| 386 | s->init_state = 0; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1) |
| 392 | { |
| 393 | int ret; |
| 394 | |
| 395 | ret = pic_get_irq(s); |
| 396 | if (ret >= 0) { |
| 397 | if (addr1 >> 7) { |
| 398 | s->pics_state->pics[0].isr &= ~(1 << 2); |
| 399 | s->pics_state->pics[0].irr &= ~(1 << 2); |
| 400 | } |
| 401 | s->irr &= ~(1 << ret); |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 402 | pic_clear_isr(s, ret); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 403 | if (addr1 >> 7 || ret != 2) |
| 404 | pic_update_irq(s->pics_state); |
| 405 | } else { |
| 406 | ret = 0x07; |
| 407 | pic_update_irq(s->pics_state); |
| 408 | } |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static u32 pic_ioport_read(void *opaque, u32 addr1) |
| 414 | { |
| 415 | struct kvm_kpic_state *s = opaque; |
| 416 | unsigned int addr; |
| 417 | int ret; |
| 418 | |
| 419 | addr = addr1; |
| 420 | addr &= 1; |
| 421 | if (s->poll) { |
| 422 | ret = pic_poll_read(s, addr1); |
| 423 | s->poll = 0; |
| 424 | } else |
| 425 | if (addr == 0) |
| 426 | if (s->read_reg_select) |
| 427 | ret = s->isr; |
| 428 | else |
| 429 | ret = s->irr; |
| 430 | else |
| 431 | ret = s->imr; |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static void elcr_ioport_write(void *opaque, u32 addr, u32 val) |
| 436 | { |
| 437 | struct kvm_kpic_state *s = opaque; |
| 438 | s->elcr = val & s->elcr_mask; |
| 439 | } |
| 440 | |
| 441 | static u32 elcr_ioport_read(void *opaque, u32 addr1) |
| 442 | { |
| 443 | struct kvm_kpic_state *s = opaque; |
| 444 | return s->elcr; |
| 445 | } |
| 446 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 447 | static int picdev_in_range(gpa_t addr) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 448 | { |
| 449 | switch (addr) { |
| 450 | case 0x20: |
| 451 | case 0x21: |
| 452 | case 0xa0: |
| 453 | case 0xa1: |
| 454 | case 0x4d0: |
| 455 | case 0x4d1: |
| 456 | return 1; |
| 457 | default: |
| 458 | return 0; |
| 459 | } |
| 460 | } |
| 461 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 462 | static inline struct kvm_pic *to_pic(struct kvm_io_device *dev) |
| 463 | { |
| 464 | return container_of(dev, struct kvm_pic, dev); |
| 465 | } |
| 466 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 467 | static int picdev_write(struct kvm_io_device *this, |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 468 | gpa_t addr, int len, const void *val) |
| 469 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 470 | struct kvm_pic *s = to_pic(this); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 471 | unsigned char data = *(unsigned char *)val; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 472 | if (!picdev_in_range(addr)) |
| 473 | return -EOPNOTSUPP; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 474 | |
| 475 | if (len != 1) { |
| 476 | if (printk_ratelimit()) |
| 477 | printk(KERN_ERR "PIC: non byte write\n"); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 478 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 479 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 480 | pic_lock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 481 | switch (addr) { |
| 482 | case 0x20: |
| 483 | case 0x21: |
| 484 | case 0xa0: |
| 485 | case 0xa1: |
| 486 | pic_ioport_write(&s->pics[addr >> 7], addr, data); |
| 487 | break; |
| 488 | case 0x4d0: |
| 489 | case 0x4d1: |
| 490 | elcr_ioport_write(&s->pics[addr & 1], addr, data); |
| 491 | break; |
| 492 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 493 | pic_unlock(s); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 494 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 495 | } |
| 496 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 497 | static int picdev_read(struct kvm_io_device *this, |
| 498 | gpa_t addr, int len, void *val) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 499 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 500 | struct kvm_pic *s = to_pic(this); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 501 | unsigned char data = 0; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 502 | if (!picdev_in_range(addr)) |
| 503 | return -EOPNOTSUPP; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 504 | |
| 505 | if (len != 1) { |
| 506 | if (printk_ratelimit()) |
| 507 | printk(KERN_ERR "PIC: non byte read\n"); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 508 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 509 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 510 | pic_lock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 511 | switch (addr) { |
| 512 | case 0x20: |
| 513 | case 0x21: |
| 514 | case 0xa0: |
| 515 | case 0xa1: |
| 516 | data = pic_ioport_read(&s->pics[addr >> 7], addr); |
| 517 | break; |
| 518 | case 0x4d0: |
| 519 | case 0x4d1: |
| 520 | data = elcr_ioport_read(&s->pics[addr & 1], addr); |
| 521 | break; |
| 522 | } |
| 523 | *(unsigned char *)val = data; |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 524 | pic_unlock(s); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 525 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* |
| 529 | * callback when PIC0 irq status changed |
| 530 | */ |
Avi Kivity | 073d461 | 2010-05-03 17:34:34 +0300 | [diff] [blame] | 531 | static void pic_irq_request(struct kvm *kvm, int level) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 532 | { |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 533 | struct kvm_pic *s = pic_irqchip(kvm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 534 | |
Gleb Natapov | 7049467 | 2011-02-09 12:09:46 +0200 | [diff] [blame] | 535 | if (!s->output) |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 536 | s->wakeup_needed = true; |
Gleb Natapov | 7049467 | 2011-02-09 12:09:46 +0200 | [diff] [blame] | 537 | s->output = level; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 538 | } |
| 539 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 540 | static const struct kvm_io_device_ops picdev_ops = { |
| 541 | .read = picdev_read, |
| 542 | .write = picdev_write, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 543 | }; |
| 544 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 545 | struct kvm_pic *kvm_create_pic(struct kvm *kvm) |
| 546 | { |
| 547 | struct kvm_pic *s; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 548 | int ret; |
| 549 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 550 | s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL); |
| 551 | if (!s) |
| 552 | return NULL; |
Avi Kivity | f4f5105 | 2010-09-19 18:44:07 +0200 | [diff] [blame] | 553 | spin_lock_init(&s->lock); |
Avi Kivity | 3f35385 | 2008-12-21 22:48:32 +0200 | [diff] [blame] | 554 | s->kvm = kvm; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 555 | s->pics[0].elcr_mask = 0xf8; |
| 556 | s->pics[1].elcr_mask = 0xde; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 557 | s->pics[0].pics_state = s; |
| 558 | s->pics[1].pics_state = s; |
| 559 | |
| 560 | /* |
| 561 | * Initialize PIO device |
| 562 | */ |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 563 | kvm_iodevice_init(&s->dev, &picdev_ops); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 564 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 565 | ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 566 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 567 | if (ret < 0) { |
| 568 | kfree(s); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 572 | return s; |
| 573 | } |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 574 | |
| 575 | void kvm_destroy_pic(struct kvm *kvm) |
| 576 | { |
| 577 | struct kvm_pic *vpic = kvm->arch.vpic; |
| 578 | |
| 579 | if (vpic) { |
| 580 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev); |
| 581 | kvm->arch.vpic = NULL; |
| 582 | kfree(vpic); |
| 583 | } |
| 584 | } |