Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
irq_comm.c
Go to the documentation of this file.
1 /*
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 <[email protected]>
19  *
20  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
21  */
22 
23 #include <linux/kvm_host.h>
24 #include <linux/slab.h>
25 #include <trace/events/kvm.h>
26 
27 #include <asm/msidef.h>
28 #ifdef CONFIG_IA64
29 #include <asm/iosapic.h>
30 #endif
31 
32 #include "irq.h"
33 
34 #include "ioapic.h"
35 
36 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
37  struct kvm *kvm, int irq_source_id, int level)
38 {
39 #ifdef CONFIG_X86
40  struct kvm_pic *pic = pic_irqchip(kvm);
41  return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
42 #else
43  return -1;
44 #endif
45 }
46 
47 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
48  struct kvm *kvm, int irq_source_id, int level)
49 {
50  struct kvm_ioapic *ioapic = kvm->arch.vioapic;
51  return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level);
52 }
53 
54 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
55 {
56 #ifdef CONFIG_IA64
57  return irq->delivery_mode ==
59 #else
60  return irq->delivery_mode == APIC_DM_LOWEST;
61 #endif
62 }
63 
65  struct kvm_lapic_irq *irq)
66 {
67  int i, r = -1;
68  struct kvm_vcpu *vcpu, *lowest = NULL;
69 
70  if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
71  kvm_is_dm_lowest_prio(irq)) {
72  printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
74  }
75 
76  if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r))
77  return r;
78 
79  kvm_for_each_vcpu(i, vcpu, kvm) {
80  if (!kvm_apic_present(vcpu))
81  continue;
82 
83  if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
84  irq->dest_id, irq->dest_mode))
85  continue;
86 
87  if (!kvm_is_dm_lowest_prio(irq)) {
88  if (r < 0)
89  r = 0;
90  r += kvm_apic_set_irq(vcpu, irq);
91  } else if (kvm_lapic_enabled(vcpu)) {
92  if (!lowest)
93  lowest = vcpu;
94  else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
95  lowest = vcpu;
96  }
97  }
98 
99  if (lowest)
100  r = kvm_apic_set_irq(lowest, irq);
101 
102  return r;
103 }
104 
106  struct kvm *kvm, int irq_source_id, int level)
107 {
108  struct kvm_lapic_irq irq;
109 
110  if (!level)
111  return -1;
112 
113  trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
114 
115  irq.dest_id = (e->msi.address_lo &
117  irq.vector = (e->msi.data &
119  irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
120  irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
121  irq.delivery_mode = e->msi.data & 0x700;
122  irq.level = 1;
123  irq.shorthand = 0;
124 
125  /* TODO Deal with RH bit of MSI message address */
126  return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
127 }
128 
129 int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
130 {
131  struct kvm_kernel_irq_routing_entry route;
132 
133  if (!irqchip_in_kernel(kvm) || msi->flags != 0)
134  return -EINVAL;
135 
136  route.msi.address_lo = msi->address_lo;
137  route.msi.address_hi = msi->address_hi;
138  route.msi.data = msi->data;
139 
140  return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
141 }
142 
143 /*
144  * Return value:
145  * < 0 Interrupt was ignored (masked or not delivered for other reasons)
146  * = 0 Interrupt was coalesced (previous irq is still pending)
147  * > 0 Number of CPUs interrupt was delivered to
148  */
149 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
150 {
152  int ret = -1, i = 0;
153  struct kvm_irq_routing_table *irq_rt;
154  struct hlist_node *n;
155 
156  trace_kvm_set_irq(irq, level, irq_source_id);
157 
158  /* Not possible to detect if the guest uses the PIC or the
159  * IOAPIC. So set the bit in both. The guest will ignore
160  * writes to the unused one.
161  */
162  rcu_read_lock();
163  irq_rt = rcu_dereference(kvm->irq_routing);
164  if (irq < irq_rt->nr_rt_entries)
165  hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
166  irq_set[i++] = *e;
167  rcu_read_unlock();
168 
169  while(i--) {
170  int r;
171  r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
172  if (r < 0)
173  continue;
174 
175  ret = r + ((ret < 0) ? 0 : ret);
176  }
177 
178  return ret;
179 }
180 
181 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
182 {
183  struct kvm_irq_ack_notifier *kian;
184  struct hlist_node *n;
185  int gsi;
186 
187  trace_kvm_ack_irq(irqchip, pin);
188 
189  rcu_read_lock();
190  gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
191  if (gsi != -1)
192  hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list,
193  link)
194  if (kian->gsi == gsi)
195  kian->irq_acked(kian);
196  rcu_read_unlock();
197 }
198 
200  struct kvm_irq_ack_notifier *kian)
201 {
202  mutex_lock(&kvm->irq_lock);
203  hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
204  mutex_unlock(&kvm->irq_lock);
205 }
206 
208  struct kvm_irq_ack_notifier *kian)
209 {
210  mutex_lock(&kvm->irq_lock);
211  hlist_del_init_rcu(&kian->link);
212  mutex_unlock(&kvm->irq_lock);
213  synchronize_rcu();
214 }
215 
217 {
218  unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
219  int irq_source_id;
220 
221  mutex_lock(&kvm->irq_lock);
222  irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
223 
224  if (irq_source_id >= BITS_PER_LONG) {
225  printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
226  irq_source_id = -EFAULT;
227  goto unlock;
228  }
229 
230  ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
231 #ifdef CONFIG_X86
232  ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
233 #endif
234  set_bit(irq_source_id, bitmap);
235 unlock:
236  mutex_unlock(&kvm->irq_lock);
237 
238  return irq_source_id;
239 }
240 
241 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
242 {
243  ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
244 #ifdef CONFIG_X86
245  ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
246 #endif
247 
248  mutex_lock(&kvm->irq_lock);
249  if (irq_source_id < 0 ||
250  irq_source_id >= BITS_PER_LONG) {
251  printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
252  goto unlock;
253  }
254  clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
255  if (!irqchip_in_kernel(kvm))
256  goto unlock;
257 
258  kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
259 #ifdef CONFIG_X86
260  kvm_pic_clear_all(pic_irqchip(kvm), irq_source_id);
261 #endif
262 unlock:
263  mutex_unlock(&kvm->irq_lock);
264 }
265 
266 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
267  struct kvm_irq_mask_notifier *kimn)
268 {
269  mutex_lock(&kvm->irq_lock);
270  kimn->irq = irq;
271  hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
272  mutex_unlock(&kvm->irq_lock);
273 }
274 
276  struct kvm_irq_mask_notifier *kimn)
277 {
278  mutex_lock(&kvm->irq_lock);
279  hlist_del_rcu(&kimn->link);
280  mutex_unlock(&kvm->irq_lock);
281  synchronize_rcu();
282 }
283 
284 void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
285  bool mask)
286 {
287  struct kvm_irq_mask_notifier *kimn;
288  struct hlist_node *n;
289  int gsi;
290 
291  rcu_read_lock();
292  gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
293  if (gsi != -1)
294  hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link)
295  if (kimn->irq == gsi)
296  kimn->func(kimn, mask);
297  rcu_read_unlock();
298 }
299 
301 {
302  /* Called only during vm destruction. Nobody can use the pointer
303  at this stage */
304  kfree(kvm->irq_routing);
305 }
306 
307 static int setup_routing_entry(struct kvm_irq_routing_table *rt,
309  const struct kvm_irq_routing_entry *ue)
310 {
311  int r = -EINVAL;
312  int delta;
313  unsigned max_pin;
314  struct kvm_kernel_irq_routing_entry *ei;
315  struct hlist_node *n;
316 
317  /*
318  * Do not allow GSI to be mapped to the same irqchip more than once.
319  * Allow only one to one mapping between GSI and MSI.
320  */
321  hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
322  if (ei->type == KVM_IRQ_ROUTING_MSI ||
323  ue->type == KVM_IRQ_ROUTING_MSI ||
324  ue->u.irqchip.irqchip == ei->irqchip.irqchip)
325  return r;
326 
327  e->gsi = ue->gsi;
328  e->type = ue->type;
329  switch (ue->type) {
330  case KVM_IRQ_ROUTING_IRQCHIP:
331  delta = 0;
332  switch (ue->u.irqchip.irqchip) {
334  e->set = kvm_set_pic_irq;
335  max_pin = PIC_NUM_PINS;
336  break;
338  e->set = kvm_set_pic_irq;
339  max_pin = PIC_NUM_PINS;
340  delta = 8;
341  break;
342  case KVM_IRQCHIP_IOAPIC:
343  max_pin = KVM_IOAPIC_NUM_PINS;
344  e->set = kvm_set_ioapic_irq;
345  break;
346  default:
347  goto out;
348  }
349  e->irqchip.irqchip = ue->u.irqchip.irqchip;
350  e->irqchip.pin = ue->u.irqchip.pin + delta;
351  if (e->irqchip.pin >= max_pin)
352  goto out;
353  rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
354  break;
355  case KVM_IRQ_ROUTING_MSI:
356  e->set = kvm_set_msi;
357  e->msi.address_lo = ue->u.msi.address_lo;
358  e->msi.address_hi = ue->u.msi.address_hi;
359  e->msi.data = ue->u.msi.data;
360  break;
361  default:
362  goto out;
363  }
364 
365  hlist_add_head(&e->link, &rt->map[e->gsi]);
366  r = 0;
367 out:
368  return r;
369 }
370 
371 
373  const struct kvm_irq_routing_entry *ue,
374  unsigned nr,
375  unsigned flags)
376 {
377  struct kvm_irq_routing_table *new, *old;
378  u32 i, j, nr_rt_entries = 0;
379  int r;
380 
381  for (i = 0; i < nr; ++i) {
382  if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
383  return -EINVAL;
384  nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
385  }
386 
387  nr_rt_entries += 1;
388 
389  new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
390  + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
391  GFP_KERNEL);
392 
393  if (!new)
394  return -ENOMEM;
395 
396  new->rt_entries = (void *)&new->map[nr_rt_entries];
397 
398  new->nr_rt_entries = nr_rt_entries;
399  for (i = 0; i < 3; i++)
400  for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
401  new->chip[i][j] = -1;
402 
403  for (i = 0; i < nr; ++i) {
404  r = -EINVAL;
405  if (ue->flags)
406  goto out;
407  r = setup_routing_entry(new, &new->rt_entries[i], ue);
408  if (r)
409  goto out;
410  ++ue;
411  }
412 
413  mutex_lock(&kvm->irq_lock);
414  old = kvm->irq_routing;
415  kvm_irq_routing_update(kvm, new);
416  mutex_unlock(&kvm->irq_lock);
417 
418  synchronize_rcu();
419 
420  new = old;
421  r = 0;
422 
423 out:
424  kfree(new);
425  return r;
426 }
427 
428 #define IOAPIC_ROUTING_ENTRY(irq) \
429  { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
430  .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
431 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
432 
433 #ifdef CONFIG_X86
434 # define PIC_ROUTING_ENTRY(irq) \
435  { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
436  .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
437 # define ROUTING_ENTRY2(irq) \
438  IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
439 #else
440 # define ROUTING_ENTRY2(irq) \
441  IOAPIC_ROUTING_ENTRY(irq)
442 #endif
443 
444 static const struct kvm_irq_routing_entry default_routing[] = {
457 #ifdef CONFIG_IA64
470 #endif
471 };
472 
474 {
475  return kvm_set_irq_routing(kvm, default_routing,
476  ARRAY_SIZE(default_routing), 0);
477 }