iosapic.c 29 KB

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