iosapic.c 30 KB

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