iosapic.c 30 KB

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