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>
35 static void pic_clear_isr(struct kvm_kpic_state
*s
, int irq
)
37 s
->isr
&= ~(1 << irq
);
38 s
->isr_ack
|= (1 << irq
);
39 if (s
!= &s
->pics_state
->pics
[0])
41 kvm_notify_acked_irq(s
->pics_state
->kvm
, SELECT_PIC(irq
), irq
);
44 void kvm_pic_clear_isr_ack(struct kvm
*kvm
)
46 struct kvm_pic
*s
= pic_irqchip(kvm
);
48 s
->pics
[0].isr_ack
= 0xff;
49 s
->pics
[1].isr_ack
= 0xff;
50 spin_unlock(&s
->lock
);
54 * set irq level. If an edge is detected, then the IRR is set to 1
56 static inline int pic_set_irq1(struct kvm_kpic_state
*s
, int irq
, int level
)
60 if (s
->elcr
& mask
) /* level triggered */
62 ret
= !(s
->irr
& mask
);
69 else /* edge triggered */
71 if ((s
->last_irr
& mask
) == 0) {
72 ret
= !(s
->irr
& mask
);
79 return (s
->imr
& mask
) ? -1 : ret
;
83 * return the highest priority found in mask (highest = smallest
84 * number). Return 8 if no irq
86 static inline int get_priority(struct kvm_kpic_state
*s
, int mask
)
92 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
98 * return the pic wanted interrupt. return -1 if none
100 static int pic_get_irq(struct kvm_kpic_state
*s
)
102 int mask
, cur_priority
, priority
;
104 mask
= s
->irr
& ~s
->imr
;
105 priority
= get_priority(s
, mask
);
109 * compute current priority. If special fully nested mode on the
110 * master, the IRQ coming from the slave is not taken into account
111 * for the priority computation.
114 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
116 cur_priority
= get_priority(s
, mask
);
117 if (priority
< cur_priority
)
119 * higher priority found: an irq should be generated
121 return (priority
+ s
->priority_add
) & 7;
127 * raise irq to CPU if necessary. must be called every time the active
130 static void pic_update_irq(struct kvm_pic
*s
)
134 irq2
= pic_get_irq(&s
->pics
[1]);
137 * if irq request by slave pic, signal master PIC
139 pic_set_irq1(&s
->pics
[0], 2, 1);
140 pic_set_irq1(&s
->pics
[0], 2, 0);
142 irq
= pic_get_irq(&s
->pics
[0]);
144 s
->irq_request(s
->irq_request_opaque
, 1);
146 s
->irq_request(s
->irq_request_opaque
, 0);
149 void kvm_pic_update_irq(struct kvm_pic
*s
)
153 spin_unlock(&s
->lock
);
156 int kvm_pic_set_irq(void *opaque
, int irq
, int level
)
158 struct kvm_pic
*s
= opaque
;
162 if (irq
>= 0 && irq
< PIC_NUM_PINS
) {
163 ret
= pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
165 trace_kvm_pic_set_irq(irq
>> 3, irq
& 7, s
->pics
[irq
>> 3].elcr
,
166 s
->pics
[irq
>> 3].imr
, ret
== 0);
168 spin_unlock(&s
->lock
);
174 * acknowledge interrupt 'irq'
176 static inline void pic_intack(struct kvm_kpic_state
*s
, int irq
)
180 if (s
->rotate_on_auto_eoi
)
181 s
->priority_add
= (irq
+ 1) & 7;
182 pic_clear_isr(s
, irq
);
185 * We don't clear a level sensitive interrupt here
187 if (!(s
->elcr
& (1 << irq
)))
188 s
->irr
&= ~(1 << irq
);
191 int kvm_pic_read_irq(struct kvm
*kvm
)
193 int irq
, irq2
, intno
;
194 struct kvm_pic
*s
= pic_irqchip(kvm
);
197 irq
= pic_get_irq(&s
->pics
[0]);
199 pic_intack(&s
->pics
[0], irq
);
201 irq2
= pic_get_irq(&s
->pics
[1]);
203 pic_intack(&s
->pics
[1], irq2
);
206 * spurious IRQ on slave controller
209 intno
= s
->pics
[1].irq_base
+ irq2
;
212 intno
= s
->pics
[0].irq_base
+ irq
;
215 * spurious IRQ on host controller
218 intno
= s
->pics
[0].irq_base
+ irq
;
221 spin_unlock(&s
->lock
);
226 void kvm_pic_reset(struct kvm_kpic_state
*s
)
229 struct kvm
*kvm
= s
->pics_state
->irq_request_opaque
;
230 struct kvm_vcpu
*vcpu0
= kvm
->bsp_vcpu
;
232 if (s
== &s
->pics_state
->pics
[0])
237 for (irq
= 0; irq
< PIC_NUM_PINS
/2; irq
++) {
238 if (vcpu0
&& kvm_apic_accept_pic_intr(vcpu0
))
239 if (s
->irr
& (1 << irq
) || s
->isr
& (1 << irq
)) {
241 kvm_notify_acked_irq(kvm
, SELECT_PIC(n
), n
);
251 s
->read_reg_select
= 0;
256 s
->rotate_on_auto_eoi
= 0;
257 s
->special_fully_nested_mode
= 0;
261 static void pic_ioport_write(void *opaque
, u32 addr
, u32 val
)
263 struct kvm_kpic_state
*s
= opaque
;
264 int priority
, cmd
, irq
;
269 kvm_pic_reset(s
); /* init */
271 * deassert a pending interrupt
273 s
->pics_state
->irq_request(s
->pics_state
->
274 irq_request_opaque
, 0);
278 printk(KERN_ERR
"single mode not supported");
281 "level sensitive irq not supported");
282 } else if (val
& 0x08) {
286 s
->read_reg_select
= val
& 1;
288 s
->special_mask
= (val
>> 5) & 1;
294 s
->rotate_on_auto_eoi
= cmd
>> 2;
296 case 1: /* end of interrupt */
298 priority
= get_priority(s
, s
->isr
);
300 irq
= (priority
+ s
->priority_add
) & 7;
301 pic_clear_isr(s
, irq
);
303 s
->priority_add
= (irq
+ 1) & 7;
304 pic_update_irq(s
->pics_state
);
309 pic_clear_isr(s
, irq
);
310 pic_update_irq(s
->pics_state
);
313 s
->priority_add
= (val
+ 1) & 7;
314 pic_update_irq(s
->pics_state
);
318 s
->priority_add
= (irq
+ 1) & 7;
319 pic_clear_isr(s
, irq
);
320 pic_update_irq(s
->pics_state
);
323 break; /* no operation */
327 switch (s
->init_state
) {
328 case 0: /* normal mode */
330 pic_update_irq(s
->pics_state
);
333 s
->irq_base
= val
& 0xf8;
343 s
->special_fully_nested_mode
= (val
>> 4) & 1;
344 s
->auto_eoi
= (val
>> 1) & 1;
350 static u32
pic_poll_read(struct kvm_kpic_state
*s
, u32 addr1
)
354 ret
= pic_get_irq(s
);
357 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
358 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
360 s
->irr
&= ~(1 << ret
);
361 pic_clear_isr(s
, ret
);
362 if (addr1
>> 7 || ret
!= 2)
363 pic_update_irq(s
->pics_state
);
366 pic_update_irq(s
->pics_state
);
372 static u32
pic_ioport_read(void *opaque
, u32 addr1
)
374 struct kvm_kpic_state
*s
= opaque
;
381 ret
= pic_poll_read(s
, addr1
);
385 if (s
->read_reg_select
)
394 static void elcr_ioport_write(void *opaque
, u32 addr
, u32 val
)
396 struct kvm_kpic_state
*s
= opaque
;
397 s
->elcr
= val
& s
->elcr_mask
;
400 static u32
elcr_ioport_read(void *opaque
, u32 addr1
)
402 struct kvm_kpic_state
*s
= opaque
;
406 static int picdev_in_range(gpa_t addr
)
421 static inline struct kvm_pic
*to_pic(struct kvm_io_device
*dev
)
423 return container_of(dev
, struct kvm_pic
, dev
);
426 static int picdev_write(struct kvm_io_device
*this,
427 gpa_t addr
, int len
, const void *val
)
429 struct kvm_pic
*s
= to_pic(this);
430 unsigned char data
= *(unsigned char *)val
;
431 if (!picdev_in_range(addr
))
435 if (printk_ratelimit())
436 printk(KERN_ERR
"PIC: non byte write\n");
445 pic_ioport_write(&s
->pics
[addr
>> 7], addr
, data
);
449 elcr_ioport_write(&s
->pics
[addr
& 1], addr
, data
);
452 spin_unlock(&s
->lock
);
456 static int picdev_read(struct kvm_io_device
*this,
457 gpa_t addr
, int len
, void *val
)
459 struct kvm_pic
*s
= to_pic(this);
460 unsigned char data
= 0;
461 if (!picdev_in_range(addr
))
465 if (printk_ratelimit())
466 printk(KERN_ERR
"PIC: non byte read\n");
475 data
= pic_ioport_read(&s
->pics
[addr
>> 7], addr
);
479 data
= elcr_ioport_read(&s
->pics
[addr
& 1], addr
);
482 *(unsigned char *)val
= data
;
483 spin_unlock(&s
->lock
);
488 * callback when PIC0 irq status changed
490 static void pic_irq_request(void *opaque
, int level
)
492 struct kvm
*kvm
= opaque
;
493 struct kvm_vcpu
*vcpu
= kvm
->bsp_vcpu
;
494 struct kvm_pic
*s
= pic_irqchip(kvm
);
495 int irq
= pic_get_irq(&s
->pics
[0]);
498 if (vcpu
&& level
&& (s
->pics
[0].isr_ack
& (1 << irq
))) {
499 s
->pics
[0].isr_ack
&= ~(1 << irq
);
504 static const struct kvm_io_device_ops picdev_ops
= {
506 .write
= picdev_write
,
509 struct kvm_pic
*kvm_create_pic(struct kvm
*kvm
)
514 s
= kzalloc(sizeof(struct kvm_pic
), GFP_KERNEL
);
517 spin_lock_init(&s
->lock
);
519 s
->pics
[0].elcr_mask
= 0xf8;
520 s
->pics
[1].elcr_mask
= 0xde;
521 s
->irq_request
= pic_irq_request
;
522 s
->irq_request_opaque
= kvm
;
523 s
->pics
[0].pics_state
= s
;
524 s
->pics
[1].pics_state
= s
;
527 * Initialize PIO device
529 kvm_iodevice_init(&s
->dev
, &picdev_ops
);
530 ret
= kvm_io_bus_register_dev(kvm
, &kvm
->pio_bus
, &s
->dev
);