blob: caf6e1b3d9578ca5776c171a6b12cab0d9d2dab0 [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
Avi Kivity073d4612010-05-03 17:34:34 +030037static void pic_irq_request(struct kvm *kvm, int level);
38
Jan Kiszka50a085b2010-02-24 10:41:58 +010039static void pic_lock(struct kvm_pic *s)
40 __acquires(&s->lock)
41{
42 raw_spin_lock(&s->lock);
43}
44
45static void pic_unlock(struct kvm_pic *s)
46 __releases(&s->lock)
47{
48 bool wakeup = s->wakeup_needed;
49 struct kvm_vcpu *vcpu;
50
51 s->wakeup_needed = false;
52
53 raw_spin_unlock(&s->lock);
54
55 if (wakeup) {
56 vcpu = s->kvm->bsp_vcpu;
57 if (vcpu)
58 kvm_vcpu_kick(vcpu);
59 }
60}
61
Avi Kivity7edd0ce2008-07-07 14:45:39 +030062static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
63{
64 s->isr &= ~(1 << irq);
Marcelo Tosattie4825802008-09-24 20:28:34 -030065 s->isr_ack |= (1 << irq);
Gleb Natapov938396a2009-08-04 15:30:28 +030066 if (s != &s->pics_state->pics[0])
67 irq += 8;
Gleb Natapoveba02262009-08-24 11:54:25 +030068 /*
69 * We are dropping lock while calling ack notifiers since ack
70 * notifier callbacks for assigned devices call into PIC recursively.
71 * Other interrupt may be delivered to PIC while lock is dropped but
72 * it should be safe since PIC state is already updated at this stage.
73 */
Jan Kiszka50a085b2010-02-24 10:41:58 +010074 pic_unlock(s->pics_state);
Gleb Natapov938396a2009-08-04 15:30:28 +030075 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +010076 pic_lock(s->pics_state);
Marcelo Tosattie4825802008-09-24 20:28:34 -030077}
78
79void kvm_pic_clear_isr_ack(struct kvm *kvm)
80{
81 struct kvm_pic *s = pic_irqchip(kvm);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000082
Jan Kiszka50a085b2010-02-24 10:41:58 +010083 pic_lock(s);
Marcelo Tosattie4825802008-09-24 20:28:34 -030084 s->pics[0].isr_ack = 0xff;
85 s->pics[1].isr_ack = 0xff;
Jan Kiszka50a085b2010-02-24 10:41:58 +010086 pic_unlock(s);
Avi Kivity7edd0ce2008-07-07 14:45:39 +030087}
88
Eddie Dong85f455f2007-07-06 12:20:49 +030089/*
90 * set irq level. If an edge is detected, then the IRR is set to 1
91 */
Gleb Natapov49256632009-02-04 17:28:14 +020092static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030093{
Gleb Natapov49256632009-02-04 17:28:14 +020094 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030095 mask = 1 << irq;
96 if (s->elcr & mask) /* level triggered */
97 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020098 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030099 s->irr |= mask;
100 s->last_irr |= mask;
101 } else {
102 s->irr &= ~mask;
103 s->last_irr &= ~mask;
104 }
105 else /* edge triggered */
106 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200107 if ((s->last_irr & mask) == 0) {
108 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300109 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200110 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300111 s->last_irr |= mask;
112 } else
113 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200114
115 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300116}
117
118/*
119 * return the highest priority found in mask (highest = smallest
120 * number). Return 8 if no irq
121 */
122static inline int get_priority(struct kvm_kpic_state *s, int mask)
123{
124 int priority;
125 if (mask == 0)
126 return 8;
127 priority = 0;
128 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
129 priority++;
130 return priority;
131}
132
133/*
134 * return the pic wanted interrupt. return -1 if none
135 */
136static int pic_get_irq(struct kvm_kpic_state *s)
137{
138 int mask, cur_priority, priority;
139
140 mask = s->irr & ~s->imr;
141 priority = get_priority(s, mask);
142 if (priority == 8)
143 return -1;
144 /*
145 * compute current priority. If special fully nested mode on the
146 * master, the IRQ coming from the slave is not taken into account
147 * for the priority computation.
148 */
149 mask = s->isr;
150 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
151 mask &= ~(1 << 2);
152 cur_priority = get_priority(s, mask);
153 if (priority < cur_priority)
154 /*
155 * higher priority found: an irq should be generated
156 */
157 return (priority + s->priority_add) & 7;
158 else
159 return -1;
160}
161
162/*
163 * raise irq to CPU if necessary. must be called every time the active
164 * irq may change
165 */
166static void pic_update_irq(struct kvm_pic *s)
167{
168 int irq2, irq;
169
170 irq2 = pic_get_irq(&s->pics[1]);
171 if (irq2 >= 0) {
172 /*
173 * if irq request by slave pic, signal master PIC
174 */
175 pic_set_irq1(&s->pics[0], 2, 1);
176 pic_set_irq1(&s->pics[0], 2, 0);
177 }
178 irq = pic_get_irq(&s->pics[0]);
179 if (irq >= 0)
Avi Kivity073d4612010-05-03 17:34:34 +0300180 pic_irq_request(s->kvm, 1);
Eddie Dong85f455f2007-07-06 12:20:49 +0300181 else
Avi Kivity073d4612010-05-03 17:34:34 +0300182 pic_irq_request(s->kvm, 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300183}
184
He, Qing6ceb9d72007-07-26 11:05:18 +0300185void kvm_pic_update_irq(struct kvm_pic *s)
186{
Jan Kiszka50a085b2010-02-24 10:41:58 +0100187 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300188 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100189 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300190}
191
Gleb Natapov49256632009-02-04 17:28:14 +0200192int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300193{
194 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200195 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300196
Jan Kiszka50a085b2010-02-24 10:41:58 +0100197 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300198 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200199 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300200 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300201 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
202 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300203 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100204 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200205
206 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300207}
208
209/*
210 * acknowledge interrupt 'irq'
211 */
212static inline void pic_intack(struct kvm_kpic_state *s, int irq)
213{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300214 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300215 /*
216 * We don't clear a level sensitive interrupt here
217 */
218 if (!(s->elcr & (1 << irq)))
219 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300220
221 if (s->auto_eoi) {
222 if (s->rotate_on_auto_eoi)
223 s->priority_add = (irq + 1) & 7;
224 pic_clear_isr(s, irq);
225 }
226
Eddie Dong85f455f2007-07-06 12:20:49 +0300227}
228
Marcelo Tosattif5244722008-07-26 17:01:00 -0300229int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300230{
231 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300232 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300233
Jan Kiszka50a085b2010-02-24 10:41:58 +0100234 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300235 irq = pic_get_irq(&s->pics[0]);
236 if (irq >= 0) {
237 pic_intack(&s->pics[0], irq);
238 if (irq == 2) {
239 irq2 = pic_get_irq(&s->pics[1]);
240 if (irq2 >= 0)
241 pic_intack(&s->pics[1], irq2);
242 else
243 /*
244 * spurious IRQ on slave controller
245 */
246 irq2 = 7;
247 intno = s->pics[1].irq_base + irq2;
248 irq = irq2 + 8;
249 } else
250 intno = s->pics[0].irq_base + irq;
251 } else {
252 /*
253 * spurious IRQ on host controller
254 */
255 irq = 7;
256 intno = s->pics[0].irq_base + irq;
257 }
258 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100259 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300260
261 return intno;
262}
263
Eddie Dong2fcceae2007-10-10 12:14:25 +0200264void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300265{
Gleb Natapov79c727d2009-08-24 11:54:18 +0300266 int irq;
Avi Kivity073d4612010-05-03 17:34:34 +0300267 struct kvm_vcpu *vcpu0 = s->pics_state->kvm->bsp_vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300268 u8 irr = s->irr, isr = s->imr;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300269
Eddie Dong85f455f2007-07-06 12:20:49 +0300270 s->last_irr = 0;
271 s->irr = 0;
272 s->imr = 0;
273 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300274 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300275 s->priority_add = 0;
276 s->irq_base = 0;
277 s->read_reg_select = 0;
278 s->poll = 0;
279 s->special_mask = 0;
280 s->init_state = 0;
281 s->auto_eoi = 0;
282 s->rotate_on_auto_eoi = 0;
283 s->special_fully_nested_mode = 0;
284 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300285
286 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
287 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
288 if (irr & (1 << irq) || isr & (1 << irq)) {
289 pic_clear_isr(s, irq);
290 }
291 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300292}
293
294static void pic_ioport_write(void *opaque, u32 addr, u32 val)
295{
296 struct kvm_kpic_state *s = opaque;
297 int priority, cmd, irq;
298
299 addr &= 1;
300 if (addr == 0) {
301 if (val & 0x10) {
Eddie Dong2fcceae2007-10-10 12:14:25 +0200302 kvm_pic_reset(s); /* init */
Eddie Dong85f455f2007-07-06 12:20:49 +0300303 /*
304 * deassert a pending interrupt
305 */
Avi Kivity073d4612010-05-03 17:34:34 +0300306 pic_irq_request(s->pics_state->kvm, 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300307 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 */
Avi Kivity073d4612010-05-03 17:34:34 +0300522static void pic_irq_request(struct kvm *kvm, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300523{
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300524 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300525 struct kvm_pic *s = pic_irqchip(kvm);
526 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300527
Marcelo Tosattie4825802008-09-24 20:28:34 -0300528 s->output = level;
529 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
530 s->pics[0].isr_ack &= ~(1 << irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100531 s->wakeup_needed = true;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300532 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300533}
534
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400535static const struct kvm_io_device_ops picdev_ops = {
536 .read = picdev_read,
537 .write = picdev_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400538};
539
Eddie Dong85f455f2007-07-06 12:20:49 +0300540struct kvm_pic *kvm_create_pic(struct kvm *kvm)
541{
542 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400543 int ret;
544
Eddie Dong85f455f2007-07-06 12:20:49 +0300545 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
546 if (!s)
547 return NULL;
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000548 raw_spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200549 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300550 s->pics[0].elcr_mask = 0xf8;
551 s->pics[1].elcr_mask = 0xde;
Eddie Dong85f455f2007-07-06 12:20:49 +0300552 s->pics[0].pics_state = s;
553 s->pics[1].pics_state = s;
554
555 /*
556 * Initialize PIO device
557 */
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400558 kvm_iodevice_init(&s->dev, &picdev_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200559 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200560 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200561 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400562 if (ret < 0) {
563 kfree(s);
564 return NULL;
565 }
566
Eddie Dong85f455f2007-07-06 12:20:49 +0300567 return s;
568}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800569
570void kvm_destroy_pic(struct kvm *kvm)
571{
572 struct kvm_pic *vpic = kvm->arch.vpic;
573
574 if (vpic) {
575 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev);
576 kvm->arch.vpic = NULL;
577 kfree(vpic);
578 }
579}