iosapic.c 31 KB

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