blob: b6a73537e1efd2270a00d52612342ed168081b55 [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
Jan Kiszkabd801582011-09-12 11:26:22 +020037#define pr_pic_unimpl(fmt, ...) \
38 pr_err_ratelimited("kvm: pic: " fmt, ## __VA_ARGS__)
39
Avi Kivity073d4612010-05-03 17:34:34 +030040static void pic_irq_request(struct kvm *kvm, int level);
41
Jan Kiszka50a085b2010-02-24 10:41:58 +010042static void pic_lock(struct kvm_pic *s)
43 __acquires(&s->lock)
44{
Avi Kivityf4f51052010-09-19 18:44:07 +020045 spin_lock(&s->lock);
Jan Kiszka50a085b2010-02-24 10:41:58 +010046}
47
48static void pic_unlock(struct kvm_pic *s)
49 __releases(&s->lock)
50{
51 bool wakeup = s->wakeup_needed;
Chris Lalancette529df652010-06-21 11:29:40 -040052 struct kvm_vcpu *vcpu, *found = NULL;
53 int i;
Jan Kiszka50a085b2010-02-24 10:41:58 +010054
55 s->wakeup_needed = false;
56
Avi Kivityf4f51052010-09-19 18:44:07 +020057 spin_unlock(&s->lock);
Jan Kiszka50a085b2010-02-24 10:41:58 +010058
59 if (wakeup) {
Chris Lalancette529df652010-06-21 11:29:40 -040060 kvm_for_each_vcpu(i, vcpu, s->kvm) {
61 if (kvm_apic_accept_pic_intr(vcpu)) {
62 found = vcpu;
63 break;
64 }
65 }
66
67 if (!found)
Avi Kivityae0635b2010-07-27 14:51:44 +030068 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);
Gleb Natapov938396a2009-08-04 15:30:28 +030078 if (s != &s->pics_state->pics[0])
79 irq += 8;
Gleb Natapoveba02262009-08-24 11:54:25 +030080 /*
81 * We are dropping lock while calling ack notifiers since ack
82 * notifier callbacks for assigned devices call into PIC recursively.
83 * Other interrupt may be delivered to PIC while lock is dropped but
84 * it should be safe since PIC state is already updated at this stage.
85 */
Jan Kiszka50a085b2010-02-24 10:41:58 +010086 pic_unlock(s->pics_state);
Gleb Natapov938396a2009-08-04 15:30:28 +030087 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +010088 pic_lock(s->pics_state);
Marcelo Tosattie4825802008-09-24 20:28:34 -030089}
90
Eddie Dong85f455f2007-07-06 12:20:49 +030091/*
92 * set irq level. If an edge is detected, then the IRR is set to 1
93 */
Gleb Natapov49256632009-02-04 17:28:14 +020094static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030095{
Gleb Natapov49256632009-02-04 17:28:14 +020096 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030097 mask = 1 << irq;
98 if (s->elcr & mask) /* level triggered */
99 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200100 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300101 s->irr |= mask;
102 s->last_irr |= mask;
103 } else {
104 s->irr &= ~mask;
105 s->last_irr &= ~mask;
106 }
107 else /* edge triggered */
108 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200109 if ((s->last_irr & mask) == 0) {
110 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300111 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200112 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300113 s->last_irr |= mask;
114 } else
115 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200116
117 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300118}
119
120/*
121 * return the highest priority found in mask (highest = smallest
122 * number). Return 8 if no irq
123 */
124static inline int get_priority(struct kvm_kpic_state *s, int mask)
125{
126 int priority;
127 if (mask == 0)
128 return 8;
129 priority = 0;
130 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
131 priority++;
132 return priority;
133}
134
135/*
136 * return the pic wanted interrupt. return -1 if none
137 */
138static int pic_get_irq(struct kvm_kpic_state *s)
139{
140 int mask, cur_priority, priority;
141
142 mask = s->irr & ~s->imr;
143 priority = get_priority(s, mask);
144 if (priority == 8)
145 return -1;
146 /*
147 * compute current priority. If special fully nested mode on the
148 * master, the IRQ coming from the slave is not taken into account
149 * for the priority computation.
150 */
151 mask = s->isr;
152 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
153 mask &= ~(1 << 2);
154 cur_priority = get_priority(s, mask);
155 if (priority < cur_priority)
156 /*
157 * higher priority found: an irq should be generated
158 */
159 return (priority + s->priority_add) & 7;
160 else
161 return -1;
162}
163
164/*
165 * raise irq to CPU if necessary. must be called every time the active
166 * irq may change
167 */
168static void pic_update_irq(struct kvm_pic *s)
169{
170 int irq2, irq;
171
172 irq2 = pic_get_irq(&s->pics[1]);
173 if (irq2 >= 0) {
174 /*
175 * if irq request by slave pic, signal master PIC
176 */
177 pic_set_irq1(&s->pics[0], 2, 1);
178 pic_set_irq1(&s->pics[0], 2, 0);
179 }
180 irq = pic_get_irq(&s->pics[0]);
Avi Kivity36633f32010-05-03 17:38:06 +0300181 pic_irq_request(s->kvm, irq >= 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300182}
183
He, Qing6ceb9d72007-07-26 11:05:18 +0300184void kvm_pic_update_irq(struct kvm_pic *s)
185{
Jan Kiszka50a085b2010-02-24 10:41:58 +0100186 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300187 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100188 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300189}
190
Gleb Natapov49256632009-02-04 17:28:14 +0200191int kvm_pic_set_irq(void *opaque, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300192{
193 struct kvm_pic *s = opaque;
Gleb Natapov49256632009-02-04 17:28:14 +0200194 int ret = -1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300195
Jan Kiszka50a085b2010-02-24 10:41:58 +0100196 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300197 if (irq >= 0 && irq < PIC_NUM_PINS) {
Gleb Natapov49256632009-02-04 17:28:14 +0200198 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300199 pic_update_irq(s);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300200 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
201 s->pics[irq >> 3].imr, ret == 0);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300202 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100203 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200204
205 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300206}
207
208/*
209 * acknowledge interrupt 'irq'
210 */
211static inline void pic_intack(struct kvm_kpic_state *s, int irq)
212{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300213 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300214 /*
215 * We don't clear a level sensitive interrupt here
216 */
217 if (!(s->elcr & (1 << irq)))
218 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300219
220 if (s->auto_eoi) {
221 if (s->rotate_on_auto_eoi)
222 s->priority_add = (irq + 1) & 7;
223 pic_clear_isr(s, irq);
224 }
225
Eddie Dong85f455f2007-07-06 12:20:49 +0300226}
227
Marcelo Tosattif5244722008-07-26 17:01:00 -0300228int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300229{
230 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300231 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300232
Jan Kiszka50a085b2010-02-24 10:41:58 +0100233 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300234 irq = pic_get_irq(&s->pics[0]);
235 if (irq >= 0) {
236 pic_intack(&s->pics[0], irq);
237 if (irq == 2) {
238 irq2 = pic_get_irq(&s->pics[1]);
239 if (irq2 >= 0)
240 pic_intack(&s->pics[1], irq2);
241 else
242 /*
243 * spurious IRQ on slave controller
244 */
245 irq2 = 7;
246 intno = s->pics[1].irq_base + irq2;
247 irq = irq2 + 8;
248 } else
249 intno = s->pics[0].irq_base + irq;
250 } else {
251 /*
252 * spurious IRQ on host controller
253 */
254 irq = 7;
255 intno = s->pics[0].irq_base + irq;
256 }
257 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100258 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300259
260 return intno;
261}
262
Eddie Dong2fcceae2007-10-10 12:14:25 +0200263void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300264{
Gleb Natapovd546cb42011-12-15 12:38:40 +0200265 int irq, i;
266 struct kvm_vcpu *vcpu;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300267 u8 irr = s->irr, isr = s->imr;
Gleb Natapovd546cb42011-12-15 12:38:40 +0200268 bool found = false;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300269
Eddie Dong85f455f2007-07-06 12:20:49 +0300270 s->last_irr = 0;
271 s->irr = 0;
272 s->imr = 0;
273 s->isr = 0;
274 s->priority_add = 0;
275 s->irq_base = 0;
276 s->read_reg_select = 0;
277 s->poll = 0;
278 s->special_mask = 0;
279 s->init_state = 0;
280 s->auto_eoi = 0;
281 s->rotate_on_auto_eoi = 0;
282 s->special_fully_nested_mode = 0;
283 s->init4 = 0;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300284
Gleb Natapovd546cb42011-12-15 12:38:40 +0200285 kvm_for_each_vcpu(i, vcpu, s->pics_state->kvm)
286 if (kvm_apic_accept_pic_intr(vcpu)) {
287 found = true;
288 break;
289 }
290
291
292 if (!found)
293 return;
294
295 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
296 if (irr & (1 << irq) || isr & (1 << irq))
297 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300298}
299
300static void pic_ioport_write(void *opaque, u32 addr, u32 val)
301{
302 struct kvm_kpic_state *s = opaque;
303 int priority, cmd, irq;
304
305 addr &= 1;
306 if (addr == 0) {
307 if (val & 0x10) {
Eddie Dong85f455f2007-07-06 12:20:49 +0300308 s->init4 = val & 1;
Avi Kivity9ed049c2010-08-30 12:18:24 +0300309 s->last_irr = 0;
310 s->imr = 0;
311 s->priority_add = 0;
312 s->special_mask = 0;
313 s->read_reg_select = 0;
314 if (!s->init4) {
315 s->special_fully_nested_mode = 0;
316 s->auto_eoi = 0;
317 }
318 s->init_state = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +0300319 if (val & 0x02)
Jan Kiszkabd801582011-09-12 11:26:22 +0200320 pr_pic_unimpl("single mode not supported");
Eddie Dong85f455f2007-07-06 12:20:49 +0300321 if (val & 0x08)
Jan Kiszkabd801582011-09-12 11:26:22 +0200322 pr_pic_unimpl(
323 "level sensitive irq not supported");
Eddie Dong85f455f2007-07-06 12:20:49 +0300324 } else if (val & 0x08) {
325 if (val & 0x04)
326 s->poll = 1;
327 if (val & 0x02)
328 s->read_reg_select = val & 1;
329 if (val & 0x40)
330 s->special_mask = (val >> 5) & 1;
331 } else {
332 cmd = val >> 5;
333 switch (cmd) {
334 case 0:
335 case 4:
336 s->rotate_on_auto_eoi = cmd >> 2;
337 break;
338 case 1: /* end of interrupt */
339 case 5:
340 priority = get_priority(s, s->isr);
341 if (priority != 8) {
342 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300343 if (cmd == 5)
344 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300345 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300346 pic_update_irq(s->pics_state);
347 }
348 break;
349 case 3:
350 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300351 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300352 pic_update_irq(s->pics_state);
353 break;
354 case 6:
355 s->priority_add = (val + 1) & 7;
356 pic_update_irq(s->pics_state);
357 break;
358 case 7:
359 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300360 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300361 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300362 pic_update_irq(s->pics_state);
363 break;
364 default:
365 break; /* no operation */
366 }
367 }
368 } else
369 switch (s->init_state) {
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300370 case 0: { /* normal mode */
371 u8 imr_diff = s->imr ^ val,
372 off = (s == &s->pics_state->pics[0]) ? 0 : 8;
Eddie Dong85f455f2007-07-06 12:20:49 +0300373 s->imr = val;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300374 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
375 if (imr_diff & (1 << irq))
376 kvm_fire_mask_notifiers(
377 s->pics_state->kvm,
378 SELECT_PIC(irq + off),
379 irq + off,
380 !!(s->imr & (1 << irq)));
Eddie Dong85f455f2007-07-06 12:20:49 +0300381 pic_update_irq(s->pics_state);
382 break;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300383 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300384 case 1:
385 s->irq_base = val & 0xf8;
386 s->init_state = 2;
387 break;
388 case 2:
389 if (s->init4)
390 s->init_state = 3;
391 else
392 s->init_state = 0;
393 break;
394 case 3:
395 s->special_fully_nested_mode = (val >> 4) & 1;
396 s->auto_eoi = (val >> 1) & 1;
397 s->init_state = 0;
398 break;
399 }
400}
401
402static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
403{
404 int ret;
405
406 ret = pic_get_irq(s);
407 if (ret >= 0) {
408 if (addr1 >> 7) {
409 s->pics_state->pics[0].isr &= ~(1 << 2);
410 s->pics_state->pics[0].irr &= ~(1 << 2);
411 }
412 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300413 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300414 if (addr1 >> 7 || ret != 2)
415 pic_update_irq(s->pics_state);
416 } else {
417 ret = 0x07;
418 pic_update_irq(s->pics_state);
419 }
420
421 return ret;
422}
423
424static u32 pic_ioport_read(void *opaque, u32 addr1)
425{
426 struct kvm_kpic_state *s = opaque;
427 unsigned int addr;
428 int ret;
429
430 addr = addr1;
431 addr &= 1;
432 if (s->poll) {
433 ret = pic_poll_read(s, addr1);
434 s->poll = 0;
435 } else
436 if (addr == 0)
437 if (s->read_reg_select)
438 ret = s->isr;
439 else
440 ret = s->irr;
441 else
442 ret = s->imr;
443 return ret;
444}
445
446static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
447{
448 struct kvm_kpic_state *s = opaque;
449 s->elcr = val & s->elcr_mask;
450}
451
452static u32 elcr_ioport_read(void *opaque, u32 addr1)
453{
454 struct kvm_kpic_state *s = opaque;
455 return s->elcr;
456}
457
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300458static int picdev_in_range(gpa_t addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300459{
460 switch (addr) {
461 case 0x20:
462 case 0x21:
463 case 0xa0:
464 case 0xa1:
465 case 0x4d0:
466 case 0x4d1:
467 return 1;
468 default:
469 return 0;
470 }
471}
472
Sasha Levin743eeb02011-07-27 16:00:48 +0300473static int picdev_write(struct kvm_pic *s,
Eddie Dong85f455f2007-07-06 12:20:49 +0300474 gpa_t addr, int len, const void *val)
475{
Eddie Dong85f455f2007-07-06 12:20:49 +0300476 unsigned char data = *(unsigned char *)val;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300477 if (!picdev_in_range(addr))
478 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300479
480 if (len != 1) {
Jan Kiszkabd801582011-09-12 11:26:22 +0200481 pr_pic_unimpl("non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300482 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300483 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100484 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300485 switch (addr) {
486 case 0x20:
487 case 0x21:
488 case 0xa0:
489 case 0xa1:
490 pic_ioport_write(&s->pics[addr >> 7], addr, data);
491 break;
492 case 0x4d0:
493 case 0x4d1:
494 elcr_ioport_write(&s->pics[addr & 1], addr, data);
495 break;
496 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100497 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300498 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300499}
500
Sasha Levin743eeb02011-07-27 16:00:48 +0300501static int picdev_read(struct kvm_pic *s,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300502 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300503{
Eddie Dong85f455f2007-07-06 12:20:49 +0300504 unsigned char data = 0;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300505 if (!picdev_in_range(addr))
506 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300507
508 if (len != 1) {
Jan Kiszkabd801582011-09-12 11:26:22 +0200509 pr_pic_unimpl("non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300510 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300511 }
Jan Kiszka50a085b2010-02-24 10:41:58 +0100512 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300513 switch (addr) {
514 case 0x20:
515 case 0x21:
516 case 0xa0:
517 case 0xa1:
518 data = pic_ioport_read(&s->pics[addr >> 7], addr);
519 break;
520 case 0x4d0:
521 case 0x4d1:
522 data = elcr_ioport_read(&s->pics[addr & 1], addr);
523 break;
524 }
525 *(unsigned char *)val = data;
Jan Kiszka50a085b2010-02-24 10:41:58 +0100526 pic_unlock(s);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300527 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300528}
529
Sasha Levin743eeb02011-07-27 16:00:48 +0300530static int picdev_master_write(struct kvm_io_device *dev,
531 gpa_t addr, int len, const void *val)
532{
533 return picdev_write(container_of(dev, struct kvm_pic, dev_master),
534 addr, len, val);
535}
536
537static int picdev_master_read(struct kvm_io_device *dev,
538 gpa_t addr, int len, void *val)
539{
540 return picdev_read(container_of(dev, struct kvm_pic, dev_master),
541 addr, len, val);
542}
543
544static int picdev_slave_write(struct kvm_io_device *dev,
545 gpa_t addr, int len, const void *val)
546{
547 return picdev_write(container_of(dev, struct kvm_pic, dev_slave),
548 addr, len, val);
549}
550
551static int picdev_slave_read(struct kvm_io_device *dev,
552 gpa_t addr, int len, void *val)
553{
554 return picdev_read(container_of(dev, struct kvm_pic, dev_slave),
555 addr, len, val);
556}
557
558static int picdev_eclr_write(struct kvm_io_device *dev,
559 gpa_t addr, int len, const void *val)
560{
561 return picdev_write(container_of(dev, struct kvm_pic, dev_eclr),
562 addr, len, val);
563}
564
565static int picdev_eclr_read(struct kvm_io_device *dev,
566 gpa_t addr, int len, void *val)
567{
568 return picdev_read(container_of(dev, struct kvm_pic, dev_eclr),
569 addr, len, val);
570}
571
Eddie Dong85f455f2007-07-06 12:20:49 +0300572/*
573 * callback when PIC0 irq status changed
574 */
Avi Kivity073d4612010-05-03 17:34:34 +0300575static void pic_irq_request(struct kvm *kvm, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300576{
Marcelo Tosattie4825802008-09-24 20:28:34 -0300577 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300578
Gleb Natapov70494672011-02-09 12:09:46 +0200579 if (!s->output)
Jan Kiszka50a085b2010-02-24 10:41:58 +0100580 s->wakeup_needed = true;
Gleb Natapov70494672011-02-09 12:09:46 +0200581 s->output = level;
Eddie Dong85f455f2007-07-06 12:20:49 +0300582}
583
Sasha Levin743eeb02011-07-27 16:00:48 +0300584static const struct kvm_io_device_ops picdev_master_ops = {
585 .read = picdev_master_read,
586 .write = picdev_master_write,
587};
588
589static const struct kvm_io_device_ops picdev_slave_ops = {
590 .read = picdev_slave_read,
591 .write = picdev_slave_write,
592};
593
594static const struct kvm_io_device_ops picdev_eclr_ops = {
595 .read = picdev_eclr_read,
596 .write = picdev_eclr_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400597};
598
Eddie Dong85f455f2007-07-06 12:20:49 +0300599struct kvm_pic *kvm_create_pic(struct kvm *kvm)
600{
601 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400602 int ret;
603
Eddie Dong85f455f2007-07-06 12:20:49 +0300604 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
605 if (!s)
606 return NULL;
Avi Kivityf4f51052010-09-19 18:44:07 +0200607 spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200608 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300609 s->pics[0].elcr_mask = 0xf8;
610 s->pics[1].elcr_mask = 0xde;
Eddie Dong85f455f2007-07-06 12:20:49 +0300611 s->pics[0].pics_state = s;
612 s->pics[1].pics_state = s;
613
614 /*
615 * Initialize PIO device
616 */
Sasha Levin743eeb02011-07-27 16:00:48 +0300617 kvm_iodevice_init(&s->dev_master, &picdev_master_ops);
618 kvm_iodevice_init(&s->dev_slave, &picdev_slave_ops);
619 kvm_iodevice_init(&s->dev_eclr, &picdev_eclr_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200620 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300621 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,
622 &s->dev_master);
623 if (ret < 0)
624 goto fail_unlock;
625
626 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2, &s->dev_slave);
627 if (ret < 0)
628 goto fail_unreg_2;
629
630 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2, &s->dev_eclr);
631 if (ret < 0)
632 goto fail_unreg_1;
633
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200634 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400635
Eddie Dong85f455f2007-07-06 12:20:49 +0300636 return s;
Sasha Levin743eeb02011-07-27 16:00:48 +0300637
638fail_unreg_1:
639 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_slave);
640
641fail_unreg_2:
642 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_master);
643
644fail_unlock:
645 mutex_unlock(&kvm->slots_lock);
646
647 kfree(s);
648
649 return NULL;
Eddie Dong85f455f2007-07-06 12:20:49 +0300650}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800651
652void kvm_destroy_pic(struct kvm *kvm)
653{
654 struct kvm_pic *vpic = kvm->arch.vpic;
655
656 if (vpic) {
Sasha Levin743eeb02011-07-27 16:00:48 +0300657 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_master);
658 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_slave);
659 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_eclr);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800660 kvm->arch.vpic = NULL;
661 kfree(vpic);
662 }
663}