blob: 07771da85de55a75db97f4c7522ad465ce386b26 [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 */
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000047 raw_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);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000049 raw_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);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000055
56 raw_spin_lock(&s->lock);
Marcelo Tosattie4825802008-09-24 20:28:34 -030057 s->pics[0].isr_ack = 0xff;
58 s->pics[1].isr_ack = 0xff;
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000059 raw_spin_unlock(&s->lock);
Avi Kivity7edd0ce2008-07-07 14:45:39 +030060}
61
Eddie Dong85f455f2007-07-06 12:20:49 +030062/*
63 * set irq level. If an edge is detected, then the IRR is set to 1
64 */
Gleb Natapov49256632009-02-04 17:28:14 +020065static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030066{
Gleb Natapov49256632009-02-04 17:28:14 +020067 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030068 mask = 1 << irq;
69 if (s->elcr & mask) /* level triggered */
70 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020071 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030072 s->irr |= mask;
73 s->last_irr |= mask;
74 } else {
75 s->irr &= ~mask;
76 s->last_irr &= ~mask;
77 }
78 else /* edge triggered */
79 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020080 if ((s->last_irr & mask) == 0) {
81 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030082 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +020083 }
Eddie Dong85f455f2007-07-06 12:20:49 +030084 s->last_irr |= mask;
85 } else
86 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +020087
88 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +030089}
90
91/*
92 * return the highest priority found in mask (highest = smallest
93 * number). Return 8 if no irq
94 */
95static inline int get_priority(struct kvm_kpic_state *s, int mask)
96{
97 int priority;
98 if (mask == 0)
99 return 8;
100 priority = 0;
101 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
102 priority++;
103 return priority;
104}
105
106/*
107 * return the pic wanted interrupt. return -1 if none
108 */
109static int pic_get_irq(struct kvm_kpic_state *s)
110{
111 int mask, cur_priority, priority;
112
113 mask = s->irr & ~s->imr;
114 priority = get_priority(s, mask);
115 if (priority == 8)
116 return -1;
117 /*
118 * compute current priority. If special fully nested mode on the
119 * master, the IRQ coming from the slave is not taken into account
120 * for the priority computation.
121 */
122 mask = s->isr;
123 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
124 mask &= ~(1 << 2);
125 cur_priority = get_priority(s, mask);
126 if (priority < cur_priority)
127 /*
128 * higher priority found: an irq should be generated
129 */
130 return (priority + s->priority_add) & 7;
131 else
132 return -1;
133}
134
135/*
136 * raise irq to CPU if necessary. must be called every time the active
137 * irq may change
138 */
139static void pic_update_irq(struct kvm_pic *s)
140{
141 int irq2, irq;
142
143 irq2 = pic_get_irq(&s->pics[1]);
144 if (irq2 >= 0) {
145 /*
146 * if irq request by slave pic, signal master PIC
147 */
148 pic_set_irq1(&s->pics[0], 2, 1);
149 pic_set_irq1(&s->pics[0], 2, 0);
150 }
151 irq = pic_get_irq(&s->pics[0]);
152 if (irq >= 0)
153 s->irq_request(s->irq_request_opaque, 1);
154 else
155 s->irq_request(s->irq_request_opaque, 0);
156}
157
He, Qing6ceb9d72007-07-26 11:05:18 +0300158void kvm_pic_update_irq(struct kvm_pic *s)
159{
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000160 raw_spin_lock(&s->lock);
He, Qing6ceb9d72007-07-26 11:05:18 +0300161 pic_update_irq(s);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000162 raw_spin_unlock(&s->lock);
He, Qing6ceb9d72007-07-26 11:05:18 +0300163}
164
Gleb Natapov49256632009-02-04 17:28:14 +0200165int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300166{
167 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200168 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300169
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000170 raw_spin_lock(&s->lock);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300171 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200172 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300173 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300174 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
175 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300176 }
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000177 raw_spin_unlock(&s->lock);
Gleb Natapov49256632009-02-04 17:28:14 +0200178
179 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300180}
181
182/*
183 * acknowledge interrupt 'irq'
184 */
185static inline void pic_intack(struct kvm_kpic_state *s, int irq)
186{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300187 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300188 /*
189 * We don't clear a level sensitive interrupt here
190 */
191 if (!(s->elcr & (1 << irq)))
192 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300193
194 if (s->auto_eoi) {
195 if (s->rotate_on_auto_eoi)
196 s->priority_add = (irq + 1) & 7;
197 pic_clear_isr(s, irq);
198 }
199
Eddie Dong85f455f2007-07-06 12:20:49 +0300200}
201
Marcelo Tosattif5244722008-07-26 17:01:00 -0300202int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300203{
204 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300205 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300206
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000207 raw_spin_lock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300208 irq = pic_get_irq(&s->pics[0]);
209 if (irq >= 0) {
210 pic_intack(&s->pics[0], irq);
211 if (irq == 2) {
212 irq2 = pic_get_irq(&s->pics[1]);
213 if (irq2 >= 0)
214 pic_intack(&s->pics[1], irq2);
215 else
216 /*
217 * spurious IRQ on slave controller
218 */
219 irq2 = 7;
220 intno = s->pics[1].irq_base + irq2;
221 irq = irq2 + 8;
222 } else
223 intno = s->pics[0].irq_base + irq;
224 } else {
225 /*
226 * spurious IRQ on host controller
227 */
228 irq = 7;
229 intno = s->pics[0].irq_base + irq;
230 }
231 pic_update_irq(s);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000232 raw_spin_unlock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300233
234 return intno;
235}
236
Eddie Dong2fcceae2007-10-10 12:14:25 +0200237void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300238{
Gleb Natapov79c727d2009-08-24 11:54:18 +0300239 int irq;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300240 struct kvm *kvm = s->pics_state->irq_request_opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300241 struct kvm_vcpu *vcpu0 = kvm->bsp_vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300242 u8 irr = s->irr, isr = s->imr;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300243
Eddie Dong85f455f2007-07-06 12:20:49 +0300244 s->last_irr = 0;
245 s->irr = 0;
246 s->imr = 0;
247 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300248 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300249 s->priority_add = 0;
250 s->irq_base = 0;
251 s->read_reg_select = 0;
252 s->poll = 0;
253 s->special_mask = 0;
254 s->init_state = 0;
255 s->auto_eoi = 0;
256 s->rotate_on_auto_eoi = 0;
257 s->special_fully_nested_mode = 0;
258 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300259
260 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
261 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
262 if (irr & (1 << irq) || isr & (1 << irq)) {
263 pic_clear_isr(s, irq);
264 }
265 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300266}
267
268static void pic_ioport_write(void *opaque, u32 addr, u32 val)
269{
270 struct kvm_kpic_state *s = opaque;
271 int priority, cmd, irq;
272
273 addr &= 1;
274 if (addr == 0) {
275 if (val & 0x10) {
Eddie Dong2fcceae2007-10-10 12:14:25 +0200276 kvm_pic_reset(s); /* init */
Eddie Dong85f455f2007-07-06 12:20:49 +0300277 /*
278 * deassert a pending interrupt
279 */
280 s->pics_state->irq_request(s->pics_state->
281 irq_request_opaque, 0);
282 s->init_state = 1;
283 s->init4 = val & 1;
284 if (val & 0x02)
285 printk(KERN_ERR "single mode not supported");
286 if (val & 0x08)
287 printk(KERN_ERR
288 "level sensitive irq not supported");
289 } else if (val & 0x08) {
290 if (val & 0x04)
291 s->poll = 1;
292 if (val & 0x02)
293 s->read_reg_select = val & 1;
294 if (val & 0x40)
295 s->special_mask = (val >> 5) & 1;
296 } else {
297 cmd = val >> 5;
298 switch (cmd) {
299 case 0:
300 case 4:
301 s->rotate_on_auto_eoi = cmd >> 2;
302 break;
303 case 1: /* end of interrupt */
304 case 5:
305 priority = get_priority(s, s->isr);
306 if (priority != 8) {
307 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300308 if (cmd == 5)
309 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300310 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300311 pic_update_irq(s->pics_state);
312 }
313 break;
314 case 3:
315 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300316 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300317 pic_update_irq(s->pics_state);
318 break;
319 case 6:
320 s->priority_add = (val + 1) & 7;
321 pic_update_irq(s->pics_state);
322 break;
323 case 7:
324 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300325 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300326 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300327 pic_update_irq(s->pics_state);
328 break;
329 default:
330 break; /* no operation */
331 }
332 }
333 } else
334 switch (s->init_state) {
335 case 0: /* normal mode */
336 s->imr = val;
337 pic_update_irq(s->pics_state);
338 break;
339 case 1:
340 s->irq_base = val & 0xf8;
341 s->init_state = 2;
342 break;
343 case 2:
344 if (s->init4)
345 s->init_state = 3;
346 else
347 s->init_state = 0;
348 break;
349 case 3:
350 s->special_fully_nested_mode = (val >> 4) & 1;
351 s->auto_eoi = (val >> 1) & 1;
352 s->init_state = 0;
353 break;
354 }
355}
356
357static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
358{
359 int ret;
360
361 ret = pic_get_irq(s);
362 if (ret >= 0) {
363 if (addr1 >> 7) {
364 s->pics_state->pics[0].isr &= ~(1 << 2);
365 s->pics_state->pics[0].irr &= ~(1 << 2);
366 }
367 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300368 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300369 if (addr1 >> 7 || ret != 2)
370 pic_update_irq(s->pics_state);
371 } else {
372 ret = 0x07;
373 pic_update_irq(s->pics_state);
374 }
375
376 return ret;
377}
378
379static u32 pic_ioport_read(void *opaque, u32 addr1)
380{
381 struct kvm_kpic_state *s = opaque;
382 unsigned int addr;
383 int ret;
384
385 addr = addr1;
386 addr &= 1;
387 if (s->poll) {
388 ret = pic_poll_read(s, addr1);
389 s->poll = 0;
390 } else
391 if (addr == 0)
392 if (s->read_reg_select)
393 ret = s->isr;
394 else
395 ret = s->irr;
396 else
397 ret = s->imr;
398 return ret;
399}
400
401static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
402{
403 struct kvm_kpic_state *s = opaque;
404 s->elcr = val & s->elcr_mask;
405}
406
407static u32 elcr_ioport_read(void *opaque, u32 addr1)
408{
409 struct kvm_kpic_state *s = opaque;
410 return s->elcr;
411}
412
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300413static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300414{
415 switch (addr) {
416 case 0x20:
417 case 0x21:
418 case 0xa0:
419 case 0xa1:
420 case 0x4d0:
421 case 0x4d1:
422 return 1;
423 default:
424 return 0;
425 }
426}
427
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400428static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
429{
430 return container_of(dev, struct kvm_pic, dev);
431}
432
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300433static int picdev_write(struct kvm_io_device *this,
Eddie Dong85f455f2007-07-06 12:20:49 +0300434 gpa_t addr, int len, const void *val)
435{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400436 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300437 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300438 if (!picdev_in_range(addr))
439 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300440
441 if (len != 1) {
442 if (printk_ratelimit())
443 printk(KERN_ERR "PIC: non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300444 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300445 }
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000446 raw_spin_lock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300447 switch (addr) {
448 case 0x20:
449 case 0x21:
450 case 0xa0:
451 case 0xa1:
452 pic_ioport_write(&s->pics[addr >> 7], addr, data);
453 break;
454 case 0x4d0:
455 case 0x4d1:
456 elcr_ioport_write(&s->pics[addr & 1], addr, data);
457 break;
458 }
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000459 raw_spin_unlock(&s->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300460 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300461}
462
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300463static int picdev_read(struct kvm_io_device *this,
464 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300465{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400466 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300467 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300468 if (!picdev_in_range(addr))
469 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300470
471 if (len != 1) {
472 if (printk_ratelimit())
473 printk(KERN_ERR "PIC: non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300474 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300475 }
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000476 raw_spin_lock(&s->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +0300477 switch (addr) {
478 case 0x20:
479 case 0x21:
480 case 0xa0:
481 case 0xa1:
482 data = pic_ioport_read(&s->pics[addr >> 7], addr);
483 break;
484 case 0x4d0:
485 case 0x4d1:
486 data = elcr_ioport_read(&s->pics[addr & 1], addr);
487 break;
488 }
489 *(unsigned char *)val = data;
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000490 raw_spin_unlock(&s->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300491 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300492}
493
494/*
495 * callback when PIC0 irq status changed
496 */
497static void pic_irq_request(void *opaque, int level)
498{
499 struct kvm *kvm = opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300500 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300501 struct kvm_pic *s = pic_irqchip(kvm);
502 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300503
Marcelo Tosattie4825802008-09-24 20:28:34 -0300504 s->output = level;
505 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
506 s->pics[0].isr_ack &= ~(1 << irq);
Gleb Natapov956f97c2009-08-04 15:30:27 +0300507 kvm_vcpu_kick(vcpu);
Marcelo Tosattie4825802008-09-24 20:28:34 -0300508 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300509}
510
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400511static const struct kvm_io_device_ops picdev_ops = {
512 .read = picdev_read,
513 .write = picdev_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400514};
515
Eddie Dong85f455f2007-07-06 12:20:49 +0300516struct kvm_pic *kvm_create_pic(struct kvm *kvm)
517{
518 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400519 int ret;
520
Eddie Dong85f455f2007-07-06 12:20:49 +0300521 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
522 if (!s)
523 return NULL;
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +0000524 raw_spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200525 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300526 s->pics[0].elcr_mask = 0xf8;
527 s->pics[1].elcr_mask = 0xde;
528 s->irq_request = pic_irq_request;
529 s->irq_request_opaque = kvm;
530 s->pics[0].pics_state = s;
531 s->pics[1].pics_state = s;
532
533 /*
534 * Initialize PIO device
535 */
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400536 kvm_iodevice_init(&s->dev, &picdev_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200537 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200538 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200539 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400540 if (ret < 0) {
541 kfree(s);
542 return NULL;
543 }
544
Eddie Dong85f455f2007-07-06 12:20:49 +0300545 return s;
546}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800547
548void kvm_destroy_pic(struct kvm *kvm)
549{
550 struct kvm_pic *vpic = kvm->arch.vpic;
551
552 if (vpic) {
553 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev);
554 kvm->arch.vpic = NULL;
555 kfree(vpic);
556 }
557}