events.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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 <linux/pci.h>
  32. #include <asm/desc.h>
  33. #include <asm/ptrace.h>
  34. #include <asm/irq.h>
  35. #include <asm/idle.h>
  36. #include <asm/io_apic.h>
  37. #include <asm/sync_bitops.h>
  38. #include <asm/xen/pci.h>
  39. #include <asm/xen/hypercall.h>
  40. #include <asm/xen/hypervisor.h>
  41. #include <xen/xen.h>
  42. #include <xen/hvm.h>
  43. #include <xen/xen-ops.h>
  44. #include <xen/events.h>
  45. #include <xen/interface/xen.h>
  46. #include <xen/interface/event_channel.h>
  47. #include <xen/interface/hvm/hvm_op.h>
  48. #include <xen/interface/hvm/params.h>
  49. /*
  50. * This lock protects updates to the following mapping and reference-count
  51. * arrays. The lock does not need to be acquired to read the mapping tables.
  52. */
  53. static DEFINE_SPINLOCK(irq_mapping_update_lock);
  54. /* IRQ <-> VIRQ mapping. */
  55. static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
  56. /* IRQ <-> IPI mapping */
  57. static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
  58. /* Interrupt types. */
  59. enum xen_irq_type {
  60. IRQT_UNBOUND = 0,
  61. IRQT_PIRQ,
  62. IRQT_VIRQ,
  63. IRQT_IPI,
  64. IRQT_EVTCHN
  65. };
  66. /*
  67. * Packed IRQ information:
  68. * type - enum xen_irq_type
  69. * event channel - irq->event channel mapping
  70. * cpu - cpu this event channel is bound to
  71. * index - type-specific information:
  72. * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
  73. * guest, or GSI (real passthrough IRQ) of the device.
  74. * VIRQ - virq number
  75. * IPI - IPI vector
  76. * EVTCHN -
  77. */
  78. struct irq_info
  79. {
  80. enum xen_irq_type type; /* type */
  81. unsigned short evtchn; /* event channel */
  82. unsigned short cpu; /* cpu bound */
  83. union {
  84. unsigned short virq;
  85. enum ipi_vector ipi;
  86. struct {
  87. unsigned short pirq;
  88. unsigned short gsi;
  89. unsigned char vector;
  90. unsigned char flags;
  91. } pirq;
  92. } u;
  93. };
  94. #define PIRQ_NEEDS_EOI (1 << 0)
  95. #define PIRQ_SHAREABLE (1 << 1)
  96. static struct irq_info *irq_info;
  97. static int *pirq_to_irq;
  98. static int *evtchn_to_irq;
  99. struct cpu_evtchn_s {
  100. unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
  101. };
  102. static __initdata struct cpu_evtchn_s init_evtchn_mask = {
  103. .bits[0 ... (NR_EVENT_CHANNELS/BITS_PER_LONG)-1] = ~0ul,
  104. };
  105. static struct cpu_evtchn_s *cpu_evtchn_mask_p = &init_evtchn_mask;
  106. static inline unsigned long *cpu_evtchn_mask(int cpu)
  107. {
  108. return cpu_evtchn_mask_p[cpu].bits;
  109. }
  110. /* Xen will never allocate port zero for any purpose. */
  111. #define VALID_EVTCHN(chn) ((chn) != 0)
  112. static struct irq_chip xen_dynamic_chip;
  113. static struct irq_chip xen_percpu_chip;
  114. static struct irq_chip xen_pirq_chip;
  115. /* Constructor for packed IRQ information. */
  116. static struct irq_info mk_unbound_info(void)
  117. {
  118. return (struct irq_info) { .type = IRQT_UNBOUND };
  119. }
  120. static struct irq_info mk_evtchn_info(unsigned short evtchn)
  121. {
  122. return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
  123. .cpu = 0 };
  124. }
  125. static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
  126. {
  127. return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
  128. .cpu = 0, .u.ipi = ipi };
  129. }
  130. static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
  131. {
  132. return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
  133. .cpu = 0, .u.virq = virq };
  134. }
  135. static struct irq_info mk_pirq_info(unsigned short evtchn, unsigned short pirq,
  136. unsigned short gsi, unsigned short vector)
  137. {
  138. return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
  139. .cpu = 0,
  140. .u.pirq = { .pirq = pirq, .gsi = gsi, .vector = vector } };
  141. }
  142. /*
  143. * Accessors for packed IRQ information.
  144. */
  145. static struct irq_info *info_for_irq(unsigned irq)
  146. {
  147. return &irq_info[irq];
  148. }
  149. static unsigned int evtchn_from_irq(unsigned irq)
  150. {
  151. if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
  152. return 0;
  153. return info_for_irq(irq)->evtchn;
  154. }
  155. unsigned irq_from_evtchn(unsigned int evtchn)
  156. {
  157. return evtchn_to_irq[evtchn];
  158. }
  159. EXPORT_SYMBOL_GPL(irq_from_evtchn);
  160. static enum ipi_vector ipi_from_irq(unsigned irq)
  161. {
  162. struct irq_info *info = info_for_irq(irq);
  163. BUG_ON(info == NULL);
  164. BUG_ON(info->type != IRQT_IPI);
  165. return info->u.ipi;
  166. }
  167. static unsigned virq_from_irq(unsigned irq)
  168. {
  169. struct irq_info *info = info_for_irq(irq);
  170. BUG_ON(info == NULL);
  171. BUG_ON(info->type != IRQT_VIRQ);
  172. return info->u.virq;
  173. }
  174. static unsigned pirq_from_irq(unsigned irq)
  175. {
  176. struct irq_info *info = info_for_irq(irq);
  177. BUG_ON(info == NULL);
  178. BUG_ON(info->type != IRQT_PIRQ);
  179. return info->u.pirq.pirq;
  180. }
  181. static unsigned gsi_from_irq(unsigned irq)
  182. {
  183. struct irq_info *info = info_for_irq(irq);
  184. BUG_ON(info == NULL);
  185. BUG_ON(info->type != IRQT_PIRQ);
  186. return info->u.pirq.gsi;
  187. }
  188. static enum xen_irq_type type_from_irq(unsigned irq)
  189. {
  190. return info_for_irq(irq)->type;
  191. }
  192. static unsigned cpu_from_irq(unsigned irq)
  193. {
  194. return info_for_irq(irq)->cpu;
  195. }
  196. static unsigned int cpu_from_evtchn(unsigned int evtchn)
  197. {
  198. int irq = evtchn_to_irq[evtchn];
  199. unsigned ret = 0;
  200. if (irq != -1)
  201. ret = cpu_from_irq(irq);
  202. return ret;
  203. }
  204. static bool pirq_needs_eoi(unsigned irq)
  205. {
  206. struct irq_info *info = info_for_irq(irq);
  207. BUG_ON(info->type != IRQT_PIRQ);
  208. return info->u.pirq.flags & PIRQ_NEEDS_EOI;
  209. }
  210. static inline unsigned long active_evtchns(unsigned int cpu,
  211. struct shared_info *sh,
  212. unsigned int idx)
  213. {
  214. return (sh->evtchn_pending[idx] &
  215. cpu_evtchn_mask(cpu)[idx] &
  216. ~sh->evtchn_mask[idx]);
  217. }
  218. static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
  219. {
  220. int irq = evtchn_to_irq[chn];
  221. BUG_ON(irq == -1);
  222. #ifdef CONFIG_SMP
  223. cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
  224. #endif
  225. clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
  226. set_bit(chn, cpu_evtchn_mask(cpu));
  227. irq_info[irq].cpu = cpu;
  228. }
  229. static void init_evtchn_cpu_bindings(void)
  230. {
  231. int i;
  232. #ifdef CONFIG_SMP
  233. struct irq_desc *desc;
  234. /* By default all event channels notify CPU#0. */
  235. for_each_irq_desc(i, desc) {
  236. cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
  237. }
  238. #endif
  239. for_each_possible_cpu(i)
  240. memset(cpu_evtchn_mask(i),
  241. (i == 0) ? ~0 : 0, sizeof(struct cpu_evtchn_s));
  242. }
  243. static inline void clear_evtchn(int port)
  244. {
  245. struct shared_info *s = HYPERVISOR_shared_info;
  246. sync_clear_bit(port, &s->evtchn_pending[0]);
  247. }
  248. static inline void set_evtchn(int port)
  249. {
  250. struct shared_info *s = HYPERVISOR_shared_info;
  251. sync_set_bit(port, &s->evtchn_pending[0]);
  252. }
  253. static inline int test_evtchn(int port)
  254. {
  255. struct shared_info *s = HYPERVISOR_shared_info;
  256. return sync_test_bit(port, &s->evtchn_pending[0]);
  257. }
  258. /**
  259. * notify_remote_via_irq - send event to remote end of event channel via irq
  260. * @irq: irq of event channel to send event to
  261. *
  262. * Unlike notify_remote_via_evtchn(), this is safe to use across
  263. * save/restore. Notifications on a broken connection are silently
  264. * dropped.
  265. */
  266. void notify_remote_via_irq(int irq)
  267. {
  268. int evtchn = evtchn_from_irq(irq);
  269. if (VALID_EVTCHN(evtchn))
  270. notify_remote_via_evtchn(evtchn);
  271. }
  272. EXPORT_SYMBOL_GPL(notify_remote_via_irq);
  273. static void mask_evtchn(int port)
  274. {
  275. struct shared_info *s = HYPERVISOR_shared_info;
  276. sync_set_bit(port, &s->evtchn_mask[0]);
  277. }
  278. static void unmask_evtchn(int port)
  279. {
  280. struct shared_info *s = HYPERVISOR_shared_info;
  281. unsigned int cpu = get_cpu();
  282. BUG_ON(!irqs_disabled());
  283. /* Slow path (hypercall) if this is a non-local port. */
  284. if (unlikely(cpu != cpu_from_evtchn(port))) {
  285. struct evtchn_unmask unmask = { .port = port };
  286. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  287. } else {
  288. struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
  289. sync_clear_bit(port, &s->evtchn_mask[0]);
  290. /*
  291. * The following is basically the equivalent of
  292. * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
  293. * the interrupt edge' if the channel is masked.
  294. */
  295. if (sync_test_bit(port, &s->evtchn_pending[0]) &&
  296. !sync_test_and_set_bit(port / BITS_PER_LONG,
  297. &vcpu_info->evtchn_pending_sel))
  298. vcpu_info->evtchn_upcall_pending = 1;
  299. }
  300. put_cpu();
  301. }
  302. static int xen_allocate_irq_dynamic(void)
  303. {
  304. int first = 0;
  305. int irq;
  306. #ifdef CONFIG_X86_IO_APIC
  307. /*
  308. * For an HVM guest or domain 0 which see "real" (emulated or
  309. * actual repectively) GSIs we allocate dynamic IRQs
  310. * e.g. those corresponding to event channels or MSIs
  311. * etc. from the range above those "real" GSIs to avoid
  312. * collisions.
  313. */
  314. if (xen_initial_domain() || xen_hvm_domain())
  315. first = get_nr_irqs_gsi();
  316. #endif
  317. retry:
  318. irq = irq_alloc_desc_from(first, -1);
  319. if (irq == -ENOMEM && first > NR_IRQS_LEGACY) {
  320. printk(KERN_ERR "Out of dynamic IRQ space and eating into GSI space. You should increase nr_irqs\n");
  321. first = max(NR_IRQS_LEGACY, first - NR_IRQS_LEGACY);
  322. goto retry;
  323. }
  324. if (irq < 0)
  325. panic("No available IRQ to bind to: increase nr_irqs!\n");
  326. return irq;
  327. }
  328. static int xen_allocate_irq_gsi(unsigned gsi)
  329. {
  330. int irq;
  331. /*
  332. * A PV guest has no concept of a GSI (since it has no ACPI
  333. * nor access to/knowledge of the physical APICs). Therefore
  334. * all IRQs are dynamically allocated from the entire IRQ
  335. * space.
  336. */
  337. if (xen_pv_domain() && !xen_initial_domain())
  338. return xen_allocate_irq_dynamic();
  339. /* Legacy IRQ descriptors are already allocated by the arch. */
  340. if (gsi < NR_IRQS_LEGACY)
  341. return gsi;
  342. irq = irq_alloc_desc_at(gsi, -1);
  343. if (irq < 0)
  344. panic("Unable to allocate to IRQ%d (%d)\n", gsi, irq);
  345. return irq;
  346. }
  347. static void xen_free_irq(unsigned irq)
  348. {
  349. /* Legacy IRQ descriptors are managed by the arch. */
  350. if (irq < NR_IRQS_LEGACY)
  351. return;
  352. irq_free_desc(irq);
  353. }
  354. static void pirq_unmask_notify(int irq)
  355. {
  356. struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
  357. if (unlikely(pirq_needs_eoi(irq))) {
  358. int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
  359. WARN_ON(rc);
  360. }
  361. }
  362. static void pirq_query_unmask(int irq)
  363. {
  364. struct physdev_irq_status_query irq_status;
  365. struct irq_info *info = info_for_irq(irq);
  366. BUG_ON(info->type != IRQT_PIRQ);
  367. irq_status.irq = pirq_from_irq(irq);
  368. if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
  369. irq_status.flags = 0;
  370. info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
  371. if (irq_status.flags & XENIRQSTAT_needs_eoi)
  372. info->u.pirq.flags |= PIRQ_NEEDS_EOI;
  373. }
  374. static bool probing_irq(int irq)
  375. {
  376. struct irq_desc *desc = irq_to_desc(irq);
  377. return desc && desc->action == NULL;
  378. }
  379. static unsigned int __startup_pirq(unsigned int irq)
  380. {
  381. struct evtchn_bind_pirq bind_pirq;
  382. struct irq_info *info = info_for_irq(irq);
  383. int evtchn = evtchn_from_irq(irq);
  384. int rc;
  385. BUG_ON(info->type != IRQT_PIRQ);
  386. if (VALID_EVTCHN(evtchn))
  387. goto out;
  388. bind_pirq.pirq = pirq_from_irq(irq);
  389. /* NB. We are happy to share unless we are probing. */
  390. bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
  391. BIND_PIRQ__WILL_SHARE : 0;
  392. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
  393. if (rc != 0) {
  394. if (!probing_irq(irq))
  395. printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
  396. irq);
  397. return 0;
  398. }
  399. evtchn = bind_pirq.port;
  400. pirq_query_unmask(irq);
  401. evtchn_to_irq[evtchn] = irq;
  402. bind_evtchn_to_cpu(evtchn, 0);
  403. info->evtchn = evtchn;
  404. out:
  405. unmask_evtchn(evtchn);
  406. pirq_unmask_notify(irq);
  407. return 0;
  408. }
  409. static unsigned int startup_pirq(struct irq_data *data)
  410. {
  411. return __startup_pirq(data->irq);
  412. }
  413. static void shutdown_pirq(struct irq_data *data)
  414. {
  415. struct evtchn_close close;
  416. unsigned int irq = data->irq;
  417. struct irq_info *info = info_for_irq(irq);
  418. int evtchn = evtchn_from_irq(irq);
  419. BUG_ON(info->type != IRQT_PIRQ);
  420. if (!VALID_EVTCHN(evtchn))
  421. return;
  422. mask_evtchn(evtchn);
  423. close.port = evtchn;
  424. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  425. BUG();
  426. bind_evtchn_to_cpu(evtchn, 0);
  427. evtchn_to_irq[evtchn] = -1;
  428. info->evtchn = 0;
  429. }
  430. static void enable_pirq(struct irq_data *data)
  431. {
  432. startup_pirq(data);
  433. }
  434. static void disable_pirq(struct irq_data *data)
  435. {
  436. }
  437. static void ack_pirq(struct irq_data *data)
  438. {
  439. int evtchn = evtchn_from_irq(data->irq);
  440. move_native_irq(data->irq);
  441. if (VALID_EVTCHN(evtchn)) {
  442. mask_evtchn(evtchn);
  443. clear_evtchn(evtchn);
  444. }
  445. }
  446. static int find_irq_by_gsi(unsigned gsi)
  447. {
  448. int irq;
  449. for (irq = 0; irq < nr_irqs; irq++) {
  450. struct irq_info *info = info_for_irq(irq);
  451. if (info == NULL || info->type != IRQT_PIRQ)
  452. continue;
  453. if (gsi_from_irq(irq) == gsi)
  454. return irq;
  455. }
  456. return -1;
  457. }
  458. int xen_allocate_pirq(unsigned gsi, int shareable, char *name)
  459. {
  460. return xen_map_pirq_gsi(gsi, gsi, shareable, name);
  461. }
  462. /*
  463. * Do not make any assumptions regarding the relationship between the
  464. * IRQ number returned here and the Xen pirq argument.
  465. *
  466. * Note: We don't assign an event channel until the irq actually started
  467. * up. Return an existing irq if we've already got one for the gsi.
  468. */
  469. int xen_map_pirq_gsi(unsigned pirq, unsigned gsi, int shareable, char *name)
  470. {
  471. int irq = -1;
  472. struct physdev_irq irq_op;
  473. spin_lock(&irq_mapping_update_lock);
  474. if ((pirq > nr_irqs) || (gsi > nr_irqs)) {
  475. printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
  476. pirq > nr_irqs ? "pirq" :"",
  477. gsi > nr_irqs ? "gsi" : "");
  478. goto out;
  479. }
  480. irq = find_irq_by_gsi(gsi);
  481. if (irq != -1) {
  482. printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
  483. irq, gsi);
  484. goto out; /* XXX need refcount? */
  485. }
  486. irq = xen_allocate_irq_gsi(gsi);
  487. set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
  488. handle_level_irq, name);
  489. irq_op.irq = irq;
  490. irq_op.vector = 0;
  491. /* Only the privileged domain can do this. For non-priv, the pcifront
  492. * driver provides a PCI bus that does the call to do exactly
  493. * this in the priv domain. */
  494. if (xen_initial_domain() &&
  495. HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
  496. xen_free_irq(irq);
  497. irq = -ENOSPC;
  498. goto out;
  499. }
  500. irq_info[irq] = mk_pirq_info(0, pirq, gsi, irq_op.vector);
  501. irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0;
  502. pirq_to_irq[pirq] = irq;
  503. out:
  504. spin_unlock(&irq_mapping_update_lock);
  505. return irq;
  506. }
  507. #ifdef CONFIG_PCI_MSI
  508. int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
  509. {
  510. int rc;
  511. struct physdev_get_free_pirq op_get_free_pirq;
  512. op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
  513. rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
  514. WARN_ONCE(rc == -ENOSYS,
  515. "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
  516. return rc ? -1 : op_get_free_pirq.pirq;
  517. }
  518. int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
  519. int pirq, int vector, const char *name)
  520. {
  521. int irq, ret;
  522. spin_lock(&irq_mapping_update_lock);
  523. irq = xen_allocate_irq_dynamic();
  524. if (irq == -1)
  525. goto out;
  526. set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
  527. handle_level_irq, name);
  528. irq_info[irq] = mk_pirq_info(0, pirq, 0, vector);
  529. pirq_to_irq[pirq] = irq;
  530. ret = set_irq_msi(irq, msidesc);
  531. if (ret < 0)
  532. goto error_irq;
  533. out:
  534. spin_unlock(&irq_mapping_update_lock);
  535. return irq;
  536. error_irq:
  537. spin_unlock(&irq_mapping_update_lock);
  538. xen_free_irq(irq);
  539. return -1;
  540. }
  541. #endif
  542. int xen_destroy_irq(int irq)
  543. {
  544. struct irq_desc *desc;
  545. struct physdev_unmap_pirq unmap_irq;
  546. struct irq_info *info = info_for_irq(irq);
  547. int rc = -ENOENT;
  548. spin_lock(&irq_mapping_update_lock);
  549. desc = irq_to_desc(irq);
  550. if (!desc)
  551. goto out;
  552. if (xen_initial_domain()) {
  553. unmap_irq.pirq = info->u.pirq.pirq;
  554. unmap_irq.domid = DOMID_SELF;
  555. rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
  556. if (rc) {
  557. printk(KERN_WARNING "unmap irq failed %d\n", rc);
  558. goto out;
  559. }
  560. }
  561. pirq_to_irq[info->u.pirq.pirq] = -1;
  562. irq_info[irq] = mk_unbound_info();
  563. xen_free_irq(irq);
  564. out:
  565. spin_unlock(&irq_mapping_update_lock);
  566. return rc;
  567. }
  568. int xen_irq_from_pirq(unsigned pirq)
  569. {
  570. return pirq_to_irq[pirq];
  571. }
  572. int bind_evtchn_to_irq(unsigned int evtchn)
  573. {
  574. int irq;
  575. spin_lock(&irq_mapping_update_lock);
  576. irq = evtchn_to_irq[evtchn];
  577. if (irq == -1) {
  578. irq = xen_allocate_irq_dynamic();
  579. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  580. handle_fasteoi_irq, "event");
  581. evtchn_to_irq[evtchn] = irq;
  582. irq_info[irq] = mk_evtchn_info(evtchn);
  583. }
  584. spin_unlock(&irq_mapping_update_lock);
  585. return irq;
  586. }
  587. EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
  588. static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
  589. {
  590. struct evtchn_bind_ipi bind_ipi;
  591. int evtchn, irq;
  592. spin_lock(&irq_mapping_update_lock);
  593. irq = per_cpu(ipi_to_irq, cpu)[ipi];
  594. if (irq == -1) {
  595. irq = xen_allocate_irq_dynamic();
  596. if (irq < 0)
  597. goto out;
  598. set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
  599. handle_percpu_irq, "ipi");
  600. bind_ipi.vcpu = cpu;
  601. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  602. &bind_ipi) != 0)
  603. BUG();
  604. evtchn = bind_ipi.port;
  605. evtchn_to_irq[evtchn] = irq;
  606. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  607. per_cpu(ipi_to_irq, cpu)[ipi] = irq;
  608. bind_evtchn_to_cpu(evtchn, cpu);
  609. }
  610. out:
  611. spin_unlock(&irq_mapping_update_lock);
  612. return irq;
  613. }
  614. int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
  615. {
  616. struct evtchn_bind_virq bind_virq;
  617. int evtchn, irq;
  618. spin_lock(&irq_mapping_update_lock);
  619. irq = per_cpu(virq_to_irq, cpu)[virq];
  620. if (irq == -1) {
  621. irq = xen_allocate_irq_dynamic();
  622. set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
  623. handle_percpu_irq, "virq");
  624. bind_virq.virq = virq;
  625. bind_virq.vcpu = cpu;
  626. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  627. &bind_virq) != 0)
  628. BUG();
  629. evtchn = bind_virq.port;
  630. evtchn_to_irq[evtchn] = irq;
  631. irq_info[irq] = mk_virq_info(evtchn, virq);
  632. per_cpu(virq_to_irq, cpu)[virq] = irq;
  633. bind_evtchn_to_cpu(evtchn, cpu);
  634. }
  635. spin_unlock(&irq_mapping_update_lock);
  636. return irq;
  637. }
  638. static void unbind_from_irq(unsigned int irq)
  639. {
  640. struct evtchn_close close;
  641. int evtchn = evtchn_from_irq(irq);
  642. spin_lock(&irq_mapping_update_lock);
  643. if (VALID_EVTCHN(evtchn)) {
  644. close.port = evtchn;
  645. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  646. BUG();
  647. switch (type_from_irq(irq)) {
  648. case IRQT_VIRQ:
  649. per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
  650. [virq_from_irq(irq)] = -1;
  651. break;
  652. case IRQT_IPI:
  653. per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
  654. [ipi_from_irq(irq)] = -1;
  655. break;
  656. default:
  657. break;
  658. }
  659. /* Closed ports are implicitly re-bound to VCPU0. */
  660. bind_evtchn_to_cpu(evtchn, 0);
  661. evtchn_to_irq[evtchn] = -1;
  662. }
  663. if (irq_info[irq].type != IRQT_UNBOUND) {
  664. irq_info[irq] = mk_unbound_info();
  665. xen_free_irq(irq);
  666. }
  667. spin_unlock(&irq_mapping_update_lock);
  668. }
  669. int bind_evtchn_to_irqhandler(unsigned int evtchn,
  670. irq_handler_t handler,
  671. unsigned long irqflags,
  672. const char *devname, void *dev_id)
  673. {
  674. unsigned int irq;
  675. int retval;
  676. irq = bind_evtchn_to_irq(evtchn);
  677. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  678. if (retval != 0) {
  679. unbind_from_irq(irq);
  680. return retval;
  681. }
  682. return irq;
  683. }
  684. EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
  685. int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
  686. irq_handler_t handler,
  687. unsigned long irqflags, const char *devname, void *dev_id)
  688. {
  689. unsigned int irq;
  690. int retval;
  691. irq = bind_virq_to_irq(virq, cpu);
  692. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  693. if (retval != 0) {
  694. unbind_from_irq(irq);
  695. return retval;
  696. }
  697. return irq;
  698. }
  699. EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
  700. int bind_ipi_to_irqhandler(enum ipi_vector ipi,
  701. unsigned int cpu,
  702. irq_handler_t handler,
  703. unsigned long irqflags,
  704. const char *devname,
  705. void *dev_id)
  706. {
  707. int irq, retval;
  708. irq = bind_ipi_to_irq(ipi, cpu);
  709. if (irq < 0)
  710. return irq;
  711. irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
  712. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  713. if (retval != 0) {
  714. unbind_from_irq(irq);
  715. return retval;
  716. }
  717. return irq;
  718. }
  719. void unbind_from_irqhandler(unsigned int irq, void *dev_id)
  720. {
  721. free_irq(irq, dev_id);
  722. unbind_from_irq(irq);
  723. }
  724. EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
  725. void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
  726. {
  727. int irq = per_cpu(ipi_to_irq, cpu)[vector];
  728. BUG_ON(irq < 0);
  729. notify_remote_via_irq(irq);
  730. }
  731. irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
  732. {
  733. struct shared_info *sh = HYPERVISOR_shared_info;
  734. int cpu = smp_processor_id();
  735. unsigned long *cpu_evtchn = cpu_evtchn_mask(cpu);
  736. int i;
  737. unsigned long flags;
  738. static DEFINE_SPINLOCK(debug_lock);
  739. struct vcpu_info *v;
  740. spin_lock_irqsave(&debug_lock, flags);
  741. printk("\nvcpu %d\n ", cpu);
  742. for_each_online_cpu(i) {
  743. int pending;
  744. v = per_cpu(xen_vcpu, i);
  745. pending = (get_irq_regs() && i == cpu)
  746. ? xen_irqs_disabled(get_irq_regs())
  747. : v->evtchn_upcall_mask;
  748. printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
  749. pending, v->evtchn_upcall_pending,
  750. (int)(sizeof(v->evtchn_pending_sel)*2),
  751. v->evtchn_pending_sel);
  752. }
  753. v = per_cpu(xen_vcpu, cpu);
  754. printk("\npending:\n ");
  755. for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
  756. printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
  757. sh->evtchn_pending[i],
  758. i % 8 == 0 ? "\n " : " ");
  759. printk("\nglobal mask:\n ");
  760. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  761. printk("%0*lx%s",
  762. (int)(sizeof(sh->evtchn_mask[0])*2),
  763. sh->evtchn_mask[i],
  764. i % 8 == 0 ? "\n " : " ");
  765. printk("\nglobally unmasked:\n ");
  766. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  767. printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
  768. sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
  769. i % 8 == 0 ? "\n " : " ");
  770. printk("\nlocal cpu%d mask:\n ", cpu);
  771. for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
  772. printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
  773. cpu_evtchn[i],
  774. i % 8 == 0 ? "\n " : " ");
  775. printk("\nlocally unmasked:\n ");
  776. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
  777. unsigned long pending = sh->evtchn_pending[i]
  778. & ~sh->evtchn_mask[i]
  779. & cpu_evtchn[i];
  780. printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
  781. pending, i % 8 == 0 ? "\n " : " ");
  782. }
  783. printk("\npending list:\n");
  784. for (i = 0; i < NR_EVENT_CHANNELS; i++) {
  785. if (sync_test_bit(i, sh->evtchn_pending)) {
  786. int word_idx = i / BITS_PER_LONG;
  787. printk(" %d: event %d -> irq %d%s%s%s\n",
  788. cpu_from_evtchn(i), i,
  789. evtchn_to_irq[i],
  790. sync_test_bit(word_idx, &v->evtchn_pending_sel)
  791. ? "" : " l2-clear",
  792. !sync_test_bit(i, sh->evtchn_mask)
  793. ? "" : " globally-masked",
  794. sync_test_bit(i, cpu_evtchn)
  795. ? "" : " locally-masked");
  796. }
  797. }
  798. spin_unlock_irqrestore(&debug_lock, flags);
  799. return IRQ_HANDLED;
  800. }
  801. static DEFINE_PER_CPU(unsigned, xed_nesting_count);
  802. /*
  803. * Search the CPUs pending events bitmasks. For each one found, map
  804. * the event number to an irq, and feed it into do_IRQ() for
  805. * handling.
  806. *
  807. * Xen uses a two-level bitmap to speed searching. The first level is
  808. * a bitset of words which contain pending event bits. The second
  809. * level is a bitset of pending events themselves.
  810. */
  811. static void __xen_evtchn_do_upcall(void)
  812. {
  813. int cpu = get_cpu();
  814. struct shared_info *s = HYPERVISOR_shared_info;
  815. struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
  816. unsigned count;
  817. do {
  818. unsigned long pending_words;
  819. vcpu_info->evtchn_upcall_pending = 0;
  820. if (__this_cpu_inc_return(xed_nesting_count) - 1)
  821. goto out;
  822. #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
  823. /* Clear master flag /before/ clearing selector flag. */
  824. wmb();
  825. #endif
  826. pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
  827. while (pending_words != 0) {
  828. unsigned long pending_bits;
  829. int word_idx = __ffs(pending_words);
  830. pending_words &= ~(1UL << word_idx);
  831. while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
  832. int bit_idx = __ffs(pending_bits);
  833. int port = (word_idx * BITS_PER_LONG) + bit_idx;
  834. int irq = evtchn_to_irq[port];
  835. struct irq_desc *desc;
  836. mask_evtchn(port);
  837. clear_evtchn(port);
  838. if (irq != -1) {
  839. desc = irq_to_desc(irq);
  840. if (desc)
  841. generic_handle_irq_desc(irq, desc);
  842. }
  843. }
  844. }
  845. BUG_ON(!irqs_disabled());
  846. count = __this_cpu_read(xed_nesting_count);
  847. __this_cpu_write(xed_nesting_count, 0);
  848. } while (count != 1 || vcpu_info->evtchn_upcall_pending);
  849. out:
  850. put_cpu();
  851. }
  852. void xen_evtchn_do_upcall(struct pt_regs *regs)
  853. {
  854. struct pt_regs *old_regs = set_irq_regs(regs);
  855. exit_idle();
  856. irq_enter();
  857. __xen_evtchn_do_upcall();
  858. irq_exit();
  859. set_irq_regs(old_regs);
  860. }
  861. void xen_hvm_evtchn_do_upcall(void)
  862. {
  863. __xen_evtchn_do_upcall();
  864. }
  865. EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
  866. /* Rebind a new event channel to an existing irq. */
  867. void rebind_evtchn_irq(int evtchn, int irq)
  868. {
  869. struct irq_info *info = info_for_irq(irq);
  870. /* Make sure the irq is masked, since the new event channel
  871. will also be masked. */
  872. disable_irq(irq);
  873. spin_lock(&irq_mapping_update_lock);
  874. /* After resume the irq<->evtchn mappings are all cleared out */
  875. BUG_ON(evtchn_to_irq[evtchn] != -1);
  876. /* Expect irq to have been bound before,
  877. so there should be a proper type */
  878. BUG_ON(info->type == IRQT_UNBOUND);
  879. evtchn_to_irq[evtchn] = irq;
  880. irq_info[irq] = mk_evtchn_info(evtchn);
  881. spin_unlock(&irq_mapping_update_lock);
  882. /* new event channels are always bound to cpu 0 */
  883. irq_set_affinity(irq, cpumask_of(0));
  884. /* Unmask the event channel. */
  885. enable_irq(irq);
  886. }
  887. /* Rebind an evtchn so that it gets delivered to a specific cpu */
  888. static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
  889. {
  890. struct evtchn_bind_vcpu bind_vcpu;
  891. int evtchn = evtchn_from_irq(irq);
  892. if (!VALID_EVTCHN(evtchn))
  893. return -1;
  894. /*
  895. * Events delivered via platform PCI interrupts are always
  896. * routed to vcpu 0 and hence cannot be rebound.
  897. */
  898. if (xen_hvm_domain() && !xen_have_vector_callback)
  899. return -1;
  900. /* Send future instances of this interrupt to other vcpu. */
  901. bind_vcpu.port = evtchn;
  902. bind_vcpu.vcpu = tcpu;
  903. /*
  904. * If this fails, it usually just indicates that we're dealing with a
  905. * virq or IPI channel, which don't actually need to be rebound. Ignore
  906. * it, but don't do the xenlinux-level rebind in that case.
  907. */
  908. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
  909. bind_evtchn_to_cpu(evtchn, tcpu);
  910. return 0;
  911. }
  912. static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
  913. bool force)
  914. {
  915. unsigned tcpu = cpumask_first(dest);
  916. return rebind_irq_to_cpu(data->irq, tcpu);
  917. }
  918. int resend_irq_on_evtchn(unsigned int irq)
  919. {
  920. int masked, evtchn = evtchn_from_irq(irq);
  921. struct shared_info *s = HYPERVISOR_shared_info;
  922. if (!VALID_EVTCHN(evtchn))
  923. return 1;
  924. masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
  925. sync_set_bit(evtchn, s->evtchn_pending);
  926. if (!masked)
  927. unmask_evtchn(evtchn);
  928. return 1;
  929. }
  930. static void enable_dynirq(struct irq_data *data)
  931. {
  932. int evtchn = evtchn_from_irq(data->irq);
  933. if (VALID_EVTCHN(evtchn))
  934. unmask_evtchn(evtchn);
  935. }
  936. static void disable_dynirq(struct irq_data *data)
  937. {
  938. int evtchn = evtchn_from_irq(data->irq);
  939. if (VALID_EVTCHN(evtchn))
  940. mask_evtchn(evtchn);
  941. }
  942. static void ack_dynirq(struct irq_data *data)
  943. {
  944. int evtchn = evtchn_from_irq(data->irq);
  945. move_masked_irq(data->irq);
  946. if (VALID_EVTCHN(evtchn))
  947. unmask_evtchn(evtchn);
  948. }
  949. static int retrigger_dynirq(struct irq_data *data)
  950. {
  951. int evtchn = evtchn_from_irq(data->irq);
  952. struct shared_info *sh = HYPERVISOR_shared_info;
  953. int ret = 0;
  954. if (VALID_EVTCHN(evtchn)) {
  955. int masked;
  956. masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
  957. sync_set_bit(evtchn, sh->evtchn_pending);
  958. if (!masked)
  959. unmask_evtchn(evtchn);
  960. ret = 1;
  961. }
  962. return ret;
  963. }
  964. static void restore_pirqs(void)
  965. {
  966. int pirq, rc, irq, gsi;
  967. struct physdev_map_pirq map_irq;
  968. for (pirq = 0; pirq < nr_irqs; pirq++) {
  969. irq = pirq_to_irq[pirq];
  970. if (irq == -1)
  971. continue;
  972. /* save/restore of PT devices doesn't work, so at this point the
  973. * only devices present are GSI based emulated devices */
  974. gsi = gsi_from_irq(irq);
  975. if (!gsi)
  976. continue;
  977. map_irq.domid = DOMID_SELF;
  978. map_irq.type = MAP_PIRQ_TYPE_GSI;
  979. map_irq.index = gsi;
  980. map_irq.pirq = pirq;
  981. rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
  982. if (rc) {
  983. printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
  984. gsi, irq, pirq, rc);
  985. irq_info[irq] = mk_unbound_info();
  986. pirq_to_irq[pirq] = -1;
  987. continue;
  988. }
  989. printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
  990. __startup_pirq(irq);
  991. }
  992. }
  993. static void restore_cpu_virqs(unsigned int cpu)
  994. {
  995. struct evtchn_bind_virq bind_virq;
  996. int virq, irq, evtchn;
  997. for (virq = 0; virq < NR_VIRQS; virq++) {
  998. if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
  999. continue;
  1000. BUG_ON(virq_from_irq(irq) != virq);
  1001. /* Get a new binding from Xen. */
  1002. bind_virq.virq = virq;
  1003. bind_virq.vcpu = cpu;
  1004. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  1005. &bind_virq) != 0)
  1006. BUG();
  1007. evtchn = bind_virq.port;
  1008. /* Record the new mapping. */
  1009. evtchn_to_irq[evtchn] = irq;
  1010. irq_info[irq] = mk_virq_info(evtchn, virq);
  1011. bind_evtchn_to_cpu(evtchn, cpu);
  1012. }
  1013. }
  1014. static void restore_cpu_ipis(unsigned int cpu)
  1015. {
  1016. struct evtchn_bind_ipi bind_ipi;
  1017. int ipi, irq, evtchn;
  1018. for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
  1019. if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
  1020. continue;
  1021. BUG_ON(ipi_from_irq(irq) != ipi);
  1022. /* Get a new binding from Xen. */
  1023. bind_ipi.vcpu = cpu;
  1024. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  1025. &bind_ipi) != 0)
  1026. BUG();
  1027. evtchn = bind_ipi.port;
  1028. /* Record the new mapping. */
  1029. evtchn_to_irq[evtchn] = irq;
  1030. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  1031. bind_evtchn_to_cpu(evtchn, cpu);
  1032. }
  1033. }
  1034. /* Clear an irq's pending state, in preparation for polling on it */
  1035. void xen_clear_irq_pending(int irq)
  1036. {
  1037. int evtchn = evtchn_from_irq(irq);
  1038. if (VALID_EVTCHN(evtchn))
  1039. clear_evtchn(evtchn);
  1040. }
  1041. EXPORT_SYMBOL(xen_clear_irq_pending);
  1042. void xen_set_irq_pending(int irq)
  1043. {
  1044. int evtchn = evtchn_from_irq(irq);
  1045. if (VALID_EVTCHN(evtchn))
  1046. set_evtchn(evtchn);
  1047. }
  1048. bool xen_test_irq_pending(int irq)
  1049. {
  1050. int evtchn = evtchn_from_irq(irq);
  1051. bool ret = false;
  1052. if (VALID_EVTCHN(evtchn))
  1053. ret = test_evtchn(evtchn);
  1054. return ret;
  1055. }
  1056. /* Poll waiting for an irq to become pending with timeout. In the usual case,
  1057. * the irq will be disabled so it won't deliver an interrupt. */
  1058. void xen_poll_irq_timeout(int irq, u64 timeout)
  1059. {
  1060. evtchn_port_t evtchn = evtchn_from_irq(irq);
  1061. if (VALID_EVTCHN(evtchn)) {
  1062. struct sched_poll poll;
  1063. poll.nr_ports = 1;
  1064. poll.timeout = timeout;
  1065. set_xen_guest_handle(poll.ports, &evtchn);
  1066. if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
  1067. BUG();
  1068. }
  1069. }
  1070. EXPORT_SYMBOL(xen_poll_irq_timeout);
  1071. /* Poll waiting for an irq to become pending. In the usual case, the
  1072. * irq will be disabled so it won't deliver an interrupt. */
  1073. void xen_poll_irq(int irq)
  1074. {
  1075. xen_poll_irq_timeout(irq, 0 /* no timeout */);
  1076. }
  1077. void xen_irq_resume(void)
  1078. {
  1079. unsigned int cpu, irq, evtchn;
  1080. init_evtchn_cpu_bindings();
  1081. /* New event-channel space is not 'live' yet. */
  1082. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  1083. mask_evtchn(evtchn);
  1084. /* No IRQ <-> event-channel mappings. */
  1085. for (irq = 0; irq < nr_irqs; irq++)
  1086. irq_info[irq].evtchn = 0; /* zap event-channel binding */
  1087. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  1088. evtchn_to_irq[evtchn] = -1;
  1089. for_each_possible_cpu(cpu) {
  1090. restore_cpu_virqs(cpu);
  1091. restore_cpu_ipis(cpu);
  1092. }
  1093. restore_pirqs();
  1094. }
  1095. static struct irq_chip xen_dynamic_chip __read_mostly = {
  1096. .name = "xen-dyn",
  1097. .irq_disable = disable_dynirq,
  1098. .irq_mask = disable_dynirq,
  1099. .irq_unmask = enable_dynirq,
  1100. .irq_eoi = ack_dynirq,
  1101. .irq_set_affinity = set_affinity_irq,
  1102. .irq_retrigger = retrigger_dynirq,
  1103. };
  1104. static struct irq_chip xen_pirq_chip __read_mostly = {
  1105. .name = "xen-pirq",
  1106. .irq_startup = startup_pirq,
  1107. .irq_shutdown = shutdown_pirq,
  1108. .irq_enable = enable_pirq,
  1109. .irq_unmask = enable_pirq,
  1110. .irq_disable = disable_pirq,
  1111. .irq_mask = disable_pirq,
  1112. .irq_ack = ack_pirq,
  1113. .irq_set_affinity = set_affinity_irq,
  1114. .irq_retrigger = retrigger_dynirq,
  1115. };
  1116. static struct irq_chip xen_percpu_chip __read_mostly = {
  1117. .name = "xen-percpu",
  1118. .irq_disable = disable_dynirq,
  1119. .irq_mask = disable_dynirq,
  1120. .irq_unmask = enable_dynirq,
  1121. .irq_ack = ack_dynirq,
  1122. };
  1123. int xen_set_callback_via(uint64_t via)
  1124. {
  1125. struct xen_hvm_param a;
  1126. a.domid = DOMID_SELF;
  1127. a.index = HVM_PARAM_CALLBACK_IRQ;
  1128. a.value = via;
  1129. return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
  1130. }
  1131. EXPORT_SYMBOL_GPL(xen_set_callback_via);
  1132. #ifdef CONFIG_XEN_PVHVM
  1133. /* Vector callbacks are better than PCI interrupts to receive event
  1134. * channel notifications because we can receive vector callbacks on any
  1135. * vcpu and we don't need PCI support or APIC interactions. */
  1136. void xen_callback_vector(void)
  1137. {
  1138. int rc;
  1139. uint64_t callback_via;
  1140. if (xen_have_vector_callback) {
  1141. callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
  1142. rc = xen_set_callback_via(callback_via);
  1143. if (rc) {
  1144. printk(KERN_ERR "Request for Xen HVM callback vector"
  1145. " failed.\n");
  1146. xen_have_vector_callback = 0;
  1147. return;
  1148. }
  1149. printk(KERN_INFO "Xen HVM callback vector for event delivery is "
  1150. "enabled\n");
  1151. /* in the restore case the vector has already been allocated */
  1152. if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
  1153. alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
  1154. }
  1155. }
  1156. #else
  1157. void xen_callback_vector(void) {}
  1158. #endif
  1159. void __init xen_init_IRQ(void)
  1160. {
  1161. int i;
  1162. cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
  1163. GFP_KERNEL);
  1164. irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
  1165. /* We are using nr_irqs as the maximum number of pirq available but
  1166. * that number is actually chosen by Xen and we don't know exactly
  1167. * what it is. Be careful choosing high pirq numbers. */
  1168. pirq_to_irq = kcalloc(nr_irqs, sizeof(*pirq_to_irq), GFP_KERNEL);
  1169. for (i = 0; i < nr_irqs; i++)
  1170. pirq_to_irq[i] = -1;
  1171. evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
  1172. GFP_KERNEL);
  1173. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  1174. evtchn_to_irq[i] = -1;
  1175. init_evtchn_cpu_bindings();
  1176. /* No event channels are 'live' right now. */
  1177. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  1178. mask_evtchn(i);
  1179. if (xen_hvm_domain()) {
  1180. xen_callback_vector();
  1181. native_init_IRQ();
  1182. /* pci_xen_hvm_init must be called after native_init_IRQ so that
  1183. * __acpi_register_gsi can point at the right function */
  1184. pci_xen_hvm_init();
  1185. } else {
  1186. irq_ctx_init(smp_processor_id());
  1187. if (xen_initial_domain())
  1188. xen_setup_pirqs();
  1189. }
  1190. }