blob: f628234fbeca06683876543f07476f10111812b7 [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
Nicolas Kaiser9611c182010-10-06 14:23:22 +02006 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
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{
Avi Kivityf4f51052010-09-19 18:44:07 +020042 spin_lock(&s->lock);
Jan Kiszka50a085b2010-02-24 10:41:58 +010043}
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
Avi Kivityf4f51052010-09-19 18:44:07 +020054 spin_unlock(&s->lock);
Jan Kiszka50a085b2010-02-24 10:41:58 +010055
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
Avi Kivityae0635b2010-07-27 14:51:44 +030067 if (!found)
68 return;
69
Avi Kivity3842d132010-07-27 12:30:24 +030070 kvm_make_request(KVM_REQ_EVENT, found);
Chris Lalancette529df652010-06-21 11:29:40 -040071 kvm_vcpu_kick(found);
Jan Kiszka50a085b2010-02-24 10:41:58 +010072 }
73}
74
Avi Kivity7edd0ce2008-07-07 14:45:39 +030075static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
76{
77 s->isr &= ~(1 << irq);
Marcelo Tosattie4825802008-09-24 20:28:34 -030078 s->isr_ack |= (1 << irq);
Gleb Natapov938396a2009-08-04 15:30:28 +030079 if (s != &s->pics_state->pics[0])
80 irq += 8;
Gleb Natapoveba02262009-08-24 11:54:25 +030081 /*
82 * We are dropping lock while calling ack notifiers since ack
83 * notifier callbacks for assigned devices call into PIC recursively.
84 * Other interrupt may be delivered to PIC while lock is dropped but
85 * it should be safe since PIC state is already updated at this stage.
86 */
Jan Kiszka50a085b2010-02-24 10:41:58 +010087 pic_unlock(s->pics_state);
Gleb Natapov938396a2009-08-04 15:30:28 +030088 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +010089 pic_lock(s->pics_state);
Marcelo Tosattie4825802008-09-24 20:28:34 -030090}
91
92void kvm_pic_clear_isr_ack(struct kvm *kvm)
93{
94 struct kvm_pic *s = pic_irqchip(kvm);
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +000095
Jan Kiszka50a085b2010-02-24 10:41:58 +010096 pic_lock(s);
Marcelo Tosattie4825802008-09-24 20:28:34 -030097 s->pics[0].isr_ack = 0xff;
98 s->pics[1].isr_ack = 0xff;
Jan Kiszka50a085b2010-02-24 10:41:58 +010099 pic_unlock(s);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300100}
101
Eddie Dong85f455f2007-07-06 12:20:49 +0300102/*
103 * set irq level. If an edge is detected, then the IRR is set to 1
104 */
Gleb Natapov49256632009-02-04 17:28:14 +0200105static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300106{
Gleb Natapov49256632009-02-04 17:28:14 +0200107 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300108 mask = 1 << irq;
109 if (s->elcr & mask) /* level triggered */
110 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200111 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300112 s->irr |= mask;
113 s->last_irr |= mask;
114 } else {
115 s->irr &= ~mask;
116 s->last_irr &= ~mask;
117 }
118 else /* edge triggered */
119 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200120 if ((s->last_irr & mask) == 0) {
121 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300122 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200123 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300124 s->last_irr |= mask;
125 } else
126 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200127
128 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300129}
130
131/*
132 * return the highest priority found in mask (highest = smallest
133 * number). Return 8 if no irq
134 */
135static inline int get_priority(struct kvm_kpic_state *s, int mask)
136{
137 int priority;
138 if (mask == 0)
139 return 8;
140 priority = 0;
141 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
142 priority++;
143 return priority;
144}
145
146/*
147 * return the pic wanted interrupt. return -1 if none
148 */
149static int pic_get_irq(struct kvm_kpic_state *s)
150{
151 int mask, cur_priority, priority;
152
153 mask = s->irr & ~s->imr;
154 priority = get_priority(s, mask);
155 if (priority == 8)
156 return -1;
157 /*
158 * compute current priority. If special fully nested mode on the
159 * master, the IRQ coming from the slave is not taken into account
160 * for the priority computation.
161 */
162 mask = s->isr;
163 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
164 mask &= ~(1 << 2);
165 cur_priority = get_priority(s, mask);
166 if (priority < cur_priority)
167 /*
168 * higher priority found: an irq should be generated
169 */
170 return (priority + s->priority_add) & 7;
171 else
172 return -1;
173}
174
175/*
176 * raise irq to CPU if necessary. must be called every time the active
177 * irq may change
178 */
179static void pic_update_irq(struct kvm_pic *s)
180{
181 int irq2, irq;
182
183 irq2 = pic_get_irq(&s->pics[1]);
184 if (irq2 >= 0) {
185 /*
186 * if irq request by slave pic, signal master PIC
187 */
188 pic_set_irq1(&s->pics[0], 2, 1);
189 pic_set_irq1(&s->pics[0], 2, 0);
190 }
191 irq = pic_get_irq(&s->pics[0]);
Avi Kivity36633f32010-05-03 17:38:06 +0300192 pic_irq_request(s->kvm, irq >= 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300193}
194
He, Qing6ceb9d72007-07-26 11:05:18 +0300195void kvm_pic_update_irq(struct kvm_pic *s)
196{
Jan Kiszka50a085b2010-02-24 10:41:58 +0100197 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300198 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100199 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300200}
201
Gleb Natapov49256632009-02-04 17:28:14 +0200202int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300203{
204 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200205 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300206
Jan Kiszka50a085b2010-02-24 10:41:58 +0100207 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300208 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200209 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300210 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300211 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
212 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300213 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100214 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200215
216 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300217}
218
219/*
220 * acknowledge interrupt 'irq'
221 */
222static inline void pic_intack(struct kvm_kpic_state *s, int irq)
223{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300224 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300225 /*
226 * We don't clear a level sensitive interrupt here
227 */
228 if (!(s->elcr & (1 << irq)))
229 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300230
231 if (s->auto_eoi) {
232 if (s->rotate_on_auto_eoi)
233 s->priority_add = (irq + 1) & 7;
234 pic_clear_isr(s, irq);
235 }
236
Eddie Dong85f455f2007-07-06 12:20:49 +0300237}
238
Marcelo Tosattif5244722008-07-26 17:01:00 -0300239int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300240{
241 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300242 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300243
Jan Kiszka50a085b2010-02-24 10:41:58 +0100244 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300245 irq = pic_get_irq(&s->pics[0]);
246 if (irq >= 0) {
247 pic_intack(&s->pics[0], irq);
248 if (irq == 2) {
249 irq2 = pic_get_irq(&s->pics[1]);
250 if (irq2 >= 0)
251 pic_intack(&s->pics[1], irq2);
252 else
253 /*
254 * spurious IRQ on slave controller
255 */
256 irq2 = 7;
257 intno = s->pics[1].irq_base + irq2;
258 irq = irq2 + 8;
259 } else
260 intno = s->pics[0].irq_base + irq;
261 } else {
262 /*
263 * spurious IRQ on host controller
264 */
265 irq = 7;
266 intno = s->pics[0].irq_base + irq;
267 }
268 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100269 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300270
271 return intno;
272}
273
Eddie Dong2fcceae2007-10-10 12:14:25 +0200274void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300275{
Gleb Natapov79c727d2009-08-24 11:54:18 +0300276 int irq;
Avi Kivity073d4612010-05-03 17:34:34 +0300277 struct kvm_vcpu *vcpu0 = s->pics_state->kvm->bsp_vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300278 u8 irr = s->irr, isr = s->imr;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300279
Eddie Dong85f455f2007-07-06 12:20:49 +0300280 s->last_irr = 0;
281 s->irr = 0;
282 s->imr = 0;
283 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300284 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300285 s->priority_add = 0;
286 s->irq_base = 0;
287 s->read_reg_select = 0;
288 s->poll = 0;
289 s->special_mask = 0;
290 s->init_state = 0;
291 s->auto_eoi = 0;
292 s->rotate_on_auto_eoi = 0;
293 s->special_fully_nested_mode = 0;
294 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300295
296 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
297 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
298 if (irr & (1 << irq) || isr & (1 << irq)) {
299 pic_clear_isr(s, irq);
300 }
301 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300302}
303
304static void pic_ioport_write(void *opaque, u32 addr, u32 val)
305{
306 struct kvm_kpic_state *s = opaque;
307 int priority, cmd, irq;
308
309 addr &= 1;
310 if (addr == 0) {
311 if (val & 0x10) {
Eddie Dong85f455f2007-07-06 12:20:49 +0300312 s->init4 = val & 1;
Avi Kivity9ed049c2010-08-30 12:18:24 +0300313 s->last_irr = 0;
314 s->imr = 0;
315 s->priority_add = 0;
316 s->special_mask = 0;
317 s->read_reg_select = 0;
318 if (!s->init4) {
319 s->special_fully_nested_mode = 0;
320 s->auto_eoi = 0;
321 }
322 s->init_state = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300323 if (val & 0x02)
324 printk(KERN_ERR "single mode not supported");
325 if (val & 0x08)
326 printk(KERN_ERR
327 "level sensitive irq not supported");
328 } else if (val & 0x08) {
329 if (val & 0x04)
330 s->poll = 1;
331 if (val & 0x02)
332 s->read_reg_select = val & 1;
333 if (val & 0x40)
334 s->special_mask = (val >> 5) & 1;
335 } else {
336 cmd = val >> 5;
337 switch (cmd) {
338 case 0:
339 case 4:
340 s->rotate_on_auto_eoi = cmd >> 2;
341 break;
342 case 1: /* end of interrupt */
343 case 5:
344 priority = get_priority(s, s->isr);
345 if (priority != 8) {
346 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300347 if (cmd == 5)
348 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300349 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300350 pic_update_irq(s->pics_state);
351 }
352 break;
353 case 3:
354 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300355 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300356 pic_update_irq(s->pics_state);
357 break;
358 case 6:
359 s->priority_add = (val + 1) & 7;
360 pic_update_irq(s->pics_state);
361 break;
362 case 7:
363 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300364 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300365 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300366 pic_update_irq(s->pics_state);
367 break;
368 default:
369 break; /* no operation */
370 }
371 }
372 } else
373 switch (s->init_state) {
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300374 case 0: { /* normal mode */
375 u8 imr_diff = s->imr ^ val,
376 off = (s == &s->pics_state->pics[0]) ? 0 : 8;
Eddie Dong85f455f2007-07-06 12:20:49 +0300377 s->imr = val;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300378 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
379 if (imr_diff & (1 << irq))
380 kvm_fire_mask_notifiers(
381 s->pics_state->kvm,
382 SELECT_PIC(irq + off),
383 irq + off,
384 !!(s->imr & (1 << irq)));
Eddie Dong85f455f2007-07-06 12:20:49 +0300385 pic_update_irq(s->pics_state);
386 break;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300387 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300388 case 1:
389 s->irq_base = val & 0xf8;
390 s->init_state = 2;
391 break;
392 case 2:
393 if (s->init4)
394 s->init_state = 3;
395 else
396 s->init_state = 0;
397 break;
398 case 3:
399 s->special_fully_nested_mode = (val >> 4) & 1;
400 s->auto_eoi = (val >> 1) & 1;
401 s->init_state = 0;
402 break;
403 }
404}
405
406static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
407{
408 int ret;
409
410 ret = pic_get_irq(s);
411 if (ret >= 0) {
412 if (addr1 >> 7) {
413 s->pics_state->pics[0].isr &= ~(1 << 2);
414 s->pics_state->pics[0].irr &= ~(1 << 2);
415 }
416 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300417 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300418 if (addr1 >> 7 || ret != 2)
419 pic_update_irq(s->pics_state);
420 } else {
421 ret = 0x07;
422 pic_update_irq(s->pics_state);
423 }
424
425 return ret;
426}
427
428static u32 pic_ioport_read(void *opaque, u32 addr1)
429{
430 struct kvm_kpic_state *s = opaque;
431 unsigned int addr;
432 int ret;
433
434 addr = addr1;
435 addr &= 1;
436 if (s->poll) {
437 ret = pic_poll_read(s, addr1);
438 s->poll = 0;
439 } else
440 if (addr == 0)
441 if (s->read_reg_select)
442 ret = s->isr;
443 else
444 ret = s->irr;
445 else
446 ret = s->imr;
447 return ret;
448}
449
450static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
451{
452 struct kvm_kpic_state *s = opaque;
453 s->elcr = val & s->elcr_mask;
454}
455
456static u32 elcr_ioport_read(void *opaque, u32 addr1)
457{
458 struct kvm_kpic_state *s = opaque;
459 return s->elcr;
460}
461
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300462static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300463{
464 switch (addr) {
465 case 0x20:
466 case 0x21:
467 case 0xa0:
468 case 0xa1:
469 case 0x4d0:
470 case 0x4d1:
471 return 1;
472 default:
473 return 0;
474 }
475}
476
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400477static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
478{
479 return container_of(dev, struct kvm_pic, dev);
480}
481
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300482static int picdev_write(struct kvm_io_device *this,
Eddie Dong85f455f2007-07-06 12:20:49 +0300483 gpa_t addr, int len, const void *val)
484{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400485 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300486 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300487 if (!picdev_in_range(addr))
488 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300489
490 if (len != 1) {
491 if (printk_ratelimit())
492 printk(KERN_ERR "PIC: non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300493 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300494 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100495 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300496 switch (addr) {
497 case 0x20:
498 case 0x21:
499 case 0xa0:
500 case 0xa1:
501 pic_ioport_write(&s->pics[addr >> 7], addr, data);
502 break;
503 case 0x4d0:
504 case 0x4d1:
505 elcr_ioport_write(&s->pics[addr & 1], addr, data);
506 break;
507 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100508 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300509 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300510}
511
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300512static int picdev_read(struct kvm_io_device *this,
513 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300514{
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400515 struct kvm_pic *s = to_pic(this);
Eddie Dong85f455f2007-07-06 12:20:49 +0300516 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300517 if (!picdev_in_range(addr))
518 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300519
520 if (len != 1) {
521 if (printk_ratelimit())
522 printk(KERN_ERR "PIC: non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300523 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300524 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100525 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300526 switch (addr) {
527 case 0x20:
528 case 0x21:
529 case 0xa0:
530 case 0xa1:
531 data = pic_ioport_read(&s->pics[addr >> 7], addr);
532 break;
533 case 0x4d0:
534 case 0x4d1:
535 data = elcr_ioport_read(&s->pics[addr & 1], addr);
536 break;
537 }
538 *(unsigned char *)val = data;
Jan Kiszka50a085b2010-02-24 10:41:58 +0100539 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300540 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300541}
542
543/*
544 * callback when PIC0 irq status changed
545 */
Avi Kivity073d4612010-05-03 17:34:34 +0300546static void pic_irq_request(struct kvm *kvm, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300547{
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300548 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300549 struct kvm_pic *s = pic_irqchip(kvm);
550 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300551
Marcelo Tosattie4825802008-09-24 20:28:34 -0300552 s->output = level;
553 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
554 s->pics[0].isr_ack &= ~(1 << irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100555 s->wakeup_needed = true;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300556 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300557}
558
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400559static const struct kvm_io_device_ops picdev_ops = {
560 .read = picdev_read,
561 .write = picdev_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400562};
563
Eddie Dong85f455f2007-07-06 12:20:49 +0300564struct kvm_pic *kvm_create_pic(struct kvm *kvm)
565{
566 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400567 int ret;
568
Eddie Dong85f455f2007-07-06 12:20:49 +0300569 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
570 if (!s)
571 return NULL;
Avi Kivityf4f51052010-09-19 18:44:07 +0200572 spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200573 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300574 s->pics[0].elcr_mask = 0xf8;
575 s->pics[1].elcr_mask = 0xde;
Eddie Dong85f455f2007-07-06 12:20:49 +0300576 s->pics[0].pics_state = s;
577 s->pics[1].pics_state = s;
578
579 /*
580 * Initialize PIO device
581 */
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400582 kvm_iodevice_init(&s->dev, &picdev_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200583 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200584 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &s->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200585 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400586 if (ret < 0) {
587 kfree(s);
588 return NULL;
589 }
590
Eddie Dong85f455f2007-07-06 12:20:49 +0300591 return s;
592}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800593
594void kvm_destroy_pic(struct kvm *kvm)
595{
596 struct kvm_pic *vpic = kvm->arch.vpic;
597
598 if (vpic) {
599 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev);
600 kvm->arch.vpic = NULL;
601 kfree(vpic);
602 }
603}