iosapic.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * I/O SAPIC support.
  3. *
  4. * Copyright (C) 1999 Intel Corp.
  5. * Copyright (C) 1999 Asit Mallick <asit.k.mallick@intel.com>
  6. * Copyright (C) 2000-2002 J.I. Lee <jung-ik.lee@intel.com>
  7. * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co.
  8. * David Mosberger-Tang <davidm@hpl.hp.com>
  9. * Copyright (C) 1999 VA Linux Systems
  10. * Copyright (C) 1999,2000 Walt Drummond <drummond@valinux.com>
  11. *
  12. * 00/04/19 D. Mosberger Rewritten to mirror more closely the x86 I/O
  13. * APIC code. In particular, we now have separate
  14. * handlers for edge and level triggered
  15. * interrupts.
  16. * 00/10/27 Asit Mallick, Goutham Rao <goutham.rao@intel.com> IRQ vector
  17. * allocation PCI to vector mapping, shared PCI
  18. * interrupts.
  19. * 00/10/27 D. Mosberger Document things a bit more to make them more
  20. * understandable. Clean up much of the old
  21. * IOSAPIC cruft.
  22. * 01/07/27 J.I. Lee PCI irq routing, Platform/Legacy interrupts
  23. * and fixes for ACPI S5(SoftOff) support.
  24. * 02/01/23 J.I. Lee iosapic pgm fixes for PCI irq routing from _PRT
  25. * 02/01/07 E. Focht <efocht@ess.nec.de> Redirectable interrupt
  26. * vectors in iosapic_set_affinity(),
  27. * initializations for /proc/irq/#/smp_affinity
  28. * 02/04/02 P. Diefenbaugh Cleaned up ACPI PCI IRQ routing.
  29. * 02/04/18 J.I. Lee bug fix in iosapic_init_pci_irq
  30. * 02/04/30 J.I. Lee bug fix in find_iosapic to fix ACPI PCI IRQ to
  31. * IOSAPIC mapping error
  32. * 02/07/29 T. Kochi Allocate interrupt vectors dynamically
  33. * 02/08/04 T. Kochi Cleaned up terminology (irq, global system
  34. * interrupt, vector, etc.)
  35. * 02/09/20 D. Mosberger Simplified by taking advantage of ACPI's
  36. * pci_irq code.
  37. * 03/02/19 B. Helgaas Make pcat_compat system-wide, not per-IOSAPIC.
  38. * Remove iosapic_address & gsi_base from
  39. * external interfaces. Rationalize
  40. * __init/__devinit attributes.
  41. * 04/12/04 Ashok Raj <ashok.raj@intel.com> Intel Corporation 2004
  42. * Updated to work with irq migration necessary
  43. * for CPU Hotplug
  44. */
  45. /*
  46. * Here is what the interrupt logic between a PCI device and the kernel looks
  47. * like:
  48. *
  49. * (1) A PCI device raises one of the four interrupt pins (INTA, INTB, INTC,
  50. * INTD). The device is uniquely identified by its bus-, and slot-number
  51. * (the function number does not matter here because all functions share
  52. * the same interrupt lines).
  53. *
  54. * (2) The motherboard routes the interrupt line to a pin on a IOSAPIC
  55. * controller. Multiple interrupt lines may have to share the same
  56. * IOSAPIC pin (if they're level triggered and use the same polarity).
  57. * Each interrupt line has a unique Global System Interrupt (GSI) number
  58. * which can be calculated as the sum of the controller's base GSI number
  59. * and the IOSAPIC pin number to which the line connects.
  60. *
  61. * (3) The IOSAPIC uses an internal routing table entries (RTEs) to map the
  62. * IOSAPIC pin into the IA-64 interrupt vector. This interrupt vector is then
  63. * sent to the CPU.
  64. *
  65. * (4) The kernel recognizes an interrupt as an IRQ. The IRQ interface is
  66. * used as architecture-independent interrupt handling mechanism in Linux.
  67. * As an IRQ is a number, we have to have
  68. * IA-64 interrupt vector number <-> IRQ number mapping. On smaller
  69. * systems, we use one-to-one mapping between IA-64 vector and IRQ. A
  70. * platform can implement platform_irq_to_vector(irq) and
  71. * platform_local_vector_to_irq(vector) APIs to differentiate the mapping.
  72. * Please see also arch/ia64/include/asm/hw_irq.h for those APIs.
  73. *
  74. * To sum up, there are three levels of mappings involved:
  75. *
  76. * PCI pin -> global system interrupt (GSI) -> IA-64 vector <-> IRQ
  77. *
  78. * Note: The term "IRQ" is loosely used everywhere in Linux kernel to
  79. * describeinterrupts. Now we use "IRQ" only for Linux IRQ's. ISA IRQ
  80. * (isa_irq) is the only exception in this source code.
  81. */
  82. #include <linux/acpi.h>
  83. #include <linux/init.h>
  84. #include <linux/irq.h>
  85. #include <linux/kernel.h>
  86. #include <linux/list.h>
  87. #include <linux/pci.h>
  88. #include <linux/slab.h>
  89. #include <linux/smp.h>
  90. #include <linux/string.h>
  91. #include <linux/bootmem.h>
  92. #include <asm/delay.h>
  93. #include <asm/hw_irq.h>
  94. #include <asm/io.h>
  95. #include <asm/iosapic.h>
  96. #include <asm/machvec.h>
  97. #include <asm/processor.h>
  98. #include <asm/ptrace.h>
  99. #include <asm/system.h>
  100. #undef DEBUG_INTERRUPT_ROUTING
  101. #ifdef DEBUG_INTERRUPT_ROUTING
  102. #define DBG(fmt...) printk(fmt)
  103. #else
  104. #define DBG(fmt...)
  105. #endif
  106. static DEFINE_SPINLOCK(iosapic_lock);
  107. /*
  108. * These tables map IA-64 vectors to the IOSAPIC pin that generates this
  109. * vector.
  110. */
  111. #define NO_REF_RTE 0
  112. static struct iosapic {
  113. char __iomem *addr; /* base address of IOSAPIC */
  114. unsigned int gsi_base; /* GSI base */
  115. unsigned short num_rte; /* # of RTEs on this IOSAPIC */
  116. int rtes_inuse; /* # of RTEs in use on this IOSAPIC */
  117. #ifdef CONFIG_NUMA
  118. unsigned short node; /* numa node association via pxm */
  119. #endif
  120. spinlock_t lock; /* lock for indirect reg access */
  121. } iosapic_lists[NR_IOSAPICS];
  122. struct iosapic_rte_info {
  123. struct list_head rte_list; /* RTEs sharing the same vector */
  124. char rte_index; /* IOSAPIC RTE index */
  125. int refcnt; /* reference counter */
  126. struct iosapic *iosapic;
  127. } ____cacheline_aligned;
  128. static struct iosapic_intr_info {
  129. struct list_head rtes; /* RTEs using this vector (empty =>
  130. * not an IOSAPIC interrupt) */
  131. int count; /* # of registered RTEs */
  132. u32 low32; /* current value of low word of
  133. * Redirection table entry */
  134. unsigned int dest; /* destination CPU physical ID */
  135. unsigned char dmode : 3; /* delivery mode (see iosapic.h) */
  136. unsigned char polarity: 1; /* interrupt polarity
  137. * (see iosapic.h) */
  138. unsigned char trigger : 1; /* trigger mode (see iosapic.h) */
  139. } iosapic_intr_info[NR_IRQS];
  140. static unsigned char pcat_compat __devinitdata; /* 8259 compatibility flag */
  141. static inline void
  142. iosapic_write(struct iosapic *iosapic, unsigned int reg, u32 val)
  143. {
  144. unsigned long flags;
  145. spin_lock_irqsave(&iosapic->lock, flags);
  146. __iosapic_write(iosapic->addr, reg, val);
  147. spin_unlock_irqrestore(&iosapic->lock, flags);
  148. }
  149. /*
  150. * Find an IOSAPIC associated with a GSI
  151. */
  152. static inline int
  153. find_iosapic (unsigned int gsi)
  154. {
  155. int i;
  156. for (i = 0; i < NR_IOSAPICS; i++) {
  157. if ((unsigned) (gsi - iosapic_lists[i].gsi_base) <
  158. iosapic_lists[i].num_rte)
  159. return i;
  160. }
  161. return -1;
  162. }
  163. static inline int __gsi_to_irq(unsigned int gsi)
  164. {
  165. int irq;
  166. struct iosapic_intr_info *info;
  167. struct iosapic_rte_info *rte;
  168. for (irq = 0; irq < NR_IRQS; irq++) {
  169. info = &iosapic_intr_info[irq];
  170. list_for_each_entry(rte, &info->rtes, rte_list)
  171. if (rte->iosapic->gsi_base + rte->rte_index == gsi)
  172. return irq;
  173. }
  174. return -1;
  175. }
  176. int
  177. gsi_to_irq (unsigned int gsi)
  178. {
  179. unsigned long flags;
  180. int irq;
  181. spin_lock_irqsave(&iosapic_lock, flags);
  182. irq = __gsi_to_irq(gsi);
  183. spin_unlock_irqrestore(&iosapic_lock, flags);
  184. return irq;
  185. }
  186. static struct iosapic_rte_info *find_rte(unsigned int irq, unsigned int gsi)
  187. {
  188. struct iosapic_rte_info *rte;
  189. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list)
  190. if (rte->iosapic->gsi_base + rte->rte_index == gsi)
  191. return rte;
  192. return NULL;
  193. }
  194. static void
  195. set_rte (unsigned int gsi, unsigned int irq, unsigned int dest, int mask)
  196. {
  197. unsigned long pol, trigger, dmode;
  198. u32 low32, high32;
  199. int rte_index;
  200. char redir;
  201. struct iosapic_rte_info *rte;
  202. ia64_vector vector = irq_to_vector(irq);
  203. DBG(KERN_DEBUG"IOSAPIC: routing vector %d to 0x%x\n", vector, dest);
  204. rte = find_rte(irq, gsi);
  205. if (!rte)
  206. return; /* not an IOSAPIC interrupt */
  207. rte_index = rte->rte_index;
  208. pol = iosapic_intr_info[irq].polarity;
  209. trigger = iosapic_intr_info[irq].trigger;
  210. dmode = iosapic_intr_info[irq].dmode;
  211. redir = (dmode == IOSAPIC_LOWEST_PRIORITY) ? 1 : 0;
  212. #ifdef CONFIG_SMP
  213. set_irq_affinity_info(irq, (int)(dest & 0xffff), redir);
  214. #endif
  215. low32 = ((pol << IOSAPIC_POLARITY_SHIFT) |
  216. (trigger << IOSAPIC_TRIGGER_SHIFT) |
  217. (dmode << IOSAPIC_DELIVERY_SHIFT) |
  218. ((mask ? 1 : 0) << IOSAPIC_MASK_SHIFT) |
  219. vector);
  220. /* dest contains both id and eid */
  221. high32 = (dest << IOSAPIC_DEST_SHIFT);
  222. iosapic_write(rte->iosapic, IOSAPIC_RTE_HIGH(rte_index), high32);
  223. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  224. iosapic_intr_info[irq].low32 = low32;
  225. iosapic_intr_info[irq].dest = dest;
  226. }
  227. static void
  228. nop (unsigned int irq)
  229. {
  230. /* do nothing... */
  231. }
  232. #ifdef CONFIG_KEXEC
  233. void
  234. kexec_disable_iosapic(void)
  235. {
  236. struct iosapic_intr_info *info;
  237. struct iosapic_rte_info *rte;
  238. ia64_vector vec;
  239. int irq;
  240. for (irq = 0; irq < NR_IRQS; irq++) {
  241. info = &iosapic_intr_info[irq];
  242. vec = irq_to_vector(irq);
  243. list_for_each_entry(rte, &info->rtes,
  244. rte_list) {
  245. iosapic_write(rte->iosapic,
  246. IOSAPIC_RTE_LOW(rte->rte_index),
  247. IOSAPIC_MASK|vec);
  248. iosapic_eoi(rte->iosapic->addr, vec);
  249. }
  250. }
  251. }
  252. #endif
  253. static void
  254. mask_irq (unsigned int irq)
  255. {
  256. u32 low32;
  257. int rte_index;
  258. struct iosapic_rte_info *rte;
  259. if (!iosapic_intr_info[irq].count)
  260. return; /* not an IOSAPIC interrupt! */
  261. /* set only the mask bit */
  262. low32 = iosapic_intr_info[irq].low32 |= IOSAPIC_MASK;
  263. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  264. rte_index = rte->rte_index;
  265. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  266. }
  267. }
  268. static void
  269. unmask_irq (unsigned int irq)
  270. {
  271. u32 low32;
  272. int rte_index;
  273. struct iosapic_rte_info *rte;
  274. if (!iosapic_intr_info[irq].count)
  275. return; /* not an IOSAPIC interrupt! */
  276. low32 = iosapic_intr_info[irq].low32 &= ~IOSAPIC_MASK;
  277. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  278. rte_index = rte->rte_index;
  279. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  280. }
  281. }
  282. static int
  283. iosapic_set_affinity(unsigned int irq, const struct cpumask *mask)
  284. {
  285. #ifdef CONFIG_SMP
  286. u32 high32, low32;
  287. int cpu, dest, rte_index;
  288. int redir = (irq & IA64_IRQ_REDIRECTED) ? 1 : 0;
  289. struct iosapic_rte_info *rte;
  290. struct iosapic *iosapic;
  291. irq &= (~IA64_IRQ_REDIRECTED);
  292. cpu = cpumask_first_and(cpu_online_mask, mask);
  293. if (cpu >= nr_cpu_ids)
  294. return -1;
  295. if (irq_prepare_move(irq, cpu))
  296. return -1;
  297. dest = cpu_physical_id(cpu);
  298. if (!iosapic_intr_info[irq].count)
  299. return -1; /* not an IOSAPIC interrupt */
  300. set_irq_affinity_info(irq, dest, redir);
  301. /* dest contains both id and eid */
  302. high32 = dest << IOSAPIC_DEST_SHIFT;
  303. low32 = iosapic_intr_info[irq].low32 & ~(7 << IOSAPIC_DELIVERY_SHIFT);
  304. if (redir)
  305. /* change delivery mode to lowest priority */
  306. low32 |= (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
  307. else
  308. /* change delivery mode to fixed */
  309. low32 |= (IOSAPIC_FIXED << IOSAPIC_DELIVERY_SHIFT);
  310. low32 &= IOSAPIC_VECTOR_MASK;
  311. low32 |= irq_to_vector(irq);
  312. iosapic_intr_info[irq].low32 = low32;
  313. iosapic_intr_info[irq].dest = dest;
  314. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  315. iosapic = rte->iosapic;
  316. rte_index = rte->rte_index;
  317. iosapic_write(iosapic, IOSAPIC_RTE_HIGH(rte_index), high32);
  318. iosapic_write(iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  319. }
  320. #endif
  321. return 0;
  322. }
  323. /*
  324. * Handlers for level-triggered interrupts.
  325. */
  326. static unsigned int
  327. iosapic_startup_level_irq (unsigned int irq)
  328. {
  329. unmask_irq(irq);
  330. return 0;
  331. }
  332. static void
  333. iosapic_end_level_irq (unsigned int irq)
  334. {
  335. ia64_vector vec = irq_to_vector(irq);
  336. struct iosapic_rte_info *rte;
  337. int do_unmask_irq = 0;
  338. irq_complete_move(irq);
  339. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  340. do_unmask_irq = 1;
  341. mask_irq(irq);
  342. }
  343. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list)
  344. iosapic_eoi(rte->iosapic->addr, vec);
  345. if (unlikely(do_unmask_irq)) {
  346. move_masked_irq(irq);
  347. unmask_irq(irq);
  348. }
  349. }
  350. #define iosapic_shutdown_level_irq mask_irq
  351. #define iosapic_enable_level_irq unmask_irq
  352. #define iosapic_disable_level_irq mask_irq
  353. #define iosapic_ack_level_irq nop
  354. static struct irq_chip irq_type_iosapic_level = {
  355. .name = "IO-SAPIC-level",
  356. .startup = iosapic_startup_level_irq,
  357. .shutdown = iosapic_shutdown_level_irq,
  358. .enable = iosapic_enable_level_irq,
  359. .disable = iosapic_disable_level_irq,
  360. .ack = iosapic_ack_level_irq,
  361. .end = iosapic_end_level_irq,
  362. .mask = mask_irq,
  363. .unmask = unmask_irq,
  364. .set_affinity = iosapic_set_affinity
  365. };
  366. /*
  367. * Handlers for edge-triggered interrupts.
  368. */
  369. static unsigned int
  370. iosapic_startup_edge_irq (unsigned int irq)
  371. {
  372. unmask_irq(irq);
  373. /*
  374. * IOSAPIC simply drops interrupts pended while the
  375. * corresponding pin was masked, so we can't know if an
  376. * interrupt is pending already. Let's hope not...
  377. */
  378. return 0;
  379. }
  380. static void
  381. iosapic_ack_edge_irq (unsigned int irq)
  382. {
  383. struct irq_desc *idesc = irq_desc + irq;
  384. irq_complete_move(irq);
  385. move_native_irq(irq);
  386. /*
  387. * Once we have recorded IRQ_PENDING already, we can mask the
  388. * interrupt for real. This prevents IRQ storms from unhandled
  389. * devices.
  390. */
  391. if ((idesc->status & (IRQ_PENDING|IRQ_DISABLED)) ==
  392. (IRQ_PENDING|IRQ_DISABLED))
  393. mask_irq(irq);
  394. }
  395. #define iosapic_enable_edge_irq unmask_irq
  396. #define iosapic_disable_edge_irq nop
  397. #define iosapic_end_edge_irq nop
  398. static struct irq_chip irq_type_iosapic_edge = {
  399. .name = "IO-SAPIC-edge",
  400. .startup = iosapic_startup_edge_irq,
  401. .shutdown = iosapic_disable_edge_irq,
  402. .enable = iosapic_enable_edge_irq,
  403. .disable = iosapic_disable_edge_irq,
  404. .ack = iosapic_ack_edge_irq,
  405. .end = iosapic_end_edge_irq,
  406. .mask = mask_irq,
  407. .unmask = unmask_irq,
  408. .set_affinity = iosapic_set_affinity
  409. };
  410. static unsigned int
  411. iosapic_version (char __iomem *addr)
  412. {
  413. /*
  414. * IOSAPIC Version Register return 32 bit structure like:
  415. * {
  416. * unsigned int version : 8;
  417. * unsigned int reserved1 : 8;
  418. * unsigned int max_redir : 8;
  419. * unsigned int reserved2 : 8;
  420. * }
  421. */
  422. return __iosapic_read(addr, IOSAPIC_VERSION);
  423. }
  424. static int iosapic_find_sharable_irq(unsigned long trigger, unsigned long pol)
  425. {
  426. int i, irq = -ENOSPC, min_count = -1;
  427. struct iosapic_intr_info *info;
  428. /*
  429. * shared vectors for edge-triggered interrupts are not
  430. * supported yet
  431. */
  432. if (trigger == IOSAPIC_EDGE)
  433. return -EINVAL;
  434. for (i = 0; i < NR_IRQS; i++) {
  435. info = &iosapic_intr_info[i];
  436. if (info->trigger == trigger && info->polarity == pol &&
  437. (info->dmode == IOSAPIC_FIXED ||
  438. info->dmode == IOSAPIC_LOWEST_PRIORITY) &&
  439. can_request_irq(i, IRQF_SHARED)) {
  440. if (min_count == -1 || info->count < min_count) {
  441. irq = i;
  442. min_count = info->count;
  443. }
  444. }
  445. }
  446. return irq;
  447. }
  448. /*
  449. * if the given vector is already owned by other,
  450. * assign a new vector for the other and make the vector available
  451. */
  452. static void __init
  453. iosapic_reassign_vector (int irq)
  454. {
  455. int new_irq;
  456. if (iosapic_intr_info[irq].count) {
  457. new_irq = create_irq();
  458. if (new_irq < 0)
  459. panic("%s: out of interrupt vectors!\n", __func__);
  460. printk(KERN_INFO "Reassigning vector %d to %d\n",
  461. irq_to_vector(irq), irq_to_vector(new_irq));
  462. memcpy(&iosapic_intr_info[new_irq], &iosapic_intr_info[irq],
  463. sizeof(struct iosapic_intr_info));
  464. INIT_LIST_HEAD(&iosapic_intr_info[new_irq].rtes);
  465. list_move(iosapic_intr_info[irq].rtes.next,
  466. &iosapic_intr_info[new_irq].rtes);
  467. memset(&iosapic_intr_info[irq], 0,
  468. sizeof(struct iosapic_intr_info));
  469. iosapic_intr_info[irq].low32 = IOSAPIC_MASK;
  470. INIT_LIST_HEAD(&iosapic_intr_info[irq].rtes);
  471. }
  472. }
  473. static inline int irq_is_shared (int irq)
  474. {
  475. return (iosapic_intr_info[irq].count > 1);
  476. }
  477. struct irq_chip*
  478. ia64_native_iosapic_get_irq_chip(unsigned long trigger)
  479. {
  480. if (trigger == IOSAPIC_EDGE)
  481. return &irq_type_iosapic_edge;
  482. else
  483. return &irq_type_iosapic_level;
  484. }
  485. static int
  486. register_intr (unsigned int gsi, int irq, unsigned char delivery,
  487. unsigned long polarity, unsigned long trigger)
  488. {
  489. struct irq_desc *idesc;
  490. struct irq_chip *irq_type;
  491. int index;
  492. struct iosapic_rte_info *rte;
  493. index = find_iosapic(gsi);
  494. if (index < 0) {
  495. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  496. __func__, gsi);
  497. return -ENODEV;
  498. }
  499. rte = find_rte(irq, gsi);
  500. if (!rte) {
  501. rte = kzalloc(sizeof (*rte), GFP_ATOMIC);
  502. if (!rte) {
  503. printk(KERN_WARNING "%s: cannot allocate memory\n",
  504. __func__);
  505. return -ENOMEM;
  506. }
  507. rte->iosapic = &iosapic_lists[index];
  508. rte->rte_index = gsi - rte->iosapic->gsi_base;
  509. rte->refcnt++;
  510. list_add_tail(&rte->rte_list, &iosapic_intr_info[irq].rtes);
  511. iosapic_intr_info[irq].count++;
  512. iosapic_lists[index].rtes_inuse++;
  513. }
  514. else if (rte->refcnt == NO_REF_RTE) {
  515. struct iosapic_intr_info *info = &iosapic_intr_info[irq];
  516. if (info->count > 0 &&
  517. (info->trigger != trigger || info->polarity != polarity)){
  518. printk (KERN_WARNING
  519. "%s: cannot override the interrupt\n",
  520. __func__);
  521. return -EINVAL;
  522. }
  523. rte->refcnt++;
  524. iosapic_intr_info[irq].count++;
  525. iosapic_lists[index].rtes_inuse++;
  526. }
  527. iosapic_intr_info[irq].polarity = polarity;
  528. iosapic_intr_info[irq].dmode = delivery;
  529. iosapic_intr_info[irq].trigger = trigger;
  530. irq_type = iosapic_get_irq_chip(trigger);
  531. idesc = irq_desc + irq;
  532. if (irq_type != NULL && idesc->chip != irq_type) {
  533. if (idesc->chip != &no_irq_chip)
  534. printk(KERN_WARNING
  535. "%s: changing vector %d from %s to %s\n",
  536. __func__, irq_to_vector(irq),
  537. idesc->chip->name, irq_type->name);
  538. idesc->chip = irq_type;
  539. }
  540. return 0;
  541. }
  542. static unsigned int
  543. get_target_cpu (unsigned int gsi, int irq)
  544. {
  545. #ifdef CONFIG_SMP
  546. static int cpu = -1;
  547. extern int cpe_vector;
  548. cpumask_t domain = irq_to_domain(irq);
  549. /*
  550. * In case of vector shared by multiple RTEs, all RTEs that
  551. * share the vector need to use the same destination CPU.
  552. */
  553. if (iosapic_intr_info[irq].count)
  554. return iosapic_intr_info[irq].dest;
  555. /*
  556. * If the platform supports redirection via XTP, let it
  557. * distribute interrupts.
  558. */
  559. if (smp_int_redirect & SMP_IRQ_REDIRECTION)
  560. return cpu_physical_id(smp_processor_id());
  561. /*
  562. * Some interrupts (ACPI SCI, for instance) are registered
  563. * before the BSP is marked as online.
  564. */
  565. if (!cpu_online(smp_processor_id()))
  566. return cpu_physical_id(smp_processor_id());
  567. #ifdef CONFIG_ACPI
  568. if (cpe_vector > 0 && irq_to_vector(irq) == IA64_CPEP_VECTOR)
  569. return get_cpei_target_cpu();
  570. #endif
  571. #ifdef CONFIG_NUMA
  572. {
  573. int num_cpus, cpu_index, iosapic_index, numa_cpu, i = 0;
  574. const struct cpumask *cpu_mask;
  575. iosapic_index = find_iosapic(gsi);
  576. if (iosapic_index < 0 ||
  577. iosapic_lists[iosapic_index].node == MAX_NUMNODES)
  578. goto skip_numa_setup;
  579. cpu_mask = cpumask_of_node(iosapic_lists[iosapic_index].node);
  580. num_cpus = 0;
  581. for_each_cpu_and(numa_cpu, cpu_mask, &domain) {
  582. if (cpu_online(numa_cpu))
  583. num_cpus++;
  584. }
  585. if (!num_cpus)
  586. goto skip_numa_setup;
  587. /* Use irq assignment to distribute across cpus in node */
  588. cpu_index = irq % num_cpus;
  589. for_each_cpu_and(numa_cpu, cpu_mask, &domain)
  590. if (cpu_online(numa_cpu) && i++ >= cpu_index)
  591. break;
  592. if (numa_cpu < nr_cpu_ids)
  593. return cpu_physical_id(numa_cpu);
  594. }
  595. skip_numa_setup:
  596. #endif
  597. /*
  598. * Otherwise, round-robin interrupt vectors across all the
  599. * processors. (It'd be nice if we could be smarter in the
  600. * case of NUMA.)
  601. */
  602. do {
  603. if (++cpu >= nr_cpu_ids)
  604. cpu = 0;
  605. } while (!cpu_online(cpu) || !cpu_isset(cpu, domain));
  606. return cpu_physical_id(cpu);
  607. #else /* CONFIG_SMP */
  608. return cpu_physical_id(smp_processor_id());
  609. #endif
  610. }
  611. static inline unsigned char choose_dmode(void)
  612. {
  613. #ifdef CONFIG_SMP
  614. if (smp_int_redirect & SMP_IRQ_REDIRECTION)
  615. return IOSAPIC_LOWEST_PRIORITY;
  616. #endif
  617. return IOSAPIC_FIXED;
  618. }
  619. /*
  620. * ACPI can describe IOSAPIC interrupts via static tables and namespace
  621. * methods. This provides an interface to register those interrupts and
  622. * program the IOSAPIC RTE.
  623. */
  624. int
  625. iosapic_register_intr (unsigned int gsi,
  626. unsigned long polarity, unsigned long trigger)
  627. {
  628. int irq, mask = 1, err;
  629. unsigned int dest;
  630. unsigned long flags;
  631. struct iosapic_rte_info *rte;
  632. u32 low32;
  633. unsigned char dmode;
  634. /*
  635. * If this GSI has already been registered (i.e., it's a
  636. * shared interrupt, or we lost a race to register it),
  637. * don't touch the RTE.
  638. */
  639. spin_lock_irqsave(&iosapic_lock, flags);
  640. irq = __gsi_to_irq(gsi);
  641. if (irq > 0) {
  642. rte = find_rte(irq, gsi);
  643. if(iosapic_intr_info[irq].count == 0) {
  644. assign_irq_vector(irq);
  645. dynamic_irq_init(irq);
  646. } else if (rte->refcnt != NO_REF_RTE) {
  647. rte->refcnt++;
  648. goto unlock_iosapic_lock;
  649. }
  650. } else
  651. irq = create_irq();
  652. /* If vector is running out, we try to find a sharable vector */
  653. if (irq < 0) {
  654. irq = iosapic_find_sharable_irq(trigger, polarity);
  655. if (irq < 0)
  656. goto unlock_iosapic_lock;
  657. }
  658. raw_spin_lock(&irq_desc[irq].lock);
  659. dest = get_target_cpu(gsi, irq);
  660. dmode = choose_dmode();
  661. err = register_intr(gsi, irq, dmode, polarity, trigger);
  662. if (err < 0) {
  663. raw_spin_unlock(&irq_desc[irq].lock);
  664. irq = err;
  665. goto unlock_iosapic_lock;
  666. }
  667. /*
  668. * If the vector is shared and already unmasked for other
  669. * interrupt sources, don't mask it.
  670. */
  671. low32 = iosapic_intr_info[irq].low32;
  672. if (irq_is_shared(irq) && !(low32 & IOSAPIC_MASK))
  673. mask = 0;
  674. set_rte(gsi, irq, dest, mask);
  675. printk(KERN_INFO "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d\n",
  676. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  677. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  678. cpu_logical_id(dest), dest, irq_to_vector(irq));
  679. raw_spin_unlock(&irq_desc[irq].lock);
  680. unlock_iosapic_lock:
  681. spin_unlock_irqrestore(&iosapic_lock, flags);
  682. return irq;
  683. }
  684. void
  685. iosapic_unregister_intr (unsigned int gsi)
  686. {
  687. unsigned long flags;
  688. int irq, index;
  689. struct irq_desc *idesc;
  690. u32 low32;
  691. unsigned long trigger, polarity;
  692. unsigned int dest;
  693. struct iosapic_rte_info *rte;
  694. /*
  695. * If the irq associated with the gsi is not found,
  696. * iosapic_unregister_intr() is unbalanced. We need to check
  697. * this again after getting locks.
  698. */
  699. irq = gsi_to_irq(gsi);
  700. if (irq < 0) {
  701. printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n",
  702. gsi);
  703. WARN_ON(1);
  704. return;
  705. }
  706. spin_lock_irqsave(&iosapic_lock, flags);
  707. if ((rte = find_rte(irq, gsi)) == NULL) {
  708. printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n",
  709. gsi);
  710. WARN_ON(1);
  711. goto out;
  712. }
  713. if (--rte->refcnt > 0)
  714. goto out;
  715. idesc = irq_desc + irq;
  716. rte->refcnt = NO_REF_RTE;
  717. /* Mask the interrupt */
  718. low32 = iosapic_intr_info[irq].low32 | IOSAPIC_MASK;
  719. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte->rte_index), low32);
  720. iosapic_intr_info[irq].count--;
  721. index = find_iosapic(gsi);
  722. iosapic_lists[index].rtes_inuse--;
  723. WARN_ON(iosapic_lists[index].rtes_inuse < 0);
  724. trigger = iosapic_intr_info[irq].trigger;
  725. polarity = iosapic_intr_info[irq].polarity;
  726. dest = iosapic_intr_info[irq].dest;
  727. printk(KERN_INFO
  728. "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d unregistered\n",
  729. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  730. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  731. cpu_logical_id(dest), dest, irq_to_vector(irq));
  732. if (iosapic_intr_info[irq].count == 0) {
  733. #ifdef CONFIG_SMP
  734. /* Clear affinity */
  735. cpumask_setall(idesc->affinity);
  736. #endif
  737. /* Clear the interrupt information */
  738. iosapic_intr_info[irq].dest = 0;
  739. iosapic_intr_info[irq].dmode = 0;
  740. iosapic_intr_info[irq].polarity = 0;
  741. iosapic_intr_info[irq].trigger = 0;
  742. iosapic_intr_info[irq].low32 |= IOSAPIC_MASK;
  743. /* Destroy and reserve IRQ */
  744. destroy_and_reserve_irq(irq);
  745. }
  746. out:
  747. spin_unlock_irqrestore(&iosapic_lock, flags);
  748. }
  749. /*
  750. * ACPI calls this when it finds an entry for a platform interrupt.
  751. */
  752. int __init
  753. iosapic_register_platform_intr (u32 int_type, unsigned int gsi,
  754. int iosapic_vector, u16 eid, u16 id,
  755. unsigned long polarity, unsigned long trigger)
  756. {
  757. static const char * const name[] = {"unknown", "PMI", "INIT", "CPEI"};
  758. unsigned char delivery;
  759. int irq, vector, mask = 0;
  760. unsigned int dest = ((id << 8) | eid) & 0xffff;
  761. switch (int_type) {
  762. case ACPI_INTERRUPT_PMI:
  763. irq = vector = iosapic_vector;
  764. bind_irq_vector(irq, vector, CPU_MASK_ALL);
  765. /*
  766. * since PMI vector is alloc'd by FW(ACPI) not by kernel,
  767. * we need to make sure the vector is available
  768. */
  769. iosapic_reassign_vector(irq);
  770. delivery = IOSAPIC_PMI;
  771. break;
  772. case ACPI_INTERRUPT_INIT:
  773. irq = create_irq();
  774. if (irq < 0)
  775. panic("%s: out of interrupt vectors!\n", __func__);
  776. vector = irq_to_vector(irq);
  777. delivery = IOSAPIC_INIT;
  778. break;
  779. case ACPI_INTERRUPT_CPEI:
  780. irq = vector = IA64_CPE_VECTOR;
  781. BUG_ON(bind_irq_vector(irq, vector, CPU_MASK_ALL));
  782. delivery = IOSAPIC_FIXED;
  783. mask = 1;
  784. break;
  785. default:
  786. printk(KERN_ERR "%s: invalid int type 0x%x\n", __func__,
  787. int_type);
  788. return -1;
  789. }
  790. register_intr(gsi, irq, delivery, polarity, trigger);
  791. printk(KERN_INFO
  792. "PLATFORM int %s (0x%x): GSI %u (%s, %s) -> CPU %d (0x%04x)"
  793. " vector %d\n",
  794. int_type < ARRAY_SIZE(name) ? name[int_type] : "unknown",
  795. int_type, gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  796. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  797. cpu_logical_id(dest), dest, vector);
  798. set_rte(gsi, irq, dest, mask);
  799. return vector;
  800. }
  801. /*
  802. * ACPI calls this when it finds an entry for a legacy ISA IRQ override.
  803. */
  804. void __devinit
  805. iosapic_override_isa_irq (unsigned int isa_irq, unsigned int gsi,
  806. unsigned long polarity,
  807. unsigned long trigger)
  808. {
  809. int vector, irq;
  810. unsigned int dest = cpu_physical_id(smp_processor_id());
  811. unsigned char dmode;
  812. irq = vector = isa_irq_to_vector(isa_irq);
  813. BUG_ON(bind_irq_vector(irq, vector, CPU_MASK_ALL));
  814. dmode = choose_dmode();
  815. register_intr(gsi, irq, dmode, polarity, trigger);
  816. DBG("ISA: IRQ %u -> GSI %u (%s,%s) -> CPU %d (0x%04x) vector %d\n",
  817. isa_irq, gsi, trigger == IOSAPIC_EDGE ? "edge" : "level",
  818. polarity == IOSAPIC_POL_HIGH ? "high" : "low",
  819. cpu_logical_id(dest), dest, vector);
  820. set_rte(gsi, irq, dest, 1);
  821. }
  822. void __init
  823. ia64_native_iosapic_pcat_compat_init(void)
  824. {
  825. if (pcat_compat) {
  826. /*
  827. * Disable the compatibility mode interrupts (8259 style),
  828. * needs IN/OUT support enabled.
  829. */
  830. printk(KERN_INFO
  831. "%s: Disabling PC-AT compatible 8259 interrupts\n",
  832. __func__);
  833. outb(0xff, 0xA1);
  834. outb(0xff, 0x21);
  835. }
  836. }
  837. void __init
  838. iosapic_system_init (int system_pcat_compat)
  839. {
  840. int irq;
  841. for (irq = 0; irq < NR_IRQS; ++irq) {
  842. iosapic_intr_info[irq].low32 = IOSAPIC_MASK;
  843. /* mark as unused */
  844. INIT_LIST_HEAD(&iosapic_intr_info[irq].rtes);
  845. iosapic_intr_info[irq].count = 0;
  846. }
  847. pcat_compat = system_pcat_compat;
  848. if (pcat_compat)
  849. iosapic_pcat_compat_init();
  850. }
  851. static inline int
  852. iosapic_alloc (void)
  853. {
  854. int index;
  855. for (index = 0; index < NR_IOSAPICS; index++)
  856. if (!iosapic_lists[index].addr)
  857. return index;
  858. printk(KERN_WARNING "%s: failed to allocate iosapic\n", __func__);
  859. return -1;
  860. }
  861. static inline void
  862. iosapic_free (int index)
  863. {
  864. memset(&iosapic_lists[index], 0, sizeof(iosapic_lists[0]));
  865. }
  866. static inline int
  867. iosapic_check_gsi_range (unsigned int gsi_base, unsigned int ver)
  868. {
  869. int index;
  870. unsigned int gsi_end, base, end;
  871. /* check gsi range */
  872. gsi_end = gsi_base + ((ver >> 16) & 0xff);
  873. for (index = 0; index < NR_IOSAPICS; index++) {
  874. if (!iosapic_lists[index].addr)
  875. continue;
  876. base = iosapic_lists[index].gsi_base;
  877. end = base + iosapic_lists[index].num_rte - 1;
  878. if (gsi_end < base || end < gsi_base)
  879. continue; /* OK */
  880. return -EBUSY;
  881. }
  882. return 0;
  883. }
  884. int __devinit
  885. iosapic_init (unsigned long phys_addr, unsigned int gsi_base)
  886. {
  887. int num_rte, err, index;
  888. unsigned int isa_irq, ver;
  889. char __iomem *addr;
  890. unsigned long flags;
  891. spin_lock_irqsave(&iosapic_lock, flags);
  892. index = find_iosapic(gsi_base);
  893. if (index >= 0) {
  894. spin_unlock_irqrestore(&iosapic_lock, flags);
  895. return -EBUSY;
  896. }
  897. addr = ioremap(phys_addr, 0);
  898. if (addr == NULL) {
  899. spin_unlock_irqrestore(&iosapic_lock, flags);
  900. return -ENOMEM;
  901. }
  902. ver = iosapic_version(addr);
  903. if ((err = iosapic_check_gsi_range(gsi_base, ver))) {
  904. iounmap(addr);
  905. spin_unlock_irqrestore(&iosapic_lock, flags);
  906. return err;
  907. }
  908. /*
  909. * The MAX_REDIR register holds the highest input pin number
  910. * (starting from 0). We add 1 so that we can use it for
  911. * number of pins (= RTEs)
  912. */
  913. num_rte = ((ver >> 16) & 0xff) + 1;
  914. index = iosapic_alloc();
  915. iosapic_lists[index].addr = addr;
  916. iosapic_lists[index].gsi_base = gsi_base;
  917. iosapic_lists[index].num_rte = num_rte;
  918. #ifdef CONFIG_NUMA
  919. iosapic_lists[index].node = MAX_NUMNODES;
  920. #endif
  921. spin_lock_init(&iosapic_lists[index].lock);
  922. spin_unlock_irqrestore(&iosapic_lock, flags);
  923. if ((gsi_base == 0) && pcat_compat) {
  924. /*
  925. * Map the legacy ISA devices into the IOSAPIC data. Some of
  926. * these may get reprogrammed later on with data from the ACPI
  927. * Interrupt Source Override table.
  928. */
  929. for (isa_irq = 0; isa_irq < 16; ++isa_irq)
  930. iosapic_override_isa_irq(isa_irq, isa_irq,
  931. IOSAPIC_POL_HIGH,
  932. IOSAPIC_EDGE);
  933. }
  934. return 0;
  935. }
  936. #ifdef CONFIG_HOTPLUG
  937. int
  938. iosapic_remove (unsigned int gsi_base)
  939. {
  940. int index, err = 0;
  941. unsigned long flags;
  942. spin_lock_irqsave(&iosapic_lock, flags);
  943. index = find_iosapic(gsi_base);
  944. if (index < 0) {
  945. printk(KERN_WARNING "%s: No IOSAPIC for GSI base %u\n",
  946. __func__, gsi_base);
  947. goto out;
  948. }
  949. if (iosapic_lists[index].rtes_inuse) {
  950. err = -EBUSY;
  951. printk(KERN_WARNING "%s: IOSAPIC for GSI base %u is busy\n",
  952. __func__, gsi_base);
  953. goto out;
  954. }
  955. iounmap(iosapic_lists[index].addr);
  956. iosapic_free(index);
  957. out:
  958. spin_unlock_irqrestore(&iosapic_lock, flags);
  959. return err;
  960. }
  961. #endif /* CONFIG_HOTPLUG */
  962. #ifdef CONFIG_NUMA
  963. void __devinit
  964. map_iosapic_to_node(unsigned int gsi_base, int node)
  965. {
  966. int index;
  967. index = find_iosapic(gsi_base);
  968. if (index < 0) {
  969. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  970. __func__, gsi_base);
  971. return;
  972. }
  973. iosapic_lists[index].node = node;
  974. return;
  975. }
  976. #endif