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>
31 #include <linux/kvm_host.h>
33 static void pic_clear_isr(struct kvm_kpic_state
*s
, int irq
)
35 s
->isr
&= ~(1 << irq
);
39 * set irq level. If an edge is detected, then the IRR is set to 1
41 static inline void pic_set_irq1(struct kvm_kpic_state
*s
, int irq
, int level
)
45 if (s
->elcr
& mask
) /* level triggered */
53 else /* edge triggered */
55 if ((s
->last_irr
& mask
) == 0)
63 * return the highest priority found in mask (highest = smallest
64 * number). Return 8 if no irq
66 static inline int get_priority(struct kvm_kpic_state
*s
, int mask
)
72 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
78 * return the pic wanted interrupt. return -1 if none
80 static int pic_get_irq(struct kvm_kpic_state
*s
)
82 int mask
, cur_priority
, priority
;
84 mask
= s
->irr
& ~s
->imr
;
85 priority
= get_priority(s
, mask
);
89 * compute current priority. If special fully nested mode on the
90 * master, the IRQ coming from the slave is not taken into account
91 * for the priority computation.
94 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
96 cur_priority
= get_priority(s
, mask
);
97 if (priority
< cur_priority
)
99 * higher priority found: an irq should be generated
101 return (priority
+ s
->priority_add
) & 7;
107 * raise irq to CPU if necessary. must be called every time the active
110 static void pic_update_irq(struct kvm_pic
*s
)
114 irq2
= pic_get_irq(&s
->pics
[1]);
117 * if irq request by slave pic, signal master PIC
119 pic_set_irq1(&s
->pics
[0], 2, 1);
120 pic_set_irq1(&s
->pics
[0], 2, 0);
122 irq
= pic_get_irq(&s
->pics
[0]);
124 s
->irq_request(s
->irq_request_opaque
, 1);
126 s
->irq_request(s
->irq_request_opaque
, 0);
129 void kvm_pic_update_irq(struct kvm_pic
*s
)
134 void kvm_pic_set_irq(void *opaque
, int irq
, int level
)
136 struct kvm_pic
*s
= opaque
;
138 if (irq
>= 0 && irq
< PIC_NUM_PINS
) {
139 pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
145 * acknowledge interrupt 'irq'
147 static inline void pic_intack(struct kvm_kpic_state
*s
, int irq
)
151 if (s
->rotate_on_auto_eoi
)
152 s
->priority_add
= (irq
+ 1) & 7;
153 pic_clear_isr(s
, irq
);
156 * We don't clear a level sensitive interrupt here
158 if (!(s
->elcr
& (1 << irq
)))
159 s
->irr
&= ~(1 << irq
);
162 int kvm_pic_read_irq(struct kvm
*kvm
)
164 int irq
, irq2
, intno
;
165 struct kvm_pic
*s
= pic_irqchip(kvm
);
167 irq
= pic_get_irq(&s
->pics
[0]);
169 pic_intack(&s
->pics
[0], irq
);
171 irq2
= pic_get_irq(&s
->pics
[1]);
173 pic_intack(&s
->pics
[1], irq2
);
176 * spurious IRQ on slave controller
179 intno
= s
->pics
[1].irq_base
+ irq2
;
182 intno
= s
->pics
[0].irq_base
+ irq
;
185 * spurious IRQ on host controller
188 intno
= s
->pics
[0].irq_base
+ irq
;
191 kvm_notify_acked_irq(kvm
, irq
);
196 void kvm_pic_reset(struct kvm_kpic_state
*s
)
199 struct kvm
*kvm
= s
->pics_state
->irq_request_opaque
;
200 struct kvm_vcpu
*vcpu0
= kvm
->vcpus
[0];
202 if (s
== &s
->pics_state
->pics
[0])
207 for (irq
= 0; irq
< PIC_NUM_PINS
/2; irq
++) {
208 if (vcpu0
&& kvm_apic_accept_pic_intr(vcpu0
))
209 if (s
->irr
& (1 << irq
) || s
->isr
& (1 << irq
))
210 kvm_notify_acked_irq(kvm
, irq
+irqbase
);
218 s
->read_reg_select
= 0;
223 s
->rotate_on_auto_eoi
= 0;
224 s
->special_fully_nested_mode
= 0;
228 static void pic_ioport_write(void *opaque
, u32 addr
, u32 val
)
230 struct kvm_kpic_state
*s
= opaque
;
231 int priority
, cmd
, irq
;
236 kvm_pic_reset(s
); /* init */
238 * deassert a pending interrupt
240 s
->pics_state
->irq_request(s
->pics_state
->
241 irq_request_opaque
, 0);
245 printk(KERN_ERR
"single mode not supported");
248 "level sensitive irq not supported");
249 } else if (val
& 0x08) {
253 s
->read_reg_select
= val
& 1;
255 s
->special_mask
= (val
>> 5) & 1;
261 s
->rotate_on_auto_eoi
= cmd
>> 2;
263 case 1: /* end of interrupt */
265 priority
= get_priority(s
, s
->isr
);
267 irq
= (priority
+ s
->priority_add
) & 7;
268 pic_clear_isr(s
, irq
);
270 s
->priority_add
= (irq
+ 1) & 7;
271 pic_update_irq(s
->pics_state
);
276 pic_clear_isr(s
, irq
);
277 pic_update_irq(s
->pics_state
);
280 s
->priority_add
= (val
+ 1) & 7;
281 pic_update_irq(s
->pics_state
);
285 s
->priority_add
= (irq
+ 1) & 7;
286 pic_clear_isr(s
, irq
);
287 pic_update_irq(s
->pics_state
);
290 break; /* no operation */
294 switch (s
->init_state
) {
295 case 0: /* normal mode */
297 pic_update_irq(s
->pics_state
);
300 s
->irq_base
= val
& 0xf8;
310 s
->special_fully_nested_mode
= (val
>> 4) & 1;
311 s
->auto_eoi
= (val
>> 1) & 1;
317 static u32
pic_poll_read(struct kvm_kpic_state
*s
, u32 addr1
)
321 ret
= pic_get_irq(s
);
324 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
325 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
327 s
->irr
&= ~(1 << ret
);
328 pic_clear_isr(s
, ret
);
329 if (addr1
>> 7 || ret
!= 2)
330 pic_update_irq(s
->pics_state
);
333 pic_update_irq(s
->pics_state
);
339 static u32
pic_ioport_read(void *opaque
, u32 addr1
)
341 struct kvm_kpic_state
*s
= opaque
;
348 ret
= pic_poll_read(s
, addr1
);
352 if (s
->read_reg_select
)
361 static void elcr_ioport_write(void *opaque
, u32 addr
, u32 val
)
363 struct kvm_kpic_state
*s
= opaque
;
364 s
->elcr
= val
& s
->elcr_mask
;
367 static u32
elcr_ioport_read(void *opaque
, u32 addr1
)
369 struct kvm_kpic_state
*s
= opaque
;
373 static int picdev_in_range(struct kvm_io_device
*this, gpa_t addr
,
374 int len
, int is_write
)
389 static void picdev_write(struct kvm_io_device
*this,
390 gpa_t addr
, int len
, const void *val
)
392 struct kvm_pic
*s
= this->private;
393 unsigned char data
= *(unsigned char *)val
;
396 if (printk_ratelimit())
397 printk(KERN_ERR
"PIC: non byte write\n");
405 pic_ioport_write(&s
->pics
[addr
>> 7], addr
, data
);
409 elcr_ioport_write(&s
->pics
[addr
& 1], addr
, data
);
414 static void picdev_read(struct kvm_io_device
*this,
415 gpa_t addr
, int len
, void *val
)
417 struct kvm_pic
*s
= this->private;
418 unsigned char data
= 0;
421 if (printk_ratelimit())
422 printk(KERN_ERR
"PIC: non byte read\n");
430 data
= pic_ioport_read(&s
->pics
[addr
>> 7], addr
);
434 data
= elcr_ioport_read(&s
->pics
[addr
& 1], addr
);
437 *(unsigned char *)val
= data
;
441 * callback when PIC0 irq status changed
443 static void pic_irq_request(void *opaque
, int level
)
445 struct kvm
*kvm
= opaque
;
446 struct kvm_vcpu
*vcpu
= kvm
->vcpus
[0];
448 pic_irqchip(kvm
)->output
= level
;
453 struct kvm_pic
*kvm_create_pic(struct kvm
*kvm
)
456 s
= kzalloc(sizeof(struct kvm_pic
), GFP_KERNEL
);
459 s
->pics
[0].elcr_mask
= 0xf8;
460 s
->pics
[1].elcr_mask
= 0xde;
461 s
->irq_request
= pic_irq_request
;
462 s
->irq_request_opaque
= kvm
;
463 s
->pics
[0].pics_state
= s
;
464 s
->pics
[1].pics_state
= s
;
467 * Initialize PIO device
469 s
->dev
.read
= picdev_read
;
470 s
->dev
.write
= picdev_write
;
471 s
->dev
.in_range
= picdev_in_range
;
473 kvm_io_bus_register_dev(&kvm
->pio_bus
, &s
->dev
);