blob: 2c73f4493148d14d781785d279cf270bc2c8ab84 [file] [log] [blame]
Eddie Dong85f455f2007-07-06 12:20:49 +03001/*
2 * 8259 interrupt controller emulation
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2007 Intel Corporation
Avi Kivity221d0592010-05-23 18:37:00 +03006 * Copyright 2009 Red Hat, Inc. and/or its affilates.
Eddie Dong85f455f2007-07-06 12:20:49 +03007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 * Authors:
26 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
27 * Port from Qemu.
28 */
29#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Avi Kivity3f353852008-12-21 22:48:32 +020031#include <linux/bitops.h>
Eddie Dong85f455f2007-07-06 12:20:49 +030032#include "irq.h"
Avi Kivityedf88412007-12-16 11:02:48 +020033
34#include <linux/kvm_host.h>
Gleb Natapov1000ff82009-07-07 16:00:57 +030035#include "trace.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030036
Jan Kiszka50a085b2010-02-24 10:41:58 +010037static void pic_lock(struct kvm_pic *s)
38 __acquires(&s->lock)
39{
40 raw_spin_lock(&s->lock);
41}
42
43static void pic_unlock(struct kvm_pic *s)
44 __releases(&s->lock)
45{
46 bool wakeup = s->wakeup_needed;
47 struct kvm_vcpu *vcpu;
48
49 s->wakeup_needed = false;
50
51 raw_spin_unlock(&s->lock);
52
53 if (wakeup) {
54 vcpu = s->kvm->bsp_vcpu;
55 if (vcpu)
56 kvm_vcpu_kick(vcpu);
57 }
58}
59
Avi Kivity7edd0ce2008-07-07 14:45:39 +030060static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
61{
62 s->isr &= ~(1 << irq);
Marcelo Tosattie4825802008-09-24 20:28:34 -030063 s->isr_ack |= (1 << irq);
Gleb Natapov938396a2009-08-04 15:30:28 +030064 if (s != &s->pics_state->pics[0])
65 irq += 8;
Gleb Natapoveba02262009-08-24 11:54:25 +030066 /*
67 * We are dropping lock while calling ack notifiers since ack
68 * notifier callbacks for assigned devices call into PIC recursively.
69 * Other interrupt may be delivered to PIC while lock is dropped but
70 * it should be safe since PIC state is already updated at this stage.
71 */
Jan Kiszka50a085b2010-02-24 10:41:58 +010072 pic_unlock(s->pics_state);
Gleb Natapov938396a2009-08-04 15:30:28 +030073 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +010074 pic_lock(s->pics_state);
Marcelo Tosattie4825802008-09-24 20:28:34 -030075}
76
77void kvm_pic_clear_isr_ack(struct kvm *kvm)
78{
79 struct kvm_pic *s = pic_irqchip(kvm);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000080
Jan Kiszka50a085b2010-02-24 10:41:58 +010081 pic_lock(s);
Marcelo Tosattie4825802008-09-24 20:28:34 -030082 s->pics[0].isr_ack = 0xff;
83 s->pics[1].isr_ack = 0xff;
Jan Kiszka50a085b2010-02-24 10:41:58 +010084 pic_unlock(s);
Avi Kivity7edd0ce2008-07-07 14:45:39 +030085}
86
Eddie Dong85f455f2007-07-06 12:20:49 +030087/*
88 * set irq level. If an edge is detected, then the IRR is set to 1
89 */
Gleb Natapov49256632009-02-04 17:28:14 +020090static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030091{
Gleb Natapov49256632009-02-04 17:28:14 +020092 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030093 mask = 1 << irq;
94 if (s->elcr & mask) /* level triggered */
95 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020096 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030097 s->irr |= mask;
98 s->last_irr |= mask;
99 } else {
100 s->irr &= ~mask;
101 s->last_irr &= ~mask;
102 }
103 else /* edge triggered */
104 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200105 if ((s->last_irr & mask) == 0) {
106 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300107 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200108 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300109 s->last_irr |= mask;
110 } else
111 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200112
113 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300114}
115
116/*
117 * return the highest priority found in mask (highest = smallest
118 * number). Return 8 if no irq
119 */
120static inline int get_priority(struct kvm_kpic_state *s, int mask)
121{
122 int priority;
123 if (mask == 0)
124 return 8;
125 priority = 0;
126 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
127 priority++;
128 return priority;
129}
130
131/*
132 * return the pic wanted interrupt. return -1 if none
133 */
134static int pic_get_irq(struct kvm_kpic_state *s)
135{
136 int mask, cur_priority, priority;
137
138 mask = s->irr & ~s->imr;
139 priority = get_priority(s, mask);
140 if (priority == 8)
141 return -1;
142 /*
143 * compute current priority. If special fully nested mode on the
144 * master, the IRQ coming from the slave is not taken into account
145 * for the priority computation.
146 */
147 mask = s->isr;
148 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
149 mask &= ~(1 << 2);
150 cur_priority = get_priority(s, mask);
151 if (priority < cur_priority)
152 /*
153 * higher priority found: an irq should be generated
154 */
155 return (priority + s->priority_add) & 7;
156 else
157 return -1;
158}
159
160/*
161 * raise irq to CPU if necessary. must be called every time the active
162 * irq may change
163 */
164static void pic_update_irq(struct kvm_pic *s)
165{
166 int irq2, irq;
167
168 irq2 = pic_get_irq(&s->pics[1]);
169 if (irq2 >= 0) {
170 /*
171 * if irq request by slave pic, signal master PIC
172 */
173 pic_set_irq1(&s->pics[0], 2, 1);
174 pic_set_irq1(&s->pics[0], 2, 0);
175 }
176 irq = pic_get_irq(&s->pics[0]);
177 if (irq >= 0)
178 s->irq_request(s->irq_request_opaque, 1);
179 else
180 s->irq_request(s->irq_request_opaque, 0);
181}
182
He, Qing6ceb9d72007-07-26 11:05:18 +0300183void kvm_pic_update_irq(struct kvm_pic *s)
184{
Jan Kiszka50a085b2010-02-24 10:41:58 +0100185 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300186 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100187 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300188}
189
Gleb Natapov49256632009-02-04 17:28:14 +0200190int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300191{
192 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200193 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300194
Jan Kiszka50a085b2010-02-24 10:41:58 +0100195 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300196 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200197 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300198 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300199 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
200 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300201 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100202 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200203
204 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300205}
206
207/*
208 * acknowledge interrupt 'irq'
209 */
210static inline void pic_intack(struct kvm_kpic_state *s, int irq)
211{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300212 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300213 /*
214 * We don't clear a level sensitive interrupt here
215 */
216 if (!(s->elcr & (1 << irq)))
217 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300218
219 if (s->auto_eoi) {
220 if (s->rotate_on_auto_eoi)
221 s->priority_add = (irq + 1) & 7;
222 pic_clear_isr(s, irq);
223 }
224
Eddie Dong85f455f2007-07-06 12:20:49 +0300225}
226
Marcelo Tosattif5244722008-07-26 17:01:00 -0300227int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300228{
229 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300230 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300231
Jan Kiszka50a085b2010-02-24 10:41:58 +0100232 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300233 irq = pic_get_irq(&s->pics[0]);
234 if (irq >= 0) {
235 pic_intack(&s->pics[0], irq);
236 if (irq == 2) {
237 irq2 = pic_get_irq(&s->pics[1]);
238 if (irq2 >= 0)
239 pic_intack(&s->pics[1], irq2);
240 else
241 /*
242 * spurious IRQ on slave controller
243 */
244 irq2 = 7;
245 intno = s->pics[1].irq_base + irq2;
246 irq = irq2 + 8;
247 } else
248 intno = s->pics[0].irq_base + irq;
249 } else {
250 /*
251 * spurious IRQ on host controller
252 */
253 irq = 7;
254 intno = s->pics[0].irq_base + irq;
255 }
256 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100257 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300258
259 return intno;
260}
261
Eddie Dong2fcceae2007-10-10 12:14:25 +0200262void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300263{
Gleb Natapov79c727d2009-08-24 11:54:18 +0300264 int irq;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300265 struct kvm *kvm = s->pics_state->irq_request_opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300266 struct kvm_vcpu *vcpu0 = kvm->bsp_vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300267 u8 irr = s->irr, isr = s->imr;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300268
Eddie Dong85f455f2007-07-06 12:20:49 +0300269 s->last_irr = 0;
270 s->irr = 0;
271 s->imr = 0;
272 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300273 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300274 s->priority_add = 0;
275 s->irq_base = 0;
276 s->read_reg_select = 0;
277 s->poll = 0;
278 s->special_mask = 0;
279 s->init_state = 0;
280 s->auto_eoi = 0;
281 s->rotate_on_auto_eoi = 0;
282 s->special_fully_nested_mode = 0;
283 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300284
285 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
286 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
287 if (irr & (1 << irq) || isr & (1 << irq)) {
288 pic_clear_isr(s, irq);
289 }
290 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300291}
292
293static void pic_ioport_write(void *opaque, u32 addr, u32 val)
294{
295 struct kvm_kpic_state *s = opaque;
296 int priority, cmd, irq;
297
298 addr &= 1;
299 if (addr == 0) {
300 if (val & 0x10) {
Eddie Dong2fcceae2007-10-10 12:14:25 +0200301 kvm_pic_reset(s); /* init */
Eddie Dong85f455f2007-07-06 12:20:49 +0300302 /*
303 * deassert a pending interrupt
304 */
305 s->pics_state->irq_request(s->pics_state->
306 irq_request_opaque, 0);
307 s->init_state = 1;
308 s->init4 = val & 1;
309 if (val & 0x02)
310 printk(KERN_ERR "single mode not supported");
311 if (val & 0x08)
312 printk(KERN_ERR
313 "level sensitive irq not supported");
314 } else if (val & 0x08) {
315 if (val & 0x04)
316 s->poll = 1;
317 if (val & 0x02)
318 s->read_reg_select = val & 1;
319 if (val & 0x40)
320 s->special_mask = (val >> 5) & 1;
321 } else {
322 cmd = val >> 5;
323 switch (cmd) {
324 case 0:
325 case 4:
326 s->rotate_on_auto_eoi = cmd >> 2;
327 break;
328 case 1: /* end of interrupt */
329 case 5:
330 priority = get_priority(s, s->isr);
331 if (priority != 8) {
332 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300333 if (cmd == 5)
334 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300335 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300336 pic_update_irq(s->pics_state);
337 }
338 break;
339 case 3:
340 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300341 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300342 pic_update_irq(s->pics_state);
343 break;
344 case 6:
345 s->priority_add = (val + 1) & 7;
346 pic_update_irq(s->pics_state);
347 break;
348 case 7:
349 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300350 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300351 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300352 pic_update_irq(s->pics_state);
353 break;
354 default:
355 break; /* no operation */
356 }
357 }
358 } else
359 switch (s->init_state) {
360 case 0: /* normal mode */
361 s->imr = val;
362 pic_update_irq(s->pics_state);
363 break;
364 case 1:
365 s->irq_base = val & 0xf8;
366 s->init_state = 2;
367 break;
368 case 2:
369 if (s->init4)
370 s->init_state = 3;
371 else
372 s->init_state = 0;
373 break;
374 case 3:
375 s->special_fully_nested_mode = (val >> 4) & 1;
376 s->auto_eoi = (val >> 1) & 1;
377 s->init_state = 0;
378 break;
379 }
380}
381
382static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
383{
384 int ret;
385
386 ret = pic_get_irq(s);
387 if (ret >= 0) {
388 if (addr1 >> 7) {
389 s->pics_state->pics[0].isr &= ~(1 << 2);
390 s->pics_state->pics[0].irr &= ~(1 << 2);
391 }
392 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300393 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300394 if (addr1 >> 7 || ret != 2)
395 pic_update_irq(s->pics_state);
396 } else {
397 ret = 0x07;
398 pic_update_irq(s->pics_state);
399 }
400
401 return ret;
402}
403
404static u32 pic_ioport_read(void *opaque, u32 addr1)
405{
406 struct kvm_kpic_state *s = opaque;
407 unsigned int addr;
408 int ret;
409
410 addr = addr1;
411 addr &= 1;
412 if (s->poll) {
413 ret = pic_poll_read(s, addr1);
414 s->poll = 0;
415 } else
416 if (addr == 0)
417 if (s->read_reg_select)
418 ret = s->isr;
419 else
420 ret = s->irr;
421 else
422 ret = s->imr;
423 return ret;
424}
425
426static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
427{
428 struct kvm_kpic_state *s = opaque;
429 s->elcr = val & s->elcr_mask;
430}
431
432static u32 elcr_ioport_read(void *opaque, u32 addr1)
433{
434 struct kvm_kpic_state *s = opaque;
435 return s->elcr;
436}
437
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300438static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300439{
440 switch (addr) {
441 case 0x20:
442 case 0x21:
443 case 0xa0:
444 case 0xa1:
445 case 0x4d0:
446 case 0x4d1:
447 return 1;
448 default:
449 return 0;
450 }
451}
452
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400453static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
454{
455 return container_of(dev, struct kvm_pic, dev);
456}
457
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300458static int picdev_write(struct kvm_io_device *this,
Eddie Dong85f455f2007-07-06 12:20:49 +0300459 gpa_t addr, int len, const void *val)
460{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400461 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300462 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300463 if (!picdev_in_range(addr))
464 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300465
466 if (len != 1) {
467 if (printk_ratelimit())
468 printk(KERN_ERR "PIC: non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300469 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300470 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100471 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300472 switch (addr) {
473 case 0x20:
474 case 0x21:
475 case 0xa0:
476 case 0xa1:
477 pic_ioport_write(&s->pics[addr >> 7], addr, data);
478 break;
479 case 0x4d0:
480 case 0x4d1:
481 elcr_ioport_write(&s->pics[addr & 1], addr, data);
482 break;
483 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100484 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300485 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300486}
487
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300488static int picdev_read(struct kvm_io_device *this,
489 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300490{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400491 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300492 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300493 if (!picdev_in_range(addr))
494 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300495
496 if (len != 1) {
497 if (printk_ratelimit())
498 printk(KERN_ERR "PIC: non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300499 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300500 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100501 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300502 switch (addr) {
503 case 0x20:
504 case 0x21:
505 case 0xa0:
506 case 0xa1:
507 data = pic_ioport_read(&s->pics[addr >> 7], addr);
508 break;
509 case 0x4d0:
510 case 0x4d1:
511 data = elcr_ioport_read(&s->pics[addr & 1], addr);
512 break;
513 }
514 *(unsigned char *)val = data;
Jan Kiszka50a085b2010-02-24 10:41:58 +0100515 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300516 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300517}
518
519/*
520 * callback when PIC0 irq status changed
521 */
522static void pic_irq_request(void *opaque, int level)
523{
524 struct kvm *kvm = opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300525 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300526 struct kvm_pic *s = pic_irqchip(kvm);
527 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300528
Marcelo Tosattie4825802008-09-24 20:28:34 -0300529 s->output = level;
530 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
531 s->pics[0].isr_ack &= ~(1 << irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100532 s->wakeup_needed = true;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300533 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300534}
535
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400536static const struct kvm_io_device_ops picdev_ops = {
537 .read = picdev_read,
538 .write = picdev_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400539};
540
Eddie Dong85f455f2007-07-06 12:20:49 +0300541struct kvm_pic *kvm_create_pic(struct kvm *kvm)
542{
543 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400544 int ret;
545
Eddie Dong85f455f2007-07-06 12:20:49 +0300546 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
547 if (!s)
548 return NULL;
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000549 raw_spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200550 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300551 s->pics[0].elcr_mask = 0xf8;
552 s->pics[1].elcr_mask = 0xde;
553 s->irq_request = pic_irq_request;
554 s->irq_request_opaque = kvm;
555 s->pics[0].pics_state = s;
556 s->pics[1].pics_state = s;
557
558 /*
559 * Initialize PIO device
560 */
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400561 kvm_iodevice_init(&s->dev, &picdev_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200562 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200563 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200564 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400565 if (ret < 0) {
566 kfree(s);
567 return NULL;
568 }
569
Eddie Dong85f455f2007-07-06 12:20:49 +0300570 return s;
571}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800572
573void kvm_destroy_pic(struct kvm *kvm)
574{
575 struct kvm_pic *vpic = kvm->arch.vpic;
576
577 if (vpic) {
578 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev);
579 kvm->arch.vpic = NULL;
580 kfree(vpic);
581 }
582}