events.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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 get_irq_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. /* By default all event channels notify CPU#0. */
  321. cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
  322. info = kzalloc(sizeof(*info), GFP_KERNEL);
  323. if (info == NULL)
  324. panic("Unable to allocate metadata for IRQ%d\n", irq);
  325. info->type = IRQT_UNBOUND;
  326. set_irq_data(irq, info);
  327. list_add_tail(&info->list, &xen_irq_list_head);
  328. }
  329. static int xen_allocate_irq_dynamic(void)
  330. {
  331. int first = 0;
  332. int irq;
  333. #ifdef CONFIG_X86_IO_APIC
  334. /*
  335. * For an HVM guest or domain 0 which see "real" (emulated or
  336. * actual repectively) GSIs we allocate dynamic IRQs
  337. * e.g. those corresponding to event channels or MSIs
  338. * etc. from the range above those "real" GSIs to avoid
  339. * collisions.
  340. */
  341. if (xen_initial_domain() || xen_hvm_domain())
  342. first = get_nr_irqs_gsi();
  343. #endif
  344. retry:
  345. irq = irq_alloc_desc_from(first, -1);
  346. if (irq == -ENOMEM && first > NR_IRQS_LEGACY) {
  347. printk(KERN_ERR "Out of dynamic IRQ space and eating into GSI space. You should increase nr_irqs\n");
  348. first = max(NR_IRQS_LEGACY, first - NR_IRQS_LEGACY);
  349. goto retry;
  350. }
  351. if (irq < 0)
  352. panic("No available IRQ to bind to: increase nr_irqs!\n");
  353. xen_irq_init(irq);
  354. return irq;
  355. }
  356. static int xen_allocate_irq_gsi(unsigned gsi)
  357. {
  358. int irq;
  359. /*
  360. * A PV guest has no concept of a GSI (since it has no ACPI
  361. * nor access to/knowledge of the physical APICs). Therefore
  362. * all IRQs are dynamically allocated from the entire IRQ
  363. * space.
  364. */
  365. if (xen_pv_domain() && !xen_initial_domain())
  366. return xen_allocate_irq_dynamic();
  367. /* Legacy IRQ descriptors are already allocated by the arch. */
  368. if (gsi < NR_IRQS_LEGACY)
  369. irq = gsi;
  370. else
  371. irq = irq_alloc_desc_at(gsi, -1);
  372. if (irq < 0)
  373. panic("Unable to allocate to IRQ%d (%d)\n", gsi, irq);
  374. xen_irq_init(irq);
  375. return irq;
  376. }
  377. static void xen_free_irq(unsigned irq)
  378. {
  379. struct irq_info *info = get_irq_data(irq);
  380. list_del(&info->list);
  381. set_irq_data(irq, NULL);
  382. kfree(info);
  383. /* Legacy IRQ descriptors are managed by the arch. */
  384. if (irq < NR_IRQS_LEGACY)
  385. return;
  386. irq_free_desc(irq);
  387. }
  388. static void pirq_unmask_notify(int irq)
  389. {
  390. struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
  391. if (unlikely(pirq_needs_eoi(irq))) {
  392. int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
  393. WARN_ON(rc);
  394. }
  395. }
  396. static void pirq_query_unmask(int irq)
  397. {
  398. struct physdev_irq_status_query irq_status;
  399. struct irq_info *info = info_for_irq(irq);
  400. BUG_ON(info->type != IRQT_PIRQ);
  401. irq_status.irq = pirq_from_irq(irq);
  402. if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
  403. irq_status.flags = 0;
  404. info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
  405. if (irq_status.flags & XENIRQSTAT_needs_eoi)
  406. info->u.pirq.flags |= PIRQ_NEEDS_EOI;
  407. }
  408. static bool probing_irq(int irq)
  409. {
  410. struct irq_desc *desc = irq_to_desc(irq);
  411. return desc && desc->action == NULL;
  412. }
  413. static unsigned int __startup_pirq(unsigned int irq)
  414. {
  415. struct evtchn_bind_pirq bind_pirq;
  416. struct irq_info *info = info_for_irq(irq);
  417. int evtchn = evtchn_from_irq(irq);
  418. int rc;
  419. BUG_ON(info->type != IRQT_PIRQ);
  420. if (VALID_EVTCHN(evtchn))
  421. goto out;
  422. bind_pirq.pirq = pirq_from_irq(irq);
  423. /* NB. We are happy to share unless we are probing. */
  424. bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
  425. BIND_PIRQ__WILL_SHARE : 0;
  426. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
  427. if (rc != 0) {
  428. if (!probing_irq(irq))
  429. printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
  430. irq);
  431. return 0;
  432. }
  433. evtchn = bind_pirq.port;
  434. pirq_query_unmask(irq);
  435. evtchn_to_irq[evtchn] = irq;
  436. bind_evtchn_to_cpu(evtchn, 0);
  437. info->evtchn = evtchn;
  438. out:
  439. unmask_evtchn(evtchn);
  440. pirq_unmask_notify(irq);
  441. return 0;
  442. }
  443. static unsigned int startup_pirq(struct irq_data *data)
  444. {
  445. return __startup_pirq(data->irq);
  446. }
  447. static void shutdown_pirq(struct irq_data *data)
  448. {
  449. struct evtchn_close close;
  450. unsigned int irq = data->irq;
  451. struct irq_info *info = info_for_irq(irq);
  452. int evtchn = evtchn_from_irq(irq);
  453. BUG_ON(info->type != IRQT_PIRQ);
  454. if (!VALID_EVTCHN(evtchn))
  455. return;
  456. mask_evtchn(evtchn);
  457. close.port = evtchn;
  458. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  459. BUG();
  460. bind_evtchn_to_cpu(evtchn, 0);
  461. evtchn_to_irq[evtchn] = -1;
  462. info->evtchn = 0;
  463. }
  464. static void enable_pirq(struct irq_data *data)
  465. {
  466. startup_pirq(data);
  467. }
  468. static void disable_pirq(struct irq_data *data)
  469. {
  470. }
  471. static void ack_pirq(struct irq_data *data)
  472. {
  473. int evtchn = evtchn_from_irq(data->irq);
  474. move_native_irq(data->irq);
  475. if (VALID_EVTCHN(evtchn)) {
  476. mask_evtchn(evtchn);
  477. clear_evtchn(evtchn);
  478. }
  479. }
  480. static int find_irq_by_gsi(unsigned gsi)
  481. {
  482. struct irq_info *info;
  483. list_for_each_entry(info, &xen_irq_list_head, list) {
  484. if (info->type != IRQT_PIRQ)
  485. continue;
  486. if (info->u.pirq.gsi == gsi)
  487. return info->irq;
  488. }
  489. return -1;
  490. }
  491. int xen_allocate_pirq_gsi(unsigned gsi)
  492. {
  493. return gsi;
  494. }
  495. /*
  496. * Do not make any assumptions regarding the relationship between the
  497. * IRQ number returned here and the Xen pirq argument.
  498. *
  499. * Note: We don't assign an event channel until the irq actually started
  500. * up. Return an existing irq if we've already got one for the gsi.
  501. */
  502. int xen_bind_pirq_gsi_to_irq(unsigned gsi,
  503. unsigned pirq, int shareable, char *name)
  504. {
  505. int irq = -1;
  506. struct physdev_irq irq_op;
  507. spin_lock(&irq_mapping_update_lock);
  508. irq = find_irq_by_gsi(gsi);
  509. if (irq != -1) {
  510. printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
  511. irq, gsi);
  512. goto out; /* XXX need refcount? */
  513. }
  514. irq = xen_allocate_irq_gsi(gsi);
  515. set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
  516. handle_level_irq, name);
  517. irq_op.irq = irq;
  518. irq_op.vector = 0;
  519. /* Only the privileged domain can do this. For non-priv, the pcifront
  520. * driver provides a PCI bus that does the call to do exactly
  521. * this in the priv domain. */
  522. if (xen_initial_domain() &&
  523. HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
  524. xen_free_irq(irq);
  525. irq = -ENOSPC;
  526. goto out;
  527. }
  528. xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
  529. shareable ? PIRQ_SHAREABLE : 0);
  530. out:
  531. spin_unlock(&irq_mapping_update_lock);
  532. return irq;
  533. }
  534. #ifdef CONFIG_PCI_MSI
  535. int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
  536. {
  537. int rc;
  538. struct physdev_get_free_pirq op_get_free_pirq;
  539. op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
  540. rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
  541. WARN_ONCE(rc == -ENOSYS,
  542. "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
  543. return rc ? -1 : op_get_free_pirq.pirq;
  544. }
  545. int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
  546. int pirq, int vector, const char *name)
  547. {
  548. int irq, ret;
  549. spin_lock(&irq_mapping_update_lock);
  550. irq = xen_allocate_irq_dynamic();
  551. if (irq == -1)
  552. goto out;
  553. set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
  554. handle_level_irq, name);
  555. xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
  556. ret = set_irq_msi(irq, msidesc);
  557. if (ret < 0)
  558. goto error_irq;
  559. out:
  560. spin_unlock(&irq_mapping_update_lock);
  561. return irq;
  562. error_irq:
  563. spin_unlock(&irq_mapping_update_lock);
  564. xen_free_irq(irq);
  565. return -1;
  566. }
  567. #endif
  568. int xen_destroy_irq(int irq)
  569. {
  570. struct irq_desc *desc;
  571. struct physdev_unmap_pirq unmap_irq;
  572. struct irq_info *info = info_for_irq(irq);
  573. int rc = -ENOENT;
  574. spin_lock(&irq_mapping_update_lock);
  575. desc = irq_to_desc(irq);
  576. if (!desc)
  577. goto out;
  578. if (xen_initial_domain()) {
  579. unmap_irq.pirq = info->u.pirq.pirq;
  580. unmap_irq.domid = DOMID_SELF;
  581. rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
  582. if (rc) {
  583. printk(KERN_WARNING "unmap irq failed %d\n", rc);
  584. goto out;
  585. }
  586. }
  587. xen_free_irq(irq);
  588. out:
  589. spin_unlock(&irq_mapping_update_lock);
  590. return rc;
  591. }
  592. int xen_irq_from_pirq(unsigned pirq)
  593. {
  594. int irq;
  595. struct irq_info *info;
  596. spin_lock(&irq_mapping_update_lock);
  597. list_for_each_entry(info, &xen_irq_list_head, list) {
  598. if (info == NULL || info->type != IRQT_PIRQ)
  599. continue;
  600. irq = info->irq;
  601. if (info->u.pirq.pirq == pirq)
  602. goto out;
  603. }
  604. irq = -1;
  605. out:
  606. spin_lock(&irq_mapping_update_lock);
  607. return irq;
  608. }
  609. int bind_evtchn_to_irq(unsigned int evtchn)
  610. {
  611. int irq;
  612. spin_lock(&irq_mapping_update_lock);
  613. irq = evtchn_to_irq[evtchn];
  614. if (irq == -1) {
  615. irq = xen_allocate_irq_dynamic();
  616. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  617. handle_fasteoi_irq, "event");
  618. xen_irq_info_evtchn_init(irq, evtchn);
  619. }
  620. spin_unlock(&irq_mapping_update_lock);
  621. return irq;
  622. }
  623. EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
  624. static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
  625. {
  626. struct evtchn_bind_ipi bind_ipi;
  627. int evtchn, irq;
  628. spin_lock(&irq_mapping_update_lock);
  629. irq = per_cpu(ipi_to_irq, cpu)[ipi];
  630. if (irq == -1) {
  631. irq = xen_allocate_irq_dynamic();
  632. if (irq < 0)
  633. goto out;
  634. set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
  635. handle_percpu_irq, "ipi");
  636. bind_ipi.vcpu = cpu;
  637. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  638. &bind_ipi) != 0)
  639. BUG();
  640. evtchn = bind_ipi.port;
  641. xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
  642. bind_evtchn_to_cpu(evtchn, cpu);
  643. }
  644. out:
  645. spin_unlock(&irq_mapping_update_lock);
  646. return irq;
  647. }
  648. int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
  649. {
  650. struct evtchn_bind_virq bind_virq;
  651. int evtchn, irq;
  652. spin_lock(&irq_mapping_update_lock);
  653. irq = per_cpu(virq_to_irq, cpu)[virq];
  654. if (irq == -1) {
  655. irq = xen_allocate_irq_dynamic();
  656. set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
  657. handle_percpu_irq, "virq");
  658. bind_virq.virq = virq;
  659. bind_virq.vcpu = cpu;
  660. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  661. &bind_virq) != 0)
  662. BUG();
  663. evtchn = bind_virq.port;
  664. xen_irq_info_virq_init(cpu, irq, evtchn, virq);
  665. bind_evtchn_to_cpu(evtchn, cpu);
  666. }
  667. spin_unlock(&irq_mapping_update_lock);
  668. return irq;
  669. }
  670. static void unbind_from_irq(unsigned int irq)
  671. {
  672. struct evtchn_close close;
  673. int evtchn = evtchn_from_irq(irq);
  674. spin_lock(&irq_mapping_update_lock);
  675. if (VALID_EVTCHN(evtchn)) {
  676. close.port = evtchn;
  677. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  678. BUG();
  679. switch (type_from_irq(irq)) {
  680. case IRQT_VIRQ:
  681. per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
  682. [virq_from_irq(irq)] = -1;
  683. break;
  684. case IRQT_IPI:
  685. per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
  686. [ipi_from_irq(irq)] = -1;
  687. break;
  688. default:
  689. break;
  690. }
  691. /* Closed ports are implicitly re-bound to VCPU0. */
  692. bind_evtchn_to_cpu(evtchn, 0);
  693. evtchn_to_irq[evtchn] = -1;
  694. }
  695. BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
  696. xen_free_irq(irq);
  697. spin_unlock(&irq_mapping_update_lock);
  698. }
  699. int bind_evtchn_to_irqhandler(unsigned int evtchn,
  700. irq_handler_t handler,
  701. unsigned long irqflags,
  702. const char *devname, void *dev_id)
  703. {
  704. unsigned int irq;
  705. int retval;
  706. irq = bind_evtchn_to_irq(evtchn);
  707. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  708. if (retval != 0) {
  709. unbind_from_irq(irq);
  710. return retval;
  711. }
  712. return irq;
  713. }
  714. EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
  715. int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
  716. irq_handler_t handler,
  717. unsigned long irqflags, const char *devname, void *dev_id)
  718. {
  719. unsigned int irq;
  720. int retval;
  721. irq = bind_virq_to_irq(virq, cpu);
  722. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  723. if (retval != 0) {
  724. unbind_from_irq(irq);
  725. return retval;
  726. }
  727. return irq;
  728. }
  729. EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
  730. int bind_ipi_to_irqhandler(enum ipi_vector ipi,
  731. unsigned int cpu,
  732. irq_handler_t handler,
  733. unsigned long irqflags,
  734. const char *devname,
  735. void *dev_id)
  736. {
  737. int irq, retval;
  738. irq = bind_ipi_to_irq(ipi, cpu);
  739. if (irq < 0)
  740. return irq;
  741. irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
  742. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  743. if (retval != 0) {
  744. unbind_from_irq(irq);
  745. return retval;
  746. }
  747. return irq;
  748. }
  749. void unbind_from_irqhandler(unsigned int irq, void *dev_id)
  750. {
  751. free_irq(irq, dev_id);
  752. unbind_from_irq(irq);
  753. }
  754. EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
  755. void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
  756. {
  757. int irq = per_cpu(ipi_to_irq, cpu)[vector];
  758. BUG_ON(irq < 0);
  759. notify_remote_via_irq(irq);
  760. }
  761. irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
  762. {
  763. struct shared_info *sh = HYPERVISOR_shared_info;
  764. int cpu = smp_processor_id();
  765. unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
  766. int i;
  767. unsigned long flags;
  768. static DEFINE_SPINLOCK(debug_lock);
  769. struct vcpu_info *v;
  770. spin_lock_irqsave(&debug_lock, flags);
  771. printk("\nvcpu %d\n ", cpu);
  772. for_each_online_cpu(i) {
  773. int pending;
  774. v = per_cpu(xen_vcpu, i);
  775. pending = (get_irq_regs() && i == cpu)
  776. ? xen_irqs_disabled(get_irq_regs())
  777. : v->evtchn_upcall_mask;
  778. printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
  779. pending, v->evtchn_upcall_pending,
  780. (int)(sizeof(v->evtchn_pending_sel)*2),
  781. v->evtchn_pending_sel);
  782. }
  783. v = per_cpu(xen_vcpu, cpu);
  784. printk("\npending:\n ");
  785. for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
  786. printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
  787. sh->evtchn_pending[i],
  788. i % 8 == 0 ? "\n " : " ");
  789. printk("\nglobal mask:\n ");
  790. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  791. printk("%0*lx%s",
  792. (int)(sizeof(sh->evtchn_mask[0])*2),
  793. sh->evtchn_mask[i],
  794. i % 8 == 0 ? "\n " : " ");
  795. printk("\nglobally unmasked:\n ");
  796. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  797. printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
  798. sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
  799. i % 8 == 0 ? "\n " : " ");
  800. printk("\nlocal cpu%d mask:\n ", cpu);
  801. for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
  802. printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
  803. cpu_evtchn[i],
  804. i % 8 == 0 ? "\n " : " ");
  805. printk("\nlocally unmasked:\n ");
  806. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
  807. unsigned long pending = sh->evtchn_pending[i]
  808. & ~sh->evtchn_mask[i]
  809. & cpu_evtchn[i];
  810. printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
  811. pending, i % 8 == 0 ? "\n " : " ");
  812. }
  813. printk("\npending list:\n");
  814. for (i = 0; i < NR_EVENT_CHANNELS; i++) {
  815. if (sync_test_bit(i, sh->evtchn_pending)) {
  816. int word_idx = i / BITS_PER_LONG;
  817. printk(" %d: event %d -> irq %d%s%s%s\n",
  818. cpu_from_evtchn(i), i,
  819. evtchn_to_irq[i],
  820. sync_test_bit(word_idx, &v->evtchn_pending_sel)
  821. ? "" : " l2-clear",
  822. !sync_test_bit(i, sh->evtchn_mask)
  823. ? "" : " globally-masked",
  824. sync_test_bit(i, cpu_evtchn)
  825. ? "" : " locally-masked");
  826. }
  827. }
  828. spin_unlock_irqrestore(&debug_lock, flags);
  829. return IRQ_HANDLED;
  830. }
  831. static DEFINE_PER_CPU(unsigned, xed_nesting_count);
  832. /*
  833. * Search the CPUs pending events bitmasks. For each one found, map
  834. * the event number to an irq, and feed it into do_IRQ() for
  835. * handling.
  836. *
  837. * Xen uses a two-level bitmap to speed searching. The first level is
  838. * a bitset of words which contain pending event bits. The second
  839. * level is a bitset of pending events themselves.
  840. */
  841. static void __xen_evtchn_do_upcall(void)
  842. {
  843. int cpu = get_cpu();
  844. struct shared_info *s = HYPERVISOR_shared_info;
  845. struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
  846. unsigned count;
  847. do {
  848. unsigned long pending_words;
  849. vcpu_info->evtchn_upcall_pending = 0;
  850. if (__this_cpu_inc_return(xed_nesting_count) - 1)
  851. goto out;
  852. #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
  853. /* Clear master flag /before/ clearing selector flag. */
  854. wmb();
  855. #endif
  856. pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
  857. while (pending_words != 0) {
  858. unsigned long pending_bits;
  859. int word_idx = __ffs(pending_words);
  860. pending_words &= ~(1UL << word_idx);
  861. while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
  862. int bit_idx = __ffs(pending_bits);
  863. int port = (word_idx * BITS_PER_LONG) + bit_idx;
  864. int irq = evtchn_to_irq[port];
  865. struct irq_desc *desc;
  866. mask_evtchn(port);
  867. clear_evtchn(port);
  868. if (irq != -1) {
  869. desc = irq_to_desc(irq);
  870. if (desc)
  871. generic_handle_irq_desc(irq, desc);
  872. }
  873. }
  874. }
  875. BUG_ON(!irqs_disabled());
  876. count = __this_cpu_read(xed_nesting_count);
  877. __this_cpu_write(xed_nesting_count, 0);
  878. } while (count != 1 || vcpu_info->evtchn_upcall_pending);
  879. out:
  880. put_cpu();
  881. }
  882. void xen_evtchn_do_upcall(struct pt_regs *regs)
  883. {
  884. struct pt_regs *old_regs = set_irq_regs(regs);
  885. exit_idle();
  886. irq_enter();
  887. __xen_evtchn_do_upcall();
  888. irq_exit();
  889. set_irq_regs(old_regs);
  890. }
  891. void xen_hvm_evtchn_do_upcall(void)
  892. {
  893. __xen_evtchn_do_upcall();
  894. }
  895. EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
  896. /* Rebind a new event channel to an existing irq. */
  897. void rebind_evtchn_irq(int evtchn, int irq)
  898. {
  899. struct irq_info *info = info_for_irq(irq);
  900. /* Make sure the irq is masked, since the new event channel
  901. will also be masked. */
  902. disable_irq(irq);
  903. spin_lock(&irq_mapping_update_lock);
  904. /* After resume the irq<->evtchn mappings are all cleared out */
  905. BUG_ON(evtchn_to_irq[evtchn] != -1);
  906. /* Expect irq to have been bound before,
  907. so there should be a proper type */
  908. BUG_ON(info->type == IRQT_UNBOUND);
  909. xen_irq_info_evtchn_init(irq, evtchn);
  910. spin_unlock(&irq_mapping_update_lock);
  911. /* new event channels are always bound to cpu 0 */
  912. irq_set_affinity(irq, cpumask_of(0));
  913. /* Unmask the event channel. */
  914. enable_irq(irq);
  915. }
  916. /* Rebind an evtchn so that it gets delivered to a specific cpu */
  917. static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
  918. {
  919. struct evtchn_bind_vcpu bind_vcpu;
  920. int evtchn = evtchn_from_irq(irq);
  921. if (!VALID_EVTCHN(evtchn))
  922. return -1;
  923. /*
  924. * Events delivered via platform PCI interrupts are always
  925. * routed to vcpu 0 and hence cannot be rebound.
  926. */
  927. if (xen_hvm_domain() && !xen_have_vector_callback)
  928. return -1;
  929. /* Send future instances of this interrupt to other vcpu. */
  930. bind_vcpu.port = evtchn;
  931. bind_vcpu.vcpu = tcpu;
  932. /*
  933. * If this fails, it usually just indicates that we're dealing with a
  934. * virq or IPI channel, which don't actually need to be rebound. Ignore
  935. * it, but don't do the xenlinux-level rebind in that case.
  936. */
  937. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
  938. bind_evtchn_to_cpu(evtchn, tcpu);
  939. return 0;
  940. }
  941. static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
  942. bool force)
  943. {
  944. unsigned tcpu = cpumask_first(dest);
  945. return rebind_irq_to_cpu(data->irq, tcpu);
  946. }
  947. int resend_irq_on_evtchn(unsigned int irq)
  948. {
  949. int masked, evtchn = evtchn_from_irq(irq);
  950. struct shared_info *s = HYPERVISOR_shared_info;
  951. if (!VALID_EVTCHN(evtchn))
  952. return 1;
  953. masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
  954. sync_set_bit(evtchn, s->evtchn_pending);
  955. if (!masked)
  956. unmask_evtchn(evtchn);
  957. return 1;
  958. }
  959. static void enable_dynirq(struct irq_data *data)
  960. {
  961. int evtchn = evtchn_from_irq(data->irq);
  962. if (VALID_EVTCHN(evtchn))
  963. unmask_evtchn(evtchn);
  964. }
  965. static void disable_dynirq(struct irq_data *data)
  966. {
  967. int evtchn = evtchn_from_irq(data->irq);
  968. if (VALID_EVTCHN(evtchn))
  969. mask_evtchn(evtchn);
  970. }
  971. static void ack_dynirq(struct irq_data *data)
  972. {
  973. int evtchn = evtchn_from_irq(data->irq);
  974. move_masked_irq(data->irq);
  975. if (VALID_EVTCHN(evtchn))
  976. unmask_evtchn(evtchn);
  977. }
  978. static int retrigger_dynirq(struct irq_data *data)
  979. {
  980. int evtchn = evtchn_from_irq(data->irq);
  981. struct shared_info *sh = HYPERVISOR_shared_info;
  982. int ret = 0;
  983. if (VALID_EVTCHN(evtchn)) {
  984. int masked;
  985. masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
  986. sync_set_bit(evtchn, sh->evtchn_pending);
  987. if (!masked)
  988. unmask_evtchn(evtchn);
  989. ret = 1;
  990. }
  991. return ret;
  992. }
  993. static void restore_pirqs(void)
  994. {
  995. int pirq, rc, irq, gsi;
  996. struct physdev_map_pirq map_irq;
  997. struct irq_info *info;
  998. list_for_each_entry(info, &xen_irq_list_head, list) {
  999. if (info->type != IRQT_PIRQ)
  1000. continue;
  1001. pirq = info->u.pirq.pirq;
  1002. gsi = info->u.pirq.gsi;
  1003. irq = info->irq;
  1004. /* save/restore of PT devices doesn't work, so at this point the
  1005. * only devices present are GSI based emulated devices */
  1006. if (!gsi)
  1007. continue;
  1008. map_irq.domid = DOMID_SELF;
  1009. map_irq.type = MAP_PIRQ_TYPE_GSI;
  1010. map_irq.index = gsi;
  1011. map_irq.pirq = pirq;
  1012. rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
  1013. if (rc) {
  1014. printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
  1015. gsi, irq, pirq, rc);
  1016. xen_free_irq(irq);
  1017. continue;
  1018. }
  1019. printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
  1020. __startup_pirq(irq);
  1021. }
  1022. }
  1023. static void restore_cpu_virqs(unsigned int cpu)
  1024. {
  1025. struct evtchn_bind_virq bind_virq;
  1026. int virq, irq, evtchn;
  1027. for (virq = 0; virq < NR_VIRQS; virq++) {
  1028. if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
  1029. continue;
  1030. BUG_ON(virq_from_irq(irq) != virq);
  1031. /* Get a new binding from Xen. */
  1032. bind_virq.virq = virq;
  1033. bind_virq.vcpu = cpu;
  1034. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  1035. &bind_virq) != 0)
  1036. BUG();
  1037. evtchn = bind_virq.port;
  1038. /* Record the new mapping. */
  1039. xen_irq_info_virq_init(cpu, irq, evtchn, virq);
  1040. bind_evtchn_to_cpu(evtchn, cpu);
  1041. }
  1042. }
  1043. static void restore_cpu_ipis(unsigned int cpu)
  1044. {
  1045. struct evtchn_bind_ipi bind_ipi;
  1046. int ipi, irq, evtchn;
  1047. for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
  1048. if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
  1049. continue;
  1050. BUG_ON(ipi_from_irq(irq) != ipi);
  1051. /* Get a new binding from Xen. */
  1052. bind_ipi.vcpu = cpu;
  1053. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  1054. &bind_ipi) != 0)
  1055. BUG();
  1056. evtchn = bind_ipi.port;
  1057. /* Record the new mapping. */
  1058. xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
  1059. bind_evtchn_to_cpu(evtchn, cpu);
  1060. }
  1061. }
  1062. /* Clear an irq's pending state, in preparation for polling on it */
  1063. void xen_clear_irq_pending(int irq)
  1064. {
  1065. int evtchn = evtchn_from_irq(irq);
  1066. if (VALID_EVTCHN(evtchn))
  1067. clear_evtchn(evtchn);
  1068. }
  1069. EXPORT_SYMBOL(xen_clear_irq_pending);
  1070. void xen_set_irq_pending(int irq)
  1071. {
  1072. int evtchn = evtchn_from_irq(irq);
  1073. if (VALID_EVTCHN(evtchn))
  1074. set_evtchn(evtchn);
  1075. }
  1076. bool xen_test_irq_pending(int irq)
  1077. {
  1078. int evtchn = evtchn_from_irq(irq);
  1079. bool ret = false;
  1080. if (VALID_EVTCHN(evtchn))
  1081. ret = test_evtchn(evtchn);
  1082. return ret;
  1083. }
  1084. /* Poll waiting for an irq to become pending with timeout. In the usual case,
  1085. * the irq will be disabled so it won't deliver an interrupt. */
  1086. void xen_poll_irq_timeout(int irq, u64 timeout)
  1087. {
  1088. evtchn_port_t evtchn = evtchn_from_irq(irq);
  1089. if (VALID_EVTCHN(evtchn)) {
  1090. struct sched_poll poll;
  1091. poll.nr_ports = 1;
  1092. poll.timeout = timeout;
  1093. set_xen_guest_handle(poll.ports, &evtchn);
  1094. if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
  1095. BUG();
  1096. }
  1097. }
  1098. EXPORT_SYMBOL(xen_poll_irq_timeout);
  1099. /* Poll waiting for an irq to become pending. In the usual case, the
  1100. * irq will be disabled so it won't deliver an interrupt. */
  1101. void xen_poll_irq(int irq)
  1102. {
  1103. xen_poll_irq_timeout(irq, 0 /* no timeout */);
  1104. }
  1105. void xen_irq_resume(void)
  1106. {
  1107. unsigned int cpu, evtchn;
  1108. struct irq_info *info;
  1109. init_evtchn_cpu_bindings();
  1110. /* New event-channel space is not 'live' yet. */
  1111. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  1112. mask_evtchn(evtchn);
  1113. /* No IRQ <-> event-channel mappings. */
  1114. list_for_each_entry(info, &xen_irq_list_head, list)
  1115. info->evtchn = 0; /* zap event-channel binding */
  1116. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  1117. evtchn_to_irq[evtchn] = -1;
  1118. for_each_possible_cpu(cpu) {
  1119. restore_cpu_virqs(cpu);
  1120. restore_cpu_ipis(cpu);
  1121. }
  1122. restore_pirqs();
  1123. }
  1124. static struct irq_chip xen_dynamic_chip __read_mostly = {
  1125. .name = "xen-dyn",
  1126. .irq_disable = disable_dynirq,
  1127. .irq_mask = disable_dynirq,
  1128. .irq_unmask = enable_dynirq,
  1129. .irq_eoi = ack_dynirq,
  1130. .irq_set_affinity = set_affinity_irq,
  1131. .irq_retrigger = retrigger_dynirq,
  1132. };
  1133. static struct irq_chip xen_pirq_chip __read_mostly = {
  1134. .name = "xen-pirq",
  1135. .irq_startup = startup_pirq,
  1136. .irq_shutdown = shutdown_pirq,
  1137. .irq_enable = enable_pirq,
  1138. .irq_unmask = enable_pirq,
  1139. .irq_disable = disable_pirq,
  1140. .irq_mask = disable_pirq,
  1141. .irq_ack = ack_pirq,
  1142. .irq_set_affinity = set_affinity_irq,
  1143. .irq_retrigger = retrigger_dynirq,
  1144. };
  1145. static struct irq_chip xen_percpu_chip __read_mostly = {
  1146. .name = "xen-percpu",
  1147. .irq_disable = disable_dynirq,
  1148. .irq_mask = disable_dynirq,
  1149. .irq_unmask = enable_dynirq,
  1150. .irq_ack = ack_dynirq,
  1151. };
  1152. int xen_set_callback_via(uint64_t via)
  1153. {
  1154. struct xen_hvm_param a;
  1155. a.domid = DOMID_SELF;
  1156. a.index = HVM_PARAM_CALLBACK_IRQ;
  1157. a.value = via;
  1158. return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
  1159. }
  1160. EXPORT_SYMBOL_GPL(xen_set_callback_via);
  1161. #ifdef CONFIG_XEN_PVHVM
  1162. /* Vector callbacks are better than PCI interrupts to receive event
  1163. * channel notifications because we can receive vector callbacks on any
  1164. * vcpu and we don't need PCI support or APIC interactions. */
  1165. void xen_callback_vector(void)
  1166. {
  1167. int rc;
  1168. uint64_t callback_via;
  1169. if (xen_have_vector_callback) {
  1170. callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
  1171. rc = xen_set_callback_via(callback_via);
  1172. if (rc) {
  1173. printk(KERN_ERR "Request for Xen HVM callback vector"
  1174. " failed.\n");
  1175. xen_have_vector_callback = 0;
  1176. return;
  1177. }
  1178. printk(KERN_INFO "Xen HVM callback vector for event delivery is "
  1179. "enabled\n");
  1180. /* in the restore case the vector has already been allocated */
  1181. if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
  1182. alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
  1183. }
  1184. }
  1185. #else
  1186. void xen_callback_vector(void) {}
  1187. #endif
  1188. void __init xen_init_IRQ(void)
  1189. {
  1190. int i;
  1191. evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
  1192. GFP_KERNEL);
  1193. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  1194. evtchn_to_irq[i] = -1;
  1195. init_evtchn_cpu_bindings();
  1196. /* No event channels are 'live' right now. */
  1197. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  1198. mask_evtchn(i);
  1199. if (xen_hvm_domain()) {
  1200. xen_callback_vector();
  1201. native_init_IRQ();
  1202. /* pci_xen_hvm_init must be called after native_init_IRQ so that
  1203. * __acpi_register_gsi can point at the right function */
  1204. pci_xen_hvm_init();
  1205. } else {
  1206. irq_ctx_init(smp_processor_id());
  1207. if (xen_initial_domain())
  1208. xen_setup_pirqs();
  1209. }
  1210. }