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 static void pic_irq_request(struct kvm
*kvm
, int level
);
39 static void pic_lock(struct kvm_pic
*s
)
45 static void pic_unlock(struct kvm_pic
*s
)
48 bool wakeup
= s
->wakeup_needed
;
49 struct kvm_vcpu
*vcpu
, *found
= NULL
;
52 s
->wakeup_needed
= false;
54 spin_unlock(&s
->lock
);
57 kvm_for_each_vcpu(i
, vcpu
, s
->kvm
) {
58 if (kvm_apic_accept_pic_intr(vcpu
)) {
65 found
= s
->kvm
->bsp_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 s
->isr_ack
|= (1 << irq
);
79 if (s
!= &s
->pics_state
->pics
[0])
82 * We are dropping lock while calling ack notifiers since ack
83 * notifier callbacks for assigned devices call into PIC recursively.
84 * Other interrupt may be delivered to PIC while lock is dropped but
85 * it should be safe since PIC state is already updated at this stage.
87 pic_unlock(s
->pics_state
);
88 kvm_notify_acked_irq(s
->pics_state
->kvm
, SELECT_PIC(irq
), irq
);
89 pic_lock(s
->pics_state
);
92 void kvm_pic_clear_isr_ack(struct kvm
*kvm
)
94 struct kvm_pic
*s
= pic_irqchip(kvm
);
97 s
->pics
[0].isr_ack
= 0xff;
98 s
->pics
[1].isr_ack
= 0xff;
103 * set irq level. If an edge is detected, then the IRR is set to 1
105 static inline int pic_set_irq1(struct kvm_kpic_state
*s
, int irq
, int level
)
109 if (s
->elcr
& mask
) /* level triggered */
111 ret
= !(s
->irr
& mask
);
116 s
->last_irr
&= ~mask
;
118 else /* edge triggered */
120 if ((s
->last_irr
& mask
) == 0) {
121 ret
= !(s
->irr
& mask
);
126 s
->last_irr
&= ~mask
;
128 return (s
->imr
& mask
) ? -1 : ret
;
132 * return the highest priority found in mask (highest = smallest
133 * number). Return 8 if no irq
135 static inline int get_priority(struct kvm_kpic_state
*s
, int mask
)
141 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
147 * return the pic wanted interrupt. return -1 if none
149 static int pic_get_irq(struct kvm_kpic_state
*s
)
151 int mask
, cur_priority
, priority
;
153 mask
= s
->irr
& ~s
->imr
;
154 priority
= get_priority(s
, mask
);
158 * compute current priority. If special fully nested mode on the
159 * master, the IRQ coming from the slave is not taken into account
160 * for the priority computation.
163 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
165 cur_priority
= get_priority(s
, mask
);
166 if (priority
< cur_priority
)
168 * higher priority found: an irq should be generated
170 return (priority
+ s
->priority_add
) & 7;
176 * raise irq to CPU if necessary. must be called every time the active
179 static void pic_update_irq(struct kvm_pic
*s
)
183 irq2
= pic_get_irq(&s
->pics
[1]);
186 * if irq request by slave pic, signal master PIC
188 pic_set_irq1(&s
->pics
[0], 2, 1);
189 pic_set_irq1(&s
->pics
[0], 2, 0);
191 irq
= pic_get_irq(&s
->pics
[0]);
192 pic_irq_request(s
->kvm
, irq
>= 0);
195 void kvm_pic_update_irq(struct kvm_pic
*s
)
202 int kvm_pic_set_irq(void *opaque
, int irq
, int level
)
204 struct kvm_pic
*s
= opaque
;
208 if (irq
>= 0 && irq
< PIC_NUM_PINS
) {
209 ret
= pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
211 trace_kvm_pic_set_irq(irq
>> 3, irq
& 7, s
->pics
[irq
>> 3].elcr
,
212 s
->pics
[irq
>> 3].imr
, ret
== 0);
220 * acknowledge interrupt 'irq'
222 static inline void pic_intack(struct kvm_kpic_state
*s
, int irq
)
226 * We don't clear a level sensitive interrupt here
228 if (!(s
->elcr
& (1 << irq
)))
229 s
->irr
&= ~(1 << irq
);
232 if (s
->rotate_on_auto_eoi
)
233 s
->priority_add
= (irq
+ 1) & 7;
234 pic_clear_isr(s
, irq
);
239 int kvm_pic_read_irq(struct kvm
*kvm
)
241 int irq
, irq2
, intno
;
242 struct kvm_pic
*s
= pic_irqchip(kvm
);
245 irq
= pic_get_irq(&s
->pics
[0]);
247 pic_intack(&s
->pics
[0], irq
);
249 irq2
= pic_get_irq(&s
->pics
[1]);
251 pic_intack(&s
->pics
[1], irq2
);
254 * spurious IRQ on slave controller
257 intno
= s
->pics
[1].irq_base
+ irq2
;
260 intno
= s
->pics
[0].irq_base
+ irq
;
263 * spurious IRQ on host controller
266 intno
= s
->pics
[0].irq_base
+ irq
;
274 void kvm_pic_reset(struct kvm_kpic_state
*s
)
277 struct kvm_vcpu
*vcpu0
= s
->pics_state
->kvm
->bsp_vcpu
;
278 u8 irr
= s
->irr
, isr
= s
->imr
;
287 s
->read_reg_select
= 0;
292 s
->rotate_on_auto_eoi
= 0;
293 s
->special_fully_nested_mode
= 0;
296 for (irq
= 0; irq
< PIC_NUM_PINS
/2; irq
++) {
297 if (vcpu0
&& kvm_apic_accept_pic_intr(vcpu0
))
298 if (irr
& (1 << irq
) || isr
& (1 << irq
)) {
299 pic_clear_isr(s
, irq
);
304 static void pic_ioport_write(void *opaque
, u32 addr
, u32 val
)
306 struct kvm_kpic_state
*s
= opaque
;
307 int priority
, cmd
, irq
;
317 s
->read_reg_select
= 0;
319 s
->special_fully_nested_mode
= 0;
324 printk(KERN_ERR
"single mode not supported");
327 "level sensitive irq not supported");
328 } else if (val
& 0x08) {
332 s
->read_reg_select
= val
& 1;
334 s
->special_mask
= (val
>> 5) & 1;
340 s
->rotate_on_auto_eoi
= cmd
>> 2;
342 case 1: /* end of interrupt */
344 priority
= get_priority(s
, s
->isr
);
346 irq
= (priority
+ s
->priority_add
) & 7;
348 s
->priority_add
= (irq
+ 1) & 7;
349 pic_clear_isr(s
, irq
);
350 pic_update_irq(s
->pics_state
);
355 pic_clear_isr(s
, irq
);
356 pic_update_irq(s
->pics_state
);
359 s
->priority_add
= (val
+ 1) & 7;
360 pic_update_irq(s
->pics_state
);
364 s
->priority_add
= (irq
+ 1) & 7;
365 pic_clear_isr(s
, irq
);
366 pic_update_irq(s
->pics_state
);
369 break; /* no operation */
373 switch (s
->init_state
) {
374 case 0: { /* normal mode */
375 u8 imr_diff
= s
->imr
^ val
,
376 off
= (s
== &s
->pics_state
->pics
[0]) ? 0 : 8;
378 for (irq
= 0; irq
< PIC_NUM_PINS
/2; irq
++)
379 if (imr_diff
& (1 << irq
))
380 kvm_fire_mask_notifiers(
382 SELECT_PIC(irq
+ off
),
384 !!(s
->imr
& (1 << irq
)));
385 pic_update_irq(s
->pics_state
);
389 s
->irq_base
= val
& 0xf8;
399 s
->special_fully_nested_mode
= (val
>> 4) & 1;
400 s
->auto_eoi
= (val
>> 1) & 1;
406 static u32
pic_poll_read(struct kvm_kpic_state
*s
, u32 addr1
)
410 ret
= pic_get_irq(s
);
413 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
414 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
416 s
->irr
&= ~(1 << ret
);
417 pic_clear_isr(s
, ret
);
418 if (addr1
>> 7 || ret
!= 2)
419 pic_update_irq(s
->pics_state
);
422 pic_update_irq(s
->pics_state
);
428 static u32
pic_ioport_read(void *opaque
, u32 addr1
)
430 struct kvm_kpic_state
*s
= opaque
;
437 ret
= pic_poll_read(s
, addr1
);
441 if (s
->read_reg_select
)
450 static void elcr_ioport_write(void *opaque
, u32 addr
, u32 val
)
452 struct kvm_kpic_state
*s
= opaque
;
453 s
->elcr
= val
& s
->elcr_mask
;
456 static u32
elcr_ioport_read(void *opaque
, u32 addr1
)
458 struct kvm_kpic_state
*s
= opaque
;
462 static int picdev_in_range(gpa_t addr
)
477 static inline struct kvm_pic
*to_pic(struct kvm_io_device
*dev
)
479 return container_of(dev
, struct kvm_pic
, dev
);
482 static int picdev_write(struct kvm_io_device
*this,
483 gpa_t addr
, int len
, const void *val
)
485 struct kvm_pic
*s
= to_pic(this);
486 unsigned char data
= *(unsigned char *)val
;
487 if (!picdev_in_range(addr
))
491 if (printk_ratelimit())
492 printk(KERN_ERR
"PIC: non byte write\n");
501 pic_ioport_write(&s
->pics
[addr
>> 7], addr
, data
);
505 elcr_ioport_write(&s
->pics
[addr
& 1], addr
, data
);
512 static int picdev_read(struct kvm_io_device
*this,
513 gpa_t addr
, int len
, void *val
)
515 struct kvm_pic
*s
= to_pic(this);
516 unsigned char data
= 0;
517 if (!picdev_in_range(addr
))
521 if (printk_ratelimit())
522 printk(KERN_ERR
"PIC: non byte read\n");
531 data
= pic_ioport_read(&s
->pics
[addr
>> 7], addr
);
535 data
= elcr_ioport_read(&s
->pics
[addr
& 1], addr
);
538 *(unsigned char *)val
= data
;
544 * callback when PIC0 irq status changed
546 static void pic_irq_request(struct kvm
*kvm
, int level
)
548 struct kvm_vcpu
*vcpu
= kvm
->bsp_vcpu
;
549 struct kvm_pic
*s
= pic_irqchip(kvm
);
550 int irq
= pic_get_irq(&s
->pics
[0]);
553 if (vcpu
&& level
&& (s
->pics
[0].isr_ack
& (1 << irq
))) {
554 s
->pics
[0].isr_ack
&= ~(1 << irq
);
555 s
->wakeup_needed
= true;
559 static const struct kvm_io_device_ops picdev_ops
= {
561 .write
= picdev_write
,
564 struct kvm_pic
*kvm_create_pic(struct kvm
*kvm
)
569 s
= kzalloc(sizeof(struct kvm_pic
), GFP_KERNEL
);
572 spin_lock_init(&s
->lock
);
574 s
->pics
[0].elcr_mask
= 0xf8;
575 s
->pics
[1].elcr_mask
= 0xde;
576 s
->pics
[0].pics_state
= s
;
577 s
->pics
[1].pics_state
= s
;
580 * Initialize PIO device
582 kvm_iodevice_init(&s
->dev
, &picdev_ops
);
583 mutex_lock(&kvm
->slots_lock
);
584 ret
= kvm_io_bus_register_dev(kvm
, KVM_PIO_BUS
, &s
->dev
);
585 mutex_unlock(&kvm
->slots_lock
);
594 void kvm_destroy_pic(struct kvm
*kvm
)
596 struct kvm_pic
*vpic
= kvm
->arch
.vpic
;
599 kvm_io_bus_unregister_dev(kvm
, KVM_PIO_BUS
, &vpic
->dev
);
600 kvm
->arch
.vpic
= NULL
;