events.c 37 KB

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