events.c 38 KB

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