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>
32 * set irq level. If an edge is detected, then the IRR is set to 1
34 static inline void pic_set_irq1(struct kvm_kpic_state
*s
, int irq
, int level
)
38 if (s
->elcr
& mask
) /* level triggered */
46 else /* edge triggered */
48 if ((s
->last_irr
& mask
) == 0)
56 * return the highest priority found in mask (highest = smallest
57 * number). Return 8 if no irq
59 static inline int get_priority(struct kvm_kpic_state
*s
, int mask
)
65 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
71 * return the pic wanted interrupt. return -1 if none
73 static int pic_get_irq(struct kvm_kpic_state
*s
)
75 int mask
, cur_priority
, priority
;
77 mask
= s
->irr
& ~s
->imr
;
78 priority
= get_priority(s
, mask
);
82 * compute current priority. If special fully nested mode on the
83 * master, the IRQ coming from the slave is not taken into account
84 * for the priority computation.
87 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
89 cur_priority
= get_priority(s
, mask
);
90 if (priority
< cur_priority
)
92 * higher priority found: an irq should be generated
94 return (priority
+ s
->priority_add
) & 7;
100 * raise irq to CPU if necessary. must be called every time the active
103 static void pic_update_irq(struct kvm_pic
*s
)
107 irq2
= pic_get_irq(&s
->pics
[1]);
110 * if irq request by slave pic, signal master PIC
112 pic_set_irq1(&s
->pics
[0], 2, 1);
113 pic_set_irq1(&s
->pics
[0], 2, 0);
115 irq
= pic_get_irq(&s
->pics
[0]);
117 s
->irq_request(s
->irq_request_opaque
, 1);
119 s
->irq_request(s
->irq_request_opaque
, 0);
122 void kvm_pic_update_irq(struct kvm_pic
*s
)
127 void kvm_pic_set_irq(void *opaque
, int irq
, int level
)
129 struct kvm_pic
*s
= opaque
;
131 pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
136 * acknowledge interrupt 'irq'
138 static inline void pic_intack(struct kvm_kpic_state
*s
, int irq
)
141 if (s
->rotate_on_auto_eoi
)
142 s
->priority_add
= (irq
+ 1) & 7;
144 s
->isr
|= (1 << irq
);
146 * We don't clear a level sensitive interrupt here
148 if (!(s
->elcr
& (1 << irq
)))
149 s
->irr
&= ~(1 << irq
);
152 int kvm_pic_read_irq(struct kvm_pic
*s
)
154 int irq
, irq2
, intno
;
156 irq
= pic_get_irq(&s
->pics
[0]);
158 pic_intack(&s
->pics
[0], irq
);
160 irq2
= pic_get_irq(&s
->pics
[1]);
162 pic_intack(&s
->pics
[1], irq2
);
165 * spurious IRQ on slave controller
168 intno
= s
->pics
[1].irq_base
+ irq2
;
171 intno
= s
->pics
[0].irq_base
+ irq
;
174 * spurious IRQ on host controller
177 intno
= s
->pics
[0].irq_base
+ irq
;
184 void kvm_pic_reset(struct kvm_kpic_state
*s
)
192 s
->read_reg_select
= 0;
197 s
->rotate_on_auto_eoi
= 0;
198 s
->special_fully_nested_mode
= 0;
202 static void pic_ioport_write(void *opaque
, u32 addr
, u32 val
)
204 struct kvm_kpic_state
*s
= opaque
;
205 int priority
, cmd
, irq
;
210 kvm_pic_reset(s
); /* init */
212 * deassert a pending interrupt
214 s
->pics_state
->irq_request(s
->pics_state
->
215 irq_request_opaque
, 0);
219 printk(KERN_ERR
"single mode not supported");
222 "level sensitive irq not supported");
223 } else if (val
& 0x08) {
227 s
->read_reg_select
= val
& 1;
229 s
->special_mask
= (val
>> 5) & 1;
235 s
->rotate_on_auto_eoi
= cmd
>> 2;
237 case 1: /* end of interrupt */
239 priority
= get_priority(s
, s
->isr
);
241 irq
= (priority
+ s
->priority_add
) & 7;
242 s
->isr
&= ~(1 << irq
);
244 s
->priority_add
= (irq
+ 1) & 7;
245 pic_update_irq(s
->pics_state
);
250 s
->isr
&= ~(1 << irq
);
251 pic_update_irq(s
->pics_state
);
254 s
->priority_add
= (val
+ 1) & 7;
255 pic_update_irq(s
->pics_state
);
259 s
->isr
&= ~(1 << irq
);
260 s
->priority_add
= (irq
+ 1) & 7;
261 pic_update_irq(s
->pics_state
);
264 break; /* no operation */
268 switch (s
->init_state
) {
269 case 0: /* normal mode */
271 pic_update_irq(s
->pics_state
);
274 s
->irq_base
= val
& 0xf8;
284 s
->special_fully_nested_mode
= (val
>> 4) & 1;
285 s
->auto_eoi
= (val
>> 1) & 1;
291 static u32
pic_poll_read(struct kvm_kpic_state
*s
, u32 addr1
)
295 ret
= pic_get_irq(s
);
298 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
299 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
301 s
->irr
&= ~(1 << ret
);
302 s
->isr
&= ~(1 << ret
);
303 if (addr1
>> 7 || ret
!= 2)
304 pic_update_irq(s
->pics_state
);
307 pic_update_irq(s
->pics_state
);
313 static u32
pic_ioport_read(void *opaque
, u32 addr1
)
315 struct kvm_kpic_state
*s
= opaque
;
322 ret
= pic_poll_read(s
, addr1
);
326 if (s
->read_reg_select
)
335 static void elcr_ioport_write(void *opaque
, u32 addr
, u32 val
)
337 struct kvm_kpic_state
*s
= opaque
;
338 s
->elcr
= val
& s
->elcr_mask
;
341 static u32
elcr_ioport_read(void *opaque
, u32 addr1
)
343 struct kvm_kpic_state
*s
= opaque
;
347 static int picdev_in_range(struct kvm_io_device
*this, gpa_t addr
)
362 static void picdev_write(struct kvm_io_device
*this,
363 gpa_t addr
, int len
, const void *val
)
365 struct kvm_pic
*s
= this->private;
366 unsigned char data
= *(unsigned char *)val
;
369 if (printk_ratelimit())
370 printk(KERN_ERR
"PIC: non byte write\n");
378 pic_ioport_write(&s
->pics
[addr
>> 7], addr
, data
);
382 elcr_ioport_write(&s
->pics
[addr
& 1], addr
, data
);
387 static void picdev_read(struct kvm_io_device
*this,
388 gpa_t addr
, int len
, void *val
)
390 struct kvm_pic
*s
= this->private;
391 unsigned char data
= 0;
394 if (printk_ratelimit())
395 printk(KERN_ERR
"PIC: non byte read\n");
403 data
= pic_ioport_read(&s
->pics
[addr
>> 7], addr
);
407 data
= elcr_ioport_read(&s
->pics
[addr
& 1], addr
);
410 *(unsigned char *)val
= data
;
414 * callback when PIC0 irq status changed
416 static void pic_irq_request(void *opaque
, int level
)
418 struct kvm
*kvm
= opaque
;
419 struct kvm_vcpu
*vcpu
= kvm
->vcpus
[0];
421 pic_irqchip(kvm
)->output
= level
;
426 struct kvm_pic
*kvm_create_pic(struct kvm
*kvm
)
429 s
= kzalloc(sizeof(struct kvm_pic
), GFP_KERNEL
);
432 s
->pics
[0].elcr_mask
= 0xf8;
433 s
->pics
[1].elcr_mask
= 0xde;
434 s
->irq_request
= pic_irq_request
;
435 s
->irq_request_opaque
= kvm
;
436 s
->pics
[0].pics_state
= s
;
437 s
->pics
[1].pics_state
= s
;
440 * Initialize PIO device
442 s
->dev
.read
= picdev_read
;
443 s
->dev
.write
= picdev_write
;
444 s
->dev
.in_range
= picdev_in_range
;
446 kvm_io_bus_register_dev(&kvm
->pio_bus
, &s
->dev
);