2 * 8259 interrupt controller emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2007 Intel Corporation
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
29 #include <linux/bitops.h>
32 #include <linux/kvm_host.h>
34 static void pic_lock(struct kvm_pic *s)
39 static void pic_unlock(struct kvm_pic *s)
41 struct kvm *kvm = s->kvm;
42 unsigned acks = s->pending_acks;
43 bool wakeup = s->wakeup_needed;
44 struct kvm_vcpu *vcpu;
47 s->wakeup_needed = false;
49 spin_unlock(&s->lock);
52 kvm_notify_acked_irq(kvm, __ffs(acks));
57 vcpu = s->kvm->vcpus[0];
63 static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
65 s->isr &= ~(1 << irq);
66 s->isr_ack |= (1 << irq);
69 void kvm_pic_clear_isr_ack(struct kvm *kvm)
71 struct kvm_pic *s = pic_irqchip(kvm);
72 s->pics[0].isr_ack = 0xff;
73 s->pics[1].isr_ack = 0xff;
77 * set irq level. If an edge is detected, then the IRR is set to 1
79 static inline void pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
83 if (s->elcr & mask) /* level triggered */
91 else /* edge triggered */
93 if ((s->last_irr & mask) == 0)
101 * return the highest priority found in mask (highest = smallest
102 * number). Return 8 if no irq
104 static inline int get_priority(struct kvm_kpic_state *s, int mask)
110 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
116 * return the pic wanted interrupt. return -1 if none
118 static int pic_get_irq(struct kvm_kpic_state *s)
120 int mask, cur_priority, priority;
122 mask = s->irr & ~s->imr;
123 priority = get_priority(s, mask);
127 * compute current priority. If special fully nested mode on the
128 * master, the IRQ coming from the slave is not taken into account
129 * for the priority computation.
132 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
134 cur_priority = get_priority(s, mask);
135 if (priority < cur_priority)
137 * higher priority found: an irq should be generated
139 return (priority + s->priority_add) & 7;
145 * raise irq to CPU if necessary. must be called every time the active
148 static void pic_update_irq(struct kvm_pic *s)
152 irq2 = pic_get_irq(&s->pics[1]);
155 * if irq request by slave pic, signal master PIC
157 pic_set_irq1(&s->pics[0], 2, 1);
158 pic_set_irq1(&s->pics[0], 2, 0);
160 irq = pic_get_irq(&s->pics[0]);
162 s->irq_request(s->irq_request_opaque, 1);
164 s->irq_request(s->irq_request_opaque, 0);
167 void kvm_pic_update_irq(struct kvm_pic *s)
174 void kvm_pic_set_irq(void *opaque, int irq, int level)
176 struct kvm_pic *s = opaque;
179 if (irq >= 0 && irq < PIC_NUM_PINS) {
180 pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
187 * acknowledge interrupt 'irq'
189 static inline void pic_intack(struct kvm_kpic_state *s, int irq)
193 if (s->rotate_on_auto_eoi)
194 s->priority_add = (irq + 1) & 7;
195 pic_clear_isr(s, irq);
198 * We don't clear a level sensitive interrupt here
200 if (!(s->elcr & (1 << irq)))
201 s->irr &= ~(1 << irq);
204 int kvm_pic_read_irq(struct kvm *kvm)
206 int irq, irq2, intno;
207 struct kvm_pic *s = pic_irqchip(kvm);
210 irq = pic_get_irq(&s->pics[0]);
212 pic_intack(&s->pics[0], irq);
214 irq2 = pic_get_irq(&s->pics[1]);
216 pic_intack(&s->pics[1], irq2);
219 * spurious IRQ on slave controller
222 intno = s->pics[1].irq_base + irq2;
225 intno = s->pics[0].irq_base + irq;
228 * spurious IRQ on host controller
231 intno = s->pics[0].irq_base + irq;
235 kvm_notify_acked_irq(kvm, irq);
240 void kvm_pic_reset(struct kvm_kpic_state *s)
243 struct kvm *kvm = s->pics_state->irq_request_opaque;
244 struct kvm_vcpu *vcpu0 = kvm->vcpus[0];
246 if (s == &s->pics_state->pics[0])
251 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
252 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
253 if (s->irr & (1 << irq) || s->isr & (1 << irq)) {
255 s->pics_state->pending_acks |= 1 << n;
265 s->read_reg_select = 0;
270 s->rotate_on_auto_eoi = 0;
271 s->special_fully_nested_mode = 0;
275 static void pic_ioport_write(void *opaque, u32 addr, u32 val)
277 struct kvm_kpic_state *s = opaque;
278 int priority, cmd, irq;
283 kvm_pic_reset(s); /* init */
285 * deassert a pending interrupt
287 s->pics_state->irq_request(s->pics_state->
288 irq_request_opaque, 0);
292 printk(KERN_ERR "single mode not supported");
295 "level sensitive irq not supported");
296 } else if (val & 0x08) {
300 s->read_reg_select = val & 1;
302 s->special_mask = (val >> 5) & 1;
308 s->rotate_on_auto_eoi = cmd >> 2;
310 case 1: /* end of interrupt */
312 priority = get_priority(s, s->isr);
314 irq = (priority + s->priority_add) & 7;
315 pic_clear_isr(s, irq);
317 s->priority_add = (irq + 1) & 7;
318 pic_update_irq(s->pics_state);
323 pic_clear_isr(s, irq);
324 pic_update_irq(s->pics_state);
327 s->priority_add = (val + 1) & 7;
328 pic_update_irq(s->pics_state);
332 s->priority_add = (irq + 1) & 7;
333 pic_clear_isr(s, irq);
334 pic_update_irq(s->pics_state);
337 break; /* no operation */
341 switch (s->init_state) {
342 case 0: /* normal mode */
344 pic_update_irq(s->pics_state);
347 s->irq_base = val & 0xf8;
357 s->special_fully_nested_mode = (val >> 4) & 1;
358 s->auto_eoi = (val >> 1) & 1;
364 static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
368 ret = pic_get_irq(s);
371 s->pics_state->pics[0].isr &= ~(1 << 2);
372 s->pics_state->pics[0].irr &= ~(1 << 2);
374 s->irr &= ~(1 << ret);
375 pic_clear_isr(s, ret);
376 if (addr1 >> 7 || ret != 2)
377 pic_update_irq(s->pics_state);
380 pic_update_irq(s->pics_state);
386 static u32 pic_ioport_read(void *opaque, u32 addr1)
388 struct kvm_kpic_state *s = opaque;
395 ret = pic_poll_read(s, addr1);
399 if (s->read_reg_select)
408 static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
410 struct kvm_kpic_state *s = opaque;
411 s->elcr = val & s->elcr_mask;
414 static u32 elcr_ioport_read(void *opaque, u32 addr1)
416 struct kvm_kpic_state *s = opaque;
420 static int picdev_in_range(struct kvm_io_device *this, gpa_t addr,
421 int len, int is_write)
436 static void picdev_write(struct kvm_io_device *this,
437 gpa_t addr, int len, const void *val)
439 struct kvm_pic *s = this->private;
440 unsigned char data = *(unsigned char *)val;
443 if (printk_ratelimit())
444 printk(KERN_ERR "PIC: non byte write\n");
453 pic_ioport_write(&s->pics[addr >> 7], addr, data);
457 elcr_ioport_write(&s->pics[addr & 1], addr, data);
463 static void picdev_read(struct kvm_io_device *this,
464 gpa_t addr, int len, void *val)
466 struct kvm_pic *s = this->private;
467 unsigned char data = 0;
470 if (printk_ratelimit())
471 printk(KERN_ERR "PIC: non byte read\n");
480 data = pic_ioport_read(&s->pics[addr >> 7], addr);
484 data = elcr_ioport_read(&s->pics[addr & 1], addr);
487 *(unsigned char *)val = data;
492 * callback when PIC0 irq status changed
494 static void pic_irq_request(void *opaque, int level)
496 struct kvm *kvm = opaque;
497 struct kvm_vcpu *vcpu = kvm->vcpus[0];
498 struct kvm_pic *s = pic_irqchip(kvm);
499 int irq = pic_get_irq(&s->pics[0]);
502 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
503 s->pics[0].isr_ack &= ~(1 << irq);
504 s->wakeup_needed = true;
508 struct kvm_pic *kvm_create_pic(struct kvm *kvm)
511 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
514 spin_lock_init(&s->lock);
516 s->pics[0].elcr_mask = 0xf8;
517 s->pics[1].elcr_mask = 0xde;
518 s->irq_request = pic_irq_request;
519 s->irq_request_opaque = kvm;
520 s->pics[0].pics_state = s;
521 s->pics[1].pics_state = s;
524 * Initialize PIO device
526 s->dev.read = picdev_read;
527 s->dev.write = picdev_write;
528 s->dev.in_range = picdev_in_range;
530 kvm_io_bus_register_dev(&kvm->pio_bus, &s->dev);