events.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. Hardware interrupts. Not supported at present.
  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 <asm/desc.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/irq.h>
  33. #include <asm/idle.h>
  34. #include <asm/sync_bitops.h>
  35. #include <asm/xen/hypercall.h>
  36. #include <asm/xen/hypervisor.h>
  37. #include <xen/xen.h>
  38. #include <xen/hvm.h>
  39. #include <xen/xen-ops.h>
  40. #include <xen/events.h>
  41. #include <xen/interface/xen.h>
  42. #include <xen/interface/event_channel.h>
  43. #include <xen/interface/hvm/hvm_op.h>
  44. #include <xen/interface/hvm/params.h>
  45. /*
  46. * This lock protects updates to the following mapping and reference-count
  47. * arrays. The lock does not need to be acquired to read the mapping tables.
  48. */
  49. static DEFINE_SPINLOCK(irq_mapping_update_lock);
  50. /* IRQ <-> VIRQ mapping. */
  51. static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
  52. /* IRQ <-> IPI mapping */
  53. static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
  54. /* Interrupt types. */
  55. enum xen_irq_type {
  56. IRQT_UNBOUND = 0,
  57. IRQT_PIRQ,
  58. IRQT_VIRQ,
  59. IRQT_IPI,
  60. IRQT_EVTCHN
  61. };
  62. /*
  63. * Packed IRQ information:
  64. * type - enum xen_irq_type
  65. * event channel - irq->event channel mapping
  66. * cpu - cpu this event channel is bound to
  67. * index - type-specific information:
  68. * PIRQ - vector, with MSB being "needs EIO"
  69. * VIRQ - virq number
  70. * IPI - IPI vector
  71. * EVTCHN -
  72. */
  73. struct irq_info
  74. {
  75. enum xen_irq_type type; /* type */
  76. unsigned short evtchn; /* event channel */
  77. unsigned short cpu; /* cpu bound */
  78. union {
  79. unsigned short virq;
  80. enum ipi_vector ipi;
  81. struct {
  82. unsigned short gsi;
  83. unsigned short vector;
  84. } pirq;
  85. } u;
  86. };
  87. static struct irq_info irq_info[NR_IRQS];
  88. static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
  89. [0 ... NR_EVENT_CHANNELS-1] = -1
  90. };
  91. struct cpu_evtchn_s {
  92. unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
  93. };
  94. static struct cpu_evtchn_s *cpu_evtchn_mask_p;
  95. static inline unsigned long *cpu_evtchn_mask(int cpu)
  96. {
  97. return cpu_evtchn_mask_p[cpu].bits;
  98. }
  99. /* Xen will never allocate port zero for any purpose. */
  100. #define VALID_EVTCHN(chn) ((chn) != 0)
  101. static struct irq_chip xen_dynamic_chip;
  102. /* Constructor for packed IRQ information. */
  103. static struct irq_info mk_unbound_info(void)
  104. {
  105. return (struct irq_info) { .type = IRQT_UNBOUND };
  106. }
  107. static struct irq_info mk_evtchn_info(unsigned short evtchn)
  108. {
  109. return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
  110. .cpu = 0 };
  111. }
  112. static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
  113. {
  114. return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
  115. .cpu = 0, .u.ipi = ipi };
  116. }
  117. static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
  118. {
  119. return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
  120. .cpu = 0, .u.virq = virq };
  121. }
  122. static struct irq_info mk_pirq_info(unsigned short evtchn,
  123. unsigned short gsi, unsigned short vector)
  124. {
  125. return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
  126. .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
  127. }
  128. /*
  129. * Accessors for packed IRQ information.
  130. */
  131. static struct irq_info *info_for_irq(unsigned irq)
  132. {
  133. return &irq_info[irq];
  134. }
  135. static unsigned int evtchn_from_irq(unsigned irq)
  136. {
  137. return info_for_irq(irq)->evtchn;
  138. }
  139. unsigned irq_from_evtchn(unsigned int evtchn)
  140. {
  141. return evtchn_to_irq[evtchn];
  142. }
  143. EXPORT_SYMBOL_GPL(irq_from_evtchn);
  144. static enum ipi_vector ipi_from_irq(unsigned irq)
  145. {
  146. struct irq_info *info = info_for_irq(irq);
  147. BUG_ON(info == NULL);
  148. BUG_ON(info->type != IRQT_IPI);
  149. return info->u.ipi;
  150. }
  151. static unsigned virq_from_irq(unsigned irq)
  152. {
  153. struct irq_info *info = info_for_irq(irq);
  154. BUG_ON(info == NULL);
  155. BUG_ON(info->type != IRQT_VIRQ);
  156. return info->u.virq;
  157. }
  158. static unsigned gsi_from_irq(unsigned irq)
  159. {
  160. struct irq_info *info = info_for_irq(irq);
  161. BUG_ON(info == NULL);
  162. BUG_ON(info->type != IRQT_PIRQ);
  163. return info->u.pirq.gsi;
  164. }
  165. static unsigned vector_from_irq(unsigned irq)
  166. {
  167. struct irq_info *info = info_for_irq(irq);
  168. BUG_ON(info == NULL);
  169. BUG_ON(info->type != IRQT_PIRQ);
  170. return info->u.pirq.vector;
  171. }
  172. static enum xen_irq_type type_from_irq(unsigned irq)
  173. {
  174. return info_for_irq(irq)->type;
  175. }
  176. static unsigned cpu_from_irq(unsigned irq)
  177. {
  178. return info_for_irq(irq)->cpu;
  179. }
  180. static unsigned int cpu_from_evtchn(unsigned int evtchn)
  181. {
  182. int irq = evtchn_to_irq[evtchn];
  183. unsigned ret = 0;
  184. if (irq != -1)
  185. ret = cpu_from_irq(irq);
  186. return ret;
  187. }
  188. static inline unsigned long active_evtchns(unsigned int cpu,
  189. struct shared_info *sh,
  190. unsigned int idx)
  191. {
  192. return (sh->evtchn_pending[idx] &
  193. cpu_evtchn_mask(cpu)[idx] &
  194. ~sh->evtchn_mask[idx]);
  195. }
  196. static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
  197. {
  198. int irq = evtchn_to_irq[chn];
  199. BUG_ON(irq == -1);
  200. #ifdef CONFIG_SMP
  201. cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
  202. #endif
  203. __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
  204. __set_bit(chn, cpu_evtchn_mask(cpu));
  205. irq_info[irq].cpu = cpu;
  206. }
  207. static void init_evtchn_cpu_bindings(void)
  208. {
  209. #ifdef CONFIG_SMP
  210. struct irq_desc *desc;
  211. int i;
  212. /* By default all event channels notify CPU#0. */
  213. for_each_irq_desc(i, desc) {
  214. cpumask_copy(desc->affinity, cpumask_of(0));
  215. }
  216. #endif
  217. memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
  218. }
  219. static inline void clear_evtchn(int port)
  220. {
  221. struct shared_info *s = HYPERVISOR_shared_info;
  222. sync_clear_bit(port, &s->evtchn_pending[0]);
  223. }
  224. static inline void set_evtchn(int port)
  225. {
  226. struct shared_info *s = HYPERVISOR_shared_info;
  227. sync_set_bit(port, &s->evtchn_pending[0]);
  228. }
  229. static inline int test_evtchn(int port)
  230. {
  231. struct shared_info *s = HYPERVISOR_shared_info;
  232. return sync_test_bit(port, &s->evtchn_pending[0]);
  233. }
  234. /**
  235. * notify_remote_via_irq - send event to remote end of event channel via irq
  236. * @irq: irq of event channel to send event to
  237. *
  238. * Unlike notify_remote_via_evtchn(), this is safe to use across
  239. * save/restore. Notifications on a broken connection are silently
  240. * dropped.
  241. */
  242. void notify_remote_via_irq(int irq)
  243. {
  244. int evtchn = evtchn_from_irq(irq);
  245. if (VALID_EVTCHN(evtchn))
  246. notify_remote_via_evtchn(evtchn);
  247. }
  248. EXPORT_SYMBOL_GPL(notify_remote_via_irq);
  249. static void mask_evtchn(int port)
  250. {
  251. struct shared_info *s = HYPERVISOR_shared_info;
  252. sync_set_bit(port, &s->evtchn_mask[0]);
  253. }
  254. static void unmask_evtchn(int port)
  255. {
  256. struct shared_info *s = HYPERVISOR_shared_info;
  257. unsigned int cpu = get_cpu();
  258. BUG_ON(!irqs_disabled());
  259. /* Slow path (hypercall) if this is a non-local port. */
  260. if (unlikely(cpu != cpu_from_evtchn(port))) {
  261. struct evtchn_unmask unmask = { .port = port };
  262. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  263. } else {
  264. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  265. sync_clear_bit(port, &s->evtchn_mask[0]);
  266. /*
  267. * The following is basically the equivalent of
  268. * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
  269. * the interrupt edge' if the channel is masked.
  270. */
  271. if (sync_test_bit(port, &s->evtchn_pending[0]) &&
  272. !sync_test_and_set_bit(port / BITS_PER_LONG,
  273. &vcpu_info->evtchn_pending_sel))
  274. vcpu_info->evtchn_upcall_pending = 1;
  275. }
  276. put_cpu();
  277. }
  278. static int find_unbound_irq(void)
  279. {
  280. int irq;
  281. struct irq_desc *desc;
  282. for (irq = 0; irq < nr_irqs; irq++) {
  283. desc = irq_to_desc(irq);
  284. /* only 0->15 have init'd desc; handle irq > 16 */
  285. if (desc == NULL)
  286. break;
  287. if (desc->chip == &no_irq_chip)
  288. break;
  289. if (desc->chip != &xen_dynamic_chip)
  290. continue;
  291. if (irq_info[irq].type == IRQT_UNBOUND)
  292. break;
  293. }
  294. if (irq == nr_irqs)
  295. panic("No available IRQ to bind to: increase nr_irqs!\n");
  296. desc = irq_to_desc_alloc_node(irq, 0);
  297. if (WARN_ON(desc == NULL))
  298. return -1;
  299. dynamic_irq_init_keep_chip_data(irq);
  300. return irq;
  301. }
  302. int bind_evtchn_to_irq(unsigned int evtchn)
  303. {
  304. int irq;
  305. spin_lock(&irq_mapping_update_lock);
  306. irq = evtchn_to_irq[evtchn];
  307. if (irq == -1) {
  308. irq = find_unbound_irq();
  309. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  310. handle_level_irq, "event");
  311. evtchn_to_irq[evtchn] = irq;
  312. irq_info[irq] = mk_evtchn_info(evtchn);
  313. }
  314. spin_unlock(&irq_mapping_update_lock);
  315. return irq;
  316. }
  317. EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
  318. static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
  319. {
  320. struct evtchn_bind_ipi bind_ipi;
  321. int evtchn, irq;
  322. spin_lock(&irq_mapping_update_lock);
  323. irq = per_cpu(ipi_to_irq, cpu)[ipi];
  324. if (irq == -1) {
  325. irq = find_unbound_irq();
  326. if (irq < 0)
  327. goto out;
  328. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  329. handle_level_irq, "ipi");
  330. bind_ipi.vcpu = cpu;
  331. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  332. &bind_ipi) != 0)
  333. BUG();
  334. evtchn = bind_ipi.port;
  335. evtchn_to_irq[evtchn] = irq;
  336. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  337. per_cpu(ipi_to_irq, cpu)[ipi] = irq;
  338. bind_evtchn_to_cpu(evtchn, cpu);
  339. }
  340. out:
  341. spin_unlock(&irq_mapping_update_lock);
  342. return irq;
  343. }
  344. static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
  345. {
  346. struct evtchn_bind_virq bind_virq;
  347. int evtchn, irq;
  348. spin_lock(&irq_mapping_update_lock);
  349. irq = per_cpu(virq_to_irq, cpu)[virq];
  350. if (irq == -1) {
  351. bind_virq.virq = virq;
  352. bind_virq.vcpu = cpu;
  353. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  354. &bind_virq) != 0)
  355. BUG();
  356. evtchn = bind_virq.port;
  357. irq = find_unbound_irq();
  358. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  359. handle_level_irq, "virq");
  360. evtchn_to_irq[evtchn] = irq;
  361. irq_info[irq] = mk_virq_info(evtchn, virq);
  362. per_cpu(virq_to_irq, cpu)[virq] = irq;
  363. bind_evtchn_to_cpu(evtchn, cpu);
  364. }
  365. spin_unlock(&irq_mapping_update_lock);
  366. return irq;
  367. }
  368. static void unbind_from_irq(unsigned int irq)
  369. {
  370. struct evtchn_close close;
  371. int evtchn = evtchn_from_irq(irq);
  372. spin_lock(&irq_mapping_update_lock);
  373. if (VALID_EVTCHN(evtchn)) {
  374. close.port = evtchn;
  375. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  376. BUG();
  377. switch (type_from_irq(irq)) {
  378. case IRQT_VIRQ:
  379. per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
  380. [virq_from_irq(irq)] = -1;
  381. break;
  382. case IRQT_IPI:
  383. per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
  384. [ipi_from_irq(irq)] = -1;
  385. break;
  386. default:
  387. break;
  388. }
  389. /* Closed ports are implicitly re-bound to VCPU0. */
  390. bind_evtchn_to_cpu(evtchn, 0);
  391. evtchn_to_irq[evtchn] = -1;
  392. }
  393. if (irq_info[irq].type != IRQT_UNBOUND) {
  394. irq_info[irq] = mk_unbound_info();
  395. dynamic_irq_cleanup(irq);
  396. }
  397. spin_unlock(&irq_mapping_update_lock);
  398. }
  399. int bind_evtchn_to_irqhandler(unsigned int evtchn,
  400. irq_handler_t handler,
  401. unsigned long irqflags,
  402. const char *devname, void *dev_id)
  403. {
  404. unsigned int irq;
  405. int retval;
  406. irq = bind_evtchn_to_irq(evtchn);
  407. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  408. if (retval != 0) {
  409. unbind_from_irq(irq);
  410. return retval;
  411. }
  412. return irq;
  413. }
  414. EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
  415. int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
  416. irq_handler_t handler,
  417. unsigned long irqflags, const char *devname, void *dev_id)
  418. {
  419. unsigned int irq;
  420. int retval;
  421. irq = bind_virq_to_irq(virq, cpu);
  422. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  423. if (retval != 0) {
  424. unbind_from_irq(irq);
  425. return retval;
  426. }
  427. return irq;
  428. }
  429. EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
  430. int bind_ipi_to_irqhandler(enum ipi_vector ipi,
  431. unsigned int cpu,
  432. irq_handler_t handler,
  433. unsigned long irqflags,
  434. const char *devname,
  435. void *dev_id)
  436. {
  437. int irq, retval;
  438. irq = bind_ipi_to_irq(ipi, cpu);
  439. if (irq < 0)
  440. return irq;
  441. irqflags |= IRQF_NO_SUSPEND;
  442. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  443. if (retval != 0) {
  444. unbind_from_irq(irq);
  445. return retval;
  446. }
  447. return irq;
  448. }
  449. void unbind_from_irqhandler(unsigned int irq, void *dev_id)
  450. {
  451. free_irq(irq, dev_id);
  452. unbind_from_irq(irq);
  453. }
  454. EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
  455. void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
  456. {
  457. int irq = per_cpu(ipi_to_irq, cpu)[vector];
  458. BUG_ON(irq < 0);
  459. notify_remote_via_irq(irq);
  460. }
  461. irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
  462. {
  463. struct shared_info *sh = HYPERVISOR_shared_info;
  464. int cpu = smp_processor_id();
  465. int i;
  466. unsigned long flags;
  467. static DEFINE_SPINLOCK(debug_lock);
  468. spin_lock_irqsave(&debug_lock, flags);
  469. printk("vcpu %d\n ", cpu);
  470. for_each_online_cpu(i) {
  471. struct vcpu_info *v = per_cpu(xen_vcpu, i);
  472. printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
  473. (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
  474. v->evtchn_upcall_pending,
  475. v->evtchn_pending_sel);
  476. }
  477. printk("pending:\n ");
  478. for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
  479. printk("%08lx%s", sh->evtchn_pending[i],
  480. i % 8 == 0 ? "\n " : " ");
  481. printk("\nmasks:\n ");
  482. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  483. printk("%08lx%s", sh->evtchn_mask[i],
  484. i % 8 == 0 ? "\n " : " ");
  485. printk("\nunmasked:\n ");
  486. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  487. printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
  488. i % 8 == 0 ? "\n " : " ");
  489. printk("\npending list:\n");
  490. for(i = 0; i < NR_EVENT_CHANNELS; i++) {
  491. if (sync_test_bit(i, sh->evtchn_pending)) {
  492. printk(" %d: event %d -> irq %d\n",
  493. cpu_from_evtchn(i), i,
  494. evtchn_to_irq[i]);
  495. }
  496. }
  497. spin_unlock_irqrestore(&debug_lock, flags);
  498. return IRQ_HANDLED;
  499. }
  500. static DEFINE_PER_CPU(unsigned, xed_nesting_count);
  501. /*
  502. * Search the CPUs pending events bitmasks. For each one found, map
  503. * the event number to an irq, and feed it into do_IRQ() for
  504. * handling.
  505. *
  506. * Xen uses a two-level bitmap to speed searching. The first level is
  507. * a bitset of words which contain pending event bits. The second
  508. * level is a bitset of pending events themselves.
  509. */
  510. static void __xen_evtchn_do_upcall(void)
  511. {
  512. int cpu = get_cpu();
  513. struct shared_info *s = HYPERVISOR_shared_info;
  514. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  515. unsigned count;
  516. do {
  517. unsigned long pending_words;
  518. vcpu_info->evtchn_upcall_pending = 0;
  519. if (__get_cpu_var(xed_nesting_count)++)
  520. goto out;
  521. #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
  522. /* Clear master flag /before/ clearing selector flag. */
  523. wmb();
  524. #endif
  525. pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
  526. while (pending_words != 0) {
  527. unsigned long pending_bits;
  528. int word_idx = __ffs(pending_words);
  529. pending_words &= ~(1UL << word_idx);
  530. while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
  531. int bit_idx = __ffs(pending_bits);
  532. int port = (word_idx * BITS_PER_LONG) + bit_idx;
  533. int irq = evtchn_to_irq[port];
  534. struct irq_desc *desc;
  535. if (irq != -1) {
  536. desc = irq_to_desc(irq);
  537. if (desc)
  538. generic_handle_irq_desc(irq, desc);
  539. }
  540. }
  541. }
  542. BUG_ON(!irqs_disabled());
  543. count = __get_cpu_var(xed_nesting_count);
  544. __get_cpu_var(xed_nesting_count) = 0;
  545. } while (count != 1 || vcpu_info->evtchn_upcall_pending);
  546. out:
  547. put_cpu();
  548. }
  549. void xen_evtchn_do_upcall(struct pt_regs *regs)
  550. {
  551. struct pt_regs *old_regs = set_irq_regs(regs);
  552. exit_idle();
  553. irq_enter();
  554. __xen_evtchn_do_upcall();
  555. irq_exit();
  556. set_irq_regs(old_regs);
  557. }
  558. void xen_hvm_evtchn_do_upcall(void)
  559. {
  560. __xen_evtchn_do_upcall();
  561. }
  562. EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
  563. /* Rebind a new event channel to an existing irq. */
  564. void rebind_evtchn_irq(int evtchn, int irq)
  565. {
  566. struct irq_info *info = info_for_irq(irq);
  567. /* Make sure the irq is masked, since the new event channel
  568. will also be masked. */
  569. disable_irq(irq);
  570. spin_lock(&irq_mapping_update_lock);
  571. /* After resume the irq<->evtchn mappings are all cleared out */
  572. BUG_ON(evtchn_to_irq[evtchn] != -1);
  573. /* Expect irq to have been bound before,
  574. so there should be a proper type */
  575. BUG_ON(info->type == IRQT_UNBOUND);
  576. evtchn_to_irq[evtchn] = irq;
  577. irq_info[irq] = mk_evtchn_info(evtchn);
  578. spin_unlock(&irq_mapping_update_lock);
  579. /* new event channels are always bound to cpu 0 */
  580. irq_set_affinity(irq, cpumask_of(0));
  581. /* Unmask the event channel. */
  582. enable_irq(irq);
  583. }
  584. /* Rebind an evtchn so that it gets delivered to a specific cpu */
  585. static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
  586. {
  587. struct evtchn_bind_vcpu bind_vcpu;
  588. int evtchn = evtchn_from_irq(irq);
  589. /* events delivered via platform PCI interrupts are always
  590. * routed to vcpu 0 */
  591. if (!VALID_EVTCHN(evtchn) ||
  592. (xen_hvm_domain() && !xen_have_vector_callback))
  593. return -1;
  594. /* Send future instances of this interrupt to other vcpu. */
  595. bind_vcpu.port = evtchn;
  596. bind_vcpu.vcpu = tcpu;
  597. /*
  598. * If this fails, it usually just indicates that we're dealing with a
  599. * virq or IPI channel, which don't actually need to be rebound. Ignore
  600. * it, but don't do the xenlinux-level rebind in that case.
  601. */
  602. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
  603. bind_evtchn_to_cpu(evtchn, tcpu);
  604. return 0;
  605. }
  606. static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
  607. {
  608. unsigned tcpu = cpumask_first(dest);
  609. return rebind_irq_to_cpu(irq, tcpu);
  610. }
  611. int resend_irq_on_evtchn(unsigned int irq)
  612. {
  613. int masked, evtchn = evtchn_from_irq(irq);
  614. struct shared_info *s = HYPERVISOR_shared_info;
  615. if (!VALID_EVTCHN(evtchn))
  616. return 1;
  617. masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
  618. sync_set_bit(evtchn, s->evtchn_pending);
  619. if (!masked)
  620. unmask_evtchn(evtchn);
  621. return 1;
  622. }
  623. static void enable_dynirq(unsigned int irq)
  624. {
  625. int evtchn = evtchn_from_irq(irq);
  626. if (VALID_EVTCHN(evtchn))
  627. unmask_evtchn(evtchn);
  628. }
  629. static void disable_dynirq(unsigned int irq)
  630. {
  631. int evtchn = evtchn_from_irq(irq);
  632. if (VALID_EVTCHN(evtchn))
  633. mask_evtchn(evtchn);
  634. }
  635. static void ack_dynirq(unsigned int irq)
  636. {
  637. int evtchn = evtchn_from_irq(irq);
  638. move_native_irq(irq);
  639. if (VALID_EVTCHN(evtchn))
  640. clear_evtchn(evtchn);
  641. }
  642. static int retrigger_dynirq(unsigned int irq)
  643. {
  644. int evtchn = evtchn_from_irq(irq);
  645. struct shared_info *sh = HYPERVISOR_shared_info;
  646. int ret = 0;
  647. if (VALID_EVTCHN(evtchn)) {
  648. int masked;
  649. masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
  650. sync_set_bit(evtchn, sh->evtchn_pending);
  651. if (!masked)
  652. unmask_evtchn(evtchn);
  653. ret = 1;
  654. }
  655. return ret;
  656. }
  657. static void restore_cpu_virqs(unsigned int cpu)
  658. {
  659. struct evtchn_bind_virq bind_virq;
  660. int virq, irq, evtchn;
  661. for (virq = 0; virq < NR_VIRQS; virq++) {
  662. if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
  663. continue;
  664. BUG_ON(virq_from_irq(irq) != virq);
  665. /* Get a new binding from Xen. */
  666. bind_virq.virq = virq;
  667. bind_virq.vcpu = cpu;
  668. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  669. &bind_virq) != 0)
  670. BUG();
  671. evtchn = bind_virq.port;
  672. /* Record the new mapping. */
  673. evtchn_to_irq[evtchn] = irq;
  674. irq_info[irq] = mk_virq_info(evtchn, virq);
  675. bind_evtchn_to_cpu(evtchn, cpu);
  676. /* Ready for use. */
  677. unmask_evtchn(evtchn);
  678. }
  679. }
  680. static void restore_cpu_ipis(unsigned int cpu)
  681. {
  682. struct evtchn_bind_ipi bind_ipi;
  683. int ipi, irq, evtchn;
  684. for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
  685. if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
  686. continue;
  687. BUG_ON(ipi_from_irq(irq) != ipi);
  688. /* Get a new binding from Xen. */
  689. bind_ipi.vcpu = cpu;
  690. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  691. &bind_ipi) != 0)
  692. BUG();
  693. evtchn = bind_ipi.port;
  694. /* Record the new mapping. */
  695. evtchn_to_irq[evtchn] = irq;
  696. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  697. bind_evtchn_to_cpu(evtchn, cpu);
  698. /* Ready for use. */
  699. unmask_evtchn(evtchn);
  700. }
  701. }
  702. /* Clear an irq's pending state, in preparation for polling on it */
  703. void xen_clear_irq_pending(int irq)
  704. {
  705. int evtchn = evtchn_from_irq(irq);
  706. if (VALID_EVTCHN(evtchn))
  707. clear_evtchn(evtchn);
  708. }
  709. void xen_set_irq_pending(int irq)
  710. {
  711. int evtchn = evtchn_from_irq(irq);
  712. if (VALID_EVTCHN(evtchn))
  713. set_evtchn(evtchn);
  714. }
  715. bool xen_test_irq_pending(int irq)
  716. {
  717. int evtchn = evtchn_from_irq(irq);
  718. bool ret = false;
  719. if (VALID_EVTCHN(evtchn))
  720. ret = test_evtchn(evtchn);
  721. return ret;
  722. }
  723. /* Poll waiting for an irq to become pending. In the usual case, the
  724. irq will be disabled so it won't deliver an interrupt. */
  725. void xen_poll_irq(int irq)
  726. {
  727. evtchn_port_t evtchn = evtchn_from_irq(irq);
  728. if (VALID_EVTCHN(evtchn)) {
  729. struct sched_poll poll;
  730. poll.nr_ports = 1;
  731. poll.timeout = 0;
  732. set_xen_guest_handle(poll.ports, &evtchn);
  733. if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
  734. BUG();
  735. }
  736. }
  737. void xen_irq_resume(void)
  738. {
  739. unsigned int cpu, irq, evtchn;
  740. init_evtchn_cpu_bindings();
  741. /* New event-channel space is not 'live' yet. */
  742. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  743. mask_evtchn(evtchn);
  744. /* No IRQ <-> event-channel mappings. */
  745. for (irq = 0; irq < nr_irqs; irq++)
  746. irq_info[irq].evtchn = 0; /* zap event-channel binding */
  747. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  748. evtchn_to_irq[evtchn] = -1;
  749. for_each_possible_cpu(cpu) {
  750. restore_cpu_virqs(cpu);
  751. restore_cpu_ipis(cpu);
  752. }
  753. }
  754. static struct irq_chip xen_dynamic_chip __read_mostly = {
  755. .name = "xen-dyn",
  756. .disable = disable_dynirq,
  757. .mask = disable_dynirq,
  758. .unmask = enable_dynirq,
  759. .ack = ack_dynirq,
  760. .set_affinity = set_affinity_irq,
  761. .retrigger = retrigger_dynirq,
  762. };
  763. int xen_set_callback_via(uint64_t via)
  764. {
  765. struct xen_hvm_param a;
  766. a.domid = DOMID_SELF;
  767. a.index = HVM_PARAM_CALLBACK_IRQ;
  768. a.value = via;
  769. return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
  770. }
  771. EXPORT_SYMBOL_GPL(xen_set_callback_via);
  772. #ifdef CONFIG_XEN_PVHVM
  773. /* Vector callbacks are better than PCI interrupts to receive event
  774. * channel notifications because we can receive vector callbacks on any
  775. * vcpu and we don't need PCI support or APIC interactions. */
  776. void xen_callback_vector(void)
  777. {
  778. int rc;
  779. uint64_t callback_via;
  780. if (xen_have_vector_callback) {
  781. callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
  782. rc = xen_set_callback_via(callback_via);
  783. if (rc) {
  784. printk(KERN_ERR "Request for Xen HVM callback vector"
  785. " failed.\n");
  786. xen_have_vector_callback = 0;
  787. return;
  788. }
  789. printk(KERN_INFO "Xen HVM callback vector for event delivery is "
  790. "enabled\n");
  791. /* in the restore case the vector has already been allocated */
  792. if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
  793. alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
  794. }
  795. }
  796. #else
  797. void xen_callback_vector(void) {}
  798. #endif
  799. void __init xen_init_IRQ(void)
  800. {
  801. int i;
  802. cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
  803. GFP_KERNEL);
  804. BUG_ON(cpu_evtchn_mask_p == NULL);
  805. init_evtchn_cpu_bindings();
  806. /* No event channels are 'live' right now. */
  807. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  808. mask_evtchn(i);
  809. if (xen_hvm_domain()) {
  810. xen_callback_vector();
  811. native_init_IRQ();
  812. } else {
  813. irq_ctx_init(smp_processor_id());
  814. }
  815. }