iosapic.c 30 KB

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