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>
34 * set irq level. If an edge is detected, then the IRR is set to 1
36 static inline void pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
40 if (s->elcr & mask) /* level triggered */
48 else /* edge triggered */
50 if ((s->last_irr & mask) == 0)
58 * return the highest priority found in mask (highest = smallest
59 * number). Return 8 if no irq
61 static inline int get_priority(struct kvm_kpic_state *s, int mask)
67 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
73 * return the pic wanted interrupt. return -1 if none
75 static int pic_get_irq(struct kvm_kpic_state *s)
77 int mask, cur_priority, priority;
79 mask = s->irr & ~s->imr;
80 priority = get_priority(s, mask);
84 * compute current priority. If special fully nested mode on the
85 * master, the IRQ coming from the slave is not taken into account
86 * for the priority computation.
89 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
91 cur_priority = get_priority(s, mask);
92 if (priority < cur_priority)
94 * higher priority found: an irq should be generated
96 return (priority + s->priority_add) & 7;
102 * raise irq to CPU if necessary. must be called every time the active
105 static void pic_update_irq(struct kvm_pic *s)
109 irq2 = pic_get_irq(&s->pics[1]);
112 * if irq request by slave pic, signal master PIC
114 pic_set_irq1(&s->pics[0], 2, 1);
115 pic_set_irq1(&s->pics[0], 2, 0);
117 irq = pic_get_irq(&s->pics[0]);
119 s->irq_request(s->irq_request_opaque, 1);
121 s->irq_request(s->irq_request_opaque, 0);
124 void kvm_pic_update_irq(struct kvm_pic *s)
129 void kvm_pic_set_irq(void *opaque, int irq, int level)
131 struct kvm_pic *s = opaque;
133 if (irq >= 0 && irq < PIC_NUM_PINS) {
134 pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
140 * acknowledge interrupt 'irq'
142 static inline void pic_intack(struct kvm_kpic_state *s, int irq)
145 if (s->rotate_on_auto_eoi)
146 s->priority_add = (irq + 1) & 7;
148 s->isr |= (1 << irq);
150 * We don't clear a level sensitive interrupt here
152 if (!(s->elcr & (1 << irq)))
153 s->irr &= ~(1 << irq);
156 int kvm_pic_read_irq(struct kvm_pic *s)
158 int irq, irq2, intno;
160 irq = pic_get_irq(&s->pics[0]);
162 pic_intack(&s->pics[0], irq);
164 irq2 = pic_get_irq(&s->pics[1]);
166 pic_intack(&s->pics[1], irq2);
169 * spurious IRQ on slave controller
172 intno = s->pics[1].irq_base + irq2;
175 intno = s->pics[0].irq_base + irq;
178 * spurious IRQ on host controller
181 intno = s->pics[0].irq_base + irq;
188 void kvm_pic_reset(struct kvm_kpic_state *s)
196 s->read_reg_select = 0;
201 s->rotate_on_auto_eoi = 0;
202 s->special_fully_nested_mode = 0;
206 static void pic_ioport_write(void *opaque, u32 addr, u32 val)
208 struct kvm_kpic_state *s = opaque;
209 int priority, cmd, irq;
214 kvm_pic_reset(s); /* init */
216 * deassert a pending interrupt
218 s->pics_state->irq_request(s->pics_state->
219 irq_request_opaque, 0);
223 printk(KERN_ERR "single mode not supported");
226 "level sensitive irq not supported");
227 } else if (val & 0x08) {
231 s->read_reg_select = val & 1;
233 s->special_mask = (val >> 5) & 1;
239 s->rotate_on_auto_eoi = cmd >> 2;
241 case 1: /* end of interrupt */
243 priority = get_priority(s, s->isr);
245 irq = (priority + s->priority_add) & 7;
246 s->isr &= ~(1 << irq);
248 s->priority_add = (irq + 1) & 7;
249 pic_update_irq(s->pics_state);
254 s->isr &= ~(1 << irq);
255 pic_update_irq(s->pics_state);
258 s->priority_add = (val + 1) & 7;
259 pic_update_irq(s->pics_state);
263 s->isr &= ~(1 << irq);
264 s->priority_add = (irq + 1) & 7;
265 pic_update_irq(s->pics_state);
268 break; /* no operation */
272 switch (s->init_state) {
273 case 0: /* normal mode */
275 pic_update_irq(s->pics_state);
278 s->irq_base = val & 0xf8;
288 s->special_fully_nested_mode = (val >> 4) & 1;
289 s->auto_eoi = (val >> 1) & 1;
295 static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
299 ret = pic_get_irq(s);
302 s->pics_state->pics[0].isr &= ~(1 << 2);
303 s->pics_state->pics[0].irr &= ~(1 << 2);
305 s->irr &= ~(1 << ret);
306 s->isr &= ~(1 << ret);
307 if (addr1 >> 7 || ret != 2)
308 pic_update_irq(s->pics_state);
311 pic_update_irq(s->pics_state);
317 static u32 pic_ioport_read(void *opaque, u32 addr1)
319 struct kvm_kpic_state *s = opaque;
326 ret = pic_poll_read(s, addr1);
330 if (s->read_reg_select)
339 static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
341 struct kvm_kpic_state *s = opaque;
342 s->elcr = val & s->elcr_mask;
345 static u32 elcr_ioport_read(void *opaque, u32 addr1)
347 struct kvm_kpic_state *s = opaque;
351 static int picdev_in_range(struct kvm_io_device *this, gpa_t addr,
352 int len, int is_write)
367 static void picdev_write(struct kvm_io_device *this,
368 gpa_t addr, int len, const void *val)
370 struct kvm_pic *s = this->private;
371 unsigned char data = *(unsigned char *)val;
374 if (printk_ratelimit())
375 printk(KERN_ERR "PIC: non byte write\n");
383 pic_ioport_write(&s->pics[addr >> 7], addr, data);
387 elcr_ioport_write(&s->pics[addr & 1], addr, data);
392 static void picdev_read(struct kvm_io_device *this,
393 gpa_t addr, int len, void *val)
395 struct kvm_pic *s = this->private;
396 unsigned char data = 0;
399 if (printk_ratelimit())
400 printk(KERN_ERR "PIC: non byte read\n");
408 data = pic_ioport_read(&s->pics[addr >> 7], addr);
412 data = elcr_ioport_read(&s->pics[addr & 1], addr);
415 *(unsigned char *)val = data;
419 * callback when PIC0 irq status changed
421 static void pic_irq_request(void *opaque, int level)
423 struct kvm *kvm = opaque;
424 struct kvm_vcpu *vcpu = kvm->vcpus[0];
426 pic_irqchip(kvm)->output = level;
431 struct kvm_pic *kvm_create_pic(struct kvm *kvm)
434 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
437 s->pics[0].elcr_mask = 0xf8;
438 s->pics[1].elcr_mask = 0xde;
439 s->irq_request = pic_irq_request;
440 s->irq_request_opaque = kvm;
441 s->pics[0].pics_state = s;
442 s->pics[1].pics_state = s;
445 * Initialize PIO device
447 s->dev.read = picdev_read;
448 s->dev.write = picdev_write;
449 s->dev.in_range = picdev_in_range;
451 kvm_io_bus_register_dev(&kvm->pio_bus, &s->dev);