blob: 6b869ce0cc195313eba95d968d435249c3923b7d [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)
Avi Kivityae0635b2010-07-27 14:51:44 +030065 return;
66
Avi Kivity3842d132010-07-27 12:30:24 +030067 kvm_make_request(KVM_REQ_EVENT, found);
Chris Lalancette529df652010-06-21 11:29:40 -040068 kvm_vcpu_kick(found);
Jan Kiszka50a085b2010-02-24 10:41:58 +010069 }
70}
71
Avi Kivity7edd0ce2008-07-07 14:45:39 +030072static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
73{
74 s->isr &= ~(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
Eddie Dong85f455f2007-07-06 12:20:49 +030088/*
89 * set irq level. If an edge is detected, then the IRR is set to 1
90 */
Gleb Natapov49256632009-02-04 17:28:14 +020091static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030092{
Gleb Natapov49256632009-02-04 17:28:14 +020093 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030094 mask = 1 << irq;
95 if (s->elcr & mask) /* level triggered */
96 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020097 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030098 s->irr |= mask;
99 s->last_irr |= mask;
100 } else {
101 s->irr &= ~mask;
102 s->last_irr &= ~mask;
103 }
104 else /* edge triggered */
105 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200106 if ((s->last_irr & mask) == 0) {
107 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300108 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200109 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300110 s->last_irr |= mask;
111 } else
112 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200113
114 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300115}
116
117/*
118 * return the highest priority found in mask (highest = smallest
119 * number). Return 8 if no irq
120 */
121static inline int get_priority(struct kvm_kpic_state *s, int mask)
122{
123 int priority;
124 if (mask == 0)
125 return 8;
126 priority = 0;
127 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
128 priority++;
129 return priority;
130}
131
132/*
133 * return the pic wanted interrupt. return -1 if none
134 */
135static int pic_get_irq(struct kvm_kpic_state *s)
136{
137 int mask, cur_priority, priority;
138
139 mask = s->irr & ~s->imr;
140 priority = get_priority(s, mask);
141 if (priority == 8)
142 return -1;
143 /*
144 * compute current priority. If special fully nested mode on the
145 * master, the IRQ coming from the slave is not taken into account
146 * for the priority computation.
147 */
148 mask = s->isr;
149 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
150 mask &= ~(1 << 2);
151 cur_priority = get_priority(s, mask);
152 if (priority < cur_priority)
153 /*
154 * higher priority found: an irq should be generated
155 */
156 return (priority + s->priority_add) & 7;
157 else
158 return -1;
159}
160
161/*
162 * raise irq to CPU if necessary. must be called every time the active
163 * irq may change
164 */
165static void pic_update_irq(struct kvm_pic *s)
166{
167 int irq2, irq;
168
169 irq2 = pic_get_irq(&s->pics[1]);
170 if (irq2 >= 0) {
171 /*
172 * if irq request by slave pic, signal master PIC
173 */
174 pic_set_irq1(&s->pics[0], 2, 1);
175 pic_set_irq1(&s->pics[0], 2, 0);
176 }
177 irq = pic_get_irq(&s->pics[0]);
Avi Kivity36633f32010-05-03 17:38:06 +0300178 pic_irq_request(s->kvm, irq >= 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300179}
180
He, Qing6ceb9d72007-07-26 11:05:18 +0300181void kvm_pic_update_irq(struct kvm_pic *s)
182{
Jan Kiszka50a085b2010-02-24 10:41:58 +0100183 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300184 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100185 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300186}
187
Gleb Natapov49256632009-02-04 17:28:14 +0200188int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300189{
190 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200191 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300192
Jan Kiszka50a085b2010-02-24 10:41:58 +0100193 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300194 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200195 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300196 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300197 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
198 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300199 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100200 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200201
202 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300203}
204
205/*
206 * acknowledge interrupt 'irq'
207 */
208static inline void pic_intack(struct kvm_kpic_state *s, int irq)
209{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300210 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300211 /*
212 * We don't clear a level sensitive interrupt here
213 */
214 if (!(s->elcr & (1 << irq)))
215 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300216
217 if (s->auto_eoi) {
218 if (s->rotate_on_auto_eoi)
219 s->priority_add = (irq + 1) & 7;
220 pic_clear_isr(s, irq);
221 }
222
Eddie Dong85f455f2007-07-06 12:20:49 +0300223}
224
Marcelo Tosattif5244722008-07-26 17:01:00 -0300225int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300226{
227 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300228 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300229
Jan Kiszka50a085b2010-02-24 10:41:58 +0100230 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300231 irq = pic_get_irq(&s->pics[0]);
232 if (irq >= 0) {
233 pic_intack(&s->pics[0], irq);
234 if (irq == 2) {
235 irq2 = pic_get_irq(&s->pics[1]);
236 if (irq2 >= 0)
237 pic_intack(&s->pics[1], irq2);
238 else
239 /*
240 * spurious IRQ on slave controller
241 */
242 irq2 = 7;
243 intno = s->pics[1].irq_base + irq2;
244 irq = irq2 + 8;
245 } else
246 intno = s->pics[0].irq_base + irq;
247 } else {
248 /*
249 * spurious IRQ on host controller
250 */
251 irq = 7;
252 intno = s->pics[0].irq_base + irq;
253 }
254 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100255 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300256
257 return intno;
258}
259
Eddie Dong2fcceae2007-10-10 12:14:25 +0200260void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300261{
Gleb Natapov79c727d2009-08-24 11:54:18 +0300262 int irq;
Avi Kivity073d4612010-05-03 17:34:34 +0300263 struct kvm_vcpu *vcpu0 = s->pics_state->kvm->bsp_vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300264 u8 irr = s->irr, isr = s->imr;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300265
Eddie Dong85f455f2007-07-06 12:20:49 +0300266 s->last_irr = 0;
267 s->irr = 0;
268 s->imr = 0;
269 s->isr = 0;
270 s->priority_add = 0;
271 s->irq_base = 0;
272 s->read_reg_select = 0;
273 s->poll = 0;
274 s->special_mask = 0;
275 s->init_state = 0;
276 s->auto_eoi = 0;
277 s->rotate_on_auto_eoi = 0;
278 s->special_fully_nested_mode = 0;
279 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300280
281 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
282 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
283 if (irr & (1 << irq) || isr & (1 << irq)) {
284 pic_clear_isr(s, irq);
285 }
286 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300287}
288
289static void pic_ioport_write(void *opaque, u32 addr, u32 val)
290{
291 struct kvm_kpic_state *s = opaque;
292 int priority, cmd, irq;
293
294 addr &= 1;
295 if (addr == 0) {
296 if (val & 0x10) {
Eddie Dong85f455f2007-07-06 12:20:49 +0300297 s->init4 = val & 1;
Avi Kivity9ed049c2010-08-30 12:18:24 +0300298 s->last_irr = 0;
299 s->imr = 0;
300 s->priority_add = 0;
301 s->special_mask = 0;
302 s->read_reg_select = 0;
303 if (!s->init4) {
304 s->special_fully_nested_mode = 0;
305 s->auto_eoi = 0;
306 }
307 s->init_state = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300308 if (val & 0x02)
309 printk(KERN_ERR "single mode not supported");
310 if (val & 0x08)
311 printk(KERN_ERR
312 "level sensitive irq not supported");
313 } else if (val & 0x08) {
314 if (val & 0x04)
315 s->poll = 1;
316 if (val & 0x02)
317 s->read_reg_select = val & 1;
318 if (val & 0x40)
319 s->special_mask = (val >> 5) & 1;
320 } else {
321 cmd = val >> 5;
322 switch (cmd) {
323 case 0:
324 case 4:
325 s->rotate_on_auto_eoi = cmd >> 2;
326 break;
327 case 1: /* end of interrupt */
328 case 5:
329 priority = get_priority(s, s->isr);
330 if (priority != 8) {
331 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300332 if (cmd == 5)
333 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300334 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300335 pic_update_irq(s->pics_state);
336 }
337 break;
338 case 3:
339 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300340 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300341 pic_update_irq(s->pics_state);
342 break;
343 case 6:
344 s->priority_add = (val + 1) & 7;
345 pic_update_irq(s->pics_state);
346 break;
347 case 7:
348 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300349 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300350 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300351 pic_update_irq(s->pics_state);
352 break;
353 default:
354 break; /* no operation */
355 }
356 }
357 } else
358 switch (s->init_state) {
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300359 case 0: { /* normal mode */
360 u8 imr_diff = s->imr ^ val,
361 off = (s == &s->pics_state->pics[0]) ? 0 : 8;
Eddie Dong85f455f2007-07-06 12:20:49 +0300362 s->imr = val;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300363 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
364 if (imr_diff & (1 << irq))
365 kvm_fire_mask_notifiers(
366 s->pics_state->kvm,
367 SELECT_PIC(irq + off),
368 irq + off,
369 !!(s->imr & (1 << irq)));
Eddie Dong85f455f2007-07-06 12:20:49 +0300370 pic_update_irq(s->pics_state);
371 break;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300372 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300373 case 1:
374 s->irq_base = val & 0xf8;
375 s->init_state = 2;
376 break;
377 case 2:
378 if (s->init4)
379 s->init_state = 3;
380 else
381 s->init_state = 0;
382 break;
383 case 3:
384 s->special_fully_nested_mode = (val >> 4) & 1;
385 s->auto_eoi = (val >> 1) & 1;
386 s->init_state = 0;
387 break;
388 }
389}
390
391static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
392{
393 int ret;
394
395 ret = pic_get_irq(s);
396 if (ret >= 0) {
397 if (addr1 >> 7) {
398 s->pics_state->pics[0].isr &= ~(1 << 2);
399 s->pics_state->pics[0].irr &= ~(1 << 2);
400 }
401 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300402 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300403 if (addr1 >> 7 || ret != 2)
404 pic_update_irq(s->pics_state);
405 } else {
406 ret = 0x07;
407 pic_update_irq(s->pics_state);
408 }
409
410 return ret;
411}
412
413static u32 pic_ioport_read(void *opaque, u32 addr1)
414{
415 struct kvm_kpic_state *s = opaque;
416 unsigned int addr;
417 int ret;
418
419 addr = addr1;
420 addr &= 1;
421 if (s->poll) {
422 ret = pic_poll_read(s, addr1);
423 s->poll = 0;
424 } else
425 if (addr == 0)
426 if (s->read_reg_select)
427 ret = s->isr;
428 else
429 ret = s->irr;
430 else
431 ret = s->imr;
432 return ret;
433}
434
435static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
436{
437 struct kvm_kpic_state *s = opaque;
438 s->elcr = val & s->elcr_mask;
439}
440
441static u32 elcr_ioport_read(void *opaque, u32 addr1)
442{
443 struct kvm_kpic_state *s = opaque;
444 return s->elcr;
445}
446
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300447static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300448{
449 switch (addr) {
450 case 0x20:
451 case 0x21:
452 case 0xa0:
453 case 0xa1:
454 case 0x4d0:
455 case 0x4d1:
456 return 1;
457 default:
458 return 0;
459 }
460}
461
Sasha Levin743eeb02011-07-27 16:00:48 +0300462static int picdev_write(struct kvm_pic *s,
Eddie Dong85f455f2007-07-06 12:20:49 +0300463 gpa_t addr, int len, const void *val)
464{
Eddie Dong85f455f2007-07-06 12:20:49 +0300465 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300466 if (!picdev_in_range(addr))
467 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300468
469 if (len != 1) {
470 if (printk_ratelimit())
471 printk(KERN_ERR "PIC: non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300472 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300473 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100474 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300475 switch (addr) {
476 case 0x20:
477 case 0x21:
478 case 0xa0:
479 case 0xa1:
480 pic_ioport_write(&s->pics[addr >> 7], addr, data);
481 break;
482 case 0x4d0:
483 case 0x4d1:
484 elcr_ioport_write(&s->pics[addr & 1], addr, data);
485 break;
486 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100487 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300488 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300489}
490
Sasha Levin743eeb02011-07-27 16:00:48 +0300491static int picdev_read(struct kvm_pic *s,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300492 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300493{
Eddie Dong85f455f2007-07-06 12:20:49 +0300494 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300495 if (!picdev_in_range(addr))
496 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300497
498 if (len != 1) {
499 if (printk_ratelimit())
500 printk(KERN_ERR "PIC: non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300501 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300502 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100503 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300504 switch (addr) {
505 case 0x20:
506 case 0x21:
507 case 0xa0:
508 case 0xa1:
509 data = pic_ioport_read(&s->pics[addr >> 7], addr);
510 break;
511 case 0x4d0:
512 case 0x4d1:
513 data = elcr_ioport_read(&s->pics[addr & 1], addr);
514 break;
515 }
516 *(unsigned char *)val = data;
Jan Kiszka50a085b2010-02-24 10:41:58 +0100517 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300518 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300519}
520
Sasha Levin743eeb02011-07-27 16:00:48 +0300521static int picdev_master_write(struct kvm_io_device *dev,
522 gpa_t addr, int len, const void *val)
523{
524 return picdev_write(container_of(dev, struct kvm_pic, dev_master),
525 addr, len, val);
526}
527
528static int picdev_master_read(struct kvm_io_device *dev,
529 gpa_t addr, int len, void *val)
530{
531 return picdev_read(container_of(dev, struct kvm_pic, dev_master),
532 addr, len, val);
533}
534
535static int picdev_slave_write(struct kvm_io_device *dev,
536 gpa_t addr, int len, const void *val)
537{
538 return picdev_write(container_of(dev, struct kvm_pic, dev_slave),
539 addr, len, val);
540}
541
542static int picdev_slave_read(struct kvm_io_device *dev,
543 gpa_t addr, int len, void *val)
544{
545 return picdev_read(container_of(dev, struct kvm_pic, dev_slave),
546 addr, len, val);
547}
548
549static int picdev_eclr_write(struct kvm_io_device *dev,
550 gpa_t addr, int len, const void *val)
551{
552 return picdev_write(container_of(dev, struct kvm_pic, dev_eclr),
553 addr, len, val);
554}
555
556static int picdev_eclr_read(struct kvm_io_device *dev,
557 gpa_t addr, int len, void *val)
558{
559 return picdev_read(container_of(dev, struct kvm_pic, dev_eclr),
560 addr, len, val);
561}
562
Eddie Dong85f455f2007-07-06 12:20:49 +0300563/*
564 * callback when PIC0 irq status changed
565 */
Avi Kivity073d4612010-05-03 17:34:34 +0300566static void pic_irq_request(struct kvm *kvm, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300567{
Marcelo Tosattie4825802008-09-24 20:28:34 -0300568 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300569
Gleb Natapov70494672011-02-09 12:09:46 +0200570 if (!s->output)
Jan Kiszka50a085b2010-02-24 10:41:58 +0100571 s->wakeup_needed = true;
Gleb Natapov70494672011-02-09 12:09:46 +0200572 s->output = level;
Eddie Dong85f455f2007-07-06 12:20:49 +0300573}
574
Sasha Levin743eeb02011-07-27 16:00:48 +0300575static const struct kvm_io_device_ops picdev_master_ops = {
576 .read = picdev_master_read,
577 .write = picdev_master_write,
578};
579
580static const struct kvm_io_device_ops picdev_slave_ops = {
581 .read = picdev_slave_read,
582 .write = picdev_slave_write,
583};
584
585static const struct kvm_io_device_ops picdev_eclr_ops = {
586 .read = picdev_eclr_read,
587 .write = picdev_eclr_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400588};
589
Eddie Dong85f455f2007-07-06 12:20:49 +0300590struct kvm_pic *kvm_create_pic(struct kvm *kvm)
591{
592 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400593 int ret;
594
Eddie Dong85f455f2007-07-06 12:20:49 +0300595 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
596 if (!s)
597 return NULL;
Avi Kivityf4f51052010-09-19 18:44:07 +0200598 spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200599 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300600 s->pics[0].elcr_mask = 0xf8;
601 s->pics[1].elcr_mask = 0xde;
Eddie Dong85f455f2007-07-06 12:20:49 +0300602 s->pics[0].pics_state = s;
603 s->pics[1].pics_state = s;
604
605 /*
606 * Initialize PIO device
607 */
Sasha Levin743eeb02011-07-27 16:00:48 +0300608 kvm_iodevice_init(&s->dev_master, &picdev_master_ops);
609 kvm_iodevice_init(&s->dev_slave, &picdev_slave_ops);
610 kvm_iodevice_init(&s->dev_eclr, &picdev_eclr_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200611 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300612 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,
613 &s->dev_master);
614 if (ret < 0)
615 goto fail_unlock;
616
617 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2, &s->dev_slave);
618 if (ret < 0)
619 goto fail_unreg_2;
620
621 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2, &s->dev_eclr);
622 if (ret < 0)
623 goto fail_unreg_1;
624
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200625 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400626
Eddie Dong85f455f2007-07-06 12:20:49 +0300627 return s;
Sasha Levin743eeb02011-07-27 16:00:48 +0300628
629fail_unreg_1:
630 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_slave);
631
632fail_unreg_2:
633 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_master);
634
635fail_unlock:
636 mutex_unlock(&kvm->slots_lock);
637
638 kfree(s);
639
640 return NULL;
Eddie Dong85f455f2007-07-06 12:20:49 +0300641}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800642
643void kvm_destroy_pic(struct kvm *kvm)
644{
645 struct kvm_pic *vpic = kvm->arch.vpic;
646
647 if (vpic) {
Sasha Levin743eeb02011-07-27 16:00:48 +0300648 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_master);
649 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_slave);
650 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_eclr);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800651 kvm->arch.vpic = NULL;
652 kfree(vpic);
653 }
654}