blob: e4bcbddecb36f5a62cb6fe3fa1b1a84b7795eb41 [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 Kivity3f353852008-12-21 22:48:32 +020035static void pic_lock(struct kvm_pic *s)
Hannes Ederd7364a22009-02-21 02:18:13 +010036 __acquires(&s->lock)
Avi Kivity3f353852008-12-21 22:48:32 +020037{
38 spin_lock(&s->lock);
39}
40
41static void pic_unlock(struct kvm_pic *s)
Hannes Ederd7364a22009-02-21 02:18:13 +010042 __releases(&s->lock)
Avi Kivity3f353852008-12-21 22:48:32 +020043{
44 struct kvm *kvm = s->kvm;
45 unsigned acks = s->pending_acks;
46 bool wakeup = s->wakeup_needed;
47 struct kvm_vcpu *vcpu;
48
49 s->pending_acks = 0;
50 s->wakeup_needed = false;
51
52 spin_unlock(&s->lock);
53
54 while (acks) {
Marcelo Tosatti44882ee2009-01-27 15:12:38 -020055 kvm_notify_acked_irq(kvm, SELECT_PIC(__ffs(acks)),
56 __ffs(acks));
Avi Kivity3f353852008-12-21 22:48:32 +020057 acks &= acks - 1;
58 }
59
60 if (wakeup) {
Gleb Natapovc5af89b2009-06-09 15:56:26 +030061 vcpu = s->kvm->bsp_vcpu;
Avi Kivity3f353852008-12-21 22:48:32 +020062 if (vcpu)
63 kvm_vcpu_kick(vcpu);
64 }
65}
66
Avi Kivity7edd0ce2008-07-07 14:45:39 +030067static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
68{
69 s->isr &= ~(1 << irq);
Marcelo Tosattie4825802008-09-24 20:28:34 -030070 s->isr_ack |= (1 << irq);
71}
72
73void kvm_pic_clear_isr_ack(struct kvm *kvm)
74{
75 struct kvm_pic *s = pic_irqchip(kvm);
Marcelo Tosatti9f4cc122009-06-04 15:08:21 -030076 pic_lock(s);
Marcelo Tosattie4825802008-09-24 20:28:34 -030077 s->pics[0].isr_ack = 0xff;
78 s->pics[1].isr_ack = 0xff;
Marcelo Tosatti9f4cc122009-06-04 15:08:21 -030079 pic_unlock(s);
Avi Kivity7edd0ce2008-07-07 14:45:39 +030080}
81
Eddie Dong85f455f2007-07-06 12:20:49 +030082/*
83 * set irq level. If an edge is detected, then the IRR is set to 1
84 */
Gleb Natapov49256632009-02-04 17:28:14 +020085static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030086{
Gleb Natapov49256632009-02-04 17:28:14 +020087 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030088 mask = 1 << irq;
89 if (s->elcr & mask) /* level triggered */
90 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020091 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030092 s->irr |= mask;
93 s->last_irr |= mask;
94 } else {
95 s->irr &= ~mask;
96 s->last_irr &= ~mask;
97 }
98 else /* edge triggered */
99 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200100 if ((s->last_irr & mask) == 0) {
101 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300102 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200103 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300104 s->last_irr |= mask;
105 } else
106 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200107
108 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300109}
110
111/*
112 * return the highest priority found in mask (highest = smallest
113 * number). Return 8 if no irq
114 */
115static inline int get_priority(struct kvm_kpic_state *s, int mask)
116{
117 int priority;
118 if (mask == 0)
119 return 8;
120 priority = 0;
121 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
122 priority++;
123 return priority;
124}
125
126/*
127 * return the pic wanted interrupt. return -1 if none
128 */
129static int pic_get_irq(struct kvm_kpic_state *s)
130{
131 int mask, cur_priority, priority;
132
133 mask = s->irr & ~s->imr;
134 priority = get_priority(s, mask);
135 if (priority == 8)
136 return -1;
137 /*
138 * compute current priority. If special fully nested mode on the
139 * master, the IRQ coming from the slave is not taken into account
140 * for the priority computation.
141 */
142 mask = s->isr;
143 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
144 mask &= ~(1 << 2);
145 cur_priority = get_priority(s, mask);
146 if (priority < cur_priority)
147 /*
148 * higher priority found: an irq should be generated
149 */
150 return (priority + s->priority_add) & 7;
151 else
152 return -1;
153}
154
155/*
156 * raise irq to CPU if necessary. must be called every time the active
157 * irq may change
158 */
159static void pic_update_irq(struct kvm_pic *s)
160{
161 int irq2, irq;
162
163 irq2 = pic_get_irq(&s->pics[1]);
164 if (irq2 >= 0) {
165 /*
166 * if irq request by slave pic, signal master PIC
167 */
168 pic_set_irq1(&s->pics[0], 2, 1);
169 pic_set_irq1(&s->pics[0], 2, 0);
170 }
171 irq = pic_get_irq(&s->pics[0]);
172 if (irq >= 0)
173 s->irq_request(s->irq_request_opaque, 1);
174 else
175 s->irq_request(s->irq_request_opaque, 0);
176}
177
He, Qing6ceb9d72007-07-26 11:05:18 +0300178void kvm_pic_update_irq(struct kvm_pic *s)
179{
Avi Kivity3f353852008-12-21 22:48:32 +0200180 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300181 pic_update_irq(s);
Avi Kivity3f353852008-12-21 22:48:32 +0200182 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300183}
184
Gleb Natapov49256632009-02-04 17:28:14 +0200185int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300186{
187 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200188 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300189
Avi Kivity3f353852008-12-21 22:48:32 +0200190 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300191 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200192 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300193 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300194 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
195 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300196 }
Avi Kivity3f353852008-12-21 22:48:32 +0200197 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200198
199 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300200}
201
202/*
203 * acknowledge interrupt 'irq'
204 */
205static inline void pic_intack(struct kvm_kpic_state *s, int irq)
206{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300207 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300208 if (s->auto_eoi) {
209 if (s->rotate_on_auto_eoi)
210 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300211 pic_clear_isr(s, irq);
212 }
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);
218}
219
Marcelo Tosattif5244722008-07-26 17:01:00 -0300220int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300221{
222 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300223 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300224
Avi Kivity3f353852008-12-21 22:48:32 +0200225 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300226 irq = pic_get_irq(&s->pics[0]);
227 if (irq >= 0) {
228 pic_intack(&s->pics[0], irq);
229 if (irq == 2) {
230 irq2 = pic_get_irq(&s->pics[1]);
231 if (irq2 >= 0)
232 pic_intack(&s->pics[1], irq2);
233 else
234 /*
235 * spurious IRQ on slave controller
236 */
237 irq2 = 7;
238 intno = s->pics[1].irq_base + irq2;
239 irq = irq2 + 8;
240 } else
241 intno = s->pics[0].irq_base + irq;
242 } else {
243 /*
244 * spurious IRQ on host controller
245 */
246 irq = 7;
247 intno = s->pics[0].irq_base + irq;
248 }
249 pic_update_irq(s);
Avi Kivity3f353852008-12-21 22:48:32 +0200250 pic_unlock(s);
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200251 kvm_notify_acked_irq(kvm, SELECT_PIC(irq), irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300252
253 return intno;
254}
255
Eddie Dong2fcceae2007-10-10 12:14:25 +0200256void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300257{
Avi Kivity3f353852008-12-21 22:48:32 +0200258 int irq, irqbase, n;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300259 struct kvm *kvm = s->pics_state->irq_request_opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300260 struct kvm_vcpu *vcpu0 = kvm->bsp_vcpu;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300261
Marcelo Tosatti85428ac2008-08-14 20:53:25 -0300262 if (s == &s->pics_state->pics[0])
263 irqbase = 0;
264 else
265 irqbase = 8;
266
267 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
268 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
Avi Kivity3f353852008-12-21 22:48:32 +0200269 if (s->irr & (1 << irq) || s->isr & (1 << irq)) {
270 n = irq + irqbase;
271 s->pics_state->pending_acks |= 1 << n;
272 }
Marcelo Tosattif5244722008-07-26 17:01:00 -0300273 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300274 s->last_irr = 0;
275 s->irr = 0;
276 s->imr = 0;
277 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300278 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300279 s->priority_add = 0;
280 s->irq_base = 0;
281 s->read_reg_select = 0;
282 s->poll = 0;
283 s->special_mask = 0;
284 s->init_state = 0;
285 s->auto_eoi = 0;
286 s->rotate_on_auto_eoi = 0;
287 s->special_fully_nested_mode = 0;
288 s->init4 = 0;
289}
290
291static void pic_ioport_write(void *opaque, u32 addr, u32 val)
292{
293 struct kvm_kpic_state *s = opaque;
294 int priority, cmd, irq;
295
296 addr &= 1;
297 if (addr == 0) {
298 if (val & 0x10) {
Eddie Dong2fcceae2007-10-10 12:14:25 +0200299 kvm_pic_reset(s); /* init */
Eddie Dong85f455f2007-07-06 12:20:49 +0300300 /*
301 * deassert a pending interrupt
302 */
303 s->pics_state->irq_request(s->pics_state->
304 irq_request_opaque, 0);
305 s->init_state = 1;
306 s->init4 = val & 1;
307 if (val & 0x02)
308 printk(KERN_ERR "single mode not supported");
309 if (val & 0x08)
310 printk(KERN_ERR
311 "level sensitive irq not supported");
312 } else if (val & 0x08) {
313 if (val & 0x04)
314 s->poll = 1;
315 if (val & 0x02)
316 s->read_reg_select = val & 1;
317 if (val & 0x40)
318 s->special_mask = (val >> 5) & 1;
319 } else {
320 cmd = val >> 5;
321 switch (cmd) {
322 case 0:
323 case 4:
324 s->rotate_on_auto_eoi = cmd >> 2;
325 break;
326 case 1: /* end of interrupt */
327 case 5:
328 priority = get_priority(s, s->isr);
329 if (priority != 8) {
330 irq = (priority + s->priority_add) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300331 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300332 if (cmd == 5)
333 s->priority_add = (irq + 1) & 7;
334 pic_update_irq(s->pics_state);
335 }
336 break;
337 case 3:
338 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300339 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300340 pic_update_irq(s->pics_state);
341 break;
342 case 6:
343 s->priority_add = (val + 1) & 7;
344 pic_update_irq(s->pics_state);
345 break;
346 case 7:
347 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300348 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300349 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300350 pic_update_irq(s->pics_state);
351 break;
352 default:
353 break; /* no operation */
354 }
355 }
356 } else
357 switch (s->init_state) {
358 case 0: /* normal mode */
359 s->imr = val;
360 pic_update_irq(s->pics_state);
361 break;
362 case 1:
363 s->irq_base = val & 0xf8;
364 s->init_state = 2;
365 break;
366 case 2:
367 if (s->init4)
368 s->init_state = 3;
369 else
370 s->init_state = 0;
371 break;
372 case 3:
373 s->special_fully_nested_mode = (val >> 4) & 1;
374 s->auto_eoi = (val >> 1) & 1;
375 s->init_state = 0;
376 break;
377 }
378}
379
380static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
381{
382 int ret;
383
384 ret = pic_get_irq(s);
385 if (ret >= 0) {
386 if (addr1 >> 7) {
387 s->pics_state->pics[0].isr &= ~(1 << 2);
388 s->pics_state->pics[0].irr &= ~(1 << 2);
389 }
390 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300391 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300392 if (addr1 >> 7 || ret != 2)
393 pic_update_irq(s->pics_state);
394 } else {
395 ret = 0x07;
396 pic_update_irq(s->pics_state);
397 }
398
399 return ret;
400}
401
402static u32 pic_ioport_read(void *opaque, u32 addr1)
403{
404 struct kvm_kpic_state *s = opaque;
405 unsigned int addr;
406 int ret;
407
408 addr = addr1;
409 addr &= 1;
410 if (s->poll) {
411 ret = pic_poll_read(s, addr1);
412 s->poll = 0;
413 } else
414 if (addr == 0)
415 if (s->read_reg_select)
416 ret = s->isr;
417 else
418 ret = s->irr;
419 else
420 ret = s->imr;
421 return ret;
422}
423
424static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
425{
426 struct kvm_kpic_state *s = opaque;
427 s->elcr = val & s->elcr_mask;
428}
429
430static u32 elcr_ioport_read(void *opaque, u32 addr1)
431{
432 struct kvm_kpic_state *s = opaque;
433 return s->elcr;
434}
435
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300436static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300437{
438 switch (addr) {
439 case 0x20:
440 case 0x21:
441 case 0xa0:
442 case 0xa1:
443 case 0x4d0:
444 case 0x4d1:
445 return 1;
446 default:
447 return 0;
448 }
449}
450
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400451static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
452{
453 return container_of(dev, struct kvm_pic, dev);
454}
455
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300456static int picdev_write(struct kvm_io_device *this,
Eddie Dong85f455f2007-07-06 12:20:49 +0300457 gpa_t addr, int len, const void *val)
458{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400459 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300460 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300461 if (!picdev_in_range(addr))
462 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300463
464 if (len != 1) {
465 if (printk_ratelimit())
466 printk(KERN_ERR "PIC: non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300467 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300468 }
Avi Kivity3f353852008-12-21 22:48:32 +0200469 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300470 switch (addr) {
471 case 0x20:
472 case 0x21:
473 case 0xa0:
474 case 0xa1:
475 pic_ioport_write(&s->pics[addr >> 7], addr, data);
476 break;
477 case 0x4d0:
478 case 0x4d1:
479 elcr_ioport_write(&s->pics[addr & 1], addr, data);
480 break;
481 }
Avi Kivity3f353852008-12-21 22:48:32 +0200482 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300483 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300484}
485
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300486static int picdev_read(struct kvm_io_device *this,
487 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300488{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400489 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300490 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300491 if (!picdev_in_range(addr))
492 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300493
494 if (len != 1) {
495 if (printk_ratelimit())
496 printk(KERN_ERR "PIC: non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300497 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300498 }
Avi Kivity3f353852008-12-21 22:48:32 +0200499 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300500 switch (addr) {
501 case 0x20:
502 case 0x21:
503 case 0xa0:
504 case 0xa1:
505 data = pic_ioport_read(&s->pics[addr >> 7], addr);
506 break;
507 case 0x4d0:
508 case 0x4d1:
509 data = elcr_ioport_read(&s->pics[addr & 1], addr);
510 break;
511 }
512 *(unsigned char *)val = data;
Avi Kivity3f353852008-12-21 22:48:32 +0200513 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300514 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300515}
516
517/*
518 * callback when PIC0 irq status changed
519 */
520static void pic_irq_request(void *opaque, int level)
521{
522 struct kvm *kvm = opaque;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300523 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300524 struct kvm_pic *s = pic_irqchip(kvm);
525 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300526
Marcelo Tosattie4825802008-09-24 20:28:34 -0300527 s->output = level;
528 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
529 s->pics[0].isr_ack &= ~(1 << irq);
Avi Kivity3f353852008-12-21 22:48:32 +0200530 s->wakeup_needed = true;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300531 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300532}
533
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400534static const struct kvm_io_device_ops picdev_ops = {
535 .read = picdev_read,
536 .write = picdev_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400537};
538
Eddie Dong85f455f2007-07-06 12:20:49 +0300539struct kvm_pic *kvm_create_pic(struct kvm *kvm)
540{
541 struct kvm_pic *s;
542 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
543 if (!s)
544 return NULL;
Avi Kivity3f353852008-12-21 22:48:32 +0200545 spin_lock_init(&s->lock);
546 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300547 s->pics[0].elcr_mask = 0xf8;
548 s->pics[1].elcr_mask = 0xde;
549 s->irq_request = pic_irq_request;
550 s->irq_request_opaque = kvm;
551 s->pics[0].pics_state = s;
552 s->pics[1].pics_state = s;
553
554 /*
555 * Initialize PIO device
556 */
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400557 kvm_iodevice_init(&s->dev, &picdev_ops);
Michael S. Tsirkin6c474692009-06-29 22:24:26 +0300558 kvm_io_bus_register_dev(kvm, &kvm->pio_bus, &s->dev);
Eddie Dong85f455f2007-07-06 12:20:49 +0300559 return s;
560}