iosapic.c 30 KB

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