events.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. * Xen event channels
  3. *
  4. * Xen models interrupts with abstract event channels. Because each
  5. * domain gets 1024 event channels, but NR_IRQ is not that large, we
  6. * must dynamically map irqs<->event channels. The event channels
  7. * interface with the rest of the kernel by defining a xen interrupt
  8. * chip. When an event is recieved, it is mapped to an irq and sent
  9. * through the normal interrupt processing path.
  10. *
  11. * There are four kinds of events which can be mapped to an event
  12. * channel:
  13. *
  14. * 1. Inter-domain notifications. This includes all the virtual
  15. * device events, since they're driven by front-ends in another domain
  16. * (typically dom0).
  17. * 2. VIRQs, typically used for timers. These are per-cpu events.
  18. * 3. IPIs.
  19. * 4. PIRQs - Hardware interrupts.
  20. *
  21. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  22. */
  23. #include <linux/linkage.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/module.h>
  27. #include <linux/string.h>
  28. #include <linux/bootmem.h>
  29. #include <linux/slab.h>
  30. #include <linux/irqnr.h>
  31. #include <asm/desc.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/irq.h>
  34. #include <asm/idle.h>
  35. #include <asm/io_apic.h>
  36. #include <asm/sync_bitops.h>
  37. #include <asm/xen/hypercall.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <xen/xen.h>
  40. #include <xen/hvm.h>
  41. #include <xen/xen-ops.h>
  42. #include <xen/events.h>
  43. #include <xen/interface/xen.h>
  44. #include <xen/interface/event_channel.h>
  45. #include <xen/interface/hvm/hvm_op.h>
  46. #include <xen/interface/hvm/params.h>
  47. /*
  48. * This lock protects updates to the following mapping and reference-count
  49. * arrays. The lock does not need to be acquired to read the mapping tables.
  50. */
  51. static DEFINE_SPINLOCK(irq_mapping_update_lock);
  52. /* IRQ <-> VIRQ mapping. */
  53. static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
  54. /* IRQ <-> IPI mapping */
  55. static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
  56. /* Interrupt types. */
  57. enum xen_irq_type {
  58. IRQT_UNBOUND = 0,
  59. IRQT_PIRQ,
  60. IRQT_VIRQ,
  61. IRQT_IPI,
  62. IRQT_EVTCHN
  63. };
  64. /*
  65. * Packed IRQ information:
  66. * type - enum xen_irq_type
  67. * event channel - irq->event channel mapping
  68. * cpu - cpu this event channel is bound to
  69. * index - type-specific information:
  70. * PIRQ - vector, with MSB being "needs EIO"
  71. * VIRQ - virq number
  72. * IPI - IPI vector
  73. * EVTCHN -
  74. */
  75. struct irq_info
  76. {
  77. enum xen_irq_type type; /* type */
  78. unsigned short evtchn; /* event channel */
  79. unsigned short cpu; /* cpu bound */
  80. union {
  81. unsigned short virq;
  82. enum ipi_vector ipi;
  83. struct {
  84. unsigned short gsi;
  85. unsigned char vector;
  86. unsigned char flags;
  87. } pirq;
  88. } u;
  89. };
  90. #define PIRQ_NEEDS_EOI (1 << 0)
  91. static struct irq_info *irq_info;
  92. static int *evtchn_to_irq;
  93. struct cpu_evtchn_s {
  94. unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
  95. };
  96. static __initdata struct cpu_evtchn_s init_evtchn_mask = {
  97. .bits[0 ... (NR_EVENT_CHANNELS/BITS_PER_LONG)-1] = ~0ul,
  98. };
  99. static struct cpu_evtchn_s *cpu_evtchn_mask_p = &init_evtchn_mask;
  100. static inline unsigned long *cpu_evtchn_mask(int cpu)
  101. {
  102. return cpu_evtchn_mask_p[cpu].bits;
  103. }
  104. /* Xen will never allocate port zero for any purpose. */
  105. #define VALID_EVTCHN(chn) ((chn) != 0)
  106. static struct irq_chip xen_dynamic_chip;
  107. static struct irq_chip xen_percpu_chip;
  108. static struct irq_chip xen_pirq_chip;
  109. /* Constructor for packed IRQ information. */
  110. static struct irq_info mk_unbound_info(void)
  111. {
  112. return (struct irq_info) { .type = IRQT_UNBOUND };
  113. }
  114. static struct irq_info mk_evtchn_info(unsigned short evtchn)
  115. {
  116. return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
  117. .cpu = 0 };
  118. }
  119. static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
  120. {
  121. return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
  122. .cpu = 0, .u.ipi = ipi };
  123. }
  124. static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
  125. {
  126. return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
  127. .cpu = 0, .u.virq = virq };
  128. }
  129. static struct irq_info mk_pirq_info(unsigned short evtchn,
  130. unsigned short gsi, unsigned short vector)
  131. {
  132. return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
  133. .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
  134. }
  135. /*
  136. * Accessors for packed IRQ information.
  137. */
  138. static struct irq_info *info_for_irq(unsigned irq)
  139. {
  140. return &irq_info[irq];
  141. }
  142. static unsigned int evtchn_from_irq(unsigned irq)
  143. {
  144. return info_for_irq(irq)->evtchn;
  145. }
  146. unsigned irq_from_evtchn(unsigned int evtchn)
  147. {
  148. return evtchn_to_irq[evtchn];
  149. }
  150. EXPORT_SYMBOL_GPL(irq_from_evtchn);
  151. static enum ipi_vector ipi_from_irq(unsigned irq)
  152. {
  153. struct irq_info *info = info_for_irq(irq);
  154. BUG_ON(info == NULL);
  155. BUG_ON(info->type != IRQT_IPI);
  156. return info->u.ipi;
  157. }
  158. static unsigned virq_from_irq(unsigned irq)
  159. {
  160. struct irq_info *info = info_for_irq(irq);
  161. BUG_ON(info == NULL);
  162. BUG_ON(info->type != IRQT_VIRQ);
  163. return info->u.virq;
  164. }
  165. static unsigned gsi_from_irq(unsigned irq)
  166. {
  167. struct irq_info *info = info_for_irq(irq);
  168. BUG_ON(info == NULL);
  169. BUG_ON(info->type != IRQT_PIRQ);
  170. return info->u.pirq.gsi;
  171. }
  172. static unsigned vector_from_irq(unsigned irq)
  173. {
  174. struct irq_info *info = info_for_irq(irq);
  175. BUG_ON(info == NULL);
  176. BUG_ON(info->type != IRQT_PIRQ);
  177. return info->u.pirq.vector;
  178. }
  179. static enum xen_irq_type type_from_irq(unsigned irq)
  180. {
  181. return info_for_irq(irq)->type;
  182. }
  183. static unsigned cpu_from_irq(unsigned irq)
  184. {
  185. return info_for_irq(irq)->cpu;
  186. }
  187. static unsigned int cpu_from_evtchn(unsigned int evtchn)
  188. {
  189. int irq = evtchn_to_irq[evtchn];
  190. unsigned ret = 0;
  191. if (irq != -1)
  192. ret = cpu_from_irq(irq);
  193. return ret;
  194. }
  195. static bool pirq_needs_eoi(unsigned irq)
  196. {
  197. struct irq_info *info = info_for_irq(irq);
  198. BUG_ON(info->type != IRQT_PIRQ);
  199. return info->u.pirq.flags & PIRQ_NEEDS_EOI;
  200. }
  201. static inline unsigned long active_evtchns(unsigned int cpu,
  202. struct shared_info *sh,
  203. unsigned int idx)
  204. {
  205. return (sh->evtchn_pending[idx] &
  206. cpu_evtchn_mask(cpu)[idx] &
  207. ~sh->evtchn_mask[idx]);
  208. }
  209. static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
  210. {
  211. int irq = evtchn_to_irq[chn];
  212. BUG_ON(irq == -1);
  213. #ifdef CONFIG_SMP
  214. cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
  215. #endif
  216. __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
  217. __set_bit(chn, cpu_evtchn_mask(cpu));
  218. irq_info[irq].cpu = cpu;
  219. }
  220. static void init_evtchn_cpu_bindings(void)
  221. {
  222. #ifdef CONFIG_SMP
  223. struct irq_desc *desc;
  224. int i;
  225. /* By default all event channels notify CPU#0. */
  226. for_each_irq_desc(i, desc) {
  227. cpumask_copy(desc->affinity, cpumask_of(0));
  228. }
  229. #endif
  230. memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
  231. }
  232. static inline void clear_evtchn(int port)
  233. {
  234. struct shared_info *s = HYPERVISOR_shared_info;
  235. sync_clear_bit(port, &s->evtchn_pending[0]);
  236. }
  237. static inline void set_evtchn(int port)
  238. {
  239. struct shared_info *s = HYPERVISOR_shared_info;
  240. sync_set_bit(port, &s->evtchn_pending[0]);
  241. }
  242. static inline int test_evtchn(int port)
  243. {
  244. struct shared_info *s = HYPERVISOR_shared_info;
  245. return sync_test_bit(port, &s->evtchn_pending[0]);
  246. }
  247. /**
  248. * notify_remote_via_irq - send event to remote end of event channel via irq
  249. * @irq: irq of event channel to send event to
  250. *
  251. * Unlike notify_remote_via_evtchn(), this is safe to use across
  252. * save/restore. Notifications on a broken connection are silently
  253. * dropped.
  254. */
  255. void notify_remote_via_irq(int irq)
  256. {
  257. int evtchn = evtchn_from_irq(irq);
  258. if (VALID_EVTCHN(evtchn))
  259. notify_remote_via_evtchn(evtchn);
  260. }
  261. EXPORT_SYMBOL_GPL(notify_remote_via_irq);
  262. static void mask_evtchn(int port)
  263. {
  264. struct shared_info *s = HYPERVISOR_shared_info;
  265. sync_set_bit(port, &s->evtchn_mask[0]);
  266. }
  267. static void unmask_evtchn(int port)
  268. {
  269. struct shared_info *s = HYPERVISOR_shared_info;
  270. unsigned int cpu = get_cpu();
  271. BUG_ON(!irqs_disabled());
  272. /* Slow path (hypercall) if this is a non-local port. */
  273. if (unlikely(cpu != cpu_from_evtchn(port))) {
  274. struct evtchn_unmask unmask = { .port = port };
  275. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  276. } else {
  277. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  278. sync_clear_bit(port, &s->evtchn_mask[0]);
  279. /*
  280. * The following is basically the equivalent of
  281. * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
  282. * the interrupt edge' if the channel is masked.
  283. */
  284. if (sync_test_bit(port, &s->evtchn_pending[0]) &&
  285. !sync_test_and_set_bit(port / BITS_PER_LONG,
  286. &vcpu_info->evtchn_pending_sel))
  287. vcpu_info->evtchn_upcall_pending = 1;
  288. }
  289. put_cpu();
  290. }
  291. static int get_nr_hw_irqs(void)
  292. {
  293. int ret = 1;
  294. #ifdef CONFIG_X86_IO_APIC
  295. ret = get_nr_irqs_gsi();
  296. #endif
  297. return ret;
  298. }
  299. static int find_unbound_irq(void)
  300. {
  301. struct irq_data *data;
  302. int irq, res;
  303. int start = get_nr_hw_irqs();
  304. if (start == nr_irqs)
  305. goto no_irqs;
  306. /* nr_irqs is a magic value. Must not use it.*/
  307. for (irq = nr_irqs-1; irq > start; irq--) {
  308. data = irq_get_irq_data(irq);
  309. /* only 0->15 have init'd desc; handle irq > 16 */
  310. if (!data)
  311. break;
  312. if (data->chip == &no_irq_chip)
  313. break;
  314. if (data->chip != &xen_dynamic_chip)
  315. continue;
  316. if (irq_info[irq].type == IRQT_UNBOUND)
  317. return irq;
  318. }
  319. if (irq == start)
  320. goto no_irqs;
  321. res = irq_alloc_desc_at(irq, 0);
  322. if (WARN_ON(res != irq))
  323. return -1;
  324. return irq;
  325. no_irqs:
  326. panic("No available IRQ to bind to: increase nr_irqs!\n");
  327. }
  328. static bool identity_mapped_irq(unsigned irq)
  329. {
  330. /* identity map all the hardware irqs */
  331. return irq < get_nr_hw_irqs();
  332. }
  333. static void pirq_unmask_notify(int irq)
  334. {
  335. struct physdev_eoi eoi = { .irq = irq };
  336. if (unlikely(pirq_needs_eoi(irq))) {
  337. int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
  338. WARN_ON(rc);
  339. }
  340. }
  341. static void pirq_query_unmask(int irq)
  342. {
  343. struct physdev_irq_status_query irq_status;
  344. struct irq_info *info = info_for_irq(irq);
  345. BUG_ON(info->type != IRQT_PIRQ);
  346. irq_status.irq = irq;
  347. if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
  348. irq_status.flags = 0;
  349. info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
  350. if (irq_status.flags & XENIRQSTAT_needs_eoi)
  351. info->u.pirq.flags |= PIRQ_NEEDS_EOI;
  352. }
  353. static bool probing_irq(int irq)
  354. {
  355. struct irq_desc *desc = irq_to_desc(irq);
  356. return desc && desc->action == NULL;
  357. }
  358. static unsigned int startup_pirq(unsigned int irq)
  359. {
  360. struct evtchn_bind_pirq bind_pirq;
  361. struct irq_info *info = info_for_irq(irq);
  362. int evtchn = evtchn_from_irq(irq);
  363. BUG_ON(info->type != IRQT_PIRQ);
  364. if (VALID_EVTCHN(evtchn))
  365. goto out;
  366. bind_pirq.pirq = irq;
  367. /* NB. We are happy to share unless we are probing. */
  368. bind_pirq.flags = probing_irq(irq) ? 0 : BIND_PIRQ__WILL_SHARE;
  369. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq) != 0) {
  370. if (!probing_irq(irq))
  371. printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
  372. irq);
  373. return 0;
  374. }
  375. evtchn = bind_pirq.port;
  376. pirq_query_unmask(irq);
  377. evtchn_to_irq[evtchn] = irq;
  378. bind_evtchn_to_cpu(evtchn, 0);
  379. info->evtchn = evtchn;
  380. out:
  381. unmask_evtchn(evtchn);
  382. pirq_unmask_notify(irq);
  383. return 0;
  384. }
  385. static void shutdown_pirq(unsigned int irq)
  386. {
  387. struct evtchn_close close;
  388. struct irq_info *info = info_for_irq(irq);
  389. int evtchn = evtchn_from_irq(irq);
  390. BUG_ON(info->type != IRQT_PIRQ);
  391. if (!VALID_EVTCHN(evtchn))
  392. return;
  393. mask_evtchn(evtchn);
  394. close.port = evtchn;
  395. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  396. BUG();
  397. bind_evtchn_to_cpu(evtchn, 0);
  398. evtchn_to_irq[evtchn] = -1;
  399. info->evtchn = 0;
  400. }
  401. static void enable_pirq(unsigned int irq)
  402. {
  403. startup_pirq(irq);
  404. }
  405. static void disable_pirq(unsigned int irq)
  406. {
  407. }
  408. static void ack_pirq(unsigned int irq)
  409. {
  410. int evtchn = evtchn_from_irq(irq);
  411. move_native_irq(irq);
  412. if (VALID_EVTCHN(evtchn)) {
  413. mask_evtchn(evtchn);
  414. clear_evtchn(evtchn);
  415. }
  416. }
  417. static void end_pirq(unsigned int irq)
  418. {
  419. int evtchn = evtchn_from_irq(irq);
  420. struct irq_desc *desc = irq_to_desc(irq);
  421. if (WARN_ON(!desc))
  422. return;
  423. if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
  424. (IRQ_DISABLED|IRQ_PENDING)) {
  425. shutdown_pirq(irq);
  426. } else if (VALID_EVTCHN(evtchn)) {
  427. unmask_evtchn(evtchn);
  428. pirq_unmask_notify(irq);
  429. }
  430. }
  431. static int find_irq_by_gsi(unsigned gsi)
  432. {
  433. int irq;
  434. for (irq = 0; irq < nr_irqs; irq++) {
  435. struct irq_info *info = info_for_irq(irq);
  436. if (info == NULL || info->type != IRQT_PIRQ)
  437. continue;
  438. if (gsi_from_irq(irq) == gsi)
  439. return irq;
  440. }
  441. return -1;
  442. }
  443. /* xen_allocate_irq might allocate irqs from the top down, as a
  444. * consequence don't assume that the irq number returned has a low value
  445. * or can be used as a pirq number unless you know otherwise.
  446. *
  447. * One notable exception is when xen_allocate_irq is called passing an
  448. * hardware gsi as argument, in that case the irq number returned
  449. * matches the gsi number passed as first argument.
  450. * Note: We don't assign an
  451. * event channel until the irq actually started up. Return an
  452. * existing irq if we've already got one for the gsi.
  453. */
  454. int xen_allocate_pirq(unsigned gsi, char *name)
  455. {
  456. int irq;
  457. struct physdev_irq irq_op;
  458. spin_lock(&irq_mapping_update_lock);
  459. irq = find_irq_by_gsi(gsi);
  460. if (irq != -1) {
  461. printk(KERN_INFO "xen_allocate_pirq: returning irq %d for gsi %u\n",
  462. irq, gsi);
  463. goto out; /* XXX need refcount? */
  464. }
  465. if (identity_mapped_irq(gsi)) {
  466. irq = gsi;
  467. irq_to_desc_alloc_node(irq, 0);
  468. dynamic_irq_init(irq);
  469. } else
  470. irq = find_unbound_irq();
  471. set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
  472. handle_level_irq, name);
  473. irq_op.irq = irq;
  474. if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
  475. dynamic_irq_cleanup(irq);
  476. irq = -ENOSPC;
  477. goto out;
  478. }
  479. irq_info[irq] = mk_pirq_info(0, gsi, irq_op.vector);
  480. out:
  481. spin_unlock(&irq_mapping_update_lock);
  482. return irq;
  483. }
  484. int xen_vector_from_irq(unsigned irq)
  485. {
  486. return vector_from_irq(irq);
  487. }
  488. int xen_gsi_from_irq(unsigned irq)
  489. {
  490. return gsi_from_irq(irq);
  491. }
  492. int bind_evtchn_to_irq(unsigned int evtchn)
  493. {
  494. int irq;
  495. spin_lock(&irq_mapping_update_lock);
  496. irq = evtchn_to_irq[evtchn];
  497. if (irq == -1) {
  498. irq = find_unbound_irq();
  499. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  500. handle_edge_irq, "event");
  501. evtchn_to_irq[evtchn] = irq;
  502. irq_info[irq] = mk_evtchn_info(evtchn);
  503. }
  504. spin_unlock(&irq_mapping_update_lock);
  505. return irq;
  506. }
  507. EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
  508. static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
  509. {
  510. struct evtchn_bind_ipi bind_ipi;
  511. int evtchn, irq;
  512. spin_lock(&irq_mapping_update_lock);
  513. irq = per_cpu(ipi_to_irq, cpu)[ipi];
  514. if (irq == -1) {
  515. irq = find_unbound_irq();
  516. if (irq < 0)
  517. goto out;
  518. set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
  519. handle_percpu_irq, "ipi");
  520. bind_ipi.vcpu = cpu;
  521. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  522. &bind_ipi) != 0)
  523. BUG();
  524. evtchn = bind_ipi.port;
  525. evtchn_to_irq[evtchn] = irq;
  526. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  527. per_cpu(ipi_to_irq, cpu)[ipi] = irq;
  528. bind_evtchn_to_cpu(evtchn, cpu);
  529. }
  530. out:
  531. spin_unlock(&irq_mapping_update_lock);
  532. return irq;
  533. }
  534. static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
  535. {
  536. struct evtchn_bind_virq bind_virq;
  537. int evtchn, irq;
  538. spin_lock(&irq_mapping_update_lock);
  539. irq = per_cpu(virq_to_irq, cpu)[virq];
  540. if (irq == -1) {
  541. bind_virq.virq = virq;
  542. bind_virq.vcpu = cpu;
  543. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  544. &bind_virq) != 0)
  545. BUG();
  546. evtchn = bind_virq.port;
  547. irq = find_unbound_irq();
  548. set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
  549. handle_percpu_irq, "virq");
  550. evtchn_to_irq[evtchn] = irq;
  551. irq_info[irq] = mk_virq_info(evtchn, virq);
  552. per_cpu(virq_to_irq, cpu)[virq] = irq;
  553. bind_evtchn_to_cpu(evtchn, cpu);
  554. }
  555. spin_unlock(&irq_mapping_update_lock);
  556. return irq;
  557. }
  558. static void unbind_from_irq(unsigned int irq)
  559. {
  560. struct evtchn_close close;
  561. int evtchn = evtchn_from_irq(irq);
  562. spin_lock(&irq_mapping_update_lock);
  563. if (VALID_EVTCHN(evtchn)) {
  564. close.port = evtchn;
  565. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  566. BUG();
  567. switch (type_from_irq(irq)) {
  568. case IRQT_VIRQ:
  569. per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
  570. [virq_from_irq(irq)] = -1;
  571. break;
  572. case IRQT_IPI:
  573. per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
  574. [ipi_from_irq(irq)] = -1;
  575. break;
  576. default:
  577. break;
  578. }
  579. /* Closed ports are implicitly re-bound to VCPU0. */
  580. bind_evtchn_to_cpu(evtchn, 0);
  581. evtchn_to_irq[evtchn] = -1;
  582. }
  583. if (irq_info[irq].type != IRQT_UNBOUND) {
  584. irq_info[irq] = mk_unbound_info();
  585. irq_free_desc(irq);
  586. }
  587. spin_unlock(&irq_mapping_update_lock);
  588. }
  589. int bind_evtchn_to_irqhandler(unsigned int evtchn,
  590. irq_handler_t handler,
  591. unsigned long irqflags,
  592. const char *devname, void *dev_id)
  593. {
  594. unsigned int irq;
  595. int retval;
  596. irq = bind_evtchn_to_irq(evtchn);
  597. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  598. if (retval != 0) {
  599. unbind_from_irq(irq);
  600. return retval;
  601. }
  602. return irq;
  603. }
  604. EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
  605. int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
  606. irq_handler_t handler,
  607. unsigned long irqflags, const char *devname, void *dev_id)
  608. {
  609. unsigned int irq;
  610. int retval;
  611. irq = bind_virq_to_irq(virq, cpu);
  612. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  613. if (retval != 0) {
  614. unbind_from_irq(irq);
  615. return retval;
  616. }
  617. return irq;
  618. }
  619. EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
  620. int bind_ipi_to_irqhandler(enum ipi_vector ipi,
  621. unsigned int cpu,
  622. irq_handler_t handler,
  623. unsigned long irqflags,
  624. const char *devname,
  625. void *dev_id)
  626. {
  627. int irq, retval;
  628. irq = bind_ipi_to_irq(ipi, cpu);
  629. if (irq < 0)
  630. return irq;
  631. irqflags |= IRQF_NO_SUSPEND;
  632. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  633. if (retval != 0) {
  634. unbind_from_irq(irq);
  635. return retval;
  636. }
  637. return irq;
  638. }
  639. void unbind_from_irqhandler(unsigned int irq, void *dev_id)
  640. {
  641. free_irq(irq, dev_id);
  642. unbind_from_irq(irq);
  643. }
  644. EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
  645. void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
  646. {
  647. int irq = per_cpu(ipi_to_irq, cpu)[vector];
  648. BUG_ON(irq < 0);
  649. notify_remote_via_irq(irq);
  650. }
  651. irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
  652. {
  653. struct shared_info *sh = HYPERVISOR_shared_info;
  654. int cpu = smp_processor_id();
  655. int i;
  656. unsigned long flags;
  657. static DEFINE_SPINLOCK(debug_lock);
  658. spin_lock_irqsave(&debug_lock, flags);
  659. printk("vcpu %d\n ", cpu);
  660. for_each_online_cpu(i) {
  661. struct vcpu_info *v = per_cpu(xen_vcpu, i);
  662. printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
  663. (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
  664. v->evtchn_upcall_pending,
  665. v->evtchn_pending_sel);
  666. }
  667. printk("pending:\n ");
  668. for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
  669. printk("%08lx%s", sh->evtchn_pending[i],
  670. i % 8 == 0 ? "\n " : " ");
  671. printk("\nmasks:\n ");
  672. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  673. printk("%08lx%s", sh->evtchn_mask[i],
  674. i % 8 == 0 ? "\n " : " ");
  675. printk("\nunmasked:\n ");
  676. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  677. printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
  678. i % 8 == 0 ? "\n " : " ");
  679. printk("\npending list:\n");
  680. for(i = 0; i < NR_EVENT_CHANNELS; i++) {
  681. if (sync_test_bit(i, sh->evtchn_pending)) {
  682. printk(" %d: event %d -> irq %d\n",
  683. cpu_from_evtchn(i), i,
  684. evtchn_to_irq[i]);
  685. }
  686. }
  687. spin_unlock_irqrestore(&debug_lock, flags);
  688. return IRQ_HANDLED;
  689. }
  690. static DEFINE_PER_CPU(unsigned, xed_nesting_count);
  691. /*
  692. * Search the CPUs pending events bitmasks. For each one found, map
  693. * the event number to an irq, and feed it into do_IRQ() for
  694. * handling.
  695. *
  696. * Xen uses a two-level bitmap to speed searching. The first level is
  697. * a bitset of words which contain pending event bits. The second
  698. * level is a bitset of pending events themselves.
  699. */
  700. static void __xen_evtchn_do_upcall(void)
  701. {
  702. int cpu = get_cpu();
  703. struct shared_info *s = HYPERVISOR_shared_info;
  704. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  705. unsigned count;
  706. do {
  707. unsigned long pending_words;
  708. vcpu_info->evtchn_upcall_pending = 0;
  709. if (__get_cpu_var(xed_nesting_count)++)
  710. goto out;
  711. #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
  712. /* Clear master flag /before/ clearing selector flag. */
  713. wmb();
  714. #endif
  715. pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
  716. while (pending_words != 0) {
  717. unsigned long pending_bits;
  718. int word_idx = __ffs(pending_words);
  719. pending_words &= ~(1UL << word_idx);
  720. while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
  721. int bit_idx = __ffs(pending_bits);
  722. int port = (word_idx * BITS_PER_LONG) + bit_idx;
  723. int irq = evtchn_to_irq[port];
  724. struct irq_desc *desc;
  725. if (irq != -1) {
  726. desc = irq_to_desc(irq);
  727. if (desc)
  728. generic_handle_irq_desc(irq, desc);
  729. }
  730. }
  731. }
  732. BUG_ON(!irqs_disabled());
  733. count = __get_cpu_var(xed_nesting_count);
  734. __get_cpu_var(xed_nesting_count) = 0;
  735. } while (count != 1 || vcpu_info->evtchn_upcall_pending);
  736. out:
  737. put_cpu();
  738. }
  739. void xen_evtchn_do_upcall(struct pt_regs *regs)
  740. {
  741. struct pt_regs *old_regs = set_irq_regs(regs);
  742. exit_idle();
  743. irq_enter();
  744. __xen_evtchn_do_upcall();
  745. irq_exit();
  746. set_irq_regs(old_regs);
  747. }
  748. void xen_hvm_evtchn_do_upcall(void)
  749. {
  750. __xen_evtchn_do_upcall();
  751. }
  752. EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
  753. /* Rebind a new event channel to an existing irq. */
  754. void rebind_evtchn_irq(int evtchn, int irq)
  755. {
  756. struct irq_info *info = info_for_irq(irq);
  757. /* Make sure the irq is masked, since the new event channel
  758. will also be masked. */
  759. disable_irq(irq);
  760. spin_lock(&irq_mapping_update_lock);
  761. /* After resume the irq<->evtchn mappings are all cleared out */
  762. BUG_ON(evtchn_to_irq[evtchn] != -1);
  763. /* Expect irq to have been bound before,
  764. so there should be a proper type */
  765. BUG_ON(info->type == IRQT_UNBOUND);
  766. evtchn_to_irq[evtchn] = irq;
  767. irq_info[irq] = mk_evtchn_info(evtchn);
  768. spin_unlock(&irq_mapping_update_lock);
  769. /* new event channels are always bound to cpu 0 */
  770. irq_set_affinity(irq, cpumask_of(0));
  771. /* Unmask the event channel. */
  772. enable_irq(irq);
  773. }
  774. /* Rebind an evtchn so that it gets delivered to a specific cpu */
  775. static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
  776. {
  777. struct evtchn_bind_vcpu bind_vcpu;
  778. int evtchn = evtchn_from_irq(irq);
  779. /* events delivered via platform PCI interrupts are always
  780. * routed to vcpu 0 */
  781. if (!VALID_EVTCHN(evtchn) ||
  782. (xen_hvm_domain() && !xen_have_vector_callback))
  783. return -1;
  784. /* Send future instances of this interrupt to other vcpu. */
  785. bind_vcpu.port = evtchn;
  786. bind_vcpu.vcpu = tcpu;
  787. /*
  788. * If this fails, it usually just indicates that we're dealing with a
  789. * virq or IPI channel, which don't actually need to be rebound. Ignore
  790. * it, but don't do the xenlinux-level rebind in that case.
  791. */
  792. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
  793. bind_evtchn_to_cpu(evtchn, tcpu);
  794. return 0;
  795. }
  796. static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
  797. {
  798. unsigned tcpu = cpumask_first(dest);
  799. return rebind_irq_to_cpu(irq, tcpu);
  800. }
  801. int resend_irq_on_evtchn(unsigned int irq)
  802. {
  803. int masked, evtchn = evtchn_from_irq(irq);
  804. struct shared_info *s = HYPERVISOR_shared_info;
  805. if (!VALID_EVTCHN(evtchn))
  806. return 1;
  807. masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
  808. sync_set_bit(evtchn, s->evtchn_pending);
  809. if (!masked)
  810. unmask_evtchn(evtchn);
  811. return 1;
  812. }
  813. static void enable_dynirq(unsigned int irq)
  814. {
  815. int evtchn = evtchn_from_irq(irq);
  816. if (VALID_EVTCHN(evtchn))
  817. unmask_evtchn(evtchn);
  818. }
  819. static void disable_dynirq(unsigned int irq)
  820. {
  821. int evtchn = evtchn_from_irq(irq);
  822. if (VALID_EVTCHN(evtchn))
  823. mask_evtchn(evtchn);
  824. }
  825. static void ack_dynirq(unsigned int irq)
  826. {
  827. int evtchn = evtchn_from_irq(irq);
  828. move_native_irq(irq);
  829. if (VALID_EVTCHN(evtchn))
  830. clear_evtchn(evtchn);
  831. }
  832. static int retrigger_dynirq(unsigned int irq)
  833. {
  834. int evtchn = evtchn_from_irq(irq);
  835. struct shared_info *sh = HYPERVISOR_shared_info;
  836. int ret = 0;
  837. if (VALID_EVTCHN(evtchn)) {
  838. int masked;
  839. masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
  840. sync_set_bit(evtchn, sh->evtchn_pending);
  841. if (!masked)
  842. unmask_evtchn(evtchn);
  843. ret = 1;
  844. }
  845. return ret;
  846. }
  847. static void restore_cpu_virqs(unsigned int cpu)
  848. {
  849. struct evtchn_bind_virq bind_virq;
  850. int virq, irq, evtchn;
  851. for (virq = 0; virq < NR_VIRQS; virq++) {
  852. if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
  853. continue;
  854. BUG_ON(virq_from_irq(irq) != virq);
  855. /* Get a new binding from Xen. */
  856. bind_virq.virq = virq;
  857. bind_virq.vcpu = cpu;
  858. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  859. &bind_virq) != 0)
  860. BUG();
  861. evtchn = bind_virq.port;
  862. /* Record the new mapping. */
  863. evtchn_to_irq[evtchn] = irq;
  864. irq_info[irq] = mk_virq_info(evtchn, virq);
  865. bind_evtchn_to_cpu(evtchn, cpu);
  866. /* Ready for use. */
  867. unmask_evtchn(evtchn);
  868. }
  869. }
  870. static void restore_cpu_ipis(unsigned int cpu)
  871. {
  872. struct evtchn_bind_ipi bind_ipi;
  873. int ipi, irq, evtchn;
  874. for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
  875. if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
  876. continue;
  877. BUG_ON(ipi_from_irq(irq) != ipi);
  878. /* Get a new binding from Xen. */
  879. bind_ipi.vcpu = cpu;
  880. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  881. &bind_ipi) != 0)
  882. BUG();
  883. evtchn = bind_ipi.port;
  884. /* Record the new mapping. */
  885. evtchn_to_irq[evtchn] = irq;
  886. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  887. bind_evtchn_to_cpu(evtchn, cpu);
  888. /* Ready for use. */
  889. unmask_evtchn(evtchn);
  890. }
  891. }
  892. /* Clear an irq's pending state, in preparation for polling on it */
  893. void xen_clear_irq_pending(int irq)
  894. {
  895. int evtchn = evtchn_from_irq(irq);
  896. if (VALID_EVTCHN(evtchn))
  897. clear_evtchn(evtchn);
  898. }
  899. void xen_set_irq_pending(int irq)
  900. {
  901. int evtchn = evtchn_from_irq(irq);
  902. if (VALID_EVTCHN(evtchn))
  903. set_evtchn(evtchn);
  904. }
  905. bool xen_test_irq_pending(int irq)
  906. {
  907. int evtchn = evtchn_from_irq(irq);
  908. bool ret = false;
  909. if (VALID_EVTCHN(evtchn))
  910. ret = test_evtchn(evtchn);
  911. return ret;
  912. }
  913. /* Poll waiting for an irq to become pending. In the usual case, the
  914. irq will be disabled so it won't deliver an interrupt. */
  915. void xen_poll_irq(int irq)
  916. {
  917. evtchn_port_t evtchn = evtchn_from_irq(irq);
  918. if (VALID_EVTCHN(evtchn)) {
  919. struct sched_poll poll;
  920. poll.nr_ports = 1;
  921. poll.timeout = 0;
  922. set_xen_guest_handle(poll.ports, &evtchn);
  923. if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
  924. BUG();
  925. }
  926. }
  927. void xen_irq_resume(void)
  928. {
  929. unsigned int cpu, irq, evtchn;
  930. init_evtchn_cpu_bindings();
  931. /* New event-channel space is not 'live' yet. */
  932. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  933. mask_evtchn(evtchn);
  934. /* No IRQ <-> event-channel mappings. */
  935. for (irq = 0; irq < nr_irqs; irq++)
  936. irq_info[irq].evtchn = 0; /* zap event-channel binding */
  937. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  938. evtchn_to_irq[evtchn] = -1;
  939. for_each_possible_cpu(cpu) {
  940. restore_cpu_virqs(cpu);
  941. restore_cpu_ipis(cpu);
  942. }
  943. }
  944. static struct irq_chip xen_dynamic_chip __read_mostly = {
  945. .name = "xen-dyn",
  946. .disable = disable_dynirq,
  947. .mask = disable_dynirq,
  948. .unmask = enable_dynirq,
  949. .ack = ack_dynirq,
  950. .set_affinity = set_affinity_irq,
  951. .retrigger = retrigger_dynirq,
  952. };
  953. static struct irq_chip xen_pirq_chip __read_mostly = {
  954. .name = "xen-pirq",
  955. .startup = startup_pirq,
  956. .shutdown = shutdown_pirq,
  957. .enable = enable_pirq,
  958. .unmask = enable_pirq,
  959. .disable = disable_pirq,
  960. .mask = disable_pirq,
  961. .ack = ack_pirq,
  962. .end = end_pirq,
  963. .set_affinity = set_affinity_irq,
  964. .retrigger = retrigger_dynirq,
  965. };
  966. static struct irq_chip xen_percpu_chip __read_mostly = {
  967. .name = "xen-percpu",
  968. .disable = disable_dynirq,
  969. .mask = disable_dynirq,
  970. .unmask = enable_dynirq,
  971. .ack = ack_dynirq,
  972. };
  973. int xen_set_callback_via(uint64_t via)
  974. {
  975. struct xen_hvm_param a;
  976. a.domid = DOMID_SELF;
  977. a.index = HVM_PARAM_CALLBACK_IRQ;
  978. a.value = via;
  979. return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
  980. }
  981. EXPORT_SYMBOL_GPL(xen_set_callback_via);
  982. #ifdef CONFIG_XEN_PVHVM
  983. /* Vector callbacks are better than PCI interrupts to receive event
  984. * channel notifications because we can receive vector callbacks on any
  985. * vcpu and we don't need PCI support or APIC interactions. */
  986. void xen_callback_vector(void)
  987. {
  988. int rc;
  989. uint64_t callback_via;
  990. if (xen_have_vector_callback) {
  991. callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
  992. rc = xen_set_callback_via(callback_via);
  993. if (rc) {
  994. printk(KERN_ERR "Request for Xen HVM callback vector"
  995. " failed.\n");
  996. xen_have_vector_callback = 0;
  997. return;
  998. }
  999. printk(KERN_INFO "Xen HVM callback vector for event delivery is "
  1000. "enabled\n");
  1001. /* in the restore case the vector has already been allocated */
  1002. if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
  1003. alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
  1004. }
  1005. }
  1006. #else
  1007. void xen_callback_vector(void) {}
  1008. #endif
  1009. void __init xen_init_IRQ(void)
  1010. {
  1011. int i;
  1012. cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
  1013. GFP_KERNEL);
  1014. irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
  1015. evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
  1016. GFP_KERNEL);
  1017. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  1018. evtchn_to_irq[i] = -1;
  1019. init_evtchn_cpu_bindings();
  1020. /* No event channels are 'live' right now. */
  1021. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  1022. mask_evtchn(i);
  1023. if (xen_hvm_domain()) {
  1024. xen_callback_vector();
  1025. native_init_IRQ();
  1026. } else {
  1027. irq_ctx_init(smp_processor_id());
  1028. }
  1029. }