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