blob: a3711f9e580fe25ba44952b491247b8813dc0c54 [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
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 * Authors:
25 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
26 * Port from Qemu.
27 */
28#include <linux/mm.h>
Avi Kivity3f353852008-12-21 22:48:32 +020029#include <linux/bitops.h>
Eddie Dong85f455f2007-07-06 12:20:49 +030030#include "irq.h"
Avi Kivityedf88412007-12-16 11:02:48 +020031
32#include <linux/kvm_host.h>
Gleb Natapov1000ff82009-07-07 16:00:57 +030033#include "trace.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030034
Avi Kivity7edd0ce2008-07-07 14:45:39 +030035static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
36{
37 s->isr &= ~(1 << irq);
Marcelo Tosattie4825802008-09-24 20:28:34 -030038 s->isr_ack |= (1 << irq);
Gleb Natapov938396a2009-08-04 15:30:28 +030039 if (s != &s->pics_state->pics[0])
40 irq += 8;
Gleb Natapoveba02262009-08-24 11:54:25 +030041 /*
42 * We are dropping lock while calling ack notifiers since ack
43 * notifier callbacks for assigned devices call into PIC recursively.
44 * Other interrupt may be delivered to PIC while lock is dropped but
45 * it should be safe since PIC state is already updated at this stage.
46 */
47 spin_unlock(&s->pics_state->lock);
Gleb Natapov938396a2009-08-04 15:30:28 +030048 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
Gleb Natapoveba02262009-08-24 11:54:25 +030049 spin_lock(&s->pics_state->lock);
Marcelo Tosattie4825802008-09-24 20:28:34 -030050}
51
52void kvm_pic_clear_isr_ack(struct kvm *kvm)
53{
54 struct kvm_pic *s = pic_irqchip(kvm);
Gleb Natapov88ba63c2009-08-04 15:30:29 +030055 spin_lock(&s->lock);
Marcelo Tosattie4825802008-09-24 20:28:34 -030056 s->pics[0].isr_ack = 0xff;
57 s->pics[1].isr_ack = 0xff;
Gleb Natapov88ba63c2009-08-04 15:30:29 +030058 spin_unlock(&s->lock);
Avi Kivity7edd0ce2008-07-07 14:45:39 +030059}
60
Eddie Dong85f455f2007-07-06 12:20:49 +030061/*
62 * set irq level. If an edge is detected, then the IRR is set to 1
63 */
Gleb Natapov49256632009-02-04 17:28:14 +020064static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030065{
Gleb Natapov49256632009-02-04 17:28:14 +020066 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030067 mask = 1 << irq;
68 if (s->elcr & mask) /* level triggered */
69 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020070 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030071 s->irr |= mask;
72 s->last_irr |= mask;
73 } else {
74 s->irr &= ~mask;
75 s->last_irr &= ~mask;
76 }
77 else /* edge triggered */
78 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020079 if ((s->last_irr & mask) == 0) {
80 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030081 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +020082 }
Eddie Dong85f455f2007-07-06 12:20:49 +030083 s->last_irr |= mask;
84 } else
85 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +020086
87 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +030088}
89
90/*
91 * return the highest priority found in mask (highest = smallest
92 * number). Return 8 if no irq
93 */
94static inline int get_priority(struct kvm_kpic_state *s, int mask)
95{
96 int priority;
97 if (mask == 0)
98 return 8;
99 priority = 0;
100 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
101 priority++;
102 return priority;
103}
104
105/*
106 * return the pic wanted interrupt. return -1 if none
107 */
108static int pic_get_irq(struct kvm_kpic_state *s)
109{
110 int mask, cur_priority, priority;
111
112 mask = s->irr & ~s->imr;
113 priority = get_priority(s, mask);
114 if (priority == 8)
115 return -1;
116 /*
117 * compute current priority. If special fully nested mode on the
118 * master, the IRQ coming from the slave is not taken into account
119 * for the priority computation.
120 */
121 mask = s->isr;
122 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
123 mask &= ~(1 << 2);
124 cur_priority = get_priority(s, mask);
125 if (priority < cur_priority)
126 /*
127 * higher priority found: an irq should be generated
128 */
129 return (priority + s->priority_add) & 7;
130 else
131 return -1;
132}
133
134/*
135 * raise irq to CPU if necessary. must be called every time the active
136 * irq may change
137 */
138static void pic_update_irq(struct kvm_pic *s)
139{
140 int irq2, irq;
141
142 irq2 = pic_get_irq(&s->pics[1]);
143 if (irq2 >= 0) {
144 /*
145 * if irq request by slave pic, signal master PIC
146 */
147 pic_set_irq1(&s->pics[0], 2, 1);
148 pic_set_irq1(&s->pics[0], 2, 0);
149 }
150 irq = pic_get_irq(&s->pics[0]);
151 if (irq >= 0)
152 s->irq_request(s->irq_request_opaque, 1);
153 else
154 s->irq_request(s->irq_request_opaque, 0);
155}
156
He, Qing6ceb9d72007-07-26 11:05:18 +0300157void kvm_pic_update_irq(struct kvm_pic *s)
158{
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300159 spin_lock(&s->lock);
He, Qing6ceb9d72007-07-26 11:05:18 +0300160 pic_update_irq(s);
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300161 spin_unlock(&s->lock);
He, Qing6ceb9d72007-07-26 11:05:18 +0300162}
163
Gleb Natapov49256632009-02-04 17:28:14 +0200164int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300165{
166 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200167 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300168
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300169 spin_lock(&s->lock);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300170 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200171 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300172 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300173 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
174 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300175 }
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300176 spin_unlock(&s->lock);
Gleb Natapov49256632009-02-04 17:28:14 +0200177
178 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300179}
180
181/*
182 * acknowledge interrupt 'irq'
183 */
184static inline void pic_intack(struct kvm_kpic_state *s, int irq)
185{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300186 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300187 /*
188 * We don't clear a level sensitive interrupt here
189 */
190 if (!(s->elcr & (1 << irq)))
191 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300192
193 if (s->auto_eoi) {
194 if (s->rotate_on_auto_eoi)
195 s->priority_add = (irq + 1) & 7;
196 pic_clear_isr(s, irq);
197 }
198
Eddie Dong85f455f2007-07-06 12:20:49 +0300199}
200
Marcelo Tosattif5244722008-07-26 17:01:00 -0300201int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300202{
203 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300204 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300205
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300206 spin_lock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300207 irq = pic_get_irq(&s->pics[0]);
208 if (irq >= 0) {
209 pic_intack(&s->pics[0], irq);
210 if (irq == 2) {
211 irq2 = pic_get_irq(&s->pics[1]);
212 if (irq2 >= 0)
213 pic_intack(&s->pics[1], irq2);
214 else
215 /*
216 * spurious IRQ on slave controller
217 */
218 irq2 = 7;
219 intno = s->pics[1].irq_base + irq2;
220 irq = irq2 + 8;
221 } else
222 intno = s->pics[0].irq_base + irq;
223 } else {
224 /*
225 * spurious IRQ on host controller
226 */
227 irq = 7;
228 intno = s->pics[0].irq_base + irq;
229 }
230 pic_update_irq(s);
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300231 spin_unlock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300232
233 return intno;
234}
235
Eddie Dong2fcceae2007-10-10 12:14:25 +0200236void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300237{
Gleb Natapov79c727d2009-08-24 11:54:18 +0300238 int irq;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300239 struct kvm *kvm = s->pics_state->irq_request_opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300240 struct kvm_vcpu *vcpu0 = kvm->bsp_vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300241 u8 irr = s->irr, isr = s->imr;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300242
Eddie Dong85f455f2007-07-06 12:20:49 +0300243 s->last_irr = 0;
244 s->irr = 0;
245 s->imr = 0;
246 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300247 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300248 s->priority_add = 0;
249 s->irq_base = 0;
250 s->read_reg_select = 0;
251 s->poll = 0;
252 s->special_mask = 0;
253 s->init_state = 0;
254 s->auto_eoi = 0;
255 s->rotate_on_auto_eoi = 0;
256 s->special_fully_nested_mode = 0;
257 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300258
259 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
260 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
261 if (irr & (1 << irq) || isr & (1 << irq)) {
262 pic_clear_isr(s, irq);
263 }
264 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300265}
266
267static void pic_ioport_write(void *opaque, u32 addr, u32 val)
268{
269 struct kvm_kpic_state *s = opaque;
270 int priority, cmd, irq;
271
272 addr &= 1;
273 if (addr == 0) {
274 if (val & 0x10) {
Eddie Dong2fcceae2007-10-10 12:14:25 +0200275 kvm_pic_reset(s); /* init */
Eddie Dong85f455f2007-07-06 12:20:49 +0300276 /*
277 * deassert a pending interrupt
278 */
279 s->pics_state->irq_request(s->pics_state->
280 irq_request_opaque, 0);
281 s->init_state = 1;
282 s->init4 = val & 1;
283 if (val & 0x02)
284 printk(KERN_ERR "single mode not supported");
285 if (val & 0x08)
286 printk(KERN_ERR
287 "level sensitive irq not supported");
288 } else if (val & 0x08) {
289 if (val & 0x04)
290 s->poll = 1;
291 if (val & 0x02)
292 s->read_reg_select = val & 1;
293 if (val & 0x40)
294 s->special_mask = (val >> 5) & 1;
295 } else {
296 cmd = val >> 5;
297 switch (cmd) {
298 case 0:
299 case 4:
300 s->rotate_on_auto_eoi = cmd >> 2;
301 break;
302 case 1: /* end of interrupt */
303 case 5:
304 priority = get_priority(s, s->isr);
305 if (priority != 8) {
306 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300307 if (cmd == 5)
308 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300309 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300310 pic_update_irq(s->pics_state);
311 }
312 break;
313 case 3:
314 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300315 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300316 pic_update_irq(s->pics_state);
317 break;
318 case 6:
319 s->priority_add = (val + 1) & 7;
320 pic_update_irq(s->pics_state);
321 break;
322 case 7:
323 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300324 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300325 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300326 pic_update_irq(s->pics_state);
327 break;
328 default:
329 break; /* no operation */
330 }
331 }
332 } else
333 switch (s->init_state) {
334 case 0: /* normal mode */
335 s->imr = val;
336 pic_update_irq(s->pics_state);
337 break;
338 case 1:
339 s->irq_base = val & 0xf8;
340 s->init_state = 2;
341 break;
342 case 2:
343 if (s->init4)
344 s->init_state = 3;
345 else
346 s->init_state = 0;
347 break;
348 case 3:
349 s->special_fully_nested_mode = (val >> 4) & 1;
350 s->auto_eoi = (val >> 1) & 1;
351 s->init_state = 0;
352 break;
353 }
354}
355
356static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
357{
358 int ret;
359
360 ret = pic_get_irq(s);
361 if (ret >= 0) {
362 if (addr1 >> 7) {
363 s->pics_state->pics[0].isr &= ~(1 << 2);
364 s->pics_state->pics[0].irr &= ~(1 << 2);
365 }
366 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300367 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300368 if (addr1 >> 7 || ret != 2)
369 pic_update_irq(s->pics_state);
370 } else {
371 ret = 0x07;
372 pic_update_irq(s->pics_state);
373 }
374
375 return ret;
376}
377
378static u32 pic_ioport_read(void *opaque, u32 addr1)
379{
380 struct kvm_kpic_state *s = opaque;
381 unsigned int addr;
382 int ret;
383
384 addr = addr1;
385 addr &= 1;
386 if (s->poll) {
387 ret = pic_poll_read(s, addr1);
388 s->poll = 0;
389 } else
390 if (addr == 0)
391 if (s->read_reg_select)
392 ret = s->isr;
393 else
394 ret = s->irr;
395 else
396 ret = s->imr;
397 return ret;
398}
399
400static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
401{
402 struct kvm_kpic_state *s = opaque;
403 s->elcr = val & s->elcr_mask;
404}
405
406static u32 elcr_ioport_read(void *opaque, u32 addr1)
407{
408 struct kvm_kpic_state *s = opaque;
409 return s->elcr;
410}
411
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300412static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300413{
414 switch (addr) {
415 case 0x20:
416 case 0x21:
417 case 0xa0:
418 case 0xa1:
419 case 0x4d0:
420 case 0x4d1:
421 return 1;
422 default:
423 return 0;
424 }
425}
426
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400427static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
428{
429 return container_of(dev, struct kvm_pic, dev);
430}
431
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300432static int picdev_write(struct kvm_io_device *this,
Eddie Dong85f455f2007-07-06 12:20:49 +0300433 gpa_t addr, int len, const void *val)
434{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400435 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300436 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300437 if (!picdev_in_range(addr))
438 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300439
440 if (len != 1) {
441 if (printk_ratelimit())
442 printk(KERN_ERR "PIC: non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300443 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300444 }
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300445 spin_lock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300446 switch (addr) {
447 case 0x20:
448 case 0x21:
449 case 0xa0:
450 case 0xa1:
451 pic_ioport_write(&s->pics[addr >> 7], addr, data);
452 break;
453 case 0x4d0:
454 case 0x4d1:
455 elcr_ioport_write(&s->pics[addr & 1], addr, data);
456 break;
457 }
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300458 spin_unlock(&s->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300459 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300460}
461
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300462static int picdev_read(struct kvm_io_device *this,
463 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300464{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400465 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300466 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300467 if (!picdev_in_range(addr))
468 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300469
470 if (len != 1) {
471 if (printk_ratelimit())
472 printk(KERN_ERR "PIC: non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300473 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300474 }
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300475 spin_lock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300476 switch (addr) {
477 case 0x20:
478 case 0x21:
479 case 0xa0:
480 case 0xa1:
481 data = pic_ioport_read(&s->pics[addr >> 7], addr);
482 break;
483 case 0x4d0:
484 case 0x4d1:
485 data = elcr_ioport_read(&s->pics[addr & 1], addr);
486 break;
487 }
488 *(unsigned char *)val = data;
Gleb Natapov88ba63c2009-08-04 15:30:29 +0300489 spin_unlock(&s->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300490 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300491}
492
493/*
494 * callback when PIC0 irq status changed
495 */
496static void pic_irq_request(void *opaque, int level)
497{
498 struct kvm *kvm = opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300499 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300500 struct kvm_pic *s = pic_irqchip(kvm);
501 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300502
Marcelo Tosattie4825802008-09-24 20:28:34 -0300503 s->output = level;
504 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
505 s->pics[0].isr_ack &= ~(1 << irq);
Gleb Natapov956f97c2009-08-04 15:30:27 +0300506 kvm_vcpu_kick(vcpu);
Marcelo Tosattie4825802008-09-24 20:28:34 -0300507 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300508}
509
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400510static const struct kvm_io_device_ops picdev_ops = {
511 .read = picdev_read,
512 .write = picdev_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400513};
514
Eddie Dong85f455f2007-07-06 12:20:49 +0300515struct kvm_pic *kvm_create_pic(struct kvm *kvm)
516{
517 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400518 int ret;
519
Eddie Dong85f455f2007-07-06 12:20:49 +0300520 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
521 if (!s)
522 return NULL;
Avi Kivity3f353852008-12-21 22:48:32 +0200523 spin_lock_init(&s->lock);
524 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300525 s->pics[0].elcr_mask = 0xf8;
526 s->pics[1].elcr_mask = 0xde;
527 s->irq_request = pic_irq_request;
528 s->irq_request_opaque = kvm;
529 s->pics[0].pics_state = s;
530 s->pics[1].pics_state = s;
531
532 /*
533 * Initialize PIO device
534 */
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400535 kvm_iodevice_init(&s->dev, &picdev_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200536 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200537 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200538 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400539 if (ret < 0) {
540 kfree(s);
541 return NULL;
542 }
543
Eddie Dong85f455f2007-07-06 12:20:49 +0300544 return s;
545}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800546
547void kvm_destroy_pic(struct kvm *kvm)
548{
549 struct kvm_pic *vpic = kvm->arch.vpic;
550
551 if (vpic) {
552 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev);
553 kvm->arch.vpic = NULL;
554 kfree(vpic);
555 }
556}