2 * 8259 interrupt controller emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2007 Intel Corporation
6 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
26 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
30 #include <linux/slab.h>
31 #include <linux/bitops.h>
34 #include <linux/kvm_host.h>
37 #define pr_pic_unimpl(fmt, ...) \
38 pr_err_ratelimited("kvm: pic: " fmt, ## __VA_ARGS__)
40 static void pic_irq_request(struct kvm *kvm, int level);
42 static void pic_lock(struct kvm_pic *s)
48 static void pic_unlock(struct kvm_pic *s)
51 bool wakeup = s->wakeup_needed;
52 struct kvm_vcpu *vcpu;
55 s->wakeup_needed = false;
57 spin_unlock(&s->lock);
60 kvm_for_each_vcpu(i, vcpu, s->kvm) {
61 if (kvm_apic_accept_pic_intr(vcpu)) {
62 kvm_make_request(KVM_REQ_EVENT, vcpu);
70 static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
72 s->isr &= ~(1 << irq);
73 if (s != &s->pics_state->pics[0])
76 * We are dropping lock while calling ack notifiers since ack
77 * notifier callbacks for assigned devices call into PIC recursively.
78 * Other interrupt may be delivered to PIC while lock is dropped but
79 * it should be safe since PIC state is already updated at this stage.
81 pic_unlock(s->pics_state);
82 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
83 pic_lock(s->pics_state);
87 * set irq level. If an edge is detected, then the IRR is set to 1
89 static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
93 if (s->elcr & mask) /* level triggered */
95 ret = !(s->irr & mask);
100 s->last_irr &= ~mask;
102 else /* edge triggered */
104 if ((s->last_irr & mask) == 0) {
105 ret = !(s->irr & mask);
110 s->last_irr &= ~mask;
112 return (s->imr & mask) ? -1 : ret;
116 * return the highest priority found in mask (highest = smallest
117 * number). Return 8 if no irq
119 static inline int get_priority(struct kvm_kpic_state *s, int mask)
125 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
131 * return the pic wanted interrupt. return -1 if none
133 static int pic_get_irq(struct kvm_kpic_state *s)
135 int mask, cur_priority, priority;
137 mask = s->irr & ~s->imr;
138 priority = get_priority(s, mask);
142 * compute current priority. If special fully nested mode on the
143 * master, the IRQ coming from the slave is not taken into account
144 * for the priority computation.
147 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
149 cur_priority = get_priority(s, mask);
150 if (priority < cur_priority)
152 * higher priority found: an irq should be generated
154 return (priority + s->priority_add) & 7;
160 * raise irq to CPU if necessary. must be called every time the active
163 static void pic_update_irq(struct kvm_pic *s)
167 irq2 = pic_get_irq(&s->pics[1]);
170 * if irq request by slave pic, signal master PIC
172 pic_set_irq1(&s->pics[0], 2, 1);
173 pic_set_irq1(&s->pics[0], 2, 0);
175 irq = pic_get_irq(&s->pics[0]);
176 pic_irq_request(s->kvm, irq >= 0);
179 void kvm_pic_update_irq(struct kvm_pic *s)
186 int kvm_pic_set_irq(struct kvm_pic *s, int irq, int irq_source_id, int level)
190 BUG_ON(irq < 0 || irq >= PIC_NUM_PINS);
193 irq_level = __kvm_irq_line_state(&s->irq_states[irq],
194 irq_source_id, level);
195 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, irq_level);
197 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
198 s->pics[irq >> 3].imr, ret == 0);
204 void kvm_pic_clear_all(struct kvm_pic *s, int irq_source_id)
209 for (i = 0; i < PIC_NUM_PINS; i++)
210 __clear_bit(irq_source_id, &s->irq_states[i]);
215 * acknowledge interrupt 'irq'
217 static inline void pic_intack(struct kvm_kpic_state *s, int irq)
221 * We don't clear a level sensitive interrupt here
223 if (!(s->elcr & (1 << irq)))
224 s->irr &= ~(1 << irq);
227 if (s->rotate_on_auto_eoi)
228 s->priority_add = (irq + 1) & 7;
229 pic_clear_isr(s, irq);
234 int kvm_pic_read_irq(struct kvm *kvm)
236 int irq, irq2, intno;
237 struct kvm_pic *s = kvm->arch.vpic;
242 irq = pic_get_irq(&s->pics[0]);
244 pic_intack(&s->pics[0], irq);
246 irq2 = pic_get_irq(&s->pics[1]);
248 pic_intack(&s->pics[1], irq2);
251 * spurious IRQ on slave controller
254 intno = s->pics[1].irq_base + irq2;
257 intno = s->pics[0].irq_base + irq;
260 * spurious IRQ on host controller
263 intno = s->pics[0].irq_base + irq;
271 static void kvm_pic_reset(struct kvm_kpic_state *s)
275 struct kvm_vcpu *vcpu;
276 u8 edge_irr = s->irr & ~s->elcr;
284 s->read_reg_select = 0;
286 s->special_fully_nested_mode = 0;
291 kvm_for_each_vcpu(i, vcpu, s->pics_state->kvm)
292 if (kvm_apic_accept_pic_intr(vcpu)) {
301 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
302 if (edge_irr & (1 << irq))
303 pic_clear_isr(s, irq);
306 static void pic_ioport_write(void *opaque, u32 addr, u32 val)
308 struct kvm_kpic_state *s = opaque;
309 int priority, cmd, irq;
316 pr_pic_unimpl("single mode not supported");
319 "level sensitive irq not supported");
321 } else if (val & 0x08) {
325 s->read_reg_select = val & 1;
327 s->special_mask = (val >> 5) & 1;
333 s->rotate_on_auto_eoi = cmd >> 2;
335 case 1: /* end of interrupt */
337 priority = get_priority(s, s->isr);
339 irq = (priority + s->priority_add) & 7;
341 s->priority_add = (irq + 1) & 7;
342 pic_clear_isr(s, irq);
343 pic_update_irq(s->pics_state);
348 pic_clear_isr(s, irq);
349 pic_update_irq(s->pics_state);
352 s->priority_add = (val + 1) & 7;
353 pic_update_irq(s->pics_state);
357 s->priority_add = (irq + 1) & 7;
358 pic_clear_isr(s, irq);
359 pic_update_irq(s->pics_state);
362 break; /* no operation */
366 switch (s->init_state) {
367 case 0: { /* normal mode */
368 u8 imr_diff = s->imr ^ val,
369 off = (s == &s->pics_state->pics[0]) ? 0 : 8;
371 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
372 if (imr_diff & (1 << irq))
373 kvm_fire_mask_notifiers(
375 SELECT_PIC(irq + off),
377 !!(s->imr & (1 << irq)));
378 pic_update_irq(s->pics_state);
382 s->irq_base = val & 0xf8;
392 s->special_fully_nested_mode = (val >> 4) & 1;
393 s->auto_eoi = (val >> 1) & 1;
399 static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
403 ret = pic_get_irq(s);
406 s->pics_state->pics[0].isr &= ~(1 << 2);
407 s->pics_state->pics[0].irr &= ~(1 << 2);
409 s->irr &= ~(1 << ret);
410 pic_clear_isr(s, ret);
411 if (addr1 >> 7 || ret != 2)
412 pic_update_irq(s->pics_state);
415 pic_update_irq(s->pics_state);
421 static u32 pic_ioport_read(void *opaque, u32 addr)
423 struct kvm_kpic_state *s = opaque;
427 ret = pic_poll_read(s, addr);
431 if (s->read_reg_select)
440 static void elcr_ioport_write(void *opaque, u32 val)
442 struct kvm_kpic_state *s = opaque;
443 s->elcr = val & s->elcr_mask;
446 static u32 elcr_ioport_read(void *opaque)
448 struct kvm_kpic_state *s = opaque;
452 static int picdev_write(struct kvm_pic *s,
453 gpa_t addr, int len, const void *val)
455 unsigned char data = *(unsigned char *)val;
458 pr_pic_unimpl("non byte write\n");
465 pic_ioport_write(&s->pics[0], addr, data);
471 pic_ioport_write(&s->pics[1], addr, data);
477 elcr_ioport_write(&s->pics[addr & 1], data);
486 static int picdev_read(struct kvm_pic *s,
487 gpa_t addr, int len, void *val)
489 unsigned char *data = (unsigned char *)val;
493 pr_pic_unimpl("non byte read\n");
502 *data = pic_ioport_read(&s->pics[addr >> 7], addr);
508 *data = elcr_ioport_read(&s->pics[addr & 1]);
517 static int picdev_master_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
518 gpa_t addr, int len, const void *val)
520 return picdev_write(container_of(dev, struct kvm_pic, dev_master),
524 static int picdev_master_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
525 gpa_t addr, int len, void *val)
527 return picdev_read(container_of(dev, struct kvm_pic, dev_master),
531 static int picdev_slave_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
532 gpa_t addr, int len, const void *val)
534 return picdev_write(container_of(dev, struct kvm_pic, dev_slave),
538 static int picdev_slave_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
539 gpa_t addr, int len, void *val)
541 return picdev_read(container_of(dev, struct kvm_pic, dev_slave),
545 static int picdev_elcr_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
546 gpa_t addr, int len, const void *val)
548 return picdev_write(container_of(dev, struct kvm_pic, dev_elcr),
552 static int picdev_elcr_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
553 gpa_t addr, int len, void *val)
555 return picdev_read(container_of(dev, struct kvm_pic, dev_elcr),
560 * callback when PIC0 irq status changed
562 static void pic_irq_request(struct kvm *kvm, int level)
564 struct kvm_pic *s = kvm->arch.vpic;
567 s->wakeup_needed = true;
571 static const struct kvm_io_device_ops picdev_master_ops = {
572 .read = picdev_master_read,
573 .write = picdev_master_write,
576 static const struct kvm_io_device_ops picdev_slave_ops = {
577 .read = picdev_slave_read,
578 .write = picdev_slave_write,
581 static const struct kvm_io_device_ops picdev_elcr_ops = {
582 .read = picdev_elcr_read,
583 .write = picdev_elcr_write,
586 int kvm_pic_init(struct kvm *kvm)
591 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL_ACCOUNT);
594 spin_lock_init(&s->lock);
596 s->pics[0].elcr_mask = 0xf8;
597 s->pics[1].elcr_mask = 0xde;
598 s->pics[0].pics_state = s;
599 s->pics[1].pics_state = s;
602 * Initialize PIO device
604 kvm_iodevice_init(&s->dev_master, &picdev_master_ops);
605 kvm_iodevice_init(&s->dev_slave, &picdev_slave_ops);
606 kvm_iodevice_init(&s->dev_elcr, &picdev_elcr_ops);
607 mutex_lock(&kvm->slots_lock);
608 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,
613 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2, &s->dev_slave);
617 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2, &s->dev_elcr);
621 mutex_unlock(&kvm->slots_lock);
628 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_slave);
631 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_master);
634 mutex_unlock(&kvm->slots_lock);
641 void kvm_pic_destroy(struct kvm *kvm)
643 struct kvm_pic *vpic = kvm->arch.vpic;
648 mutex_lock(&kvm->slots_lock);
649 kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_master);
650 kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_slave);
651 kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_elcr);
652 mutex_unlock(&kvm->slots_lock);
654 kvm->arch.vpic = NULL;