events.c 35 KB

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