iosapic.c 29 KB

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