iosapic.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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 hw_interrupt_type 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. .set_affinity = iosapic_set_affinity
  400. };
  401. /*
  402. * Handlers for edge-triggered interrupts.
  403. */
  404. static unsigned int
  405. iosapic_startup_edge_irq (unsigned int irq)
  406. {
  407. unmask_irq(irq);
  408. /*
  409. * IOSAPIC simply drops interrupts pended while the
  410. * corresponding pin was masked, so we can't know if an
  411. * interrupt is pending already. Let's hope not...
  412. */
  413. return 0;
  414. }
  415. static void
  416. iosapic_ack_edge_irq (unsigned int irq)
  417. {
  418. irq_desc_t *idesc = irq_desc + irq;
  419. move_native_irq(irq);
  420. /*
  421. * Once we have recorded IRQ_PENDING already, we can mask the
  422. * interrupt for real. This prevents IRQ storms from unhandled
  423. * devices.
  424. */
  425. if ((idesc->status & (IRQ_PENDING|IRQ_DISABLED)) ==
  426. (IRQ_PENDING|IRQ_DISABLED))
  427. mask_irq(irq);
  428. }
  429. #define iosapic_enable_edge_irq unmask_irq
  430. #define iosapic_disable_edge_irq nop
  431. #define iosapic_end_edge_irq nop
  432. struct hw_interrupt_type irq_type_iosapic_edge = {
  433. .name = "IO-SAPIC-edge",
  434. .startup = iosapic_startup_edge_irq,
  435. .shutdown = iosapic_disable_edge_irq,
  436. .enable = iosapic_enable_edge_irq,
  437. .disable = iosapic_disable_edge_irq,
  438. .ack = iosapic_ack_edge_irq,
  439. .end = iosapic_end_edge_irq,
  440. .set_affinity = iosapic_set_affinity
  441. };
  442. unsigned int
  443. iosapic_version (char __iomem *addr)
  444. {
  445. /*
  446. * IOSAPIC Version Register return 32 bit structure like:
  447. * {
  448. * unsigned int version : 8;
  449. * unsigned int reserved1 : 8;
  450. * unsigned int max_redir : 8;
  451. * unsigned int reserved2 : 8;
  452. * }
  453. */
  454. return iosapic_read(addr, IOSAPIC_VERSION);
  455. }
  456. static int iosapic_find_sharable_vector (unsigned long trigger,
  457. unsigned long pol)
  458. {
  459. int i, vector = -1, min_count = -1;
  460. struct iosapic_intr_info *info;
  461. /*
  462. * shared vectors for edge-triggered interrupts are not
  463. * supported yet
  464. */
  465. if (trigger == IOSAPIC_EDGE)
  466. return -1;
  467. for (i = IA64_FIRST_DEVICE_VECTOR; i <= IA64_LAST_DEVICE_VECTOR; i++) {
  468. info = &iosapic_intr_info[i];
  469. if (info->trigger == trigger && info->polarity == pol &&
  470. (info->dmode == IOSAPIC_FIXED || info->dmode ==
  471. IOSAPIC_LOWEST_PRIORITY)) {
  472. if (min_count == -1 || info->count < min_count) {
  473. vector = i;
  474. min_count = info->count;
  475. }
  476. }
  477. }
  478. return vector;
  479. }
  480. /*
  481. * if the given vector is already owned by other,
  482. * assign a new vector for the other and make the vector available
  483. */
  484. static void __init
  485. iosapic_reassign_vector (int vector)
  486. {
  487. int new_vector;
  488. if (!list_empty(&iosapic_intr_info[vector].rtes)) {
  489. new_vector = assign_irq_vector(AUTO_ASSIGN);
  490. if (new_vector < 0)
  491. panic("%s: out of interrupt vectors!\n", __FUNCTION__);
  492. printk(KERN_INFO "Reassigning vector %d to %d\n",
  493. vector, new_vector);
  494. memcpy(&iosapic_intr_info[new_vector], &iosapic_intr_info[vector],
  495. sizeof(struct iosapic_intr_info));
  496. INIT_LIST_HEAD(&iosapic_intr_info[new_vector].rtes);
  497. list_move(iosapic_intr_info[vector].rtes.next,
  498. &iosapic_intr_info[new_vector].rtes);
  499. memset(&iosapic_intr_info[vector], 0,
  500. sizeof(struct iosapic_intr_info));
  501. iosapic_intr_info[vector].low32 = IOSAPIC_MASK;
  502. INIT_LIST_HEAD(&iosapic_intr_info[vector].rtes);
  503. }
  504. }
  505. static struct iosapic_rte_info *iosapic_alloc_rte (void)
  506. {
  507. int i;
  508. struct iosapic_rte_info *rte;
  509. int preallocated = 0;
  510. if (!iosapic_kmalloc_ok && list_empty(&free_rte_list)) {
  511. rte = alloc_bootmem(sizeof(struct iosapic_rte_info) *
  512. NR_PREALLOCATE_RTE_ENTRIES);
  513. if (!rte)
  514. return NULL;
  515. for (i = 0; i < NR_PREALLOCATE_RTE_ENTRIES; i++, rte++)
  516. list_add(&rte->rte_list, &free_rte_list);
  517. }
  518. if (!list_empty(&free_rte_list)) {
  519. rte = list_entry(free_rte_list.next, struct iosapic_rte_info,
  520. rte_list);
  521. list_del(&rte->rte_list);
  522. preallocated++;
  523. } else {
  524. rte = kmalloc(sizeof(struct iosapic_rte_info), GFP_ATOMIC);
  525. if (!rte)
  526. return NULL;
  527. }
  528. memset(rte, 0, sizeof(struct iosapic_rte_info));
  529. if (preallocated)
  530. rte->flags |= RTE_PREALLOCATED;
  531. return rte;
  532. }
  533. static void iosapic_free_rte (struct iosapic_rte_info *rte)
  534. {
  535. if (rte->flags & RTE_PREALLOCATED)
  536. list_add_tail(&rte->rte_list, &free_rte_list);
  537. else
  538. kfree(rte);
  539. }
  540. static inline int vector_is_shared (int vector)
  541. {
  542. return (iosapic_intr_info[vector].count > 1);
  543. }
  544. static int
  545. register_intr (unsigned int gsi, int vector, unsigned char delivery,
  546. unsigned long polarity, unsigned long trigger)
  547. {
  548. irq_desc_t *idesc;
  549. struct hw_interrupt_type *irq_type;
  550. int rte_index;
  551. int index;
  552. unsigned long gsi_base;
  553. void __iomem *iosapic_address;
  554. struct iosapic_rte_info *rte;
  555. index = find_iosapic(gsi);
  556. if (index < 0) {
  557. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  558. __FUNCTION__, gsi);
  559. return -ENODEV;
  560. }
  561. iosapic_address = iosapic_lists[index].addr;
  562. gsi_base = iosapic_lists[index].gsi_base;
  563. rte = gsi_vector_to_rte(gsi, vector);
  564. if (!rte) {
  565. rte = iosapic_alloc_rte();
  566. if (!rte) {
  567. printk(KERN_WARNING "%s: cannot allocate memory\n",
  568. __FUNCTION__);
  569. return -ENOMEM;
  570. }
  571. rte_index = gsi - gsi_base;
  572. rte->rte_index = rte_index;
  573. rte->addr = iosapic_address;
  574. rte->gsi_base = gsi_base;
  575. rte->refcnt++;
  576. list_add_tail(&rte->rte_list, &iosapic_intr_info[vector].rtes);
  577. iosapic_intr_info[vector].count++;
  578. iosapic_lists[index].rtes_inuse++;
  579. }
  580. else if (vector_is_shared(vector)) {
  581. struct iosapic_intr_info *info = &iosapic_intr_info[vector];
  582. if (info->trigger != trigger || info->polarity != polarity) {
  583. printk (KERN_WARNING
  584. "%s: cannot override the interrupt\n",
  585. __FUNCTION__);
  586. return -EINVAL;
  587. }
  588. }
  589. iosapic_intr_info[vector].polarity = polarity;
  590. iosapic_intr_info[vector].dmode = delivery;
  591. iosapic_intr_info[vector].trigger = trigger;
  592. if (trigger == IOSAPIC_EDGE)
  593. irq_type = &irq_type_iosapic_edge;
  594. else
  595. irq_type = &irq_type_iosapic_level;
  596. idesc = irq_desc + vector;
  597. if (idesc->chip != irq_type) {
  598. if (idesc->chip != &no_irq_type)
  599. printk(KERN_WARNING
  600. "%s: changing vector %d from %s to %s\n",
  601. __FUNCTION__, vector,
  602. idesc->chip->name, irq_type->name);
  603. idesc->chip = irq_type;
  604. }
  605. return 0;
  606. }
  607. static unsigned int
  608. get_target_cpu (unsigned int gsi, int vector)
  609. {
  610. #ifdef CONFIG_SMP
  611. static int cpu = -1;
  612. extern int cpe_vector;
  613. /*
  614. * In case of vector shared by multiple RTEs, all RTEs that
  615. * share the vector need to use the same destination CPU.
  616. */
  617. if (!list_empty(&iosapic_intr_info[vector].rtes))
  618. return iosapic_intr_info[vector].dest;
  619. /*
  620. * If the platform supports redirection via XTP, let it
  621. * distribute interrupts.
  622. */
  623. if (smp_int_redirect & SMP_IRQ_REDIRECTION)
  624. return cpu_physical_id(smp_processor_id());
  625. /*
  626. * Some interrupts (ACPI SCI, for instance) are registered
  627. * before the BSP is marked as online.
  628. */
  629. if (!cpu_online(smp_processor_id()))
  630. return cpu_physical_id(smp_processor_id());
  631. #ifdef CONFIG_ACPI
  632. if (cpe_vector > 0 && vector == IA64_CPEP_VECTOR)
  633. return get_cpei_target_cpu();
  634. #endif
  635. #ifdef CONFIG_NUMA
  636. {
  637. int num_cpus, cpu_index, iosapic_index, numa_cpu, i = 0;
  638. cpumask_t cpu_mask;
  639. iosapic_index = find_iosapic(gsi);
  640. if (iosapic_index < 0 ||
  641. iosapic_lists[iosapic_index].node == MAX_NUMNODES)
  642. goto skip_numa_setup;
  643. cpu_mask = node_to_cpumask(iosapic_lists[iosapic_index].node);
  644. for_each_cpu_mask(numa_cpu, cpu_mask) {
  645. if (!cpu_online(numa_cpu))
  646. cpu_clear(numa_cpu, cpu_mask);
  647. }
  648. num_cpus = cpus_weight(cpu_mask);
  649. if (!num_cpus)
  650. goto skip_numa_setup;
  651. /* Use vector assignment to distribute across cpus in node */
  652. cpu_index = vector % num_cpus;
  653. for (numa_cpu = first_cpu(cpu_mask) ; i < cpu_index ; i++)
  654. numa_cpu = next_cpu(numa_cpu, cpu_mask);
  655. if (numa_cpu != NR_CPUS)
  656. return cpu_physical_id(numa_cpu);
  657. }
  658. skip_numa_setup:
  659. #endif
  660. /*
  661. * Otherwise, round-robin interrupt vectors across all the
  662. * processors. (It'd be nice if we could be smarter in the
  663. * case of NUMA.)
  664. */
  665. do {
  666. if (++cpu >= NR_CPUS)
  667. cpu = 0;
  668. } while (!cpu_online(cpu));
  669. return cpu_physical_id(cpu);
  670. #else /* CONFIG_SMP */
  671. return cpu_physical_id(smp_processor_id());
  672. #endif
  673. }
  674. /*
  675. * ACPI can describe IOSAPIC interrupts via static tables and namespace
  676. * methods. This provides an interface to register those interrupts and
  677. * program the IOSAPIC RTE.
  678. */
  679. int
  680. iosapic_register_intr (unsigned int gsi,
  681. unsigned long polarity, unsigned long trigger)
  682. {
  683. int vector, mask = 1, err;
  684. unsigned int dest;
  685. unsigned long flags;
  686. struct iosapic_rte_info *rte;
  687. u32 low32;
  688. again:
  689. /*
  690. * If this GSI has already been registered (i.e., it's a
  691. * shared interrupt, or we lost a race to register it),
  692. * don't touch the RTE.
  693. */
  694. spin_lock_irqsave(&iosapic_lock, flags);
  695. {
  696. vector = gsi_to_vector(gsi);
  697. if (vector > 0) {
  698. rte = gsi_vector_to_rte(gsi, vector);
  699. rte->refcnt++;
  700. spin_unlock_irqrestore(&iosapic_lock, flags);
  701. return vector;
  702. }
  703. }
  704. spin_unlock_irqrestore(&iosapic_lock, flags);
  705. /* If vector is running out, we try to find a sharable vector */
  706. vector = assign_irq_vector(AUTO_ASSIGN);
  707. if (vector < 0) {
  708. vector = iosapic_find_sharable_vector(trigger, polarity);
  709. if (vector < 0)
  710. return -ENOSPC;
  711. }
  712. spin_lock_irqsave(&irq_desc[vector].lock, flags);
  713. spin_lock(&iosapic_lock);
  714. {
  715. if (gsi_to_vector(gsi) > 0) {
  716. if (list_empty(&iosapic_intr_info[vector].rtes))
  717. free_irq_vector(vector);
  718. spin_unlock(&iosapic_lock);
  719. spin_unlock_irqrestore(&irq_desc[vector].lock,
  720. flags);
  721. goto again;
  722. }
  723. dest = get_target_cpu(gsi, vector);
  724. err = register_intr(gsi, vector, IOSAPIC_LOWEST_PRIORITY,
  725. polarity, trigger);
  726. if (err < 0) {
  727. spin_unlock(&iosapic_lock);
  728. spin_unlock_irqrestore(&irq_desc[vector].lock,
  729. flags);
  730. return err;
  731. }
  732. /*
  733. * If the vector is shared and already unmasked for
  734. * other interrupt sources, don't mask it.
  735. */
  736. low32 = iosapic_intr_info[vector].low32;
  737. if (vector_is_shared(vector) && !(low32 & IOSAPIC_MASK))
  738. mask = 0;
  739. set_rte(gsi, vector, dest, mask);
  740. }
  741. spin_unlock(&iosapic_lock);
  742. spin_unlock_irqrestore(&irq_desc[vector].lock, flags);
  743. printk(KERN_INFO "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d\n",
  744. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  745. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  746. cpu_logical_id(dest), dest, vector);
  747. return vector;
  748. }
  749. void
  750. iosapic_unregister_intr (unsigned int gsi)
  751. {
  752. unsigned long flags;
  753. int irq, vector, index;
  754. irq_desc_t *idesc;
  755. u32 low32;
  756. unsigned long trigger, polarity;
  757. unsigned int dest;
  758. struct iosapic_rte_info *rte;
  759. /*
  760. * If the irq associated with the gsi is not found,
  761. * iosapic_unregister_intr() is unbalanced. We need to check
  762. * this again after getting locks.
  763. */
  764. irq = gsi_to_irq(gsi);
  765. if (irq < 0) {
  766. printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n",
  767. gsi);
  768. WARN_ON(1);
  769. return;
  770. }
  771. vector = irq_to_vector(irq);
  772. idesc = irq_desc + irq;
  773. spin_lock_irqsave(&idesc->lock, flags);
  774. spin_lock(&iosapic_lock);
  775. {
  776. if ((rte = gsi_vector_to_rte(gsi, vector)) == NULL) {
  777. printk(KERN_ERR
  778. "iosapic_unregister_intr(%u) unbalanced\n",
  779. gsi);
  780. WARN_ON(1);
  781. goto out;
  782. }
  783. if (--rte->refcnt > 0)
  784. goto out;
  785. /* Mask the interrupt */
  786. low32 = iosapic_intr_info[vector].low32 | IOSAPIC_MASK;
  787. iosapic_write(rte->addr, IOSAPIC_RTE_LOW(rte->rte_index),
  788. low32);
  789. /* Remove the rte entry from the list */
  790. list_del(&rte->rte_list);
  791. iosapic_intr_info[vector].count--;
  792. iosapic_free_rte(rte);
  793. index = find_iosapic(gsi);
  794. iosapic_lists[index].rtes_inuse--;
  795. WARN_ON(iosapic_lists[index].rtes_inuse < 0);
  796. trigger = iosapic_intr_info[vector].trigger;
  797. polarity = iosapic_intr_info[vector].polarity;
  798. dest = iosapic_intr_info[vector].dest;
  799. printk(KERN_INFO
  800. "GSI %u (%s, %s) -> CPU %d (0x%04x)"
  801. " vector %d unregistered\n",
  802. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  803. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  804. cpu_logical_id(dest), dest, vector);
  805. if (list_empty(&iosapic_intr_info[vector].rtes)) {
  806. /* Sanity check */
  807. BUG_ON(iosapic_intr_info[vector].count);
  808. /* Clear the interrupt controller descriptor */
  809. idesc->chip = &no_irq_type;
  810. /* Clear the interrupt information */
  811. memset(&iosapic_intr_info[vector], 0,
  812. sizeof(struct iosapic_intr_info));
  813. iosapic_intr_info[vector].low32 |= IOSAPIC_MASK;
  814. INIT_LIST_HEAD(&iosapic_intr_info[vector].rtes);
  815. if (idesc->action) {
  816. printk(KERN_ERR
  817. "interrupt handlers still exist on"
  818. "IRQ %u\n", irq);
  819. WARN_ON(1);
  820. }
  821. /* Free the interrupt vector */
  822. free_irq_vector(vector);
  823. }
  824. }
  825. out:
  826. spin_unlock(&iosapic_lock);
  827. spin_unlock_irqrestore(&idesc->lock, flags);
  828. }
  829. /*
  830. * ACPI calls this when it finds an entry for a platform interrupt.
  831. */
  832. int __init
  833. iosapic_register_platform_intr (u32 int_type, unsigned int gsi,
  834. int iosapic_vector, u16 eid, u16 id,
  835. unsigned long polarity, unsigned long trigger)
  836. {
  837. static const char * const name[] = {"unknown", "PMI", "INIT", "CPEI"};
  838. unsigned char delivery;
  839. int vector, mask = 0;
  840. unsigned int dest = ((id << 8) | eid) & 0xffff;
  841. switch (int_type) {
  842. case ACPI_INTERRUPT_PMI:
  843. vector = iosapic_vector;
  844. /*
  845. * since PMI vector is alloc'd by FW(ACPI) not by kernel,
  846. * we need to make sure the vector is available
  847. */
  848. iosapic_reassign_vector(vector);
  849. delivery = IOSAPIC_PMI;
  850. break;
  851. case ACPI_INTERRUPT_INIT:
  852. vector = assign_irq_vector(AUTO_ASSIGN);
  853. if (vector < 0)
  854. panic("%s: out of interrupt vectors!\n", __FUNCTION__);
  855. delivery = IOSAPIC_INIT;
  856. break;
  857. case ACPI_INTERRUPT_CPEI:
  858. vector = IA64_CPE_VECTOR;
  859. delivery = IOSAPIC_LOWEST_PRIORITY;
  860. mask = 1;
  861. break;
  862. default:
  863. printk(KERN_ERR "%s: invalid int type 0x%x\n", __FUNCTION__,
  864. int_type);
  865. return -1;
  866. }
  867. register_intr(gsi, vector, delivery, polarity, trigger);
  868. printk(KERN_INFO
  869. "PLATFORM int %s (0x%x): GSI %u (%s, %s) -> CPU %d (0x%04x)"
  870. " vector %d\n",
  871. int_type < ARRAY_SIZE(name) ? name[int_type] : "unknown",
  872. int_type, gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  873. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  874. cpu_logical_id(dest), dest, vector);
  875. set_rte(gsi, vector, dest, mask);
  876. return vector;
  877. }
  878. /*
  879. * ACPI calls this when it finds an entry for a legacy ISA IRQ override.
  880. */
  881. void __init
  882. iosapic_override_isa_irq (unsigned int isa_irq, unsigned int gsi,
  883. unsigned long polarity,
  884. unsigned long trigger)
  885. {
  886. int vector;
  887. unsigned int dest = cpu_physical_id(smp_processor_id());
  888. vector = isa_irq_to_vector(isa_irq);
  889. register_intr(gsi, vector, IOSAPIC_LOWEST_PRIORITY, polarity, trigger);
  890. DBG("ISA: IRQ %u -> GSI %u (%s,%s) -> CPU %d (0x%04x) vector %d\n",
  891. isa_irq, gsi, trigger == IOSAPIC_EDGE ? "edge" : "level",
  892. polarity == IOSAPIC_POL_HIGH ? "high" : "low",
  893. cpu_logical_id(dest), dest, vector);
  894. set_rte(gsi, vector, dest, 1);
  895. }
  896. void __init
  897. iosapic_system_init (int system_pcat_compat)
  898. {
  899. int vector;
  900. for (vector = 0; vector < IA64_NUM_VECTORS; ++vector) {
  901. iosapic_intr_info[vector].low32 = IOSAPIC_MASK;
  902. /* mark as unused */
  903. INIT_LIST_HEAD(&iosapic_intr_info[vector].rtes);
  904. }
  905. pcat_compat = system_pcat_compat;
  906. if (pcat_compat) {
  907. /*
  908. * Disable the compatibility mode interrupts (8259 style),
  909. * needs IN/OUT support enabled.
  910. */
  911. printk(KERN_INFO
  912. "%s: Disabling PC-AT compatible 8259 interrupts\n",
  913. __FUNCTION__);
  914. outb(0xff, 0xA1);
  915. outb(0xff, 0x21);
  916. }
  917. }
  918. static inline int
  919. iosapic_alloc (void)
  920. {
  921. int index;
  922. for (index = 0; index < NR_IOSAPICS; index++)
  923. if (!iosapic_lists[index].addr)
  924. return index;
  925. printk(KERN_WARNING "%s: failed to allocate iosapic\n", __FUNCTION__);
  926. return -1;
  927. }
  928. static inline void
  929. iosapic_free (int index)
  930. {
  931. memset(&iosapic_lists[index], 0, sizeof(iosapic_lists[0]));
  932. }
  933. static inline int
  934. iosapic_check_gsi_range (unsigned int gsi_base, unsigned int ver)
  935. {
  936. int index;
  937. unsigned int gsi_end, base, end;
  938. /* check gsi range */
  939. gsi_end = gsi_base + ((ver >> 16) & 0xff);
  940. for (index = 0; index < NR_IOSAPICS; index++) {
  941. if (!iosapic_lists[index].addr)
  942. continue;
  943. base = iosapic_lists[index].gsi_base;
  944. end = base + iosapic_lists[index].num_rte - 1;
  945. if (gsi_end < base || end < gsi_base)
  946. continue; /* OK */
  947. return -EBUSY;
  948. }
  949. return 0;
  950. }
  951. int __devinit
  952. iosapic_init (unsigned long phys_addr, unsigned int gsi_base)
  953. {
  954. int num_rte, err, index;
  955. unsigned int isa_irq, ver;
  956. char __iomem *addr;
  957. unsigned long flags;
  958. spin_lock_irqsave(&iosapic_lock, flags);
  959. {
  960. addr = ioremap(phys_addr, 0);
  961. ver = iosapic_version(addr);
  962. if ((err = iosapic_check_gsi_range(gsi_base, ver))) {
  963. iounmap(addr);
  964. spin_unlock_irqrestore(&iosapic_lock, flags);
  965. return err;
  966. }
  967. /*
  968. * The MAX_REDIR register holds the highest input pin
  969. * number (starting from 0).
  970. * We add 1 so that we can use it for number of pins (= RTEs)
  971. */
  972. num_rte = ((ver >> 16) & 0xff) + 1;
  973. index = iosapic_alloc();
  974. iosapic_lists[index].addr = addr;
  975. iosapic_lists[index].gsi_base = gsi_base;
  976. iosapic_lists[index].num_rte = num_rte;
  977. #ifdef CONFIG_NUMA
  978. iosapic_lists[index].node = MAX_NUMNODES;
  979. #endif
  980. }
  981. spin_unlock_irqrestore(&iosapic_lock, flags);
  982. if ((gsi_base == 0) && pcat_compat) {
  983. /*
  984. * Map the legacy ISA devices into the IOSAPIC data. Some of
  985. * these may get reprogrammed later on with data from the ACPI
  986. * Interrupt Source Override table.
  987. */
  988. for (isa_irq = 0; isa_irq < 16; ++isa_irq)
  989. iosapic_override_isa_irq(isa_irq, isa_irq,
  990. IOSAPIC_POL_HIGH,
  991. IOSAPIC_EDGE);
  992. }
  993. return 0;
  994. }
  995. #ifdef CONFIG_HOTPLUG
  996. int
  997. iosapic_remove (unsigned int gsi_base)
  998. {
  999. int index, err = 0;
  1000. unsigned long flags;
  1001. spin_lock_irqsave(&iosapic_lock, flags);
  1002. {
  1003. index = find_iosapic(gsi_base);
  1004. if (index < 0) {
  1005. printk(KERN_WARNING "%s: No IOSAPIC for GSI base %u\n",
  1006. __FUNCTION__, gsi_base);
  1007. goto out;
  1008. }
  1009. if (iosapic_lists[index].rtes_inuse) {
  1010. err = -EBUSY;
  1011. printk(KERN_WARNING
  1012. "%s: IOSAPIC for GSI base %u is busy\n",
  1013. __FUNCTION__, gsi_base);
  1014. goto out;
  1015. }
  1016. iounmap(iosapic_lists[index].addr);
  1017. iosapic_free(index);
  1018. }
  1019. out:
  1020. spin_unlock_irqrestore(&iosapic_lock, flags);
  1021. return err;
  1022. }
  1023. #endif /* CONFIG_HOTPLUG */
  1024. #ifdef CONFIG_NUMA
  1025. void __devinit
  1026. map_iosapic_to_node(unsigned int gsi_base, int node)
  1027. {
  1028. int index;
  1029. index = find_iosapic(gsi_base);
  1030. if (index < 0) {
  1031. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  1032. __FUNCTION__, gsi_base);
  1033. return;
  1034. }
  1035. iosapic_lists[index].node = node;
  1036. return;
  1037. }
  1038. #endif
  1039. static int __init iosapic_enable_kmalloc (void)
  1040. {
  1041. iosapic_kmalloc_ok = 1;
  1042. return 0;
  1043. }
  1044. core_initcall (iosapic_enable_kmalloc);