iosapic.c 31 KB

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