blob: bdcd4139eca9233bbd9e82615a1ed3c45c2ad060 [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;
David Hildenbrande21d1752017-04-07 10:50:36 +020052 struct kvm_vcpu *vcpu;
Chris Lalancette529df652010-06-21 11:29:40 -040053 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)) {
David Hildenbrande21d1752017-04-07 10:50:36 +020062 kvm_make_request(KVM_REQ_EVENT, vcpu);
63 kvm_vcpu_kick(vcpu);
64 return;
Chris Lalancette529df652010-06-21 11:29:40 -040065 }
66 }
Jan Kiszka50a085b2010-02-24 10:41:58 +010067 }
68}
69
Avi Kivity7edd0ce2008-07-07 14:45:39 +030070static void pic_clear_isr(struct kvm_kpic_state *s, int irq)
71{
72 s->isr &= ~(1 << irq);
Gleb Natapov938396a2009-08-04 15:30:28 +030073 if (s != &s->pics_state->pics[0])
74 irq += 8;
Gleb Natapoveba02262009-08-24 11:54:25 +030075 /*
76 * We are dropping lock while calling ack notifiers since ack
77 * notifier callbacks for assigned devices call into PIC recursively.
78 * Other interrupt may be delivered to PIC while lock is dropped but
79 * it should be safe since PIC state is already updated at this stage.
80 */
Jan Kiszka50a085b2010-02-24 10:41:58 +010081 pic_unlock(s->pics_state);
Gleb Natapov938396a2009-08-04 15:30:28 +030082 kvm_notify_acked_irq(s->pics_state->kvm, SELECT_PIC(irq), irq);
Jan Kiszka50a085b2010-02-24 10:41:58 +010083 pic_lock(s->pics_state);
Marcelo Tosattie4825802008-09-24 20:28:34 -030084}
85
Eddie Dong85f455f2007-07-06 12:20:49 +030086/*
87 * set irq level. If an edge is detected, then the IRR is set to 1
88 */
Gleb Natapov49256632009-02-04 17:28:14 +020089static inline int pic_set_irq1(struct kvm_kpic_state *s, int irq, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +030090{
Gleb Natapov49256632009-02-04 17:28:14 +020091 int mask, ret = 1;
Eddie Dong85f455f2007-07-06 12:20:49 +030092 mask = 1 << irq;
93 if (s->elcr & mask) /* level triggered */
94 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +020095 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +030096 s->irr |= mask;
97 s->last_irr |= mask;
98 } else {
99 s->irr &= ~mask;
100 s->last_irr &= ~mask;
101 }
102 else /* edge triggered */
103 if (level) {
Gleb Natapov49256632009-02-04 17:28:14 +0200104 if ((s->last_irr & mask) == 0) {
105 ret = !(s->irr & mask);
Eddie Dong85f455f2007-07-06 12:20:49 +0300106 s->irr |= mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200107 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300108 s->last_irr |= mask;
109 } else
110 s->last_irr &= ~mask;
Gleb Natapov49256632009-02-04 17:28:14 +0200111
112 return (s->imr & mask) ? -1 : ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300113}
114
115/*
116 * return the highest priority found in mask (highest = smallest
117 * number). Return 8 if no irq
118 */
119static inline int get_priority(struct kvm_kpic_state *s, int mask)
120{
121 int priority;
122 if (mask == 0)
123 return 8;
124 priority = 0;
125 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
126 priority++;
127 return priority;
128}
129
130/*
131 * return the pic wanted interrupt. return -1 if none
132 */
133static int pic_get_irq(struct kvm_kpic_state *s)
134{
135 int mask, cur_priority, priority;
136
137 mask = s->irr & ~s->imr;
138 priority = get_priority(s, mask);
139 if (priority == 8)
140 return -1;
141 /*
142 * compute current priority. If special fully nested mode on the
143 * master, the IRQ coming from the slave is not taken into account
144 * for the priority computation.
145 */
146 mask = s->isr;
147 if (s->special_fully_nested_mode && s == &s->pics_state->pics[0])
148 mask &= ~(1 << 2);
149 cur_priority = get_priority(s, mask);
150 if (priority < cur_priority)
151 /*
152 * higher priority found: an irq should be generated
153 */
154 return (priority + s->priority_add) & 7;
155 else
156 return -1;
157}
158
159/*
160 * raise irq to CPU if necessary. must be called every time the active
161 * irq may change
162 */
163static void pic_update_irq(struct kvm_pic *s)
164{
165 int irq2, irq;
166
167 irq2 = pic_get_irq(&s->pics[1]);
168 if (irq2 >= 0) {
169 /*
170 * if irq request by slave pic, signal master PIC
171 */
172 pic_set_irq1(&s->pics[0], 2, 1);
173 pic_set_irq1(&s->pics[0], 2, 0);
174 }
175 irq = pic_get_irq(&s->pics[0]);
Avi Kivity36633f32010-05-03 17:38:06 +0300176 pic_irq_request(s->kvm, irq >= 0);
Eddie Dong85f455f2007-07-06 12:20:49 +0300177}
178
He, Qing6ceb9d72007-07-26 11:05:18 +0300179void kvm_pic_update_irq(struct kvm_pic *s)
180{
Jan Kiszka50a085b2010-02-24 10:41:58 +0100181 pic_lock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300182 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100183 pic_unlock(s);
He, Qing6ceb9d72007-07-26 11:05:18 +0300184}
185
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300186int kvm_pic_set_irq(struct kvm_pic *s, int irq, int irq_source_id, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300187{
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300188 int ret, irq_level;
189
190 BUG_ON(irq < 0 || irq >= PIC_NUM_PINS);
Eddie Dong85f455f2007-07-06 12:20:49 +0300191
Jan Kiszka50a085b2010-02-24 10:41:58 +0100192 pic_lock(s);
Michael S. Tsirkin28a6fda2012-08-14 19:20:28 +0300193 irq_level = __kvm_irq_line_state(&s->irq_states[irq],
194 irq_source_id, level);
195 ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, irq_level);
196 pic_update_irq(s);
197 trace_kvm_pic_set_irq(irq >> 3, irq & 7, s->pics[irq >> 3].elcr,
198 s->pics[irq >> 3].imr, ret == 0);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100199 pic_unlock(s);
Gleb Natapov49256632009-02-04 17:28:14 +0200200
201 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300202}
203
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300204void kvm_pic_clear_all(struct kvm_pic *s, int irq_source_id)
205{
206 int i;
207
208 pic_lock(s);
209 for (i = 0; i < PIC_NUM_PINS; i++)
210 __clear_bit(irq_source_id, &s->irq_states[i]);
211 pic_unlock(s);
212}
213
Eddie Dong85f455f2007-07-06 12:20:49 +0300214/*
215 * acknowledge interrupt 'irq'
216 */
217static inline void pic_intack(struct kvm_kpic_state *s, int irq)
218{
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300219 s->isr |= 1 << irq;
Eddie Dong85f455f2007-07-06 12:20:49 +0300220 /*
221 * We don't clear a level sensitive interrupt here
222 */
223 if (!(s->elcr & (1 << irq)))
224 s->irr &= ~(1 << irq);
Gleb Natapoveba02262009-08-24 11:54:25 +0300225
226 if (s->auto_eoi) {
227 if (s->rotate_on_auto_eoi)
228 s->priority_add = (irq + 1) & 7;
229 pic_clear_isr(s, irq);
230 }
231
Eddie Dong85f455f2007-07-06 12:20:49 +0300232}
233
Marcelo Tosattif5244722008-07-26 17:01:00 -0300234int kvm_pic_read_irq(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300235{
236 int irq, irq2, intno;
David Hildenbrand90bca052017-04-07 10:50:23 +0200237 struct kvm_pic *s = kvm->arch.vpic;
Eddie Dong85f455f2007-07-06 12:20:49 +0300238
Gleb Natapovf3200d02012-12-10 14:05:55 +0200239 s->output = 0;
240
Jan Kiszka50a085b2010-02-24 10:41:58 +0100241 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300242 irq = pic_get_irq(&s->pics[0]);
243 if (irq >= 0) {
244 pic_intack(&s->pics[0], irq);
245 if (irq == 2) {
246 irq2 = pic_get_irq(&s->pics[1]);
247 if (irq2 >= 0)
248 pic_intack(&s->pics[1], irq2);
249 else
250 /*
251 * spurious IRQ on slave controller
252 */
253 irq2 = 7;
254 intno = s->pics[1].irq_base + irq2;
255 irq = irq2 + 8;
256 } else
257 intno = s->pics[0].irq_base + irq;
258 } else {
259 /*
260 * spurious IRQ on host controller
261 */
262 irq = 7;
263 intno = s->pics[0].irq_base + irq;
264 }
265 pic_update_irq(s);
Jan Kiszka50a085b2010-02-24 10:41:58 +0100266 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300267
268 return intno;
269}
270
David Hildenbranddc24d1d2017-04-07 10:50:37 +0200271static void kvm_pic_reset(struct kvm_kpic_state *s)
Eddie Dong85f455f2007-07-06 12:20:49 +0300272{
Gleb Natapovd546cb42011-12-15 12:38:40 +0200273 int irq, i;
274 struct kvm_vcpu *vcpu;
Gleb Natapovec798662012-09-03 14:47:25 +0300275 u8 edge_irr = s->irr & ~s->elcr;
Gleb Natapovd546cb42011-12-15 12:38:40 +0200276 bool found = false;
Marcelo Tosattif5244722008-07-26 17:01:00 -0300277
Eddie Dong85f455f2007-07-06 12:20:49 +0300278 s->last_irr = 0;
Gleb Natapovec798662012-09-03 14:47:25 +0300279 s->irr &= s->elcr;
Eddie Dong85f455f2007-07-06 12:20:49 +0300280 s->imr = 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300281 s->priority_add = 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300282 s->special_mask = 0;
Gleb Natapovec798662012-09-03 14:47:25 +0300283 s->read_reg_select = 0;
284 if (!s->init4) {
285 s->special_fully_nested_mode = 0;
286 s->auto_eoi = 0;
287 }
288 s->init_state = 1;
Gleb Natapov79c727d2009-08-24 11:54:18 +0300289
Gleb Natapovd546cb42011-12-15 12:38:40 +0200290 kvm_for_each_vcpu(i, vcpu, s->pics_state->kvm)
291 if (kvm_apic_accept_pic_intr(vcpu)) {
292 found = true;
293 break;
294 }
295
296
297 if (!found)
298 return;
299
300 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
Gleb Natapovec798662012-09-03 14:47:25 +0300301 if (edge_irr & (1 << irq))
Gleb Natapovd546cb42011-12-15 12:38:40 +0200302 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300303}
304
305static void pic_ioport_write(void *opaque, u32 addr, u32 val)
306{
307 struct kvm_kpic_state *s = opaque;
308 int priority, cmd, irq;
309
310 addr &= 1;
311 if (addr == 0) {
312 if (val & 0x10) {
Eddie Dong85f455f2007-07-06 12:20:49 +0300313 s->init4 = val & 1;
314 if (val & 0x02)
Jan Kiszkabd801582011-09-12 11:26:22 +0200315 pr_pic_unimpl("single mode not supported");
Eddie Dong85f455f2007-07-06 12:20:49 +0300316 if (val & 0x08)
Jan Kiszkabd801582011-09-12 11:26:22 +0200317 pr_pic_unimpl(
Gleb Natapovec798662012-09-03 14:47:25 +0300318 "level sensitive irq not supported");
319 kvm_pic_reset(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300320 } else if (val & 0x08) {
321 if (val & 0x04)
322 s->poll = 1;
323 if (val & 0x02)
324 s->read_reg_select = val & 1;
325 if (val & 0x40)
326 s->special_mask = (val >> 5) & 1;
327 } else {
328 cmd = val >> 5;
329 switch (cmd) {
330 case 0:
331 case 4:
332 s->rotate_on_auto_eoi = cmd >> 2;
333 break;
334 case 1: /* end of interrupt */
335 case 5:
336 priority = get_priority(s, s->isr);
337 if (priority != 8) {
338 irq = (priority + s->priority_add) & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300339 if (cmd == 5)
340 s->priority_add = (irq + 1) & 7;
Gleb Natapoveba02262009-08-24 11:54:25 +0300341 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300342 pic_update_irq(s->pics_state);
343 }
344 break;
345 case 3:
346 irq = val & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300347 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300348 pic_update_irq(s->pics_state);
349 break;
350 case 6:
351 s->priority_add = (val + 1) & 7;
352 pic_update_irq(s->pics_state);
353 break;
354 case 7:
355 irq = val & 7;
Eddie Dong85f455f2007-07-06 12:20:49 +0300356 s->priority_add = (irq + 1) & 7;
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300357 pic_clear_isr(s, irq);
Eddie Dong85f455f2007-07-06 12:20:49 +0300358 pic_update_irq(s->pics_state);
359 break;
360 default:
361 break; /* no operation */
362 }
363 }
364 } else
365 switch (s->init_state) {
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300366 case 0: { /* normal mode */
367 u8 imr_diff = s->imr ^ val,
368 off = (s == &s->pics_state->pics[0]) ? 0 : 8;
Eddie Dong85f455f2007-07-06 12:20:49 +0300369 s->imr = val;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300370 for (irq = 0; irq < PIC_NUM_PINS/2; irq++)
371 if (imr_diff & (1 << irq))
372 kvm_fire_mask_notifiers(
373 s->pics_state->kvm,
374 SELECT_PIC(irq + off),
375 irq + off,
376 !!(s->imr & (1 << irq)));
Eddie Dong85f455f2007-07-06 12:20:49 +0300377 pic_update_irq(s->pics_state);
378 break;
Gleb Natapov9195c4d2010-07-15 12:24:37 +0300379 }
Eddie Dong85f455f2007-07-06 12:20:49 +0300380 case 1:
381 s->irq_base = val & 0xf8;
382 s->init_state = 2;
383 break;
384 case 2:
385 if (s->init4)
386 s->init_state = 3;
387 else
388 s->init_state = 0;
389 break;
390 case 3:
391 s->special_fully_nested_mode = (val >> 4) & 1;
392 s->auto_eoi = (val >> 1) & 1;
393 s->init_state = 0;
394 break;
395 }
396}
397
398static u32 pic_poll_read(struct kvm_kpic_state *s, u32 addr1)
399{
400 int ret;
401
402 ret = pic_get_irq(s);
403 if (ret >= 0) {
404 if (addr1 >> 7) {
405 s->pics_state->pics[0].isr &= ~(1 << 2);
406 s->pics_state->pics[0].irr &= ~(1 << 2);
407 }
408 s->irr &= ~(1 << ret);
Avi Kivity7edd0ce2008-07-07 14:45:39 +0300409 pic_clear_isr(s, ret);
Eddie Dong85f455f2007-07-06 12:20:49 +0300410 if (addr1 >> 7 || ret != 2)
411 pic_update_irq(s->pics_state);
412 } else {
413 ret = 0x07;
414 pic_update_irq(s->pics_state);
415 }
416
417 return ret;
418}
419
David Hildenbrandb5e7cf52017-04-07 10:50:40 +0200420static u32 pic_ioport_read(void *opaque, u32 addr)
Eddie Dong85f455f2007-07-06 12:20:49 +0300421{
422 struct kvm_kpic_state *s = opaque;
Eddie Dong85f455f2007-07-06 12:20:49 +0300423 int ret;
424
Eddie Dong85f455f2007-07-06 12:20:49 +0300425 if (s->poll) {
David Hildenbrandb5e7cf52017-04-07 10:50:40 +0200426 ret = pic_poll_read(s, addr);
Eddie Dong85f455f2007-07-06 12:20:49 +0300427 s->poll = 0;
428 } else
David Hildenbrandb5e7cf52017-04-07 10:50:40 +0200429 if ((addr & 1) == 0)
Eddie Dong85f455f2007-07-06 12:20:49 +0300430 if (s->read_reg_select)
431 ret = s->isr;
432 else
433 ret = s->irr;
434 else
435 ret = s->imr;
436 return ret;
437}
438
439static void elcr_ioport_write(void *opaque, u32 addr, u32 val)
440{
441 struct kvm_kpic_state *s = opaque;
442 s->elcr = val & s->elcr_mask;
443}
444
445static u32 elcr_ioport_read(void *opaque, u32 addr1)
446{
447 struct kvm_kpic_state *s = opaque;
448 return s->elcr;
449}
450
Sasha Levin743eeb02011-07-27 16:00:48 +0300451static int picdev_write(struct kvm_pic *s,
Eddie Dong85f455f2007-07-06 12:20:49 +0300452 gpa_t addr, int len, const void *val)
453{
Eddie Dong85f455f2007-07-06 12:20:49 +0300454 unsigned char data = *(unsigned char *)val;
455
456 if (len != 1) {
Jan Kiszkabd801582011-09-12 11:26:22 +0200457 pr_pic_unimpl("non byte write\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300458 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300459 }
460 switch (addr) {
461 case 0x20:
462 case 0x21:
463 case 0xa0:
464 case 0xa1:
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200465 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300466 pic_ioport_write(&s->pics[addr >> 7], addr, data);
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200467 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300468 break;
469 case 0x4d0:
470 case 0x4d1:
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200471 pic_lock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300472 elcr_ioport_write(&s->pics[addr & 1], addr, data);
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200473 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300474 break;
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200475 default:
476 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300477 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300478 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300479}
480
Sasha Levin743eeb02011-07-27 16:00:48 +0300481static int picdev_read(struct kvm_pic *s,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300482 gpa_t addr, int len, void *val)
Eddie Dong85f455f2007-07-06 12:20:49 +0300483{
David Hildenbrand84a5c792017-04-07 10:50:39 +0200484 unsigned char *data = (unsigned char *)val;
Eddie Dong85f455f2007-07-06 12:20:49 +0300485
486 if (len != 1) {
Petr Matousek2dccb4c2015-03-11 12:16:09 +0100487 memset(val, 0, len);
Jan Kiszkabd801582011-09-12 11:26:22 +0200488 pr_pic_unimpl("non byte read\n");
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300489 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300490 }
491 switch (addr) {
492 case 0x20:
493 case 0x21:
494 case 0xa0:
495 case 0xa1:
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200496 pic_lock(s);
David Hildenbrand84a5c792017-04-07 10:50:39 +0200497 *data = pic_ioport_read(&s->pics[addr >> 7], addr);
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200498 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300499 break;
500 case 0x4d0:
501 case 0x4d1:
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200502 pic_lock(s);
David Hildenbrand84a5c792017-04-07 10:50:39 +0200503 *data = elcr_ioport_read(&s->pics[addr & 1], addr);
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200504 pic_unlock(s);
Eddie Dong85f455f2007-07-06 12:20:49 +0300505 break;
David Hildenbrand9fecaa92017-04-07 10:50:38 +0200506 default:
507 return -EOPNOTSUPP;
Eddie Dong85f455f2007-07-06 12:20:49 +0300508 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300509 return 0;
Eddie Dong85f455f2007-07-06 12:20:49 +0300510}
511
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000512static int picdev_master_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
Sasha Levin743eeb02011-07-27 16:00:48 +0300513 gpa_t addr, int len, const void *val)
514{
515 return picdev_write(container_of(dev, struct kvm_pic, dev_master),
516 addr, len, val);
517}
518
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000519static int picdev_master_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
Sasha Levin743eeb02011-07-27 16:00:48 +0300520 gpa_t addr, int len, void *val)
521{
522 return picdev_read(container_of(dev, struct kvm_pic, dev_master),
523 addr, len, val);
524}
525
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000526static int picdev_slave_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
Sasha Levin743eeb02011-07-27 16:00:48 +0300527 gpa_t addr, int len, const void *val)
528{
529 return picdev_write(container_of(dev, struct kvm_pic, dev_slave),
530 addr, len, val);
531}
532
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000533static int picdev_slave_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
Sasha Levin743eeb02011-07-27 16:00:48 +0300534 gpa_t addr, int len, void *val)
535{
536 return picdev_read(container_of(dev, struct kvm_pic, dev_slave),
537 addr, len, val);
538}
539
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000540static int picdev_eclr_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
Sasha Levin743eeb02011-07-27 16:00:48 +0300541 gpa_t addr, int len, const void *val)
542{
543 return picdev_write(container_of(dev, struct kvm_pic, dev_eclr),
544 addr, len, val);
545}
546
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000547static int picdev_eclr_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
Sasha Levin743eeb02011-07-27 16:00:48 +0300548 gpa_t addr, int len, void *val)
549{
550 return picdev_read(container_of(dev, struct kvm_pic, dev_eclr),
551 addr, len, val);
552}
553
Eddie Dong85f455f2007-07-06 12:20:49 +0300554/*
555 * callback when PIC0 irq status changed
556 */
Avi Kivity073d4612010-05-03 17:34:34 +0300557static void pic_irq_request(struct kvm *kvm, int level)
Eddie Dong85f455f2007-07-06 12:20:49 +0300558{
David Hildenbrand90bca052017-04-07 10:50:23 +0200559 struct kvm_pic *s = kvm->arch.vpic;
Eddie Dong85f455f2007-07-06 12:20:49 +0300560
Gleb Natapov70494672011-02-09 12:09:46 +0200561 if (!s->output)
Jan Kiszka50a085b2010-02-24 10:41:58 +0100562 s->wakeup_needed = true;
Gleb Natapov70494672011-02-09 12:09:46 +0200563 s->output = level;
Eddie Dong85f455f2007-07-06 12:20:49 +0300564}
565
Sasha Levin743eeb02011-07-27 16:00:48 +0300566static const struct kvm_io_device_ops picdev_master_ops = {
567 .read = picdev_master_read,
568 .write = picdev_master_write,
569};
570
571static const struct kvm_io_device_ops picdev_slave_ops = {
572 .read = picdev_slave_read,
573 .write = picdev_slave_write,
574};
575
576static const struct kvm_io_device_ops picdev_eclr_ops = {
577 .read = picdev_eclr_read,
578 .write = picdev_eclr_write,
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400579};
580
Radim Krčmář09941362016-12-16 16:10:03 +0100581int kvm_pic_init(struct kvm *kvm)
Eddie Dong85f455f2007-07-06 12:20:49 +0300582{
583 struct kvm_pic *s;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400584 int ret;
585
Eddie Dong85f455f2007-07-06 12:20:49 +0300586 s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
587 if (!s)
Radim Krčmář09941362016-12-16 16:10:03 +0100588 return -ENOMEM;
Avi Kivityf4f51052010-09-19 18:44:07 +0200589 spin_lock_init(&s->lock);
Avi Kivity3f353852008-12-21 22:48:32 +0200590 s->kvm = kvm;
Eddie Dong85f455f2007-07-06 12:20:49 +0300591 s->pics[0].elcr_mask = 0xf8;
592 s->pics[1].elcr_mask = 0xde;
Eddie Dong85f455f2007-07-06 12:20:49 +0300593 s->pics[0].pics_state = s;
594 s->pics[1].pics_state = s;
595
596 /*
597 * Initialize PIO device
598 */
Sasha Levin743eeb02011-07-27 16:00:48 +0300599 kvm_iodevice_init(&s->dev_master, &picdev_master_ops);
600 kvm_iodevice_init(&s->dev_slave, &picdev_slave_ops);
601 kvm_iodevice_init(&s->dev_eclr, &picdev_eclr_ops);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200602 mutex_lock(&kvm->slots_lock);
Sasha Levin743eeb02011-07-27 16:00:48 +0300603 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,
604 &s->dev_master);
605 if (ret < 0)
606 goto fail_unlock;
607
608 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2, &s->dev_slave);
609 if (ret < 0)
610 goto fail_unreg_2;
611
612 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2, &s->dev_eclr);
613 if (ret < 0)
614 goto fail_unreg_1;
615
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200616 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400617
Radim Krčmář09941362016-12-16 16:10:03 +0100618 kvm->arch.vpic = s;
619
620 return 0;
Sasha Levin743eeb02011-07-27 16:00:48 +0300621
622fail_unreg_1:
623 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_slave);
624
625fail_unreg_2:
626 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &s->dev_master);
627
628fail_unlock:
629 mutex_unlock(&kvm->slots_lock);
630
631 kfree(s);
632
Radim Krčmář09941362016-12-16 16:10:03 +0100633 return ret;
Eddie Dong85f455f2007-07-06 12:20:49 +0300634}
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800635
Radim Krčmář09941362016-12-16 16:10:03 +0100636void kvm_pic_destroy(struct kvm *kvm)
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800637{
Radim Krčmář09941362016-12-16 16:10:03 +0100638 struct kvm_pic *vpic = kvm->arch.vpic;
639
Peter Xu950712e2017-03-15 16:01:18 +0800640 if (!vpic)
641 return;
642
David Hildenbrand49f520b2017-04-07 10:50:29 +0200643 mutex_lock(&kvm->slots_lock);
Paolo Bonzini71ba9942015-07-29 12:31:15 +0200644 kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_master);
645 kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_slave);
646 kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_eclr);
David Hildenbrand49f520b2017-04-07 10:50:29 +0200647 mutex_unlock(&kvm->slots_lock);
Radim Krčmář09941362016-12-16 16:10:03 +0100648
649 kvm->arch.vpic = NULL;
Paolo Bonzini71ba9942015-07-29 12:31:15 +0200650 kfree(vpic);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +0800651}