events.c 37 KB

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