30 #include <linux/slab.h>
31 #include <linux/bitops.h>
37 #define pr_pic_unimpl(fmt, ...) \
38 pr_err_ratelimited("kvm: pic: " fmt, ## __VA_ARGS__)
40 static void pic_irq_request(
struct kvm *
kvm,
int level);
42 static void pic_lock(
struct kvm_pic *
s)
48 static void pic_unlock(
struct kvm_pic *
s)
51 bool wakeup =
s->wakeup_needed;
55 s->wakeup_needed =
false;
57 spin_unlock(&
s->lock);
77 s->
isr &= ~(1 << irq);
117 return (s->
imr & mask) ? -1 :
ret;
124 static inline int get_priority(
struct kvm_kpic_state *s,
int mask)
130 while ((mask & (1 << ((priority + s->
priority_add) & 7))) == 0)
143 priority = get_priority(s, mask);
154 cur_priority = get_priority(s, mask);
155 if (priority < cur_priority)
168 static void pic_update_irq(
struct kvm_pic *s)
172 irq2 = pic_get_irq(&s->
pics[1]);
177 pic_set_irq1(&s->
pics[0], 2, 1);
178 pic_set_irq1(&s->
pics[0], 2, 0);
180 irq = pic_get_irq(&s->
pics[0]);
181 pic_irq_request(s->
kvm, irq >= 0);
198 irq_level = __kvm_irq_line_state(&s->
irq_states[irq],
199 irq_source_id, level);
200 ret = pic_set_irq1(&s->
pics[irq >> 3], irq & 7, irq_level);
202 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->
pics[irq >> 3].elcr,
203 s->
pics[irq >> 3].imr, ret == 0);
228 if (!(s->
elcr & (1 << irq)))
229 s->
irr &= ~(1 << irq);
234 pic_clear_isr(s, irq);
241 int irq, irq2, intno;
242 struct kvm_pic *s = pic_irqchip(kvm);
245 irq = pic_get_irq(&s->
pics[0]);
247 pic_intack(&s->
pics[0], irq);
249 irq2 = pic_get_irq(&s->
pics[1]);
251 pic_intack(&s->
pics[1], irq2);
257 intno = s->
pics[1].irq_base + irq2;
260 intno = s->
pics[0].irq_base + irq;
266 intno = s->
pics[0].irq_base + irq;
304 if (edge_irr & (1 << irq))
305 pic_clear_isr(s, irq);
308 static void pic_ioport_write(
void *opaque,
u32 addr,
u32 val)
321 "level sensitive irq not supported");
323 }
else if (val & 0x08) {
339 priority = get_priority(s, s->
isr);
344 pic_clear_isr(s, irq);
350 pic_clear_isr(s, irq);
360 pic_clear_isr(s, irq);
374 if (imr_diff & (1 << irq))
379 !!(s->
imr & (1 << irq)));
405 ret = pic_get_irq(s);
412 pic_clear_isr(s, ret);
413 if (addr1 >> 7 || ret != 2)
423 static u32 pic_ioport_read(
void *opaque,
u32 addr1)
432 ret = pic_poll_read(s, addr1);
445 static void elcr_ioport_write(
void *opaque,
u32 addr,
u32 val)
451 static u32 elcr_ioport_read(
void *opaque,
u32 addr1)
457 static int picdev_in_range(
gpa_t addr)
472 static int picdev_write(
struct kvm_pic *s,
473 gpa_t addr,
int len,
const void *val)
475 unsigned char data = *(
unsigned char *)val;
476 if (!picdev_in_range(addr))
489 pic_ioport_write(&s->
pics[addr >> 7], addr, data);
493 elcr_ioport_write(&s->
pics[addr & 1], addr, data);
500 static int picdev_read(
struct kvm_pic *s,
501 gpa_t addr,
int len,
void *val)
503 unsigned char data = 0;
504 if (!picdev_in_range(addr))
517 data = pic_ioport_read(&s->
pics[addr >> 7], addr);
521 data = elcr_ioport_read(&s->
pics[addr & 1], addr);
524 *(
unsigned char *)val = data;
530 gpa_t addr,
int len,
const void *val)
537 gpa_t addr,
int len,
void *val)
544 gpa_t addr,
int len,
const void *val)
551 gpa_t addr,
int len,
void *val)
558 gpa_t addr,
int len,
const void *val)
565 gpa_t addr,
int len,
void *val)
574 static void pic_irq_request(
struct kvm *
kvm,
int level)
576 struct kvm_pic *s = pic_irqchip(kvm);
584 .read = picdev_master_read,
585 .write = picdev_master_write,
589 .read = picdev_slave_read,
590 .write = picdev_slave_write,
594 .read = picdev_eclr_read,
595 .write = picdev_eclr_write,
608 s->
pics[0].elcr_mask = 0xf8;
609 s->
pics[1].elcr_mask = 0xde;
610 s->
pics[0].pics_state =
s;
611 s->
pics[1].pics_state =
s;
616 kvm_iodevice_init(&s->
dev_master, &picdev_master_ops);
617 kvm_iodevice_init(&s->
dev_slave, &picdev_slave_ops);
618 kvm_iodevice_init(&s->
dev_eclr, &picdev_eclr_ops);