iosapic.c 30 KB

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