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
)
40 static void pic_unlock(struct kvm_pic
*s
)
43 struct kvm
*kvm
= s
->kvm
;
44 unsigned acks
= s
->pending_acks
;
45 bool wakeup
= s
->wakeup_needed
;
46 struct kvm_vcpu
*vcpu
;
49 s
->wakeup_needed
= false;
51 spin_unlock(&s
->lock
);
54 kvm_notify_acked_irq(kvm
, SELECT_PIC(__ffs(acks
)),
60 vcpu
= s
->kvm
->vcpus
[0];
66 static void pic_clear_isr(struct kvm_kpic_state
*s
, int irq
)
68 s
->isr
&= ~(1 << irq
);
69 s
->isr_ack
|= (1 << irq
);
72 void kvm_pic_clear_isr_ack(struct kvm
*kvm
)
74 struct kvm_pic
*s
= pic_irqchip(kvm
);
75 s
->pics
[0].isr_ack
= 0xff;
76 s
->pics
[1].isr_ack
= 0xff;
80 * set irq level. If an edge is detected, then the IRR is set to 1
82 static inline int pic_set_irq1(struct kvm_kpic_state
*s
, int irq
, int level
)
86 if (s
->elcr
& mask
) /* level triggered */
88 ret
= !(s
->irr
& mask
);
95 else /* edge triggered */
97 if ((s
->last_irr
& mask
) == 0) {
98 ret
= !(s
->irr
& mask
);
103 s
->last_irr
&= ~mask
;
105 return (s
->imr
& mask
) ? -1 : ret
;
109 * return the highest priority found in mask (highest = smallest
110 * number). Return 8 if no irq
112 static inline int get_priority(struct kvm_kpic_state
*s
, int mask
)
118 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
124 * return the pic wanted interrupt. return -1 if none
126 static int pic_get_irq(struct kvm_kpic_state
*s
)
128 int mask
, cur_priority
, priority
;
130 mask
= s
->irr
& ~s
->imr
;
131 priority
= get_priority(s
, mask
);
135 * compute current priority. If special fully nested mode on the
136 * master, the IRQ coming from the slave is not taken into account
137 * for the priority computation.
140 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
142 cur_priority
= get_priority(s
, mask
);
143 if (priority
< cur_priority
)
145 * higher priority found: an irq should be generated
147 return (priority
+ s
->priority_add
) & 7;
153 * raise irq to CPU if necessary. must be called every time the active
156 static void pic_update_irq(struct kvm_pic
*s
)
160 irq2
= pic_get_irq(&s
->pics
[1]);
163 * if irq request by slave pic, signal master PIC
165 pic_set_irq1(&s
->pics
[0], 2, 1);
166 pic_set_irq1(&s
->pics
[0], 2, 0);
168 irq
= pic_get_irq(&s
->pics
[0]);
170 s
->irq_request(s
->irq_request_opaque
, 1);
172 s
->irq_request(s
->irq_request_opaque
, 0);
175 void kvm_pic_update_irq(struct kvm_pic
*s
)
182 int kvm_pic_set_irq(void *opaque
, int irq
, int level
)
184 struct kvm_pic
*s
= opaque
;
188 if (irq
>= 0 && irq
< PIC_NUM_PINS
) {
189 ret
= pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
198 * acknowledge interrupt 'irq'
200 static inline void pic_intack(struct kvm_kpic_state
*s
, int irq
)
204 if (s
->rotate_on_auto_eoi
)
205 s
->priority_add
= (irq
+ 1) & 7;
206 pic_clear_isr(s
, irq
);
209 * We don't clear a level sensitive interrupt here
211 if (!(s
->elcr
& (1 << irq
)))
212 s
->irr
&= ~(1 << irq
);
215 int kvm_pic_read_irq(struct kvm
*kvm
)
217 int irq
, irq2
, intno
;
218 struct kvm_pic
*s
= pic_irqchip(kvm
);
221 irq
= pic_get_irq(&s
->pics
[0]);
223 pic_intack(&s
->pics
[0], irq
);
225 irq2
= pic_get_irq(&s
->pics
[1]);
227 pic_intack(&s
->pics
[1], irq2
);
230 * spurious IRQ on slave controller
233 intno
= s
->pics
[1].irq_base
+ irq2
;
236 intno
= s
->pics
[0].irq_base
+ irq
;
239 * spurious IRQ on host controller
242 intno
= s
->pics
[0].irq_base
+ irq
;
246 kvm_notify_acked_irq(kvm
, SELECT_PIC(irq
), irq
);
251 void kvm_pic_reset(struct kvm_kpic_state
*s
)
254 struct kvm
*kvm
= s
->pics_state
->irq_request_opaque
;
255 struct kvm_vcpu
*vcpu0
= kvm
->vcpus
[0];
257 if (s
== &s
->pics_state
->pics
[0])
262 for (irq
= 0; irq
< PIC_NUM_PINS
/2; irq
++) {
263 if (vcpu0
&& kvm_apic_accept_pic_intr(vcpu0
))
264 if (s
->irr
& (1 << irq
) || s
->isr
& (1 << irq
)) {
266 s
->pics_state
->pending_acks
|= 1 << n
;
276 s
->read_reg_select
= 0;
281 s
->rotate_on_auto_eoi
= 0;
282 s
->special_fully_nested_mode
= 0;
286 static void pic_ioport_write(void *opaque
, u32 addr
, u32 val
)
288 struct kvm_kpic_state
*s
= opaque
;
289 int priority
, cmd
, irq
;
294 kvm_pic_reset(s
); /* init */
296 * deassert a pending interrupt
298 s
->pics_state
->irq_request(s
->pics_state
->
299 irq_request_opaque
, 0);
303 printk(KERN_ERR
"single mode not supported");
306 "level sensitive irq not supported");
307 } else if (val
& 0x08) {
311 s
->read_reg_select
= val
& 1;
313 s
->special_mask
= (val
>> 5) & 1;
319 s
->rotate_on_auto_eoi
= cmd
>> 2;
321 case 1: /* end of interrupt */
323 priority
= get_priority(s
, s
->isr
);
325 irq
= (priority
+ s
->priority_add
) & 7;
326 pic_clear_isr(s
, irq
);
328 s
->priority_add
= (irq
+ 1) & 7;
329 pic_update_irq(s
->pics_state
);
334 pic_clear_isr(s
, irq
);
335 pic_update_irq(s
->pics_state
);
338 s
->priority_add
= (val
+ 1) & 7;
339 pic_update_irq(s
->pics_state
);
343 s
->priority_add
= (irq
+ 1) & 7;
344 pic_clear_isr(s
, irq
);
345 pic_update_irq(s
->pics_state
);
348 break; /* no operation */
352 switch (s
->init_state
) {
353 case 0: /* normal mode */
355 pic_update_irq(s
->pics_state
);
358 s
->irq_base
= val
& 0xf8;
368 s
->special_fully_nested_mode
= (val
>> 4) & 1;
369 s
->auto_eoi
= (val
>> 1) & 1;
375 static u32
pic_poll_read(struct kvm_kpic_state
*s
, u32 addr1
)
379 ret
= pic_get_irq(s
);
382 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
383 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
385 s
->irr
&= ~(1 << ret
);
386 pic_clear_isr(s
, ret
);
387 if (addr1
>> 7 || ret
!= 2)
388 pic_update_irq(s
->pics_state
);
391 pic_update_irq(s
->pics_state
);
397 static u32
pic_ioport_read(void *opaque
, u32 addr1
)
399 struct kvm_kpic_state
*s
= opaque
;
406 ret
= pic_poll_read(s
, addr1
);
410 if (s
->read_reg_select
)
419 static void elcr_ioport_write(void *opaque
, u32 addr
, u32 val
)
421 struct kvm_kpic_state
*s
= opaque
;
422 s
->elcr
= val
& s
->elcr_mask
;
425 static u32
elcr_ioport_read(void *opaque
, u32 addr1
)
427 struct kvm_kpic_state
*s
= opaque
;
431 static int picdev_in_range(struct kvm_io_device
*this, gpa_t addr
,
432 int len
, int is_write
)
447 static void picdev_write(struct kvm_io_device
*this,
448 gpa_t addr
, int len
, const void *val
)
450 struct kvm_pic
*s
= this->private;
451 unsigned char data
= *(unsigned char *)val
;
454 if (printk_ratelimit())
455 printk(KERN_ERR
"PIC: non byte write\n");
464 pic_ioport_write(&s
->pics
[addr
>> 7], addr
, data
);
468 elcr_ioport_write(&s
->pics
[addr
& 1], addr
, data
);
474 static void picdev_read(struct kvm_io_device
*this,
475 gpa_t addr
, int len
, void *val
)
477 struct kvm_pic
*s
= this->private;
478 unsigned char data
= 0;
481 if (printk_ratelimit())
482 printk(KERN_ERR
"PIC: non byte read\n");
491 data
= pic_ioport_read(&s
->pics
[addr
>> 7], addr
);
495 data
= elcr_ioport_read(&s
->pics
[addr
& 1], addr
);
498 *(unsigned char *)val
= data
;
503 * callback when PIC0 irq status changed
505 static void pic_irq_request(void *opaque
, int level
)
507 struct kvm
*kvm
= opaque
;
508 struct kvm_vcpu
*vcpu
= kvm
->vcpus
[0];
509 struct kvm_pic
*s
= pic_irqchip(kvm
);
510 int irq
= pic_get_irq(&s
->pics
[0]);
513 if (vcpu
&& level
&& (s
->pics
[0].isr_ack
& (1 << irq
))) {
514 s
->pics
[0].isr_ack
&= ~(1 << irq
);
515 s
->wakeup_needed
= true;
519 struct kvm_pic
*kvm_create_pic(struct kvm
*kvm
)
522 s
= kzalloc(sizeof(struct kvm_pic
), GFP_KERNEL
);
525 spin_lock_init(&s
->lock
);
527 s
->pics
[0].elcr_mask
= 0xf8;
528 s
->pics
[1].elcr_mask
= 0xde;
529 s
->irq_request
= pic_irq_request
;
530 s
->irq_request_opaque
= kvm
;
531 s
->pics
[0].pics_state
= s
;
532 s
->pics
[1].pics_state
= s
;
535 * Initialize PIO device
537 s
->dev
.read
= picdev_read
;
538 s
->dev
.write
= picdev_write
;
539 s
->dev
.in_range
= picdev_in_range
;
541 kvm_io_bus_register_dev(&kvm
->pio_bus
, &s
->dev
);