blob: 5162a411e4d2f4193916f9e022a018b1f0ce8a42 [file] [log] [blame]
Xiantao Zhang3de42dc2008-10-06 13:48:45 +08001/*
2 * irq_comm.c: Common API for in kernel interrupt controller
3 * Copyright (c) 2007, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 * Authors:
18 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
19 *
20 */
21
22#include <linux/kvm_host.h>
23#include "irq.h"
24
25#include "ioapic.h"
26
27/* This should be called with the kvm->lock mutex held */
Sheng Yang5550af42008-10-15 20:15:06 +080028void kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080029{
Sheng Yang5550af42008-10-15 20:15:06 +080030 unsigned long *irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
31
32 /* Logical OR for level trig interrupt */
33 if (level)
34 set_bit(irq_source_id, irq_state);
35 else
36 clear_bit(irq_source_id, irq_state);
37
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080038 /* Not possible to detect if the guest uses the PIC or the
39 * IOAPIC. So set the bit in both. The guest will ignore
40 * writes to the unused one.
41 */
Sheng Yang5550af42008-10-15 20:15:06 +080042 kvm_ioapic_set_irq(kvm->arch.vioapic, irq, !!(*irq_state));
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080043#ifdef CONFIG_X86
Sheng Yang5550af42008-10-15 20:15:06 +080044 kvm_pic_set_irq(pic_irqchip(kvm), irq, !!(*irq_state));
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080045#endif
46}
47
48void kvm_notify_acked_irq(struct kvm *kvm, unsigned gsi)
49{
50 struct kvm_irq_ack_notifier *kian;
51 struct hlist_node *n;
52
53 hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
54 if (kian->gsi == gsi)
55 kian->irq_acked(kian);
56}
57
58void kvm_register_irq_ack_notifier(struct kvm *kvm,
59 struct kvm_irq_ack_notifier *kian)
60{
61 hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
62}
63
Sheng Yange19e30e2008-10-20 16:07:10 +080064void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080065{
Mark McLoughlinfdd897e2008-12-01 13:57:46 +000066 hlist_del_init(&kian->link);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080067}
Sheng Yang5550af42008-10-15 20:15:06 +080068
69/* The caller must hold kvm->lock mutex */
70int kvm_request_irq_source_id(struct kvm *kvm)
71{
72 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
73 int irq_source_id = find_first_zero_bit(bitmap,
74 sizeof(kvm->arch.irq_sources_bitmap));
Mark McLoughlin61552362008-12-01 13:57:48 +000075
Sheng Yang5550af42008-10-15 20:15:06 +080076 if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
77 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Mark McLoughlin61552362008-12-01 13:57:48 +000078 return -EFAULT;
79 }
80
81 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
82 set_bit(irq_source_id, bitmap);
83
Sheng Yang5550af42008-10-15 20:15:06 +080084 return irq_source_id;
85}
86
87void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
88{
89 int i;
90
Mark McLoughlin61552362008-12-01 13:57:48 +000091 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
92
93 if (irq_source_id < 0 ||
Sheng Yang5550af42008-10-15 20:15:06 +080094 irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
95 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
96 return;
97 }
98 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
99 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
100 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
101}
Avi Kivity75858a82009-01-04 17:10:50 +0200102
103void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
104 struct kvm_irq_mask_notifier *kimn)
105{
106 kimn->irq = irq;
107 hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
108}
109
110void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
111 struct kvm_irq_mask_notifier *kimn)
112{
113 hlist_del(&kimn->link);
114}
115
116void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
117{
118 struct kvm_irq_mask_notifier *kimn;
119 struct hlist_node *n;
120
121 hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
122 if (kimn->irq == irq)
123 kimn->func(kimn, mask);
124}
125