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 |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 6 | * Copyright 2009 Red Hat, Inc. and/or its affilates. |
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 | { |
| 42 | raw_spin_lock(&s->lock); |
| 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 | |
| 54 | raw_spin_unlock(&s->lock); |
| 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) |
| 65 | found = s->kvm->bsp_vcpu; |
| 66 | |
| 67 | kvm_vcpu_kick(found); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 71 | static void pic_clear_isr(struct kvm_kpic_state *s, int irq) |
| 72 | { |
| 73 | s->isr &= ~(1 << irq); |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 74 | s->isr_ack |= (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 | |
| 88 | void kvm_pic_clear_isr_ack(struct kvm *kvm) |
| 89 | { |
| 90 | struct kvm_pic *s = pic_irqchip(kvm); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 91 | |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 92 | pic_lock(s); |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 93 | s->pics[0].isr_ack = 0xff; |
| 94 | s->pics[1].isr_ack = 0xff; |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 95 | pic_unlock(s); |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 96 | } |
| 97 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 98 | /* |
| 99 | * set irq level. If an edge is detected, then the IRR is set to 1 |
| 100 | */ |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 101 | 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] | 102 | { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 103 | int mask, ret = 1; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 104 | mask = 1 << irq; |
| 105 | if (s->elcr & mask) /* level triggered */ |
| 106 | if (level) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 107 | ret = !(s->irr & mask); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 108 | s->irr |= mask; |
| 109 | s->last_irr |= mask; |
| 110 | } else { |
| 111 | s->irr &= ~mask; |
| 112 | s->last_irr &= ~mask; |
| 113 | } |
| 114 | else /* edge triggered */ |
| 115 | if (level) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 116 | if ((s->last_irr & mask) == 0) { |
| 117 | ret = !(s->irr & mask); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 118 | s->irr |= mask; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 119 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 120 | s->last_irr |= mask; |
| 121 | } else |
| 122 | s->last_irr &= ~mask; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 123 | |
| 124 | return (s->imr & mask) ? -1 : ret; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * return the highest priority found in mask (highest = smallest |
| 129 | * number). Return 8 if no irq |
| 130 | */ |
| 131 | static inline int get_priority(struct kvm_kpic_state *s, int mask) |
| 132 | { |
| 133 | int priority; |
| 134 | if (mask == 0) |
| 135 | return 8; |
| 136 | priority = 0; |
| 137 | while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) |
| 138 | priority++; |
| 139 | return priority; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * return the pic wanted interrupt. return -1 if none |
| 144 | */ |
| 145 | static int pic_get_irq(struct kvm_kpic_state *s) |
| 146 | { |
| 147 | int mask, cur_priority, priority; |
| 148 | |
| 149 | mask = s->irr & ~s->imr; |
| 150 | priority = get_priority(s, mask); |
| 151 | if (priority == 8) |
| 152 | return -1; |
| 153 | /* |
| 154 | * compute current priority. If special fully nested mode on the |
| 155 | * master, the IRQ coming from the slave is not taken into account |
| 156 | * for the priority computation. |
| 157 | */ |
| 158 | mask = s->isr; |
| 159 | if (s->special_fully_nested_mode && s == &s->pics_state->pics[0]) |
| 160 | mask &= ~(1 << 2); |
| 161 | cur_priority = get_priority(s, mask); |
| 162 | if (priority < cur_priority) |
| 163 | /* |
| 164 | * higher priority found: an irq should be generated |
| 165 | */ |
| 166 | return (priority + s->priority_add) & 7; |
| 167 | else |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * raise irq to CPU if necessary. must be called every time the active |
| 173 | * irq may change |
| 174 | */ |
| 175 | static void pic_update_irq(struct kvm_pic *s) |
| 176 | { |
| 177 | int irq2, irq; |
| 178 | |
| 179 | irq2 = pic_get_irq(&s->pics[1]); |
| 180 | if (irq2 >= 0) { |
| 181 | /* |
| 182 | * if irq request by slave pic, signal master PIC |
| 183 | */ |
| 184 | pic_set_irq1(&s->pics[0], 2, 1); |
| 185 | pic_set_irq1(&s->pics[0], 2, 0); |
| 186 | } |
| 187 | irq = pic_get_irq(&s->pics[0]); |
Avi Kivity | 36633f3 | 2010-05-03 17:38:06 +0300 | [diff] [blame] | 188 | pic_irq_request(s->kvm, irq >= 0); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 189 | } |
| 190 | |
He, Qing | 6ceb9d7 | 2007-07-26 11:05:18 +0300 | [diff] [blame] | 191 | void kvm_pic_update_irq(struct kvm_pic *s) |
| 192 | { |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 193 | pic_lock(s); |
He, Qing | 6ceb9d7 | 2007-07-26 11:05:18 +0300 | [diff] [blame] | 194 | pic_update_irq(s); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 195 | pic_unlock(s); |
He, Qing | 6ceb9d7 | 2007-07-26 11:05:18 +0300 | [diff] [blame] | 196 | } |
| 197 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 198 | int kvm_pic_set_irq(void *opaque, int irq, int level) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 199 | { |
| 200 | struct kvm_pic *s = opaque; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 201 | int ret = -1; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 202 | |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 203 | pic_lock(s); |
Ben-Ami Yassour | c65bbfa | 2008-07-06 17:15:07 +0300 | [diff] [blame] | 204 | if (irq >= 0 && irq < PIC_NUM_PINS) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 205 | ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level); |
Ben-Ami Yassour | c65bbfa | 2008-07-06 17:15:07 +0300 | [diff] [blame] | 206 | pic_update_irq(s); |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 207 | trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr, |
| 208 | s->pics[irq >> 3].imr, ret == 0); |
Ben-Ami Yassour | c65bbfa | 2008-07-06 17:15:07 +0300 | [diff] [blame] | 209 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 210 | pic_unlock(s); |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 211 | |
| 212 | return ret; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * acknowledge interrupt 'irq' |
| 217 | */ |
| 218 | static inline void pic_intack(struct kvm_kpic_state *s, int irq) |
| 219 | { |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 220 | s->isr |= 1 << irq; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 221 | /* |
| 222 | * We don't clear a level sensitive interrupt here |
| 223 | */ |
| 224 | if (!(s->elcr & (1 << irq))) |
| 225 | s->irr &= ~(1 << irq); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 226 | |
| 227 | if (s->auto_eoi) { |
| 228 | if (s->rotate_on_auto_eoi) |
| 229 | s->priority_add = (irq + 1) & 7; |
| 230 | pic_clear_isr(s, irq); |
| 231 | } |
| 232 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 233 | } |
| 234 | |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 235 | int kvm_pic_read_irq(struct kvm *kvm) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 236 | { |
| 237 | int irq, irq2, intno; |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 238 | struct kvm_pic *s = pic_irqchip(kvm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 239 | |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 240 | pic_lock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 241 | irq = pic_get_irq(&s->pics[0]); |
| 242 | if (irq >= 0) { |
| 243 | pic_intack(&s->pics[0], irq); |
| 244 | if (irq == 2) { |
| 245 | irq2 = pic_get_irq(&s->pics[1]); |
| 246 | if (irq2 >= 0) |
| 247 | pic_intack(&s->pics[1], irq2); |
| 248 | else |
| 249 | /* |
| 250 | * spurious IRQ on slave controller |
| 251 | */ |
| 252 | irq2 = 7; |
| 253 | intno = s->pics[1].irq_base + irq2; |
| 254 | irq = irq2 + 8; |
| 255 | } else |
| 256 | intno = s->pics[0].irq_base + irq; |
| 257 | } else { |
| 258 | /* |
| 259 | * spurious IRQ on host controller |
| 260 | */ |
| 261 | irq = 7; |
| 262 | intno = s->pics[0].irq_base + irq; |
| 263 | } |
| 264 | pic_update_irq(s); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 265 | pic_unlock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 266 | |
| 267 | return intno; |
| 268 | } |
| 269 | |
Eddie Dong | 2fcceae | 2007-10-10 12:14:25 +0200 | [diff] [blame] | 270 | void kvm_pic_reset(struct kvm_kpic_state *s) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 271 | { |
Gleb Natapov | 79c727d | 2009-08-24 11:54:18 +0300 | [diff] [blame] | 272 | int irq; |
Avi Kivity | 073d461 | 2010-05-03 17:34:34 +0300 | [diff] [blame] | 273 | struct kvm_vcpu *vcpu0 = s->pics_state->kvm->bsp_vcpu; |
Gleb Natapov | 79c727d | 2009-08-24 11:54:18 +0300 | [diff] [blame] | 274 | u8 irr = s->irr, isr = s->imr; |
Marcelo Tosatti | f524472 | 2008-07-26 17:01:00 -0300 | [diff] [blame] | 275 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 276 | s->last_irr = 0; |
| 277 | s->irr = 0; |
| 278 | s->imr = 0; |
| 279 | s->isr = 0; |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 280 | s->isr_ack = 0xff; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 281 | s->priority_add = 0; |
| 282 | s->irq_base = 0; |
| 283 | s->read_reg_select = 0; |
| 284 | s->poll = 0; |
| 285 | s->special_mask = 0; |
| 286 | s->init_state = 0; |
| 287 | s->auto_eoi = 0; |
| 288 | s->rotate_on_auto_eoi = 0; |
| 289 | s->special_fully_nested_mode = 0; |
| 290 | s->init4 = 0; |
Gleb Natapov | 79c727d | 2009-08-24 11:54:18 +0300 | [diff] [blame] | 291 | |
| 292 | for (irq = 0; irq < PIC_NUM_PINS/2; irq++) { |
| 293 | if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0)) |
| 294 | if (irr & (1 << irq) || isr & (1 << irq)) { |
| 295 | pic_clear_isr(s, irq); |
| 296 | } |
| 297 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void pic_ioport_write(void *opaque, u32 addr, u32 val) |
| 301 | { |
| 302 | struct kvm_kpic_state *s = opaque; |
| 303 | int priority, cmd, irq; |
| 304 | |
| 305 | addr &= 1; |
| 306 | if (addr == 0) { |
| 307 | if (val & 0x10) { |
Eddie Dong | 2fcceae | 2007-10-10 12:14:25 +0200 | [diff] [blame] | 308 | kvm_pic_reset(s); /* init */ |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 309 | /* |
| 310 | * deassert a pending interrupt |
| 311 | */ |
Avi Kivity | 073d461 | 2010-05-03 17:34:34 +0300 | [diff] [blame] | 312 | pic_irq_request(s->pics_state->kvm, 0); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 313 | s->init_state = 1; |
| 314 | s->init4 = val & 1; |
| 315 | if (val & 0x02) |
| 316 | printk(KERN_ERR "single mode not supported"); |
| 317 | if (val & 0x08) |
| 318 | printk(KERN_ERR |
| 319 | "level sensitive irq not supported"); |
| 320 | } else if (val & 0x08) { |
| 321 | if (val & 0x04) |
| 322 | s->poll = 1; |
| 323 | if (val & 0x02) |
| 324 | s->read_reg_select = val & 1; |
| 325 | if (val & 0x40) |
| 326 | s->special_mask = (val >> 5) & 1; |
| 327 | } else { |
| 328 | cmd = val >> 5; |
| 329 | switch (cmd) { |
| 330 | case 0: |
| 331 | case 4: |
| 332 | s->rotate_on_auto_eoi = cmd >> 2; |
| 333 | break; |
| 334 | case 1: /* end of interrupt */ |
| 335 | case 5: |
| 336 | priority = get_priority(s, s->isr); |
| 337 | if (priority != 8) { |
| 338 | irq = (priority + s->priority_add) & 7; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 339 | if (cmd == 5) |
| 340 | s->priority_add = (irq + 1) & 7; |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 341 | pic_clear_isr(s, irq); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 342 | pic_update_irq(s->pics_state); |
| 343 | } |
| 344 | break; |
| 345 | case 3: |
| 346 | irq = val & 7; |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 347 | pic_clear_isr(s, irq); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 348 | pic_update_irq(s->pics_state); |
| 349 | break; |
| 350 | case 6: |
| 351 | s->priority_add = (val + 1) & 7; |
| 352 | pic_update_irq(s->pics_state); |
| 353 | break; |
| 354 | case 7: |
| 355 | irq = val & 7; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 356 | s->priority_add = (irq + 1) & 7; |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 357 | pic_clear_isr(s, irq); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 358 | pic_update_irq(s->pics_state); |
| 359 | break; |
| 360 | default: |
| 361 | break; /* no operation */ |
| 362 | } |
| 363 | } |
| 364 | } else |
| 365 | switch (s->init_state) { |
| 366 | case 0: /* normal mode */ |
| 367 | s->imr = val; |
| 368 | pic_update_irq(s->pics_state); |
| 369 | break; |
| 370 | case 1: |
| 371 | s->irq_base = val & 0xf8; |
| 372 | s->init_state = 2; |
| 373 | break; |
| 374 | case 2: |
| 375 | if (s->init4) |
| 376 | s->init_state = 3; |
| 377 | else |
| 378 | s->init_state = 0; |
| 379 | break; |
| 380 | case 3: |
| 381 | s->special_fully_nested_mode = (val >> 4) & 1; |
| 382 | s->auto_eoi = (val >> 1) & 1; |
| 383 | s->init_state = 0; |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1) |
| 389 | { |
| 390 | int ret; |
| 391 | |
| 392 | ret = pic_get_irq(s); |
| 393 | if (ret >= 0) { |
| 394 | if (addr1 >> 7) { |
| 395 | s->pics_state->pics[0].isr &= ~(1 << 2); |
| 396 | s->pics_state->pics[0].irr &= ~(1 << 2); |
| 397 | } |
| 398 | s->irr &= ~(1 << ret); |
Avi Kivity | 7edd0ce | 2008-07-07 14:45:39 +0300 | [diff] [blame] | 399 | pic_clear_isr(s, ret); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 400 | if (addr1 >> 7 || ret != 2) |
| 401 | pic_update_irq(s->pics_state); |
| 402 | } else { |
| 403 | ret = 0x07; |
| 404 | pic_update_irq(s->pics_state); |
| 405 | } |
| 406 | |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | static u32 pic_ioport_read(void *opaque, u32 addr1) |
| 411 | { |
| 412 | struct kvm_kpic_state *s = opaque; |
| 413 | unsigned int addr; |
| 414 | int ret; |
| 415 | |
| 416 | addr = addr1; |
| 417 | addr &= 1; |
| 418 | if (s->poll) { |
| 419 | ret = pic_poll_read(s, addr1); |
| 420 | s->poll = 0; |
| 421 | } else |
| 422 | if (addr == 0) |
| 423 | if (s->read_reg_select) |
| 424 | ret = s->isr; |
| 425 | else |
| 426 | ret = s->irr; |
| 427 | else |
| 428 | ret = s->imr; |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | static void elcr_ioport_write(void *opaque, u32 addr, u32 val) |
| 433 | { |
| 434 | struct kvm_kpic_state *s = opaque; |
| 435 | s->elcr = val & s->elcr_mask; |
| 436 | } |
| 437 | |
| 438 | static u32 elcr_ioport_read(void *opaque, u32 addr1) |
| 439 | { |
| 440 | struct kvm_kpic_state *s = opaque; |
| 441 | return s->elcr; |
| 442 | } |
| 443 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 444 | static int picdev_in_range(gpa_t addr) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 445 | { |
| 446 | switch (addr) { |
| 447 | case 0x20: |
| 448 | case 0x21: |
| 449 | case 0xa0: |
| 450 | case 0xa1: |
| 451 | case 0x4d0: |
| 452 | case 0x4d1: |
| 453 | return 1; |
| 454 | default: |
| 455 | return 0; |
| 456 | } |
| 457 | } |
| 458 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 459 | static inline struct kvm_pic *to_pic(struct kvm_io_device *dev) |
| 460 | { |
| 461 | return container_of(dev, struct kvm_pic, dev); |
| 462 | } |
| 463 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 464 | static int picdev_write(struct kvm_io_device *this, |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 465 | gpa_t addr, int len, const void *val) |
| 466 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 467 | struct kvm_pic *s = to_pic(this); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 468 | unsigned char data = *(unsigned char *)val; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 469 | if (!picdev_in_range(addr)) |
| 470 | return -EOPNOTSUPP; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 471 | |
| 472 | if (len != 1) { |
| 473 | if (printk_ratelimit()) |
| 474 | printk(KERN_ERR "PIC: non byte write\n"); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 475 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 476 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 477 | pic_lock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 478 | switch (addr) { |
| 479 | case 0x20: |
| 480 | case 0x21: |
| 481 | case 0xa0: |
| 482 | case 0xa1: |
| 483 | pic_ioport_write(&s->pics[addr >> 7], addr, data); |
| 484 | break; |
| 485 | case 0x4d0: |
| 486 | case 0x4d1: |
| 487 | elcr_ioport_write(&s->pics[addr & 1], addr, data); |
| 488 | break; |
| 489 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 490 | pic_unlock(s); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 491 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 492 | } |
| 493 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 494 | static int picdev_read(struct kvm_io_device *this, |
| 495 | gpa_t addr, int len, void *val) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 496 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 497 | struct kvm_pic *s = to_pic(this); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 498 | unsigned char data = 0; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 499 | if (!picdev_in_range(addr)) |
| 500 | return -EOPNOTSUPP; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 501 | |
| 502 | if (len != 1) { |
| 503 | if (printk_ratelimit()) |
| 504 | printk(KERN_ERR "PIC: non byte read\n"); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 505 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 506 | } |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 507 | pic_lock(s); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 508 | switch (addr) { |
| 509 | case 0x20: |
| 510 | case 0x21: |
| 511 | case 0xa0: |
| 512 | case 0xa1: |
| 513 | data = pic_ioport_read(&s->pics[addr >> 7], addr); |
| 514 | break; |
| 515 | case 0x4d0: |
| 516 | case 0x4d1: |
| 517 | data = elcr_ioport_read(&s->pics[addr & 1], addr); |
| 518 | break; |
| 519 | } |
| 520 | *(unsigned char *)val = data; |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 521 | pic_unlock(s); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 522 | return 0; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* |
| 526 | * callback when PIC0 irq status changed |
| 527 | */ |
Avi Kivity | 073d461 | 2010-05-03 17:34:34 +0300 | [diff] [blame] | 528 | static void pic_irq_request(struct kvm *kvm, int level) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 529 | { |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 530 | struct kvm_vcpu *vcpu = kvm->bsp_vcpu; |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 531 | struct kvm_pic *s = pic_irqchip(kvm); |
| 532 | int irq = pic_get_irq(&s->pics[0]); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 533 | |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 534 | s->output = level; |
| 535 | if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) { |
| 536 | s->pics[0].isr_ack &= ~(1 << irq); |
Jan Kiszka | 50a085b | 2010-02-24 10:41:58 +0100 | [diff] [blame] | 537 | s->wakeup_needed = true; |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 538 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 539 | } |
| 540 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 541 | static const struct kvm_io_device_ops picdev_ops = { |
| 542 | .read = picdev_read, |
| 543 | .write = picdev_write, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 544 | }; |
| 545 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 546 | struct kvm_pic *kvm_create_pic(struct kvm *kvm) |
| 547 | { |
| 548 | struct kvm_pic *s; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 549 | int ret; |
| 550 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 551 | s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL); |
| 552 | if (!s) |
| 553 | return NULL; |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 554 | raw_spin_lock_init(&s->lock); |
Avi Kivity | 3f35385 | 2008-12-21 22:48:32 +0200 | [diff] [blame] | 555 | s->kvm = kvm; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 556 | s->pics[0].elcr_mask = 0xf8; |
| 557 | s->pics[1].elcr_mask = 0xde; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 558 | s->pics[0].pics_state = s; |
| 559 | s->pics[1].pics_state = s; |
| 560 | |
| 561 | /* |
| 562 | * Initialize PIO device |
| 563 | */ |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 564 | kvm_iodevice_init(&s->dev, &picdev_ops); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 565 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 566 | ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 567 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 568 | if (ret < 0) { |
| 569 | kfree(s); |
| 570 | return NULL; |
| 571 | } |
| 572 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 573 | return s; |
| 574 | } |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 575 | |
| 576 | void kvm_destroy_pic(struct kvm *kvm) |
| 577 | { |
| 578 | struct kvm_pic *vpic = kvm->arch.vpic; |
| 579 | |
| 580 | if (vpic) { |
| 581 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev); |
| 582 | kvm->arch.vpic = NULL; |
| 583 | kfree(vpic); |
| 584 | } |
| 585 | } |