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
, *found
= NULL
;
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
)) {
70 kvm_make_request(KVM_REQ_EVENT
, found
);
75 static void pic_clear_isr(struct kvm_kpic_state
*s
, int irq
)
77 s
->isr
&= ~(1 << irq
);
78 if (s
!= &s
->pics_state
->pics
[0])
81 * We are dropping lock while calling ack notifiers since ack
82 * notifier callbacks for assigned devices call into PIC recursively.
83 * Other interrupt may be delivered to PIC while lock is dropped but
84 * it should be safe since PIC state is already updated at this stage.
86 pic_unlock(s
->pics_state
);
87 kvm_notify_acked_irq(s
->pics_state
->kvm
, SELECT_PIC(irq
), irq
);
88 pic_lock(s
->pics_state
);
92 * set irq level. If an edge is detected, then the IRR is set to 1
94 static inline int pic_set_irq1(struct kvm_kpic_state
*s
, int irq
, int level
)
98 if (s
->elcr
& mask
) /* level triggered */
100 ret
= !(s
->irr
& mask
);
105 s
->last_irr
&= ~mask
;
107 else /* edge triggered */
109 if ((s
->last_irr
& mask
) == 0) {
110 ret
= !(s
->irr
& mask
);
115 s
->last_irr
&= ~mask
;
117 return (s
->imr
& mask
) ? -1 : ret
;
121 * return the highest priority found in mask (highest = smallest
122 * number). Return 8 if no irq
124 static inline int get_priority(struct kvm_kpic_state
*s
, int mask
)
130 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
136 * return the pic wanted interrupt. return -1 if none
138 static int pic_get_irq(struct kvm_kpic_state
*s
)
140 int mask
, cur_priority
, priority
;
142 mask
= s
->irr
& ~s
->imr
;
143 priority
= get_priority(s
, mask
);
147 * compute current priority. If special fully nested mode on the
148 * master, the IRQ coming from the slave is not taken into account
149 * for the priority computation.
152 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
154 cur_priority
= get_priority(s
, mask
);
155 if (priority
< cur_priority
)
157 * higher priority found: an irq should be generated
159 return (priority
+ s
->priority_add
) & 7;
165 * raise irq to CPU if necessary. must be called every time the active
168 static void pic_update_irq(struct kvm_pic
*s
)
172 irq2
= pic_get_irq(&s
->pics
[1]);
175 * if irq request by slave pic, signal master PIC
177 pic_set_irq1(&s
->pics
[0], 2, 1);
178 pic_set_irq1(&s
->pics
[0], 2, 0);
180 irq
= pic_get_irq(&s
->pics
[0]);
181 pic_irq_request(s
->kvm
, irq
>= 0);
184 void kvm_pic_update_irq(struct kvm_pic
*s
)
191 int kvm_pic_set_irq(void *opaque
, int irq
, int level
)
193 struct kvm_pic
*s
= opaque
;
197 if (irq
>= 0 && irq
< PIC_NUM_PINS
) {
198 ret
= pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
200 trace_kvm_pic_set_irq(irq
>> 3, irq
& 7, s
->pics
[irq
>> 3].elcr
,
201 s
->pics
[irq
>> 3].imr
, ret
== 0);
209 * acknowledge interrupt 'irq'
211 static inline void pic_intack(struct kvm_kpic_state
*s
, int irq
)
215 * We don't clear a level sensitive interrupt here
217 if (!(s
->elcr
& (1 << irq
)))
218 s
->irr
&= ~(1 << irq
);
221 if (s
->rotate_on_auto_eoi
)
222 s
->priority_add
= (irq
+ 1) & 7;
223 pic_clear_isr(s
, irq
);
228 int kvm_pic_read_irq(struct kvm
*kvm
)
230 int irq
, irq2
, intno
;
231 struct kvm_pic
*s
= pic_irqchip(kvm
);
234 irq
= pic_get_irq(&s
->pics
[0]);
236 pic_intack(&s
->pics
[0], irq
);
238 irq2
= pic_get_irq(&s
->pics
[1]);
240 pic_intack(&s
->pics
[1], irq2
);
243 * spurious IRQ on slave controller
246 intno
= s
->pics
[1].irq_base
+ irq2
;
249 intno
= s
->pics
[0].irq_base
+ irq
;
252 * spurious IRQ on host controller
255 intno
= s
->pics
[0].irq_base
+ irq
;
263 void kvm_pic_reset(struct kvm_kpic_state
*s
)
266 struct kvm_vcpu
*vcpu0
= s
->pics_state
->kvm
->bsp_vcpu
;
267 u8 irr
= s
->irr
, isr
= s
->imr
;
275 s
->read_reg_select
= 0;
280 s
->rotate_on_auto_eoi
= 0;
281 s
->special_fully_nested_mode
= 0;
284 for (irq
= 0; irq
< PIC_NUM_PINS
/2; irq
++) {
285 if (vcpu0
&& kvm_apic_accept_pic_intr(vcpu0
))
286 if (irr
& (1 << irq
) || isr
& (1 << irq
)) {
287 pic_clear_isr(s
, irq
);
292 static void pic_ioport_write(void *opaque
, u32 addr
, u32 val
)
294 struct kvm_kpic_state
*s
= opaque
;
295 int priority
, cmd
, irq
;
305 s
->read_reg_select
= 0;
307 s
->special_fully_nested_mode
= 0;
312 pr_pic_unimpl("single mode not supported");
315 "level sensitive irq not supported");
316 } else if (val
& 0x08) {
320 s
->read_reg_select
= val
& 1;
322 s
->special_mask
= (val
>> 5) & 1;
328 s
->rotate_on_auto_eoi
= cmd
>> 2;
330 case 1: /* end of interrupt */
332 priority
= get_priority(s
, s
->isr
);
334 irq
= (priority
+ s
->priority_add
) & 7;
336 s
->priority_add
= (irq
+ 1) & 7;
337 pic_clear_isr(s
, irq
);
338 pic_update_irq(s
->pics_state
);
343 pic_clear_isr(s
, irq
);
344 pic_update_irq(s
->pics_state
);
347 s
->priority_add
= (val
+ 1) & 7;
348 pic_update_irq(s
->pics_state
);
352 s
->priority_add
= (irq
+ 1) & 7;
353 pic_clear_isr(s
, irq
);
354 pic_update_irq(s
->pics_state
);
357 break; /* no operation */
361 switch (s
->init_state
) {
362 case 0: { /* normal mode */
363 u8 imr_diff
= s
->imr
^ val
,
364 off
= (s
== &s
->pics_state
->pics
[0]) ? 0 : 8;
366 for (irq
= 0; irq
< PIC_NUM_PINS
/2; irq
++)
367 if (imr_diff
& (1 << irq
))
368 kvm_fire_mask_notifiers(
370 SELECT_PIC(irq
+ off
),
372 !!(s
->imr
& (1 << irq
)));
373 pic_update_irq(s
->pics_state
);
377 s
->irq_base
= val
& 0xf8;
387 s
->special_fully_nested_mode
= (val
>> 4) & 1;
388 s
->auto_eoi
= (val
>> 1) & 1;
394 static u32
pic_poll_read(struct kvm_kpic_state
*s
, u32 addr1
)
398 ret
= pic_get_irq(s
);
401 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
402 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
404 s
->irr
&= ~(1 << ret
);
405 pic_clear_isr(s
, ret
);
406 if (addr1
>> 7 || ret
!= 2)
407 pic_update_irq(s
->pics_state
);
410 pic_update_irq(s
->pics_state
);
416 static u32
pic_ioport_read(void *opaque
, u32 addr1
)
418 struct kvm_kpic_state
*s
= opaque
;
425 ret
= pic_poll_read(s
, addr1
);
429 if (s
->read_reg_select
)
438 static void elcr_ioport_write(void *opaque
, u32 addr
, u32 val
)
440 struct kvm_kpic_state
*s
= opaque
;
441 s
->elcr
= val
& s
->elcr_mask
;
444 static u32
elcr_ioport_read(void *opaque
, u32 addr1
)
446 struct kvm_kpic_state
*s
= opaque
;
450 static int picdev_in_range(gpa_t addr
)
465 static int picdev_write(struct kvm_pic
*s
,
466 gpa_t addr
, int len
, const void *val
)
468 unsigned char data
= *(unsigned char *)val
;
469 if (!picdev_in_range(addr
))
473 pr_pic_unimpl("non byte write\n");
482 pic_ioport_write(&s
->pics
[addr
>> 7], addr
, data
);
486 elcr_ioport_write(&s
->pics
[addr
& 1], addr
, data
);
493 static int picdev_read(struct kvm_pic
*s
,
494 gpa_t addr
, int len
, void *val
)
496 unsigned char data
= 0;
497 if (!picdev_in_range(addr
))
501 pr_pic_unimpl("non byte read\n");
510 data
= pic_ioport_read(&s
->pics
[addr
>> 7], addr
);
514 data
= elcr_ioport_read(&s
->pics
[addr
& 1], addr
);
517 *(unsigned char *)val
= data
;
522 static int picdev_master_write(struct kvm_io_device
*dev
,
523 gpa_t addr
, int len
, const void *val
)
525 return picdev_write(container_of(dev
, struct kvm_pic
, dev_master
),
529 static int picdev_master_read(struct kvm_io_device
*dev
,
530 gpa_t addr
, int len
, void *val
)
532 return picdev_read(container_of(dev
, struct kvm_pic
, dev_master
),
536 static int picdev_slave_write(struct kvm_io_device
*dev
,
537 gpa_t addr
, int len
, const void *val
)
539 return picdev_write(container_of(dev
, struct kvm_pic
, dev_slave
),
543 static int picdev_slave_read(struct kvm_io_device
*dev
,
544 gpa_t addr
, int len
, void *val
)
546 return picdev_read(container_of(dev
, struct kvm_pic
, dev_slave
),
550 static int picdev_eclr_write(struct kvm_io_device
*dev
,
551 gpa_t addr
, int len
, const void *val
)
553 return picdev_write(container_of(dev
, struct kvm_pic
, dev_eclr
),
557 static int picdev_eclr_read(struct kvm_io_device
*dev
,
558 gpa_t addr
, int len
, void *val
)
560 return picdev_read(container_of(dev
, struct kvm_pic
, dev_eclr
),
565 * callback when PIC0 irq status changed
567 static void pic_irq_request(struct kvm
*kvm
, int level
)
569 struct kvm_pic
*s
= pic_irqchip(kvm
);
572 s
->wakeup_needed
= true;
576 static const struct kvm_io_device_ops picdev_master_ops
= {
577 .read
= picdev_master_read
,
578 .write
= picdev_master_write
,
581 static const struct kvm_io_device_ops picdev_slave_ops
= {
582 .read
= picdev_slave_read
,
583 .write
= picdev_slave_write
,
586 static const struct kvm_io_device_ops picdev_eclr_ops
= {
587 .read
= picdev_eclr_read
,
588 .write
= picdev_eclr_write
,
591 struct kvm_pic
*kvm_create_pic(struct kvm
*kvm
)
596 s
= kzalloc(sizeof(struct kvm_pic
), GFP_KERNEL
);
599 spin_lock_init(&s
->lock
);
601 s
->pics
[0].elcr_mask
= 0xf8;
602 s
->pics
[1].elcr_mask
= 0xde;
603 s
->pics
[0].pics_state
= s
;
604 s
->pics
[1].pics_state
= s
;
607 * Initialize PIO device
609 kvm_iodevice_init(&s
->dev_master
, &picdev_master_ops
);
610 kvm_iodevice_init(&s
->dev_slave
, &picdev_slave_ops
);
611 kvm_iodevice_init(&s
->dev_eclr
, &picdev_eclr_ops
);
612 mutex_lock(&kvm
->slots_lock
);
613 ret
= kvm_io_bus_register_dev(kvm
, KVM_PIO_BUS
, 0x20, 2,
618 ret
= kvm_io_bus_register_dev(kvm
, KVM_PIO_BUS
, 0xa0, 2, &s
->dev_slave
);
622 ret
= kvm_io_bus_register_dev(kvm
, KVM_PIO_BUS
, 0x4d0, 2, &s
->dev_eclr
);
626 mutex_unlock(&kvm
->slots_lock
);
631 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
, &s
->dev_slave
);
634 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
, &s
->dev_master
);
637 mutex_unlock(&kvm
->slots_lock
);
644 void kvm_destroy_pic(struct kvm
*kvm
)
646 struct kvm_pic
*vpic
= kvm
->arch
.vpic
;
649 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
, &vpic
->dev_master
);
650 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
, &vpic
->dev_slave
);
651 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
, &vpic
->dev_eclr
);
652 kvm
->arch
.vpic
= NULL
;