blob: 93160375c8410ed426be0a1fb86fad5e76056214 [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>
Eddie Dong85f455f2007-07-06 12:20:49 +030033
Avi Kivity3f353852008-12-21 22:48:32 +020034static void pic_lock(struct kvm_pic *s)
35{
36 spin_lock(&s->lock);
37}
38
39static void pic_unlock(struct kvm_pic *s)
40{
41 struct kvm *kvm = s->kvm;
42 unsigned acks = s->pending_acks;
43 bool wakeup = s->wakeup_needed;
44 struct kvm_vcpu *vcpu;
45
46 s->pending_acks = 0;
47 s->wakeup_needed = false;
48
49 spin_unlock(&s->lock);
50
51 while (acks) {
Marcelo Tosatti44882ee2009-01-27 15:12:38 -020052 kvm_notify_acked_irq(kvm, SELECT_PIC(__ffs(acks)),
53 __ffs(acks));
Avi Kivity3f353852008-12-21 22:48:32 +020054 acks &= acks - 1;
55 }
56
57 if (wakeup) {
58 vcpu = s->kvm->vcpus[0];
59 if (vcpu)
60 kvm_vcpu_kick(vcpu);
61 }
62}
63
Avi Kivity7edd0ce2008-07-07 14:45:39 +030064static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
65{
66 s->isr &= ~(1 << irq);
Marcelo Tosattie4825802008-09-24 20:28:34 -030067 s->isr_ack |= (1 << irq);
68}
69
70void kvm_pic_clear_isr_ack(struct kvm *kvm)
71{
72 struct kvm_pic *s = pic_irqchip(kvm);
73 s->pics[0].isr_ack = 0xff;
74 s->pics[1].isr_ack = 0xff;
Avi Kivity7edd0ce2008-07-07 14:45:39 +030075}
76
Eddie Dong85f455f2007-07-06 12:20:49 +030077/*
78 * set irq level. If an edge is detected, then the IRR is set to 1
79 */
80static inline void pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
81{
82 int mask;
83 mask = 1 << irq;
84 if (s->elcr & mask) /* level triggered */
85 if (level) {
86 s->irr |= mask;
87 s->last_irr |= mask;
88 } else {
89 s->irr &= ~mask;
90 s->last_irr &= ~mask;
91 }
92 else /* edge triggered */
93 if (level) {
94 if ((s->last_irr & mask) == 0)
95 s->irr |= mask;
96 s->last_irr |= mask;
97 } else
98 s->last_irr &= ~mask;
99}
100
101/*
102 * return the highest priority found in mask (highest = smallest
103 * number). Return 8 if no irq
104 */
105static inline int get_priority(struct kvm_kpic_state *s, int mask)
106{
107 int priority;
108 if (mask == 0)
109 return 8;
110 priority = 0;
111 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
112 priority++;
113 return priority;
114}
115
116/*
117 * return the pic wanted interrupt. return -1 if none
118 */
119static int pic_get_irq(struct kvm_kpic_state *s)
120{
121 int mask, cur_priority, priority;
122
123 mask = s->irr & ~s->imr;
124 priority = get_priority(s, mask);
125 if (priority == 8)
126 return -1;
127 /*
128 * compute current priority. If special fully nested mode on the
129 * master, the IRQ coming from the slave is not taken into account
130 * for the priority computation.
131 */
132 mask = s->isr;
133 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
134 mask &= ~(1 << 2);
135 cur_priority = get_priority(s, mask);
136 if (priority < cur_priority)
137 /*
138 * higher priority found: an irq should be generated
139 */
140 return (priority + s->priority_add) & 7;
141 else
142 return -1;
143}
144
145/*
146 * raise irq to CPU if necessary. must be called every time the active
147 * irq may change
148 */
149static void pic_update_irq(struct kvm_pic *s)
150{
151 int irq2, irq;
152
153 irq2 = pic_get_irq(&s->pics[1]);
154 if (irq2 >= 0) {
155 /*
156 * if irq request by slave pic, signal master PIC
157 */
158 pic_set_irq1(&s->pics[0], 2, 1);
159 pic_set_irq1(&s->pics[0], 2, 0);
160 }
161 irq = pic_get_irq(&s->pics[0]);
162 if (irq >= 0)
163 s->irq_request(s->irq_request_opaque, 1);
164 else
165 s->irq_request(s->irq_request_opaque, 0);
166}
167
He, Qing6ceb9d72007-07-26 11:05:18 +0300168void kvm_pic_update_irq(struct kvm_pic *s)
169{
Avi Kivity3f353852008-12-21 22:48:32 +0200170 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300171 pic_update_irq(s);
Avi Kivity3f353852008-12-21 22:48:32 +0200172 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300173}
174
Eddie Dong85f455f2007-07-06 12:20:49 +0300175void kvm_pic_set_irq(void *opaque, int irq, int level)
176{
177 struct kvm_pic *s = opaque;
178
Avi Kivity3f353852008-12-21 22:48:32 +0200179 pic_lock(s);
Ben-Ami Yassourc65bbfa2008-07-06 17:15:07 +0300180 if (irq >= 0 && irq < PIC_NUM_PINS) {
181 pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
182 pic_update_irq(s);
183 }
Avi Kivity3f353852008-12-21 22:48:32 +0200184 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300185}
186
187/*
188 * acknowledge interrupt 'irq'
189 */
190static inline void pic_intack(struct kvm_kpic_state *s, int irq)
191{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300192 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300193 if (s->auto_eoi) {
194 if (s->rotate_on_auto_eoi)
195 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300196 pic_clear_isr(s, irq);
197 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300198 /*
199 * We don't clear a level sensitive interrupt here
200 */
201 if (!(s->elcr & (1 << irq)))
202 s->irr &= ~(1 << irq);
203}
204
Marcelo Tosattif5244722008-07-26 17:01:00 -0300205int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300206{
207 int irq, irq2, intno;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300208 struct kvm_pic *s = pic_irqchip(kvm);
Eddie Dong85f455f2007-07-06 12:20:49 +0300209
Avi Kivity3f353852008-12-21 22:48:32 +0200210 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300211 irq = pic_get_irq(&s->pics[0]);
212 if (irq >= 0) {
213 pic_intack(&s->pics[0], irq);
214 if (irq == 2) {
215 irq2 = pic_get_irq(&s->pics[1]);
216 if (irq2 >= 0)
217 pic_intack(&s->pics[1], irq2);
218 else
219 /*
220 * spurious IRQ on slave controller
221 */
222 irq2 = 7;
223 intno = s->pics[1].irq_base + irq2;
224 irq = irq2 + 8;
225 } else
226 intno = s->pics[0].irq_base + irq;
227 } else {
228 /*
229 * spurious IRQ on host controller
230 */
231 irq = 7;
232 intno = s->pics[0].irq_base + irq;
233 }
234 pic_update_irq(s);
Avi Kivity3f353852008-12-21 22:48:32 +0200235 pic_unlock(s);
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200236 kvm_notify_acked_irq(kvm, SELECT_PIC(irq), irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300237
238 return intno;
239}
240
Eddie Dong2fcceae2007-10-10 12:14:25 +0200241void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300242{
Avi Kivity3f353852008-12-21 22:48:32 +0200243 int irq, irqbase, n;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300244 struct kvm *kvm = s->pics_state->irq_request_opaque;
Marcelo Tosatti85428ac2008-08-14 20:53:25 -0300245 struct kvm_vcpu *vcpu0 = kvm->vcpus[0];
Marcelo Tosattif5244722008-07-26 17:01:00 -0300246
Marcelo Tosatti85428ac2008-08-14 20:53:25 -0300247 if (s == &s->pics_state->pics[0])
248 irqbase = 0;
249 else
250 irqbase = 8;
251
252 for (irq = 0; irq < PIC_NUM_PINS/2; irq++) {
253 if (vcpu0 && kvm_apic_accept_pic_intr(vcpu0))
Avi Kivity3f353852008-12-21 22:48:32 +0200254 if (s->irr & (1 << irq) || s->isr & (1 << irq)) {
255 n = irq + irqbase;
256 s->pics_state->pending_acks |= 1 << n;
257 }
Marcelo Tosattif5244722008-07-26 17:01:00 -0300258 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300259 s->last_irr = 0;
260 s->irr = 0;
261 s->imr = 0;
262 s->isr = 0;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300263 s->isr_ack = 0xff;
Eddie Dong85f455f2007-07-06 12:20:49 +0300264 s->priority_add = 0;
265 s->irq_base = 0;
266 s->read_reg_select = 0;
267 s->poll = 0;
268 s->special_mask = 0;
269 s->init_state = 0;
270 s->auto_eoi = 0;
271 s->rotate_on_auto_eoi = 0;
272 s->special_fully_nested_mode = 0;
273 s->init4 = 0;
274}
275
276static void pic_ioport_write(void *opaque, u32 addr, u32 val)
277{
278 struct kvm_kpic_state *s = opaque;
279 int priority, cmd, irq;
280
281 addr &= 1;
282 if (addr == 0) {
283 if (val & 0x10) {
Eddie Dong2fcceae2007-10-10 12:14:25 +0200284 kvm_pic_reset(s); /* init */
Eddie Dong85f455f2007-07-06 12:20:49 +0300285 /*
286 * deassert a pending interrupt
287 */
288 s->pics_state->irq_request(s->pics_state->
289 irq_request_opaque, 0);
290 s->init_state = 1;
291 s->init4 = val & 1;
292 if (val & 0x02)
293 printk(KERN_ERR "single mode not supported");
294 if (val & 0x08)
295 printk(KERN_ERR
296 "level sensitive irq not supported");
297 } else if (val & 0x08) {
298 if (val & 0x04)
299 s->poll = 1;
300 if (val & 0x02)
301 s->read_reg_select = val & 1;
302 if (val & 0x40)
303 s->special_mask = (val >> 5) & 1;
304 } else {
305 cmd = val >> 5;
306 switch (cmd) {
307 case 0:
308 case 4:
309 s->rotate_on_auto_eoi = cmd >> 2;
310 break;
311 case 1: /* end of interrupt */
312 case 5:
313 priority = get_priority(s, s->isr);
314 if (priority != 8) {
315 irq = (priority + s->priority_add) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300316 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300317 if (cmd == 5)
318 s->priority_add = (irq + 1) & 7;
319 pic_update_irq(s->pics_state);
320 }
321 break;
322 case 3:
323 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300324 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300325 pic_update_irq(s->pics_state);
326 break;
327 case 6:
328 s->priority_add = (val + 1) & 7;
329 pic_update_irq(s->pics_state);
330 break;
331 case 7:
332 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300333 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300334 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300335 pic_update_irq(s->pics_state);
336 break;
337 default:
338 break; /* no operation */
339 }
340 }
341 } else
342 switch (s->init_state) {
343 case 0: /* normal mode */
344 s->imr = val;
345 pic_update_irq(s->pics_state);
346 break;
347 case 1:
348 s->irq_base = val & 0xf8;
349 s->init_state = 2;
350 break;
351 case 2:
352 if (s->init4)
353 s->init_state = 3;
354 else
355 s->init_state = 0;
356 break;
357 case 3:
358 s->special_fully_nested_mode = (val >> 4) & 1;
359 s->auto_eoi = (val >> 1) & 1;
360 s->init_state = 0;
361 break;
362 }
363}
364
365static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
366{
367 int ret;
368
369 ret = pic_get_irq(s);
370 if (ret >= 0) {
371 if (addr1 >> 7) {
372 s->pics_state->pics[0].isr &= ~(1 << 2);
373 s->pics_state->pics[0].irr &= ~(1 << 2);
374 }
375 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300376 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300377 if (addr1 >> 7 || ret != 2)
378 pic_update_irq(s->pics_state);
379 } else {
380 ret = 0x07;
381 pic_update_irq(s->pics_state);
382 }
383
384 return ret;
385}
386
387static u32 pic_ioport_read(void *opaque, u32 addr1)
388{
389 struct kvm_kpic_state *s = opaque;
390 unsigned int addr;
391 int ret;
392
393 addr = addr1;
394 addr &= 1;
395 if (s->poll) {
396 ret = pic_poll_read(s, addr1);
397 s->poll = 0;
398 } else
399 if (addr == 0)
400 if (s->read_reg_select)
401 ret = s->isr;
402 else
403 ret = s->irr;
404 else
405 ret = s->imr;
406 return ret;
407}
408
409static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
410{
411 struct kvm_kpic_state *s = opaque;
412 s->elcr = val & s->elcr_mask;
413}
414
415static u32 elcr_ioport_read(void *opaque, u32 addr1)
416{
417 struct kvm_kpic_state *s = opaque;
418 return s->elcr;
419}
420
Laurent Vivier92760492008-05-30 16:05:53 +0200421static int picdev_in_range(struct kvm_io_device *this, gpa_t addr,
422 int len, int is_write)
Eddie Dong85f455f2007-07-06 12:20:49 +0300423{
424 switch (addr) {
425 case 0x20:
426 case 0x21:
427 case 0xa0:
428 case 0xa1:
429 case 0x4d0:
430 case 0x4d1:
431 return 1;
432 default:
433 return 0;
434 }
435}
436
437static void picdev_write(struct kvm_io_device *this,
438 gpa_t addr, int len, const void *val)
439{
440 struct kvm_pic *s = this->private;
441 unsigned char data = *(unsigned char *)val;
442
443 if (len != 1) {
444 if (printk_ratelimit())
445 printk(KERN_ERR "PIC: non byte write\n");
446 return;
447 }
Avi Kivity3f353852008-12-21 22:48:32 +0200448 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300449 switch (addr) {
450 case 0x20:
451 case 0x21:
452 case 0xa0:
453 case 0xa1:
454 pic_ioport_write(&s->pics[addr >> 7], addr, data);
455 break;
456 case 0x4d0:
457 case 0x4d1:
458 elcr_ioport_write(&s->pics[addr & 1], addr, data);
459 break;
460 }
Avi Kivity3f353852008-12-21 22:48:32 +0200461 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300462}
463
464static void picdev_read(struct kvm_io_device *this,
465 gpa_t addr, int len, void *val)
466{
467 struct kvm_pic *s = this->private;
468 unsigned char data = 0;
469
470 if (len != 1) {
471 if (printk_ratelimit())
472 printk(KERN_ERR "PIC: non byte read\n");
473 return;
474 }
Avi Kivity3f353852008-12-21 22:48:32 +0200475 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300476 switch (addr) {
477 case 0x20:
478 case 0x21:
479 case 0xa0:
480 case 0xa1:
481 data = pic_ioport_read(&s->pics[addr >> 7], addr);
482 break;
483 case 0x4d0:
484 case 0x4d1:
485 data = elcr_ioport_read(&s->pics[addr & 1], addr);
486 break;
487 }
488 *(unsigned char *)val = data;
Avi Kivity3f353852008-12-21 22:48:32 +0200489 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300490}
491
492/*
493 * callback when PIC0 irq status changed
494 */
495static void pic_irq_request(void *opaque, int level)
496{
497 struct kvm *kvm = opaque;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300498 struct kvm_vcpu *vcpu = kvm->vcpus[0];
Marcelo Tosattie4825802008-09-24 20:28:34 -0300499 struct kvm_pic *s = pic_irqchip(kvm);
500 int irq = pic_get_irq(&s->pics[0]);
Eddie Dong85f455f2007-07-06 12:20:49 +0300501
Marcelo Tosattie4825802008-09-24 20:28:34 -0300502 s->output = level;
503 if (vcpu && level && (s->pics[0].isr_ack & (1 << irq))) {
504 s->pics[0].isr_ack &= ~(1 << irq);
Avi Kivity3f353852008-12-21 22:48:32 +0200505 s->wakeup_needed = true;
Marcelo Tosattie4825802008-09-24 20:28:34 -0300506 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300507}
508
509struct kvm_pic *kvm_create_pic(struct kvm *kvm)
510{
511 struct kvm_pic *s;
512 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
513 if (!s)
514 return NULL;
Avi Kivity3f353852008-12-21 22:48:32 +0200515 spin_lock_init(&s->lock);
516 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300517 s->pics[0].elcr_mask = 0xf8;
518 s->pics[1].elcr_mask = 0xde;
519 s->irq_request = pic_irq_request;
520 s->irq_request_opaque = kvm;
521 s->pics[0].pics_state = s;
522 s->pics[1].pics_state = s;
523
524 /*
525 * Initialize PIO device
526 */
527 s->dev.read = picdev_read;
528 s->dev.write = picdev_write;
529 s->dev.in_range = picdev_in_range;
530 s->dev.private = s;
531 kvm_io_bus_register_dev(&kvm->pio_bus, &s->dev);
532 return s;
533}