iosapic.c 31 KB

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