blob: 819b748a33f9ff84c7660425cb1d3b28bf6cd404 [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;
Chris Lalancette529df652010-06-21 11:29:40 -040049 struct kvm_vcpu *vcpu, *found = NULL;
50 int i;
Jan Kiszka50a085b2010-02-24 10:41:58 +010051
52 s->wakeup_needed = false;
53
54 raw_spin_unlock(&s->lock);
55
56 if (wakeup) {
Chris Lalancette529df652010-06-21 11:29:40 -040057 kvm_for_each_vcpu(i, vcpu, s->kvm) {
58 if (kvm_apic_accept_pic_intr(vcpu)) {
59 found = vcpu;
60 break;
61 }
62 }
63
64 if (!found)
65 found = s->kvm->bsp_vcpu;
66
67 kvm_vcpu_kick(found);
Jan Kiszka50a085b2010-02-24 10:41:58 +010068 }
69}
70
Avi Kivity7edd0ce2008-07-07 14:45:39 +030071static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
72{
73 s->isr &= ~(1 << irq);
Marcelo Tosattie4825802008-09-24 20:28:34 -030074 s->isr_ack |= (1 << irq);
Gleb Natapov938396a2009-08-04 15:30:28 +030075 if (s != &s->pics_state->pics[0])
76 irq += 8;
Gleb Natapoveba02262009-08-24 11:54:25 +030077 /*
78 * We are dropping lock while calling ack notifiers since ack
79 * notifier callbacks for assigned devices call into PIC recursively.
80 * Other interrupt may be delivered to PIC while lock is dropped but
81 * it should be safe since PIC state is already updated at this stage.
82 */
Jan Kiszka50a085b2010-02-24 10:41:58 +010083 pic_unlock(s->pics_state);
Gleb Natapov938396a2009-08-04 15:30:28 +030084 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +010085 pic_lock(s->pics_state);
Marcelo Tosattie4825802008-09-24 20:28:34 -030086}
87
88void kvm_pic_clear_isr_ack(struct kvm *kvm)
89{
90 struct kvm_pic *s = pic_irqchip(kvm);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000091
Jan Kiszka50a085b2010-02-24 10:41:58 +010092 pic_lock(s);
Marcelo Tosattie4825802008-09-24 20:28:34 -030093 s->pics[0].isr_ack = 0xff;
94 s->pics[1].isr_ack = 0xff;
Jan Kiszka50a085b2010-02-24 10:41:58 +010095 pic_unlock(s);
Avi Kivity7edd0ce2008-07-07 14:45:39 +030096}
97
Eddie Dong85f455f2007-07-06 12:20:49 +030098/*
99 * set irq level. If an edge is detected, then the IRR is set to 1
100 */
Gleb Natapov49256632009-02-04 17:28:14 +0200101static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300102{
Gleb Natapov49256632009-02-04 17:28:14 +0200103 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300104 mask = 1 << irq;
105 if (s->elcr & mask) /* level triggered */
106 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200107 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300108 s->irr |= mask;
109 s->last_irr |= mask;
110 } else {
111 s->irr &= ~mask;
112 s->last_irr &= ~mask;
113 }
114 else /* edge triggered */
115 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200116 if ((s->last_irr & mask) == 0) {
117 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300118 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200119 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300120 s->last_irr |= mask;
121 } else
122 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200123
124 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300125}
126
127/*
128 * return the highest priority found in mask (highest = smallest
129 * number). Return 8 if no irq
130 */
131static inline int get_priority(struct kvm_kpic_state *s, int mask)
132{
133 int priority;
134 if (mask == 0)
135 return 8;
136 priority = 0;
137 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
138 priority++;
139 return priority;
140}
141
142/*
143 * return the pic wanted interrupt. return -1 if none
144 */
145static int pic_get_irq(struct kvm_kpic_state *s)
146{
147 int mask, cur_priority, priority;
148
149 mask = s->irr & ~s->imr;
150 priority = get_priority(s, mask);
151 if (priority == 8)
152 return -1;
153 /*
154 * compute current priority. If special fully nested mode on the
155 * master, the IRQ coming from the slave is not taken into account
156 * for the priority computation.
157 */
158 mask = s->isr;
159 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
160 mask &= ~(1 << 2);
161 cur_priority = get_priority(s, mask);
162 if (priority < cur_priority)
163 /*
164 * higher priority found: an irq should be generated
165 */
166 return (priority + s->priority_add) & 7;
167 else
168 return -1;
169}
170
171/*
172 * raise irq to CPU if necessary. must be called every time the active
173 * irq may change
174 */
175static void pic_update_irq(struct kvm_pic *s)
176{
177 int irq2, irq;
178
179 irq2 = pic_get_irq(&s->pics[1]);
180 if (irq2 >= 0) {
181 /*
182 * if irq request by slave pic, signal master PIC
183 */
184 pic_set_irq1(&s->pics[0], 2, 1);
185 pic_set_irq1(&s->pics[0], 2, 0);
186 }
187 irq = pic_get_irq(&s->pics[0]);
Avi Kivity36633f32010-05-03 17:38:06 +0300188 pic_irq_request(s->kvm, irq >= 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300189}
190
He, Qing6ceb9d72007-07-26 11:05:18 +0300191void kvm_pic_update_irq(struct kvm_pic *s)
192{
Jan Kiszka50a085b2010-02-24 10:41:58 +0100193 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300194 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100195 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300196}
197
Gleb Natapov49256632009-02-04 17:28:14 +0200198int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300199{
200 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200201 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300202
Jan Kiszka50a085b2010-02-24 10:41:58 +0100203 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300204 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200205 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300206 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300207 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
208 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300209 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100210 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200211
212 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300213}
214
215/*
216 * acknowledge interrupt 'irq'
217 */
218static inline void pic_intack(struct kvm_kpic_state *s, int irq)
219{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300220 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300221 /*
222 * We don't clear a level sensitive interrupt here
223 */
224 if (!(s->elcr & (1 << irq)))
225 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300226
227 if (s->auto_eoi) {
228 if (s->rotate_on_auto_eoi)
229 s->priority_add = (irq + 1) & 7;
230 pic_clear_isr(s, irq);
231 }
232
Eddie Dong85f455f2007-07-06 12:20:49 +0300233}
234
Marcelo Tosattif5244722008-07-26 17:01:00 -0300235int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300236{
237 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300238 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300239
Jan Kiszka50a085b2010-02-24 10:41:58 +0100240 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300241 irq = pic_get_irq(&s->pics[0]);
242 if (irq >= 0) {
243 pic_intack(&s->pics[0], irq);
244 if (irq == 2) {
245 irq2 = pic_get_irq(&s->pics[1]);
246 if (irq2 >= 0)
247 pic_intack(&s->pics[1], irq2);
248 else
249 /*
250 * spurious IRQ on slave controller
251 */
252 irq2 = 7;
253 intno = s->pics[1].irq_base + irq2;
254 irq = irq2 + 8;
255 } else
256 intno = s->pics[0].irq_base + irq;
257 } else {
258 /*
259 * spurious IRQ on host controller
260 */
261 irq = 7;
262 intno = s->pics[0].irq_base + irq;
263 }
264 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100265 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300266
267 return intno;
268}
269
Eddie Dong2fcceae2007-10-10 12:14:25 +0200270void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300271{
Gleb Natapov79c727d2009-08-24 11:54:18 +0300272 int irq;
Avi Kivity073d4612010-05-03 17:34:34 +0300273 struct kvm_vcpu *vcpu0 = s->pics_state->kvm->bsp_vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300274 u8 irr = s->irr, isr = s->imr;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300275
Eddie Dong85f455f2007-07-06 12:20:49 +0300276 s->last_irr = 0;
277 s->irr = 0;
278 s->imr = 0;
279 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300280 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300281 s->priority_add = 0;
282 s->irq_base = 0;
283 s->read_reg_select = 0;
284 s->poll = 0;
285 s->special_mask = 0;
286 s->init_state = 0;
287 s->auto_eoi = 0;
288 s->rotate_on_auto_eoi = 0;
289 s->special_fully_nested_mode = 0;
290 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300291
292 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
293 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
294 if (irr & (1 << irq) || isr & (1 << irq)) {
295 pic_clear_isr(s, irq);
296 }
297 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300298}
299
300static void pic_ioport_write(void *opaque, u32 addr, u32 val)
301{
302 struct kvm_kpic_state *s = opaque;
303 int priority, cmd, irq;
304
305 addr &= 1;
306 if (addr == 0) {
307 if (val & 0x10) {
Eddie Dong2fcceae2007-10-10 12:14:25 +0200308 kvm_pic_reset(s); /* init */
Eddie Dong85f455f2007-07-06 12:20:49 +0300309 /*
310 * deassert a pending interrupt
311 */
Avi Kivity073d4612010-05-03 17:34:34 +0300312 pic_irq_request(s->pics_state->kvm, 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300313 s->init_state = 1;
314 s->init4 = val & 1;
315 if (val & 0x02)
316 printk(KERN_ERR "single mode not supported");
317 if (val & 0x08)
318 printk(KERN_ERR
319 "level sensitive irq not supported");
320 } else if (val & 0x08) {
321 if (val & 0x04)
322 s->poll = 1;
323 if (val & 0x02)
324 s->read_reg_select = val & 1;
325 if (val & 0x40)
326 s->special_mask = (val >> 5) & 1;
327 } else {
328 cmd = val >> 5;
329 switch (cmd) {
330 case 0:
331 case 4:
332 s->rotate_on_auto_eoi = cmd >> 2;
333 break;
334 case 1: /* end of interrupt */
335 case 5:
336 priority = get_priority(s, s->isr);
337 if (priority != 8) {
338 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300339 if (cmd == 5)
340 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300341 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300342 pic_update_irq(s->pics_state);
343 }
344 break;
345 case 3:
346 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300347 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300348 pic_update_irq(s->pics_state);
349 break;
350 case 6:
351 s->priority_add = (val + 1) & 7;
352 pic_update_irq(s->pics_state);
353 break;
354 case 7:
355 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300356 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300357 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300358 pic_update_irq(s->pics_state);
359 break;
360 default:
361 break; /* no operation */
362 }
363 }
364 } else
365 switch (s->init_state) {
366 case 0: /* normal mode */
367 s->imr = val;
368 pic_update_irq(s->pics_state);
369 break;
370 case 1:
371 s->irq_base = val & 0xf8;
372 s->init_state = 2;
373 break;
374 case 2:
375 if (s->init4)
376 s->init_state = 3;
377 else
378 s->init_state = 0;
379 break;
380 case 3:
381 s->special_fully_nested_mode = (val >> 4) & 1;
382 s->auto_eoi = (val >> 1) & 1;
383 s->init_state = 0;
384 break;
385 }
386}
387
388static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
389{
390 int ret;
391
392 ret = pic_get_irq(s);
393 if (ret >= 0) {
394 if (addr1 >> 7) {
395 s->pics_state->pics[0].isr &= ~(1 << 2);
396 s->pics_state->pics[0].irr &= ~(1 << 2);
397 }
398 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300399 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300400 if (addr1 >> 7 || ret != 2)
401 pic_update_irq(s->pics_state);
402 } else {
403 ret = 0x07;
404 pic_update_irq(s->pics_state);
405 }
406
407 return ret;
408}
409
410static u32 pic_ioport_read(void *opaque, u32 addr1)
411{
412 struct kvm_kpic_state *s = opaque;
413 unsigned int addr;
414 int ret;
415
416 addr = addr1;
417 addr &= 1;
418 if (s->poll) {
419 ret = pic_poll_read(s, addr1);
420 s->poll = 0;
421 } else
422 if (addr == 0)
423 if (s->read_reg_select)
424 ret = s->isr;
425 else
426 ret = s->irr;
427 else
428 ret = s->imr;
429 return ret;
430}
431
432static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
433{
434 struct kvm_kpic_state *s = opaque;
435 s->elcr = val & s->elcr_mask;
436}
437
438static u32 elcr_ioport_read(void *opaque, u32 addr1)
439{
440 struct kvm_kpic_state *s = opaque;
441 return s->elcr;
442}
443
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300444static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300445{
446 switch (addr) {
447 case 0x20:
448 case 0x21:
449 case 0xa0:
450 case 0xa1:
451 case 0x4d0:
452 case 0x4d1:
453 return 1;
454 default:
455 return 0;
456 }
457}
458
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400459static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
460{
461 return container_of(dev, struct kvm_pic, dev);
462}
463
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300464static int picdev_write(struct kvm_io_device *this,
Eddie Dong85f455f2007-07-06 12:20:49 +0300465 gpa_t addr, int len, const void *val)
466{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400467 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300468 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300469 if (!picdev_in_range(addr))
470 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300471
472 if (len != 1) {
473 if (printk_ratelimit())
474 printk(KERN_ERR "PIC: non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300475 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300476 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100477 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300478 switch (addr) {
479 case 0x20:
480 case 0x21:
481 case 0xa0:
482 case 0xa1:
483 pic_ioport_write(&s->pics[addr >> 7], addr, data);
484 break;
485 case 0x4d0:
486 case 0x4d1:
487 elcr_ioport_write(&s->pics[addr & 1], addr, data);
488 break;
489 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100490 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300491 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300492}
493
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300494static int picdev_read(struct kvm_io_device *this,
495 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300496{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400497 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300498 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300499 if (!picdev_in_range(addr))
500 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300501
502 if (len != 1) {
503 if (printk_ratelimit())
504 printk(KERN_ERR "PIC: non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300505 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300506 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100507 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300508 switch (addr) {
509 case 0x20:
510 case 0x21:
511 case 0xa0:
512 case 0xa1:
513 data = pic_ioport_read(&s->pics[addr >> 7], addr);
514 break;
515 case 0x4d0:
516 case 0x4d1:
517 data = elcr_ioport_read(&s->pics[addr & 1], addr);
518 break;
519 }
520 *(unsigned char *)val = data;
Jan Kiszka50a085b2010-02-24 10:41:58 +0100521 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300522 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300523}
524
525/*
526 * callback when PIC0 irq status changed
527 */
Avi Kivity073d4612010-05-03 17:34:34 +0300528static void pic_irq_request(struct kvm *kvm, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300529{
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300530 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300531 struct kvm_pic *s = pic_irqchip(kvm);
532 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300533
Marcelo Tosattie4825802008-09-24 20:28:34 -0300534 s->output = level;
535 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
536 s->pics[0].isr_ack &= ~(1 << irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100537 s->wakeup_needed = true;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300538 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300539}
540
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400541static const struct kvm_io_device_ops picdev_ops = {
542 .read = picdev_read,
543 .write = picdev_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400544};
545
Eddie Dong85f455f2007-07-06 12:20:49 +0300546struct kvm_pic *kvm_create_pic(struct kvm *kvm)
547{
548 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400549 int ret;
550
Eddie Dong85f455f2007-07-06 12:20:49 +0300551 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
552 if (!s)
553 return NULL;
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000554 raw_spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200555 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300556 s->pics[0].elcr_mask = 0xf8;
557 s->pics[1].elcr_mask = 0xde;
Eddie Dong85f455f2007-07-06 12:20:49 +0300558 s->pics[0].pics_state = s;
559 s->pics[1].pics_state = s;
560
561 /*
562 * Initialize PIO device
563 */
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400564 kvm_iodevice_init(&s->dev, &picdev_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200565 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200566 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200567 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400568 if (ret < 0) {
569 kfree(s);
570 return NULL;
571 }
572
Eddie Dong85f455f2007-07-06 12:20:49 +0300573 return s;
574}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800575
576void kvm_destroy_pic(struct kvm *kvm)
577{
578 struct kvm_pic *vpic = kvm->arch.vpic;
579
580 if (vpic) {
581 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev);
582 kvm->arch.vpic = NULL;
583 kfree(vpic);
584 }
585}