iosapic.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /*
  2. * I/O SAPIC support.
  3. *
  4. * Copyright (C) 1999 Intel Corp.
  5. * Copyright (C) 1999 Asit Mallick <asit.k.mallick@intel.com>
  6. * Copyright (C) 2000-2002 J.I. Lee <jung-ik.lee@intel.com>
  7. * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co.
  8. * David Mosberger-Tang <davidm@hpl.hp.com>
  9. * Copyright (C) 1999 VA Linux Systems
  10. * Copyright (C) 1999,2000 Walt Drummond <drummond@valinux.com>
  11. *
  12. * 00/04/19 D. Mosberger Rewritten to mirror more closely the x86 I/O
  13. * APIC code. In particular, we now have separate
  14. * handlers for edge and level triggered
  15. * interrupts.
  16. * 00/10/27 Asit Mallick, Goutham Rao <goutham.rao@intel.com> IRQ vector
  17. * allocation PCI to vector mapping, shared PCI
  18. * interrupts.
  19. * 00/10/27 D. Mosberger Document things a bit more to make them more
  20. * understandable. Clean up much of the old
  21. * IOSAPIC cruft.
  22. * 01/07/27 J.I. Lee PCI irq routing, Platform/Legacy interrupts
  23. * and fixes for ACPI S5(SoftOff) support.
  24. * 02/01/23 J.I. Lee iosapic pgm fixes for PCI irq routing from _PRT
  25. * 02/01/07 E. Focht <efocht@ess.nec.de> Redirectable interrupt
  26. * vectors in iosapic_set_affinity(),
  27. * initializations for /proc/irq/#/smp_affinity
  28. * 02/04/02 P. Diefenbaugh Cleaned up ACPI PCI IRQ routing.
  29. * 02/04/18 J.I. Lee bug fix in iosapic_init_pci_irq
  30. * 02/04/30 J.I. Lee bug fix in find_iosapic to fix ACPI PCI IRQ to
  31. * IOSAPIC mapping error
  32. * 02/07/29 T. Kochi Allocate interrupt vectors dynamically
  33. * 02/08/04 T. Kochi Cleaned up terminology (irq, global system
  34. * interrupt, vector, etc.)
  35. * 02/09/20 D. Mosberger Simplified by taking advantage of ACPI's
  36. * pci_irq code.
  37. * 03/02/19 B. Helgaas Make pcat_compat system-wide, not per-IOSAPIC.
  38. * Remove iosapic_address & gsi_base from
  39. * external interfaces. Rationalize
  40. * __init/__devinit attributes.
  41. * 04/12/04 Ashok Raj <ashok.raj@intel.com> Intel Corporation 2004
  42. * Updated to work with irq migration necessary
  43. * for CPU Hotplug
  44. */
  45. /*
  46. * Here is what the interrupt logic between a PCI device and the kernel looks
  47. * like:
  48. *
  49. * (1) A PCI device raises one of the four interrupt pins (INTA, INTB, INTC,
  50. * INTD). The device is uniquely identified by its bus-, and slot-number
  51. * (the function number does not matter here because all functions share
  52. * the same interrupt lines).
  53. *
  54. * (2) The motherboard routes the interrupt line to a pin on a IOSAPIC
  55. * controller. Multiple interrupt lines may have to share the same
  56. * IOSAPIC pin (if they're level triggered and use the same polarity).
  57. * Each interrupt line has a unique Global System Interrupt (GSI) number
  58. * which can be calculated as the sum of the controller's base GSI number
  59. * and the IOSAPIC pin number to which the line connects.
  60. *
  61. * (3) The IOSAPIC uses an internal routing table entries (RTEs) to map the
  62. * IOSAPIC pin into the IA-64 interrupt vector. This interrupt vector is then
  63. * sent to the CPU.
  64. *
  65. * (4) The kernel recognizes an interrupt as an IRQ. The IRQ interface is
  66. * used as architecture-independent interrupt handling mechanism in Linux.
  67. * As an IRQ is a number, we have to have
  68. * IA-64 interrupt vector number <-> IRQ number mapping. On smaller
  69. * systems, we use one-to-one mapping between IA-64 vector and IRQ. A
  70. * platform can implement platform_irq_to_vector(irq) and
  71. * platform_local_vector_to_irq(vector) APIs to differentiate the mapping.
  72. * Please see also include/asm-ia64/hw_irq.h for those APIs.
  73. *
  74. * To sum up, there are three levels of mappings involved:
  75. *
  76. * PCI pin -> global system interrupt (GSI) -> IA-64 vector <-> IRQ
  77. *
  78. * Note: The term "IRQ" is loosely used everywhere in Linux kernel to
  79. * describeinterrupts. Now we use "IRQ" only for Linux IRQ's. ISA IRQ
  80. * (isa_irq) is the only exception in this source code.
  81. */
  82. #include <linux/acpi.h>
  83. #include <linux/init.h>
  84. #include <linux/irq.h>
  85. #include <linux/kernel.h>
  86. #include <linux/list.h>
  87. #include <linux/pci.h>
  88. #include <linux/smp.h>
  89. #include <linux/string.h>
  90. #include <linux/bootmem.h>
  91. #include <asm/delay.h>
  92. #include <asm/hw_irq.h>
  93. #include <asm/io.h>
  94. #include <asm/iosapic.h>
  95. #include <asm/machvec.h>
  96. #include <asm/processor.h>
  97. #include <asm/ptrace.h>
  98. #include <asm/system.h>
  99. #undef DEBUG_INTERRUPT_ROUTING
  100. #ifdef DEBUG_INTERRUPT_ROUTING
  101. #define DBG(fmt...) printk(fmt)
  102. #else
  103. #define DBG(fmt...)
  104. #endif
  105. #define NR_PREALLOCATE_RTE_ENTRIES \
  106. (PAGE_SIZE / sizeof(struct iosapic_rte_info))
  107. #define RTE_PREALLOCATED (1)
  108. static DEFINE_SPINLOCK(iosapic_lock);
  109. /*
  110. * These tables map IA-64 vectors to the IOSAPIC pin that generates this
  111. * vector.
  112. */
  113. #define NO_REF_RTE 0
  114. static struct iosapic {
  115. char __iomem *addr; /* base address of IOSAPIC */
  116. unsigned int gsi_base; /* GSI base */
  117. unsigned short num_rte; /* # of RTEs on this IOSAPIC */
  118. int rtes_inuse; /* # of RTEs in use on this IOSAPIC */
  119. #ifdef CONFIG_NUMA
  120. unsigned short node; /* numa node association via pxm */
  121. #endif
  122. spinlock_t lock; /* lock for indirect reg access */
  123. } iosapic_lists[NR_IOSAPICS];
  124. struct iosapic_rte_info {
  125. struct list_head rte_list; /* RTEs sharing the same vector */
  126. char rte_index; /* IOSAPIC RTE index */
  127. int refcnt; /* reference counter */
  128. unsigned int flags; /* flags */
  129. struct iosapic *iosapic;
  130. } ____cacheline_aligned;
  131. static struct iosapic_intr_info {
  132. struct list_head rtes; /* RTEs using this vector (empty =>
  133. * not an IOSAPIC interrupt) */
  134. int count; /* # of RTEs that shares this vector */
  135. u32 low32; /* current value of low word of
  136. * Redirection table entry */
  137. unsigned int dest; /* destination CPU physical ID */
  138. unsigned char dmode : 3; /* delivery mode (see iosapic.h) */
  139. unsigned char polarity: 1; /* interrupt polarity
  140. * (see iosapic.h) */
  141. unsigned char trigger : 1; /* trigger mode (see iosapic.h) */
  142. } iosapic_intr_info[NR_IRQS];
  143. static unsigned char pcat_compat __devinitdata; /* 8259 compatibility flag */
  144. static int iosapic_kmalloc_ok;
  145. static LIST_HEAD(free_rte_list);
  146. static inline void
  147. iosapic_write(struct iosapic *iosapic, unsigned int reg, u32 val)
  148. {
  149. unsigned long flags;
  150. spin_lock_irqsave(&iosapic->lock, flags);
  151. __iosapic_write(iosapic->addr, reg, val);
  152. spin_unlock_irqrestore(&iosapic->lock, flags);
  153. }
  154. /*
  155. * Find an IOSAPIC associated with a GSI
  156. */
  157. static inline int
  158. find_iosapic (unsigned int gsi)
  159. {
  160. int i;
  161. for (i = 0; i < NR_IOSAPICS; i++) {
  162. if ((unsigned) (gsi - iosapic_lists[i].gsi_base) <
  163. iosapic_lists[i].num_rte)
  164. return i;
  165. }
  166. return -1;
  167. }
  168. static inline int __gsi_to_irq(unsigned int gsi)
  169. {
  170. int irq;
  171. struct iosapic_intr_info *info;
  172. struct iosapic_rte_info *rte;
  173. for (irq = 0; irq < NR_IRQS; irq++) {
  174. info = &iosapic_intr_info[irq];
  175. list_for_each_entry(rte, &info->rtes, rte_list)
  176. if (rte->iosapic->gsi_base + rte->rte_index == gsi)
  177. return irq;
  178. }
  179. return -1;
  180. }
  181. /*
  182. * Translate GSI number to the corresponding IA-64 interrupt vector. If no
  183. * entry exists, return -1.
  184. */
  185. inline int
  186. gsi_to_vector (unsigned int gsi)
  187. {
  188. int irq = __gsi_to_irq(gsi);
  189. if (check_irq_used(irq) < 0)
  190. return -1;
  191. return irq_to_vector(irq);
  192. }
  193. int
  194. gsi_to_irq (unsigned int gsi)
  195. {
  196. unsigned long flags;
  197. int irq;
  198. spin_lock_irqsave(&iosapic_lock, flags);
  199. irq = __gsi_to_irq(gsi);
  200. spin_unlock_irqrestore(&iosapic_lock, flags);
  201. return irq;
  202. }
  203. static struct iosapic_rte_info *find_rte(unsigned int irq, unsigned int gsi)
  204. {
  205. struct iosapic_rte_info *rte;
  206. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list)
  207. if (rte->iosapic->gsi_base + rte->rte_index == gsi)
  208. return rte;
  209. return NULL;
  210. }
  211. static void
  212. set_rte (unsigned int gsi, unsigned int irq, unsigned int dest, int mask)
  213. {
  214. unsigned long pol, trigger, dmode;
  215. u32 low32, high32;
  216. int rte_index;
  217. char redir;
  218. struct iosapic_rte_info *rte;
  219. ia64_vector vector = irq_to_vector(irq);
  220. DBG(KERN_DEBUG"IOSAPIC: routing vector %d to 0x%x\n", vector, dest);
  221. rte = find_rte(irq, gsi);
  222. if (!rte)
  223. return; /* not an IOSAPIC interrupt */
  224. rte_index = rte->rte_index;
  225. pol = iosapic_intr_info[irq].polarity;
  226. trigger = iosapic_intr_info[irq].trigger;
  227. dmode = iosapic_intr_info[irq].dmode;
  228. redir = (dmode == IOSAPIC_LOWEST_PRIORITY) ? 1 : 0;
  229. #ifdef CONFIG_SMP
  230. set_irq_affinity_info(irq, (int)(dest & 0xffff), redir);
  231. #endif
  232. low32 = ((pol << IOSAPIC_POLARITY_SHIFT) |
  233. (trigger << IOSAPIC_TRIGGER_SHIFT) |
  234. (dmode << IOSAPIC_DELIVERY_SHIFT) |
  235. ((mask ? 1 : 0) << IOSAPIC_MASK_SHIFT) |
  236. vector);
  237. /* dest contains both id and eid */
  238. high32 = (dest << IOSAPIC_DEST_SHIFT);
  239. iosapic_write(rte->iosapic, IOSAPIC_RTE_HIGH(rte_index), high32);
  240. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  241. iosapic_intr_info[irq].low32 = low32;
  242. iosapic_intr_info[irq].dest = dest;
  243. }
  244. static void
  245. nop (unsigned int irq)
  246. {
  247. /* do nothing... */
  248. }
  249. #ifdef CONFIG_KEXEC
  250. void
  251. kexec_disable_iosapic(void)
  252. {
  253. struct iosapic_intr_info *info;
  254. struct iosapic_rte_info *rte;
  255. ia64_vector vec;
  256. int irq;
  257. for (irq = 0; irq < NR_IRQS; irq++) {
  258. info = &iosapic_intr_info[irq];
  259. vec = irq_to_vector(irq);
  260. list_for_each_entry(rte, &info->rtes,
  261. rte_list) {
  262. iosapic_write(rte->iosapic,
  263. IOSAPIC_RTE_LOW(rte->rte_index),
  264. IOSAPIC_MASK|vec);
  265. iosapic_eoi(rte->iosapic->addr, vec);
  266. }
  267. }
  268. }
  269. #endif
  270. static void
  271. mask_irq (unsigned int irq)
  272. {
  273. u32 low32;
  274. int rte_index;
  275. struct iosapic_rte_info *rte;
  276. if (list_empty(&iosapic_intr_info[irq].rtes))
  277. return; /* not an IOSAPIC interrupt! */
  278. /* set only the mask bit */
  279. low32 = iosapic_intr_info[irq].low32 |= IOSAPIC_MASK;
  280. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  281. rte_index = rte->rte_index;
  282. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  283. }
  284. }
  285. static void
  286. unmask_irq (unsigned int irq)
  287. {
  288. u32 low32;
  289. int rte_index;
  290. struct iosapic_rte_info *rte;
  291. if (list_empty(&iosapic_intr_info[irq].rtes))
  292. return; /* not an IOSAPIC interrupt! */
  293. low32 = iosapic_intr_info[irq].low32 &= ~IOSAPIC_MASK;
  294. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  295. rte_index = rte->rte_index;
  296. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  297. }
  298. }
  299. static void
  300. iosapic_set_affinity (unsigned int irq, cpumask_t mask)
  301. {
  302. #ifdef CONFIG_SMP
  303. u32 high32, low32;
  304. int dest, rte_index;
  305. int redir = (irq & IA64_IRQ_REDIRECTED) ? 1 : 0;
  306. struct iosapic_rte_info *rte;
  307. struct iosapic *iosapic;
  308. irq &= (~IA64_IRQ_REDIRECTED);
  309. cpus_and(mask, mask, cpu_online_map);
  310. if (cpus_empty(mask))
  311. return;
  312. if (reassign_irq_vector(irq, first_cpu(mask)))
  313. return;
  314. dest = cpu_physical_id(first_cpu(mask));
  315. if (list_empty(&iosapic_intr_info[irq].rtes))
  316. return; /* not an IOSAPIC interrupt */
  317. set_irq_affinity_info(irq, dest, redir);
  318. /* dest contains both id and eid */
  319. high32 = dest << IOSAPIC_DEST_SHIFT;
  320. low32 = iosapic_intr_info[irq].low32 & ~(7 << IOSAPIC_DELIVERY_SHIFT);
  321. if (redir)
  322. /* change delivery mode to lowest priority */
  323. low32 |= (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
  324. else
  325. /* change delivery mode to fixed */
  326. low32 |= (IOSAPIC_FIXED << IOSAPIC_DELIVERY_SHIFT);
  327. low32 &= IOSAPIC_VECTOR_MASK;
  328. low32 |= irq_to_vector(irq);
  329. iosapic_intr_info[irq].low32 = low32;
  330. iosapic_intr_info[irq].dest = dest;
  331. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  332. iosapic = rte->iosapic;
  333. rte_index = rte->rte_index;
  334. iosapic_write(iosapic, IOSAPIC_RTE_HIGH(rte_index), high32);
  335. iosapic_write(iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  336. }
  337. #endif
  338. }
  339. /*
  340. * Handlers for level-triggered interrupts.
  341. */
  342. static unsigned int
  343. iosapic_startup_level_irq (unsigned int irq)
  344. {
  345. unmask_irq(irq);
  346. return 0;
  347. }
  348. static void
  349. iosapic_end_level_irq (unsigned int irq)
  350. {
  351. ia64_vector vec = irq_to_vector(irq);
  352. struct iosapic_rte_info *rte;
  353. int do_unmask_irq = 0;
  354. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  355. do_unmask_irq = 1;
  356. mask_irq(irq);
  357. }
  358. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list)
  359. iosapic_eoi(rte->iosapic->addr, vec);
  360. if (unlikely(do_unmask_irq)) {
  361. move_masked_irq(irq);
  362. unmask_irq(irq);
  363. }
  364. }
  365. #define iosapic_shutdown_level_irq mask_irq
  366. #define iosapic_enable_level_irq unmask_irq
  367. #define iosapic_disable_level_irq mask_irq
  368. #define iosapic_ack_level_irq nop
  369. struct irq_chip irq_type_iosapic_level = {
  370. .name = "IO-SAPIC-level",
  371. .startup = iosapic_startup_level_irq,
  372. .shutdown = iosapic_shutdown_level_irq,
  373. .enable = iosapic_enable_level_irq,
  374. .disable = iosapic_disable_level_irq,
  375. .ack = iosapic_ack_level_irq,
  376. .end = iosapic_end_level_irq,
  377. .mask = mask_irq,
  378. .unmask = unmask_irq,
  379. .set_affinity = iosapic_set_affinity
  380. };
  381. /*
  382. * Handlers for edge-triggered interrupts.
  383. */
  384. static unsigned int
  385. iosapic_startup_edge_irq (unsigned int irq)
  386. {
  387. unmask_irq(irq);
  388. /*
  389. * IOSAPIC simply drops interrupts pended while the
  390. * corresponding pin was masked, so we can't know if an
  391. * interrupt is pending already. Let's hope not...
  392. */
  393. return 0;
  394. }
  395. static void
  396. iosapic_ack_edge_irq (unsigned int irq)
  397. {
  398. irq_desc_t *idesc = irq_desc + irq;
  399. move_native_irq(irq);
  400. /*
  401. * Once we have recorded IRQ_PENDING already, we can mask the
  402. * interrupt for real. This prevents IRQ storms from unhandled
  403. * devices.
  404. */
  405. if ((idesc->status & (IRQ_PENDING|IRQ_DISABLED)) ==
  406. (IRQ_PENDING|IRQ_DISABLED))
  407. mask_irq(irq);
  408. }
  409. #define iosapic_enable_edge_irq unmask_irq
  410. #define iosapic_disable_edge_irq nop
  411. #define iosapic_end_edge_irq nop
  412. struct irq_chip irq_type_iosapic_edge = {
  413. .name = "IO-SAPIC-edge",
  414. .startup = iosapic_startup_edge_irq,
  415. .shutdown = iosapic_disable_edge_irq,
  416. .enable = iosapic_enable_edge_irq,
  417. .disable = iosapic_disable_edge_irq,
  418. .ack = iosapic_ack_edge_irq,
  419. .end = iosapic_end_edge_irq,
  420. .mask = mask_irq,
  421. .unmask = unmask_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_irq(unsigned long trigger, unsigned long pol)
  439. {
  440. int i, irq = -ENOSPC, min_count = -1;
  441. struct iosapic_intr_info *info;
  442. /*
  443. * shared vectors for edge-triggered interrupts are not
  444. * supported yet
  445. */
  446. if (trigger == IOSAPIC_EDGE)
  447. return -EINVAL;
  448. for (i = 0; i <= NR_IRQS; i++) {
  449. info = &iosapic_intr_info[i];
  450. if (info->trigger == trigger && info->polarity == pol &&
  451. (info->dmode == IOSAPIC_FIXED ||
  452. info->dmode == IOSAPIC_LOWEST_PRIORITY) &&
  453. can_request_irq(i, IRQF_SHARED)) {
  454. if (min_count == -1 || info->count < min_count) {
  455. irq = i;
  456. min_count = info->count;
  457. }
  458. }
  459. }
  460. return irq;
  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 irq)
  468. {
  469. int new_irq;
  470. if (!list_empty(&iosapic_intr_info[irq].rtes)) {
  471. new_irq = create_irq();
  472. if (new_irq < 0)
  473. panic("%s: out of interrupt vectors!\n", __FUNCTION__);
  474. printk(KERN_INFO "Reassigning vector %d to %d\n",
  475. irq_to_vector(irq), irq_to_vector(new_irq));
  476. memcpy(&iosapic_intr_info[new_irq], &iosapic_intr_info[irq],
  477. sizeof(struct iosapic_intr_info));
  478. INIT_LIST_HEAD(&iosapic_intr_info[new_irq].rtes);
  479. list_move(iosapic_intr_info[irq].rtes.next,
  480. &iosapic_intr_info[new_irq].rtes);
  481. memset(&iosapic_intr_info[irq], 0,
  482. sizeof(struct iosapic_intr_info));
  483. iosapic_intr_info[irq].low32 = IOSAPIC_MASK;
  484. INIT_LIST_HEAD(&iosapic_intr_info[irq].rtes);
  485. }
  486. }
  487. static struct iosapic_rte_info * __init_refok 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 inline int irq_is_shared (int irq)
  516. {
  517. return (iosapic_intr_info[irq].count > 1);
  518. }
  519. static int
  520. register_intr (unsigned int gsi, int irq, unsigned char delivery,
  521. unsigned long polarity, unsigned long trigger)
  522. {
  523. irq_desc_t *idesc;
  524. struct hw_interrupt_type *irq_type;
  525. int index;
  526. struct iosapic_rte_info *rte;
  527. index = find_iosapic(gsi);
  528. if (index < 0) {
  529. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  530. __FUNCTION__, gsi);
  531. return -ENODEV;
  532. }
  533. rte = find_rte(irq, gsi);
  534. if (!rte) {
  535. rte = iosapic_alloc_rte();
  536. if (!rte) {
  537. printk(KERN_WARNING "%s: cannot allocate memory\n",
  538. __FUNCTION__);
  539. return -ENOMEM;
  540. }
  541. rte->iosapic = &iosapic_lists[index];
  542. rte->rte_index = gsi - rte->iosapic->gsi_base;
  543. rte->refcnt++;
  544. list_add_tail(&rte->rte_list, &iosapic_intr_info[irq].rtes);
  545. iosapic_intr_info[irq].count++;
  546. iosapic_lists[index].rtes_inuse++;
  547. }
  548. else if (rte->refcnt == NO_REF_RTE) {
  549. struct iosapic_intr_info *info = &iosapic_intr_info[irq];
  550. if (info->count > 0 &&
  551. (info->trigger != trigger || info->polarity != polarity)){
  552. printk (KERN_WARNING
  553. "%s: cannot override the interrupt\n",
  554. __FUNCTION__);
  555. return -EINVAL;
  556. }
  557. rte->refcnt++;
  558. iosapic_intr_info[irq].count++;
  559. iosapic_lists[index].rtes_inuse++;
  560. }
  561. iosapic_intr_info[irq].polarity = polarity;
  562. iosapic_intr_info[irq].dmode = delivery;
  563. iosapic_intr_info[irq].trigger = trigger;
  564. if (trigger == IOSAPIC_EDGE)
  565. irq_type = &irq_type_iosapic_edge;
  566. else
  567. irq_type = &irq_type_iosapic_level;
  568. idesc = irq_desc + irq;
  569. if (idesc->chip != irq_type) {
  570. if (idesc->chip != &no_irq_type)
  571. printk(KERN_WARNING
  572. "%s: changing vector %d from %s to %s\n",
  573. __FUNCTION__, irq_to_vector(irq),
  574. idesc->chip->name, irq_type->name);
  575. idesc->chip = irq_type;
  576. }
  577. return 0;
  578. }
  579. static unsigned int
  580. get_target_cpu (unsigned int gsi, int irq)
  581. {
  582. #ifdef CONFIG_SMP
  583. static int cpu = -1;
  584. extern int cpe_vector;
  585. cpumask_t domain = irq_to_domain(irq);
  586. /*
  587. * In case of vector shared by multiple RTEs, all RTEs that
  588. * share the vector need to use the same destination CPU.
  589. */
  590. if (!list_empty(&iosapic_intr_info[irq].rtes))
  591. return iosapic_intr_info[irq].dest;
  592. /*
  593. * If the platform supports redirection via XTP, let it
  594. * distribute interrupts.
  595. */
  596. if (smp_int_redirect & SMP_IRQ_REDIRECTION)
  597. return cpu_physical_id(smp_processor_id());
  598. /*
  599. * Some interrupts (ACPI SCI, for instance) are registered
  600. * before the BSP is marked as online.
  601. */
  602. if (!cpu_online(smp_processor_id()))
  603. return cpu_physical_id(smp_processor_id());
  604. #ifdef CONFIG_ACPI
  605. if (cpe_vector > 0 && irq_to_vector(irq) == IA64_CPEP_VECTOR)
  606. return get_cpei_target_cpu();
  607. #endif
  608. #ifdef CONFIG_NUMA
  609. {
  610. int num_cpus, cpu_index, iosapic_index, numa_cpu, i = 0;
  611. cpumask_t cpu_mask;
  612. iosapic_index = find_iosapic(gsi);
  613. if (iosapic_index < 0 ||
  614. iosapic_lists[iosapic_index].node == MAX_NUMNODES)
  615. goto skip_numa_setup;
  616. cpu_mask = node_to_cpumask(iosapic_lists[iosapic_index].node);
  617. cpus_and(cpu_mask, cpu_mask, domain);
  618. for_each_cpu_mask(numa_cpu, cpu_mask) {
  619. if (!cpu_online(numa_cpu))
  620. cpu_clear(numa_cpu, cpu_mask);
  621. }
  622. num_cpus = cpus_weight(cpu_mask);
  623. if (!num_cpus)
  624. goto skip_numa_setup;
  625. /* Use irq assignment to distribute across cpus in node */
  626. cpu_index = irq % num_cpus;
  627. for (numa_cpu = first_cpu(cpu_mask) ; i < cpu_index ; i++)
  628. numa_cpu = next_cpu(numa_cpu, cpu_mask);
  629. if (numa_cpu != NR_CPUS)
  630. return cpu_physical_id(numa_cpu);
  631. }
  632. skip_numa_setup:
  633. #endif
  634. /*
  635. * Otherwise, round-robin interrupt vectors across all the
  636. * processors. (It'd be nice if we could be smarter in the
  637. * case of NUMA.)
  638. */
  639. do {
  640. if (++cpu >= NR_CPUS)
  641. cpu = 0;
  642. } while (!cpu_online(cpu) || !cpu_isset(cpu, domain));
  643. return cpu_physical_id(cpu);
  644. #else /* CONFIG_SMP */
  645. return cpu_physical_id(smp_processor_id());
  646. #endif
  647. }
  648. /*
  649. * ACPI can describe IOSAPIC interrupts via static tables and namespace
  650. * methods. This provides an interface to register those interrupts and
  651. * program the IOSAPIC RTE.
  652. */
  653. int
  654. iosapic_register_intr (unsigned int gsi,
  655. unsigned long polarity, unsigned long trigger)
  656. {
  657. int irq, mask = 1, err;
  658. unsigned int dest;
  659. unsigned long flags;
  660. struct iosapic_rte_info *rte;
  661. u32 low32;
  662. /*
  663. * If this GSI has already been registered (i.e., it's a
  664. * shared interrupt, or we lost a race to register it),
  665. * don't touch the RTE.
  666. */
  667. spin_lock_irqsave(&iosapic_lock, flags);
  668. irq = __gsi_to_irq(gsi);
  669. if (irq > 0) {
  670. rte = find_rte(irq, gsi);
  671. if(iosapic_intr_info[irq].count == 0) {
  672. assign_irq_vector(irq);
  673. dynamic_irq_init(irq);
  674. } else if (rte->refcnt != NO_REF_RTE) {
  675. rte->refcnt++;
  676. goto unlock_iosapic_lock;
  677. }
  678. } else
  679. irq = create_irq();
  680. /* If vector is running out, we try to find a sharable vector */
  681. if (irq < 0) {
  682. irq = iosapic_find_sharable_irq(trigger, polarity);
  683. if (irq < 0)
  684. goto unlock_iosapic_lock;
  685. }
  686. spin_lock(&irq_desc[irq].lock);
  687. dest = get_target_cpu(gsi, irq);
  688. err = register_intr(gsi, irq, IOSAPIC_LOWEST_PRIORITY,
  689. polarity, trigger);
  690. if (err < 0) {
  691. irq = err;
  692. goto unlock_all;
  693. }
  694. /*
  695. * If the vector is shared and already unmasked for other
  696. * interrupt sources, don't mask it.
  697. */
  698. low32 = iosapic_intr_info[irq].low32;
  699. if (irq_is_shared(irq) && !(low32 & IOSAPIC_MASK))
  700. mask = 0;
  701. set_rte(gsi, irq, dest, mask);
  702. printk(KERN_INFO "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d\n",
  703. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  704. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  705. cpu_logical_id(dest), dest, irq_to_vector(irq));
  706. unlock_all:
  707. spin_unlock(&irq_desc[irq].lock);
  708. unlock_iosapic_lock:
  709. spin_unlock_irqrestore(&iosapic_lock, flags);
  710. return irq;
  711. }
  712. void
  713. iosapic_unregister_intr (unsigned int gsi)
  714. {
  715. unsigned long flags;
  716. int irq, index;
  717. irq_desc_t *idesc;
  718. u32 low32;
  719. unsigned long trigger, polarity;
  720. unsigned int dest;
  721. struct iosapic_rte_info *rte;
  722. /*
  723. * If the irq associated with the gsi is not found,
  724. * iosapic_unregister_intr() is unbalanced. We need to check
  725. * this again after getting locks.
  726. */
  727. irq = gsi_to_irq(gsi);
  728. if (irq < 0) {
  729. printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n",
  730. gsi);
  731. WARN_ON(1);
  732. return;
  733. }
  734. spin_lock_irqsave(&iosapic_lock, flags);
  735. if ((rte = find_rte(irq, gsi)) == NULL) {
  736. printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n",
  737. gsi);
  738. WARN_ON(1);
  739. goto out;
  740. }
  741. if (--rte->refcnt > 0)
  742. goto out;
  743. idesc = irq_desc + irq;
  744. rte->refcnt = NO_REF_RTE;
  745. /* Mask the interrupt */
  746. low32 = iosapic_intr_info[irq].low32 | IOSAPIC_MASK;
  747. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte->rte_index), low32);
  748. iosapic_intr_info[irq].count--;
  749. index = find_iosapic(gsi);
  750. iosapic_lists[index].rtes_inuse--;
  751. WARN_ON(iosapic_lists[index].rtes_inuse < 0);
  752. trigger = iosapic_intr_info[irq].trigger;
  753. polarity = iosapic_intr_info[irq].polarity;
  754. dest = iosapic_intr_info[irq].dest;
  755. printk(KERN_INFO
  756. "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d unregistered\n",
  757. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  758. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  759. cpu_logical_id(dest), dest, irq_to_vector(irq));
  760. if (iosapic_intr_info[irq].count == 0) {
  761. #ifdef CONFIG_SMP
  762. /* Clear affinity */
  763. cpus_setall(idesc->affinity);
  764. #endif
  765. /* Clear the interrupt information */
  766. iosapic_intr_info[irq].dest = 0;
  767. iosapic_intr_info[irq].dmode = 0;
  768. iosapic_intr_info[irq].polarity = 0;
  769. iosapic_intr_info[irq].trigger = 0;
  770. iosapic_intr_info[irq].low32 |= IOSAPIC_MASK;
  771. /* Destroy and reserve IRQ */
  772. destroy_and_reserve_irq(irq);
  773. }
  774. out:
  775. spin_unlock_irqrestore(&iosapic_lock, flags);
  776. }
  777. /*
  778. * ACPI calls this when it finds an entry for a platform interrupt.
  779. */
  780. int __init
  781. iosapic_register_platform_intr (u32 int_type, unsigned int gsi,
  782. int iosapic_vector, u16 eid, u16 id,
  783. unsigned long polarity, unsigned long trigger)
  784. {
  785. static const char * const name[] = {"unknown", "PMI", "INIT", "CPEI"};
  786. unsigned char delivery;
  787. int irq, vector, mask = 0;
  788. unsigned int dest = ((id << 8) | eid) & 0xffff;
  789. switch (int_type) {
  790. case ACPI_INTERRUPT_PMI:
  791. irq = vector = iosapic_vector;
  792. bind_irq_vector(irq, vector, CPU_MASK_ALL);
  793. /*
  794. * since PMI vector is alloc'd by FW(ACPI) not by kernel,
  795. * we need to make sure the vector is available
  796. */
  797. iosapic_reassign_vector(irq);
  798. delivery = IOSAPIC_PMI;
  799. break;
  800. case ACPI_INTERRUPT_INIT:
  801. irq = create_irq();
  802. if (irq < 0)
  803. panic("%s: out of interrupt vectors!\n", __FUNCTION__);
  804. vector = irq_to_vector(irq);
  805. delivery = IOSAPIC_INIT;
  806. break;
  807. case ACPI_INTERRUPT_CPEI:
  808. irq = vector = IA64_CPE_VECTOR;
  809. BUG_ON(bind_irq_vector(irq, vector, CPU_MASK_ALL));
  810. delivery = IOSAPIC_LOWEST_PRIORITY;
  811. mask = 1;
  812. break;
  813. default:
  814. printk(KERN_ERR "%s: invalid int type 0x%x\n", __FUNCTION__,
  815. int_type);
  816. return -1;
  817. }
  818. register_intr(gsi, irq, delivery, polarity, trigger);
  819. printk(KERN_INFO
  820. "PLATFORM int %s (0x%x): GSI %u (%s, %s) -> CPU %d (0x%04x)"
  821. " vector %d\n",
  822. int_type < ARRAY_SIZE(name) ? name[int_type] : "unknown",
  823. int_type, gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  824. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  825. cpu_logical_id(dest), dest, vector);
  826. set_rte(gsi, irq, dest, mask);
  827. return vector;
  828. }
  829. /*
  830. * ACPI calls this when it finds an entry for a legacy ISA IRQ override.
  831. */
  832. void __devinit
  833. iosapic_override_isa_irq (unsigned int isa_irq, unsigned int gsi,
  834. unsigned long polarity,
  835. unsigned long trigger)
  836. {
  837. int vector, irq;
  838. unsigned int dest = cpu_physical_id(smp_processor_id());
  839. irq = vector = isa_irq_to_vector(isa_irq);
  840. BUG_ON(bind_irq_vector(irq, vector, CPU_MASK_ALL));
  841. register_intr(gsi, irq, IOSAPIC_LOWEST_PRIORITY, polarity, trigger);
  842. DBG("ISA: IRQ %u -> GSI %u (%s,%s) -> CPU %d (0x%04x) vector %d\n",
  843. isa_irq, gsi, trigger == IOSAPIC_EDGE ? "edge" : "level",
  844. polarity == IOSAPIC_POL_HIGH ? "high" : "low",
  845. cpu_logical_id(dest), dest, vector);
  846. set_rte(gsi, irq, dest, 1);
  847. }
  848. void __init
  849. iosapic_system_init (int system_pcat_compat)
  850. {
  851. int irq;
  852. for (irq = 0; irq < NR_IRQS; ++irq) {
  853. iosapic_intr_info[irq].low32 = IOSAPIC_MASK;
  854. /* mark as unused */
  855. INIT_LIST_HEAD(&iosapic_intr_info[irq].rtes);
  856. iosapic_intr_info[irq].count = 0;
  857. }
  858. pcat_compat = system_pcat_compat;
  859. if (pcat_compat) {
  860. /*
  861. * Disable the compatibility mode interrupts (8259 style),
  862. * needs IN/OUT support enabled.
  863. */
  864. printk(KERN_INFO
  865. "%s: Disabling PC-AT compatible 8259 interrupts\n",
  866. __FUNCTION__);
  867. outb(0xff, 0xA1);
  868. outb(0xff, 0x21);
  869. }
  870. }
  871. static inline int
  872. iosapic_alloc (void)
  873. {
  874. int index;
  875. for (index = 0; index < NR_IOSAPICS; index++)
  876. if (!iosapic_lists[index].addr)
  877. return index;
  878. printk(KERN_WARNING "%s: failed to allocate iosapic\n", __FUNCTION__);
  879. return -1;
  880. }
  881. static inline void
  882. iosapic_free (int index)
  883. {
  884. memset(&iosapic_lists[index], 0, sizeof(iosapic_lists[0]));
  885. }
  886. static inline int
  887. iosapic_check_gsi_range (unsigned int gsi_base, unsigned int ver)
  888. {
  889. int index;
  890. unsigned int gsi_end, base, end;
  891. /* check gsi range */
  892. gsi_end = gsi_base + ((ver >> 16) & 0xff);
  893. for (index = 0; index < NR_IOSAPICS; index++) {
  894. if (!iosapic_lists[index].addr)
  895. continue;
  896. base = iosapic_lists[index].gsi_base;
  897. end = base + iosapic_lists[index].num_rte - 1;
  898. if (gsi_end < base || end < gsi_base)
  899. continue; /* OK */
  900. return -EBUSY;
  901. }
  902. return 0;
  903. }
  904. int __devinit
  905. iosapic_init (unsigned long phys_addr, unsigned int gsi_base)
  906. {
  907. int num_rte, err, index;
  908. unsigned int isa_irq, ver;
  909. char __iomem *addr;
  910. unsigned long flags;
  911. spin_lock_irqsave(&iosapic_lock, flags);
  912. index = find_iosapic(gsi_base);
  913. if (index >= 0) {
  914. spin_unlock_irqrestore(&iosapic_lock, flags);
  915. return -EBUSY;
  916. }
  917. addr = ioremap(phys_addr, 0);
  918. ver = iosapic_version(addr);
  919. if ((err = iosapic_check_gsi_range(gsi_base, ver))) {
  920. iounmap(addr);
  921. spin_unlock_irqrestore(&iosapic_lock, flags);
  922. return err;
  923. }
  924. /*
  925. * The MAX_REDIR register holds the highest input pin number
  926. * (starting from 0). We add 1 so that we can use it for
  927. * number of pins (= RTEs)
  928. */
  929. num_rte = ((ver >> 16) & 0xff) + 1;
  930. index = iosapic_alloc();
  931. iosapic_lists[index].addr = addr;
  932. iosapic_lists[index].gsi_base = gsi_base;
  933. iosapic_lists[index].num_rte = num_rte;
  934. #ifdef CONFIG_NUMA
  935. iosapic_lists[index].node = MAX_NUMNODES;
  936. #endif
  937. spin_lock_init(&iosapic_lists[index].lock);
  938. spin_unlock_irqrestore(&iosapic_lock, flags);
  939. if ((gsi_base == 0) && pcat_compat) {
  940. /*
  941. * Map the legacy ISA devices into the IOSAPIC data. Some of
  942. * these may get reprogrammed later on with data from the ACPI
  943. * Interrupt Source Override table.
  944. */
  945. for (isa_irq = 0; isa_irq < 16; ++isa_irq)
  946. iosapic_override_isa_irq(isa_irq, isa_irq,
  947. IOSAPIC_POL_HIGH,
  948. IOSAPIC_EDGE);
  949. }
  950. return 0;
  951. }
  952. #ifdef CONFIG_HOTPLUG
  953. int
  954. iosapic_remove (unsigned int gsi_base)
  955. {
  956. int index, err = 0;
  957. unsigned long flags;
  958. spin_lock_irqsave(&iosapic_lock, flags);
  959. index = find_iosapic(gsi_base);
  960. if (index < 0) {
  961. printk(KERN_WARNING "%s: No IOSAPIC for GSI base %u\n",
  962. __FUNCTION__, gsi_base);
  963. goto out;
  964. }
  965. if (iosapic_lists[index].rtes_inuse) {
  966. err = -EBUSY;
  967. printk(KERN_WARNING "%s: IOSAPIC for GSI base %u is busy\n",
  968. __FUNCTION__, gsi_base);
  969. goto out;
  970. }
  971. iounmap(iosapic_lists[index].addr);
  972. iosapic_free(index);
  973. out:
  974. spin_unlock_irqrestore(&iosapic_lock, flags);
  975. return err;
  976. }
  977. #endif /* CONFIG_HOTPLUG */
  978. #ifdef CONFIG_NUMA
  979. void __devinit
  980. map_iosapic_to_node(unsigned int gsi_base, int node)
  981. {
  982. int index;
  983. index = find_iosapic(gsi_base);
  984. if (index < 0) {
  985. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  986. __FUNCTION__, gsi_base);
  987. return;
  988. }
  989. iosapic_lists[index].node = node;
  990. return;
  991. }
  992. #endif
  993. static int __init iosapic_enable_kmalloc (void)
  994. {
  995. iosapic_kmalloc_ok = 1;
  996. return 0;
  997. }
  998. core_initcall (iosapic_enable_kmalloc);