io_apic_64.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  1. /*
  2. * Intel IO-APIC support for multi-Pentium hosts.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
  5. *
  6. * Many thanks to Stig Venaas for trying out countless experimental
  7. * patches and reporting/debugging problems patiently!
  8. *
  9. * (c) 1999, Multiple IO-APIC support, developed by
  10. * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
  11. * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
  12. * further tested and cleaned up by Zach Brown <zab@redhat.com>
  13. * and Ingo Molnar <mingo@redhat.com>
  14. *
  15. * Fixes
  16. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  17. * thanks to Eric Gilmore
  18. * and Rolf G. Tews
  19. * for testing these extensively
  20. * Paul Diefenbaugh : Added full ACPI support
  21. */
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/sched.h>
  27. #include <linux/pci.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <linux/acpi.h>
  30. #include <linux/sysdev.h>
  31. #include <linux/msi.h>
  32. #include <linux/htirq.h>
  33. #include <linux/dmar.h>
  34. #include <linux/jiffies.h>
  35. #ifdef CONFIG_ACPI
  36. #include <acpi/acpi_bus.h>
  37. #endif
  38. #include <linux/bootmem.h>
  39. #include <asm/idle.h>
  40. #include <asm/io.h>
  41. #include <asm/smp.h>
  42. #include <asm/desc.h>
  43. #include <asm/proto.h>
  44. #include <asm/acpi.h>
  45. #include <asm/dma.h>
  46. #include <asm/nmi.h>
  47. #include <asm/msidef.h>
  48. #include <asm/hypertransport.h>
  49. #include <mach_ipi.h>
  50. #include <mach_apic.h>
  51. struct irq_cfg {
  52. cpumask_t domain;
  53. cpumask_t old_domain;
  54. unsigned move_cleanup_count;
  55. u8 vector;
  56. u8 move_in_progress : 1;
  57. };
  58. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  59. struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
  60. [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
  61. [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
  62. [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
  63. [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
  64. [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
  65. [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
  66. [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
  67. [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
  68. [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
  69. [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
  70. [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
  71. [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
  72. [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
  73. [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
  74. [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
  75. [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
  76. };
  77. static int assign_irq_vector(int irq, cpumask_t mask);
  78. #define __apicdebuginit __init
  79. int sis_apic_bug; /* not actually supported, dummy for compile */
  80. static int no_timer_check;
  81. static int disable_timer_pin_1 __initdata;
  82. int timer_over_8254 __initdata = 1;
  83. /* Where if anywhere is the i8259 connect in external int mode */
  84. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  85. static DEFINE_SPINLOCK(ioapic_lock);
  86. DEFINE_SPINLOCK(vector_lock);
  87. /*
  88. * # of IRQ routing registers
  89. */
  90. int nr_ioapic_registers[MAX_IO_APICS];
  91. /* I/O APIC entries */
  92. struct mpc_config_ioapic mp_ioapics[MAX_IO_APICS];
  93. int nr_ioapics;
  94. /* MP IRQ source entries */
  95. struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  96. /* # of MP IRQ source entries */
  97. int mp_irq_entries;
  98. /*
  99. * Rough estimation of how many shared IRQs there are, can
  100. * be changed anytime.
  101. */
  102. #define MAX_PLUS_SHARED_IRQS NR_IRQS
  103. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  104. /*
  105. * This is performance-critical, we want to do it O(1)
  106. *
  107. * the indexing order of this array favors 1:1 mappings
  108. * between pins and IRQs.
  109. */
  110. static struct irq_pin_list {
  111. short apic, pin, next;
  112. } irq_2_pin[PIN_MAP_SIZE];
  113. struct io_apic {
  114. unsigned int index;
  115. unsigned int unused[3];
  116. unsigned int data;
  117. };
  118. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  119. {
  120. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  121. + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
  122. }
  123. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  124. {
  125. struct io_apic __iomem *io_apic = io_apic_base(apic);
  126. writel(reg, &io_apic->index);
  127. return readl(&io_apic->data);
  128. }
  129. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  130. {
  131. struct io_apic __iomem *io_apic = io_apic_base(apic);
  132. writel(reg, &io_apic->index);
  133. writel(value, &io_apic->data);
  134. }
  135. /*
  136. * Re-write a value: to be used for read-modify-write
  137. * cycles where the read already set up the index register.
  138. */
  139. static inline void io_apic_modify(unsigned int apic, unsigned int value)
  140. {
  141. struct io_apic __iomem *io_apic = io_apic_base(apic);
  142. writel(value, &io_apic->data);
  143. }
  144. static bool io_apic_level_ack_pending(unsigned int irq)
  145. {
  146. struct irq_pin_list *entry;
  147. unsigned long flags;
  148. spin_lock_irqsave(&ioapic_lock, flags);
  149. entry = irq_2_pin + irq;
  150. for (;;) {
  151. unsigned int reg;
  152. int pin;
  153. pin = entry->pin;
  154. if (pin == -1)
  155. break;
  156. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  157. /* Is the remote IRR bit set? */
  158. if ((reg >> 14) & 1) {
  159. spin_unlock_irqrestore(&ioapic_lock, flags);
  160. return true;
  161. }
  162. if (!entry->next)
  163. break;
  164. entry = irq_2_pin + entry->next;
  165. }
  166. spin_unlock_irqrestore(&ioapic_lock, flags);
  167. return false;
  168. }
  169. /*
  170. * Synchronize the IO-APIC and the CPU by doing
  171. * a dummy read from the IO-APIC
  172. */
  173. static inline void io_apic_sync(unsigned int apic)
  174. {
  175. struct io_apic __iomem *io_apic = io_apic_base(apic);
  176. readl(&io_apic->data);
  177. }
  178. #define __DO_ACTION(R, ACTION, FINAL) \
  179. \
  180. { \
  181. int pin; \
  182. struct irq_pin_list *entry = irq_2_pin + irq; \
  183. \
  184. BUG_ON(irq >= NR_IRQS); \
  185. for (;;) { \
  186. unsigned int reg; \
  187. pin = entry->pin; \
  188. if (pin == -1) \
  189. break; \
  190. reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
  191. reg ACTION; \
  192. io_apic_modify(entry->apic, reg); \
  193. FINAL; \
  194. if (!entry->next) \
  195. break; \
  196. entry = irq_2_pin + entry->next; \
  197. } \
  198. }
  199. union entry_union {
  200. struct { u32 w1, w2; };
  201. struct IO_APIC_route_entry entry;
  202. };
  203. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  204. {
  205. union entry_union eu;
  206. unsigned long flags;
  207. spin_lock_irqsave(&ioapic_lock, flags);
  208. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  209. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  210. spin_unlock_irqrestore(&ioapic_lock, flags);
  211. return eu.entry;
  212. }
  213. /*
  214. * When we write a new IO APIC routing entry, we need to write the high
  215. * word first! If the mask bit in the low word is clear, we will enable
  216. * the interrupt, and we need to make sure the entry is fully populated
  217. * before that happens.
  218. */
  219. static void
  220. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  221. {
  222. union entry_union eu;
  223. eu.entry = e;
  224. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  225. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  226. }
  227. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  228. {
  229. unsigned long flags;
  230. spin_lock_irqsave(&ioapic_lock, flags);
  231. __ioapic_write_entry(apic, pin, e);
  232. spin_unlock_irqrestore(&ioapic_lock, flags);
  233. }
  234. /*
  235. * When we mask an IO APIC routing entry, we need to write the low
  236. * word first, in order to set the mask bit before we change the
  237. * high bits!
  238. */
  239. static void ioapic_mask_entry(int apic, int pin)
  240. {
  241. unsigned long flags;
  242. union entry_union eu = { .entry.mask = 1 };
  243. spin_lock_irqsave(&ioapic_lock, flags);
  244. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  245. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  246. spin_unlock_irqrestore(&ioapic_lock, flags);
  247. }
  248. #ifdef CONFIG_SMP
  249. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
  250. {
  251. int apic, pin;
  252. struct irq_pin_list *entry = irq_2_pin + irq;
  253. BUG_ON(irq >= NR_IRQS);
  254. for (;;) {
  255. unsigned int reg;
  256. apic = entry->apic;
  257. pin = entry->pin;
  258. if (pin == -1)
  259. break;
  260. io_apic_write(apic, 0x11 + pin*2, dest);
  261. reg = io_apic_read(apic, 0x10 + pin*2);
  262. reg &= ~0x000000ff;
  263. reg |= vector;
  264. io_apic_modify(apic, reg);
  265. if (!entry->next)
  266. break;
  267. entry = irq_2_pin + entry->next;
  268. }
  269. }
  270. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  271. {
  272. struct irq_cfg *cfg = irq_cfg + irq;
  273. unsigned long flags;
  274. unsigned int dest;
  275. cpumask_t tmp;
  276. cpus_and(tmp, mask, cpu_online_map);
  277. if (cpus_empty(tmp))
  278. return;
  279. if (assign_irq_vector(irq, mask))
  280. return;
  281. cpus_and(tmp, cfg->domain, mask);
  282. dest = cpu_mask_to_apicid(tmp);
  283. /*
  284. * Only the high 8 bits are valid.
  285. */
  286. dest = SET_APIC_LOGICAL_ID(dest);
  287. spin_lock_irqsave(&ioapic_lock, flags);
  288. __target_IO_APIC_irq(irq, dest, cfg->vector);
  289. irq_desc[irq].affinity = mask;
  290. spin_unlock_irqrestore(&ioapic_lock, flags);
  291. }
  292. #endif
  293. /*
  294. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  295. * shared ISA-space IRQs, so we have to support them. We are super
  296. * fast in the common case, and fast for shared ISA-space IRQs.
  297. */
  298. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  299. {
  300. static int first_free_entry = NR_IRQS;
  301. struct irq_pin_list *entry = irq_2_pin + irq;
  302. BUG_ON(irq >= NR_IRQS);
  303. while (entry->next)
  304. entry = irq_2_pin + entry->next;
  305. if (entry->pin != -1) {
  306. entry->next = first_free_entry;
  307. entry = irq_2_pin + entry->next;
  308. if (++first_free_entry >= PIN_MAP_SIZE)
  309. panic("io_apic.c: ran out of irq_2_pin entries!");
  310. }
  311. entry->apic = apic;
  312. entry->pin = pin;
  313. }
  314. #define DO_ACTION(name,R,ACTION, FINAL) \
  315. \
  316. static void name##_IO_APIC_irq (unsigned int irq) \
  317. __DO_ACTION(R, ACTION, FINAL)
  318. DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
  319. /* mask = 1 */
  320. DO_ACTION( __unmask, 0, &= 0xfffeffff, )
  321. /* mask = 0 */
  322. static void mask_IO_APIC_irq (unsigned int irq)
  323. {
  324. unsigned long flags;
  325. spin_lock_irqsave(&ioapic_lock, flags);
  326. __mask_IO_APIC_irq(irq);
  327. spin_unlock_irqrestore(&ioapic_lock, flags);
  328. }
  329. static void unmask_IO_APIC_irq (unsigned int irq)
  330. {
  331. unsigned long flags;
  332. spin_lock_irqsave(&ioapic_lock, flags);
  333. __unmask_IO_APIC_irq(irq);
  334. spin_unlock_irqrestore(&ioapic_lock, flags);
  335. }
  336. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  337. {
  338. struct IO_APIC_route_entry entry;
  339. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  340. entry = ioapic_read_entry(apic, pin);
  341. if (entry.delivery_mode == dest_SMI)
  342. return;
  343. /*
  344. * Disable it in the IO-APIC irq-routing table:
  345. */
  346. ioapic_mask_entry(apic, pin);
  347. }
  348. static void clear_IO_APIC (void)
  349. {
  350. int apic, pin;
  351. for (apic = 0; apic < nr_ioapics; apic++)
  352. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  353. clear_IO_APIC_pin(apic, pin);
  354. }
  355. int skip_ioapic_setup;
  356. int ioapic_force;
  357. static int __init parse_noapic(char *str)
  358. {
  359. disable_ioapic_setup();
  360. return 0;
  361. }
  362. early_param("noapic", parse_noapic);
  363. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  364. static int __init disable_timer_pin_setup(char *arg)
  365. {
  366. disable_timer_pin_1 = 1;
  367. return 1;
  368. }
  369. __setup("disable_timer_pin_1", disable_timer_pin_setup);
  370. static int __init setup_disable_8254_timer(char *s)
  371. {
  372. timer_over_8254 = -1;
  373. return 1;
  374. }
  375. static int __init setup_enable_8254_timer(char *s)
  376. {
  377. timer_over_8254 = 2;
  378. return 1;
  379. }
  380. __setup("disable_8254_timer", setup_disable_8254_timer);
  381. __setup("enable_8254_timer", setup_enable_8254_timer);
  382. /*
  383. * Find the IRQ entry number of a certain pin.
  384. */
  385. static int find_irq_entry(int apic, int pin, int type)
  386. {
  387. int i;
  388. for (i = 0; i < mp_irq_entries; i++)
  389. if (mp_irqs[i].mpc_irqtype == type &&
  390. (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
  391. mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
  392. mp_irqs[i].mpc_dstirq == pin)
  393. return i;
  394. return -1;
  395. }
  396. /*
  397. * Find the pin to which IRQ[irq] (ISA) is connected
  398. */
  399. static int __init find_isa_irq_pin(int irq, int type)
  400. {
  401. int i;
  402. for (i = 0; i < mp_irq_entries; i++) {
  403. int lbus = mp_irqs[i].mpc_srcbus;
  404. if (test_bit(lbus, mp_bus_not_pci) &&
  405. (mp_irqs[i].mpc_irqtype == type) &&
  406. (mp_irqs[i].mpc_srcbusirq == irq))
  407. return mp_irqs[i].mpc_dstirq;
  408. }
  409. return -1;
  410. }
  411. static int __init find_isa_irq_apic(int irq, int type)
  412. {
  413. int i;
  414. for (i = 0; i < mp_irq_entries; i++) {
  415. int lbus = mp_irqs[i].mpc_srcbus;
  416. if (test_bit(lbus, mp_bus_not_pci) &&
  417. (mp_irqs[i].mpc_irqtype == type) &&
  418. (mp_irqs[i].mpc_srcbusirq == irq))
  419. break;
  420. }
  421. if (i < mp_irq_entries) {
  422. int apic;
  423. for(apic = 0; apic < nr_ioapics; apic++) {
  424. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
  425. return apic;
  426. }
  427. }
  428. return -1;
  429. }
  430. /*
  431. * Find a specific PCI IRQ entry.
  432. * Not an __init, possibly needed by modules
  433. */
  434. static int pin_2_irq(int idx, int apic, int pin);
  435. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  436. {
  437. int apic, i, best_guess = -1;
  438. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  439. bus, slot, pin);
  440. if (mp_bus_id_to_pci_bus[bus] == -1) {
  441. apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  442. return -1;
  443. }
  444. for (i = 0; i < mp_irq_entries; i++) {
  445. int lbus = mp_irqs[i].mpc_srcbus;
  446. for (apic = 0; apic < nr_ioapics; apic++)
  447. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
  448. mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
  449. break;
  450. if (!test_bit(lbus, mp_bus_not_pci) &&
  451. !mp_irqs[i].mpc_irqtype &&
  452. (bus == lbus) &&
  453. (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
  454. int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
  455. if (!(apic || IO_APIC_IRQ(irq)))
  456. continue;
  457. if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
  458. return irq;
  459. /*
  460. * Use the first all-but-pin matching entry as a
  461. * best-guess fuzzy result for broken mptables.
  462. */
  463. if (best_guess < 0)
  464. best_guess = irq;
  465. }
  466. }
  467. BUG_ON(best_guess >= NR_IRQS);
  468. return best_guess;
  469. }
  470. /* ISA interrupts are always polarity zero edge triggered,
  471. * when listed as conforming in the MP table. */
  472. #define default_ISA_trigger(idx) (0)
  473. #define default_ISA_polarity(idx) (0)
  474. /* PCI interrupts are always polarity one level triggered,
  475. * when listed as conforming in the MP table. */
  476. #define default_PCI_trigger(idx) (1)
  477. #define default_PCI_polarity(idx) (1)
  478. static int MPBIOS_polarity(int idx)
  479. {
  480. int bus = mp_irqs[idx].mpc_srcbus;
  481. int polarity;
  482. /*
  483. * Determine IRQ line polarity (high active or low active):
  484. */
  485. switch (mp_irqs[idx].mpc_irqflag & 3)
  486. {
  487. case 0: /* conforms, ie. bus-type dependent polarity */
  488. if (test_bit(bus, mp_bus_not_pci))
  489. polarity = default_ISA_polarity(idx);
  490. else
  491. polarity = default_PCI_polarity(idx);
  492. break;
  493. case 1: /* high active */
  494. {
  495. polarity = 0;
  496. break;
  497. }
  498. case 2: /* reserved */
  499. {
  500. printk(KERN_WARNING "broken BIOS!!\n");
  501. polarity = 1;
  502. break;
  503. }
  504. case 3: /* low active */
  505. {
  506. polarity = 1;
  507. break;
  508. }
  509. default: /* invalid */
  510. {
  511. printk(KERN_WARNING "broken BIOS!!\n");
  512. polarity = 1;
  513. break;
  514. }
  515. }
  516. return polarity;
  517. }
  518. static int MPBIOS_trigger(int idx)
  519. {
  520. int bus = mp_irqs[idx].mpc_srcbus;
  521. int trigger;
  522. /*
  523. * Determine IRQ trigger mode (edge or level sensitive):
  524. */
  525. switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
  526. {
  527. case 0: /* conforms, ie. bus-type dependent */
  528. if (test_bit(bus, mp_bus_not_pci))
  529. trigger = default_ISA_trigger(idx);
  530. else
  531. trigger = default_PCI_trigger(idx);
  532. break;
  533. case 1: /* edge */
  534. {
  535. trigger = 0;
  536. break;
  537. }
  538. case 2: /* reserved */
  539. {
  540. printk(KERN_WARNING "broken BIOS!!\n");
  541. trigger = 1;
  542. break;
  543. }
  544. case 3: /* level */
  545. {
  546. trigger = 1;
  547. break;
  548. }
  549. default: /* invalid */
  550. {
  551. printk(KERN_WARNING "broken BIOS!!\n");
  552. trigger = 0;
  553. break;
  554. }
  555. }
  556. return trigger;
  557. }
  558. static inline int irq_polarity(int idx)
  559. {
  560. return MPBIOS_polarity(idx);
  561. }
  562. static inline int irq_trigger(int idx)
  563. {
  564. return MPBIOS_trigger(idx);
  565. }
  566. static int pin_2_irq(int idx, int apic, int pin)
  567. {
  568. int irq, i;
  569. int bus = mp_irqs[idx].mpc_srcbus;
  570. /*
  571. * Debugging check, we are in big trouble if this message pops up!
  572. */
  573. if (mp_irqs[idx].mpc_dstirq != pin)
  574. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  575. if (test_bit(bus, mp_bus_not_pci)) {
  576. irq = mp_irqs[idx].mpc_srcbusirq;
  577. } else {
  578. /*
  579. * PCI IRQs are mapped in order
  580. */
  581. i = irq = 0;
  582. while (i < apic)
  583. irq += nr_ioapic_registers[i++];
  584. irq += pin;
  585. }
  586. BUG_ON(irq >= NR_IRQS);
  587. return irq;
  588. }
  589. static int __assign_irq_vector(int irq, cpumask_t mask)
  590. {
  591. /*
  592. * NOTE! The local APIC isn't very good at handling
  593. * multiple interrupts at the same interrupt level.
  594. * As the interrupt level is determined by taking the
  595. * vector number and shifting that right by 4, we
  596. * want to spread these out a bit so that they don't
  597. * all fall in the same interrupt level.
  598. *
  599. * Also, we've got to be careful not to trash gate
  600. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  601. */
  602. static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  603. unsigned int old_vector;
  604. int cpu;
  605. struct irq_cfg *cfg;
  606. BUG_ON((unsigned)irq >= NR_IRQS);
  607. cfg = &irq_cfg[irq];
  608. /* Only try and allocate irqs on cpus that are present */
  609. cpus_and(mask, mask, cpu_online_map);
  610. if ((cfg->move_in_progress) || cfg->move_cleanup_count)
  611. return -EBUSY;
  612. old_vector = cfg->vector;
  613. if (old_vector) {
  614. cpumask_t tmp;
  615. cpus_and(tmp, cfg->domain, mask);
  616. if (!cpus_empty(tmp))
  617. return 0;
  618. }
  619. for_each_cpu_mask(cpu, mask) {
  620. cpumask_t domain, new_mask;
  621. int new_cpu;
  622. int vector, offset;
  623. domain = vector_allocation_domain(cpu);
  624. cpus_and(new_mask, domain, cpu_online_map);
  625. vector = current_vector;
  626. offset = current_offset;
  627. next:
  628. vector += 8;
  629. if (vector >= FIRST_SYSTEM_VECTOR) {
  630. /* If we run out of vectors on large boxen, must share them. */
  631. offset = (offset + 1) % 8;
  632. vector = FIRST_DEVICE_VECTOR + offset;
  633. }
  634. if (unlikely(current_vector == vector))
  635. continue;
  636. if (vector == IA32_SYSCALL_VECTOR)
  637. goto next;
  638. for_each_cpu_mask(new_cpu, new_mask)
  639. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  640. goto next;
  641. /* Found one! */
  642. current_vector = vector;
  643. current_offset = offset;
  644. if (old_vector) {
  645. cfg->move_in_progress = 1;
  646. cfg->old_domain = cfg->domain;
  647. }
  648. for_each_cpu_mask(new_cpu, new_mask)
  649. per_cpu(vector_irq, new_cpu)[vector] = irq;
  650. cfg->vector = vector;
  651. cfg->domain = domain;
  652. return 0;
  653. }
  654. return -ENOSPC;
  655. }
  656. static int assign_irq_vector(int irq, cpumask_t mask)
  657. {
  658. int err;
  659. unsigned long flags;
  660. spin_lock_irqsave(&vector_lock, flags);
  661. err = __assign_irq_vector(irq, mask);
  662. spin_unlock_irqrestore(&vector_lock, flags);
  663. return err;
  664. }
  665. static void __clear_irq_vector(int irq)
  666. {
  667. struct irq_cfg *cfg;
  668. cpumask_t mask;
  669. int cpu, vector;
  670. BUG_ON((unsigned)irq >= NR_IRQS);
  671. cfg = &irq_cfg[irq];
  672. BUG_ON(!cfg->vector);
  673. vector = cfg->vector;
  674. cpus_and(mask, cfg->domain, cpu_online_map);
  675. for_each_cpu_mask(cpu, mask)
  676. per_cpu(vector_irq, cpu)[vector] = -1;
  677. cfg->vector = 0;
  678. cpus_clear(cfg->domain);
  679. }
  680. void __setup_vector_irq(int cpu)
  681. {
  682. /* Initialize vector_irq on a new cpu */
  683. /* This function must be called with vector_lock held */
  684. int irq, vector;
  685. /* Mark the inuse vectors */
  686. for (irq = 0; irq < NR_IRQS; ++irq) {
  687. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  688. continue;
  689. vector = irq_cfg[irq].vector;
  690. per_cpu(vector_irq, cpu)[vector] = irq;
  691. }
  692. /* Mark the free vectors */
  693. for (vector = 0; vector < NR_VECTORS; ++vector) {
  694. irq = per_cpu(vector_irq, cpu)[vector];
  695. if (irq < 0)
  696. continue;
  697. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  698. per_cpu(vector_irq, cpu)[vector] = -1;
  699. }
  700. }
  701. static struct irq_chip ioapic_chip;
  702. static void ioapic_register_intr(int irq, unsigned long trigger)
  703. {
  704. if (trigger) {
  705. irq_desc[irq].status |= IRQ_LEVEL;
  706. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  707. handle_fasteoi_irq, "fasteoi");
  708. } else {
  709. irq_desc[irq].status &= ~IRQ_LEVEL;
  710. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  711. handle_edge_irq, "edge");
  712. }
  713. }
  714. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
  715. int trigger, int polarity)
  716. {
  717. struct irq_cfg *cfg = irq_cfg + irq;
  718. struct IO_APIC_route_entry entry;
  719. cpumask_t mask;
  720. if (!IO_APIC_IRQ(irq))
  721. return;
  722. mask = TARGET_CPUS;
  723. if (assign_irq_vector(irq, mask))
  724. return;
  725. cpus_and(mask, cfg->domain, mask);
  726. apic_printk(APIC_VERBOSE,KERN_DEBUG
  727. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  728. "IRQ %d Mode:%i Active:%i)\n",
  729. apic, mp_ioapics[apic].mpc_apicid, pin, cfg->vector,
  730. irq, trigger, polarity);
  731. /*
  732. * add it to the IO-APIC irq-routing table:
  733. */
  734. memset(&entry,0,sizeof(entry));
  735. entry.delivery_mode = INT_DELIVERY_MODE;
  736. entry.dest_mode = INT_DEST_MODE;
  737. entry.dest = cpu_mask_to_apicid(mask);
  738. entry.mask = 0; /* enable IRQ */
  739. entry.trigger = trigger;
  740. entry.polarity = polarity;
  741. entry.vector = cfg->vector;
  742. /* Mask level triggered irqs.
  743. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  744. */
  745. if (trigger)
  746. entry.mask = 1;
  747. ioapic_register_intr(irq, trigger);
  748. if (irq < 16)
  749. disable_8259A_irq(irq);
  750. ioapic_write_entry(apic, pin, entry);
  751. }
  752. static void __init setup_IO_APIC_irqs(void)
  753. {
  754. int apic, pin, idx, irq, first_notcon = 1;
  755. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  756. for (apic = 0; apic < nr_ioapics; apic++) {
  757. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  758. idx = find_irq_entry(apic,pin,mp_INT);
  759. if (idx == -1) {
  760. if (first_notcon) {
  761. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  762. first_notcon = 0;
  763. } else
  764. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  765. continue;
  766. }
  767. if (!first_notcon) {
  768. apic_printk(APIC_VERBOSE, " not connected.\n");
  769. first_notcon = 1;
  770. }
  771. irq = pin_2_irq(idx, apic, pin);
  772. add_pin_to_irq(irq, apic, pin);
  773. setup_IO_APIC_irq(apic, pin, irq,
  774. irq_trigger(idx), irq_polarity(idx));
  775. }
  776. }
  777. if (!first_notcon)
  778. apic_printk(APIC_VERBOSE, " not connected.\n");
  779. }
  780. /*
  781. * Set up the 8259A-master output pin as broadcast to all
  782. * CPUs.
  783. */
  784. static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
  785. {
  786. struct IO_APIC_route_entry entry;
  787. memset(&entry, 0, sizeof(entry));
  788. disable_8259A_irq(0);
  789. /* mask LVT0 */
  790. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  791. /*
  792. * We use logical delivery to get the timer IRQ
  793. * to the first CPU.
  794. */
  795. entry.dest_mode = INT_DEST_MODE;
  796. entry.mask = 0; /* unmask IRQ now */
  797. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  798. entry.delivery_mode = INT_DELIVERY_MODE;
  799. entry.polarity = 0;
  800. entry.trigger = 0;
  801. entry.vector = vector;
  802. /*
  803. * The timer IRQ doesn't have to know that behind the
  804. * scene we have a 8259A-master in AEOI mode ...
  805. */
  806. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  807. /*
  808. * Add it to the IO-APIC irq-routing table:
  809. */
  810. ioapic_write_entry(apic, pin, entry);
  811. enable_8259A_irq(0);
  812. }
  813. void __apicdebuginit print_IO_APIC(void)
  814. {
  815. int apic, i;
  816. union IO_APIC_reg_00 reg_00;
  817. union IO_APIC_reg_01 reg_01;
  818. union IO_APIC_reg_02 reg_02;
  819. unsigned long flags;
  820. if (apic_verbosity == APIC_QUIET)
  821. return;
  822. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  823. for (i = 0; i < nr_ioapics; i++)
  824. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  825. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  826. /*
  827. * We are a bit conservative about what we expect. We have to
  828. * know about every hardware change ASAP.
  829. */
  830. printk(KERN_INFO "testing the IO APIC.......................\n");
  831. for (apic = 0; apic < nr_ioapics; apic++) {
  832. spin_lock_irqsave(&ioapic_lock, flags);
  833. reg_00.raw = io_apic_read(apic, 0);
  834. reg_01.raw = io_apic_read(apic, 1);
  835. if (reg_01.bits.version >= 0x10)
  836. reg_02.raw = io_apic_read(apic, 2);
  837. spin_unlock_irqrestore(&ioapic_lock, flags);
  838. printk("\n");
  839. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  840. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  841. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  842. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  843. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  844. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  845. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  846. if (reg_01.bits.version >= 0x10) {
  847. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  848. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  849. }
  850. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  851. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  852. " Stat Dmod Deli Vect: \n");
  853. for (i = 0; i <= reg_01.bits.entries; i++) {
  854. struct IO_APIC_route_entry entry;
  855. entry = ioapic_read_entry(apic, i);
  856. printk(KERN_DEBUG " %02x %03X ",
  857. i,
  858. entry.dest
  859. );
  860. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  861. entry.mask,
  862. entry.trigger,
  863. entry.irr,
  864. entry.polarity,
  865. entry.delivery_status,
  866. entry.dest_mode,
  867. entry.delivery_mode,
  868. entry.vector
  869. );
  870. }
  871. }
  872. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  873. for (i = 0; i < NR_IRQS; i++) {
  874. struct irq_pin_list *entry = irq_2_pin + i;
  875. if (entry->pin < 0)
  876. continue;
  877. printk(KERN_DEBUG "IRQ%d ", i);
  878. for (;;) {
  879. printk("-> %d:%d", entry->apic, entry->pin);
  880. if (!entry->next)
  881. break;
  882. entry = irq_2_pin + entry->next;
  883. }
  884. printk("\n");
  885. }
  886. printk(KERN_INFO ".................................... done.\n");
  887. return;
  888. }
  889. #if 0
  890. static __apicdebuginit void print_APIC_bitfield (int base)
  891. {
  892. unsigned int v;
  893. int i, j;
  894. if (apic_verbosity == APIC_QUIET)
  895. return;
  896. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  897. for (i = 0; i < 8; i++) {
  898. v = apic_read(base + i*0x10);
  899. for (j = 0; j < 32; j++) {
  900. if (v & (1<<j))
  901. printk("1");
  902. else
  903. printk("0");
  904. }
  905. printk("\n");
  906. }
  907. }
  908. void __apicdebuginit print_local_APIC(void * dummy)
  909. {
  910. unsigned int v, ver, maxlvt;
  911. if (apic_verbosity == APIC_QUIET)
  912. return;
  913. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  914. smp_processor_id(), hard_smp_processor_id());
  915. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(read_apic_id()));
  916. v = apic_read(APIC_LVR);
  917. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  918. ver = GET_APIC_VERSION(v);
  919. maxlvt = lapic_get_maxlvt();
  920. v = apic_read(APIC_TASKPRI);
  921. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  922. v = apic_read(APIC_ARBPRI);
  923. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  924. v & APIC_ARBPRI_MASK);
  925. v = apic_read(APIC_PROCPRI);
  926. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  927. v = apic_read(APIC_EOI);
  928. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  929. v = apic_read(APIC_RRR);
  930. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  931. v = apic_read(APIC_LDR);
  932. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  933. v = apic_read(APIC_DFR);
  934. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  935. v = apic_read(APIC_SPIV);
  936. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  937. printk(KERN_DEBUG "... APIC ISR field:\n");
  938. print_APIC_bitfield(APIC_ISR);
  939. printk(KERN_DEBUG "... APIC TMR field:\n");
  940. print_APIC_bitfield(APIC_TMR);
  941. printk(KERN_DEBUG "... APIC IRR field:\n");
  942. print_APIC_bitfield(APIC_IRR);
  943. v = apic_read(APIC_ESR);
  944. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  945. v = apic_read(APIC_ICR);
  946. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  947. v = apic_read(APIC_ICR2);
  948. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  949. v = apic_read(APIC_LVTT);
  950. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  951. if (maxlvt > 3) { /* PC is LVT#4. */
  952. v = apic_read(APIC_LVTPC);
  953. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  954. }
  955. v = apic_read(APIC_LVT0);
  956. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  957. v = apic_read(APIC_LVT1);
  958. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  959. if (maxlvt > 2) { /* ERR is LVT#3. */
  960. v = apic_read(APIC_LVTERR);
  961. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  962. }
  963. v = apic_read(APIC_TMICT);
  964. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  965. v = apic_read(APIC_TMCCT);
  966. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  967. v = apic_read(APIC_TDCR);
  968. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  969. printk("\n");
  970. }
  971. void print_all_local_APICs (void)
  972. {
  973. on_each_cpu(print_local_APIC, NULL, 1, 1);
  974. }
  975. void __apicdebuginit print_PIC(void)
  976. {
  977. unsigned int v;
  978. unsigned long flags;
  979. if (apic_verbosity == APIC_QUIET)
  980. return;
  981. printk(KERN_DEBUG "\nprinting PIC contents\n");
  982. spin_lock_irqsave(&i8259A_lock, flags);
  983. v = inb(0xa1) << 8 | inb(0x21);
  984. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  985. v = inb(0xa0) << 8 | inb(0x20);
  986. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  987. outb(0x0b,0xa0);
  988. outb(0x0b,0x20);
  989. v = inb(0xa0) << 8 | inb(0x20);
  990. outb(0x0a,0xa0);
  991. outb(0x0a,0x20);
  992. spin_unlock_irqrestore(&i8259A_lock, flags);
  993. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  994. v = inb(0x4d1) << 8 | inb(0x4d0);
  995. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  996. }
  997. #endif /* 0 */
  998. void __init enable_IO_APIC(void)
  999. {
  1000. union IO_APIC_reg_01 reg_01;
  1001. int i8259_apic, i8259_pin;
  1002. int i, apic;
  1003. unsigned long flags;
  1004. for (i = 0; i < PIN_MAP_SIZE; i++) {
  1005. irq_2_pin[i].pin = -1;
  1006. irq_2_pin[i].next = 0;
  1007. }
  1008. /*
  1009. * The number of IO-APIC IRQ registers (== #pins):
  1010. */
  1011. for (apic = 0; apic < nr_ioapics; apic++) {
  1012. spin_lock_irqsave(&ioapic_lock, flags);
  1013. reg_01.raw = io_apic_read(apic, 1);
  1014. spin_unlock_irqrestore(&ioapic_lock, flags);
  1015. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1016. }
  1017. for(apic = 0; apic < nr_ioapics; apic++) {
  1018. int pin;
  1019. /* See if any of the pins is in ExtINT mode */
  1020. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1021. struct IO_APIC_route_entry entry;
  1022. entry = ioapic_read_entry(apic, pin);
  1023. /* If the interrupt line is enabled and in ExtInt mode
  1024. * I have found the pin where the i8259 is connected.
  1025. */
  1026. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1027. ioapic_i8259.apic = apic;
  1028. ioapic_i8259.pin = pin;
  1029. goto found_i8259;
  1030. }
  1031. }
  1032. }
  1033. found_i8259:
  1034. /* Look to see what if the MP table has reported the ExtINT */
  1035. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1036. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1037. /* Trust the MP table if nothing is setup in the hardware */
  1038. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1039. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1040. ioapic_i8259.pin = i8259_pin;
  1041. ioapic_i8259.apic = i8259_apic;
  1042. }
  1043. /* Complain if the MP table and the hardware disagree */
  1044. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1045. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1046. {
  1047. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1048. }
  1049. /*
  1050. * Do not trust the IO-APIC being empty at bootup
  1051. */
  1052. clear_IO_APIC();
  1053. }
  1054. /*
  1055. * Not an __init, needed by the reboot code
  1056. */
  1057. void disable_IO_APIC(void)
  1058. {
  1059. /*
  1060. * Clear the IO-APIC before rebooting:
  1061. */
  1062. clear_IO_APIC();
  1063. /*
  1064. * If the i8259 is routed through an IOAPIC
  1065. * Put that IOAPIC in virtual wire mode
  1066. * so legacy interrupts can be delivered.
  1067. */
  1068. if (ioapic_i8259.pin != -1) {
  1069. struct IO_APIC_route_entry entry;
  1070. memset(&entry, 0, sizeof(entry));
  1071. entry.mask = 0; /* Enabled */
  1072. entry.trigger = 0; /* Edge */
  1073. entry.irr = 0;
  1074. entry.polarity = 0; /* High */
  1075. entry.delivery_status = 0;
  1076. entry.dest_mode = 0; /* Physical */
  1077. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1078. entry.vector = 0;
  1079. entry.dest = GET_APIC_ID(read_apic_id());
  1080. /*
  1081. * Add it to the IO-APIC irq-routing table:
  1082. */
  1083. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1084. }
  1085. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1086. }
  1087. /*
  1088. * There is a nasty bug in some older SMP boards, their mptable lies
  1089. * about the timer IRQ. We do the following to work around the situation:
  1090. *
  1091. * - timer IRQ defaults to IO-APIC IRQ
  1092. * - if this function detects that timer IRQs are defunct, then we fall
  1093. * back to ISA timer IRQs
  1094. */
  1095. static int __init timer_irq_works(void)
  1096. {
  1097. unsigned long t1 = jiffies;
  1098. unsigned long flags;
  1099. local_save_flags(flags);
  1100. local_irq_enable();
  1101. /* Let ten ticks pass... */
  1102. mdelay((10 * 1000) / HZ);
  1103. local_irq_restore(flags);
  1104. /*
  1105. * Expect a few ticks at least, to be sure some possible
  1106. * glue logic does not lock up after one or two first
  1107. * ticks in a non-ExtINT mode. Also the local APIC
  1108. * might have cached one ExtINT interrupt. Finally, at
  1109. * least one tick may be lost due to delays.
  1110. */
  1111. /* jiffies wrap? */
  1112. if (time_after(jiffies, t1 + 4))
  1113. return 1;
  1114. return 0;
  1115. }
  1116. /*
  1117. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1118. * number of pending IRQ events unhandled. These cases are very rare,
  1119. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1120. * better to do it this way as thus we do not have to be aware of
  1121. * 'pending' interrupts in the IRQ path, except at this point.
  1122. */
  1123. /*
  1124. * Edge triggered needs to resend any interrupt
  1125. * that was delayed but this is now handled in the device
  1126. * independent code.
  1127. */
  1128. /*
  1129. * Starting up a edge-triggered IO-APIC interrupt is
  1130. * nasty - we need to make sure that we get the edge.
  1131. * If it is already asserted for some reason, we need
  1132. * return 1 to indicate that is was pending.
  1133. *
  1134. * This is not complete - we should be able to fake
  1135. * an edge even if it isn't on the 8259A...
  1136. */
  1137. static unsigned int startup_ioapic_irq(unsigned int irq)
  1138. {
  1139. int was_pending = 0;
  1140. unsigned long flags;
  1141. spin_lock_irqsave(&ioapic_lock, flags);
  1142. if (irq < 16) {
  1143. disable_8259A_irq(irq);
  1144. if (i8259A_irq_pending(irq))
  1145. was_pending = 1;
  1146. }
  1147. __unmask_IO_APIC_irq(irq);
  1148. spin_unlock_irqrestore(&ioapic_lock, flags);
  1149. return was_pending;
  1150. }
  1151. static int ioapic_retrigger_irq(unsigned int irq)
  1152. {
  1153. struct irq_cfg *cfg = &irq_cfg[irq];
  1154. cpumask_t mask;
  1155. unsigned long flags;
  1156. spin_lock_irqsave(&vector_lock, flags);
  1157. mask = cpumask_of_cpu(first_cpu(cfg->domain));
  1158. send_IPI_mask(mask, cfg->vector);
  1159. spin_unlock_irqrestore(&vector_lock, flags);
  1160. return 1;
  1161. }
  1162. /*
  1163. * Level and edge triggered IO-APIC interrupts need different handling,
  1164. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1165. * handled with the level-triggered descriptor, but that one has slightly
  1166. * more overhead. Level-triggered interrupts cannot be handled with the
  1167. * edge-triggered handler, without risking IRQ storms and other ugly
  1168. * races.
  1169. */
  1170. #ifdef CONFIG_SMP
  1171. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1172. {
  1173. unsigned vector, me;
  1174. ack_APIC_irq();
  1175. exit_idle();
  1176. irq_enter();
  1177. me = smp_processor_id();
  1178. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1179. unsigned int irq;
  1180. struct irq_desc *desc;
  1181. struct irq_cfg *cfg;
  1182. irq = __get_cpu_var(vector_irq)[vector];
  1183. if (irq >= NR_IRQS)
  1184. continue;
  1185. desc = irq_desc + irq;
  1186. cfg = irq_cfg + irq;
  1187. spin_lock(&desc->lock);
  1188. if (!cfg->move_cleanup_count)
  1189. goto unlock;
  1190. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  1191. goto unlock;
  1192. __get_cpu_var(vector_irq)[vector] = -1;
  1193. cfg->move_cleanup_count--;
  1194. unlock:
  1195. spin_unlock(&desc->lock);
  1196. }
  1197. irq_exit();
  1198. }
  1199. static void irq_complete_move(unsigned int irq)
  1200. {
  1201. struct irq_cfg *cfg = irq_cfg + irq;
  1202. unsigned vector, me;
  1203. if (likely(!cfg->move_in_progress))
  1204. return;
  1205. vector = ~get_irq_regs()->orig_ax;
  1206. me = smp_processor_id();
  1207. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  1208. cpumask_t cleanup_mask;
  1209. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1210. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1211. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1212. cfg->move_in_progress = 0;
  1213. }
  1214. }
  1215. #else
  1216. static inline void irq_complete_move(unsigned int irq) {}
  1217. #endif
  1218. static void ack_apic_edge(unsigned int irq)
  1219. {
  1220. irq_complete_move(irq);
  1221. move_native_irq(irq);
  1222. ack_APIC_irq();
  1223. }
  1224. static void ack_apic_level(unsigned int irq)
  1225. {
  1226. int do_unmask_irq = 0;
  1227. irq_complete_move(irq);
  1228. #ifdef CONFIG_GENERIC_PENDING_IRQ
  1229. /* If we are moving the irq we need to mask it */
  1230. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  1231. do_unmask_irq = 1;
  1232. mask_IO_APIC_irq(irq);
  1233. }
  1234. #endif
  1235. /*
  1236. * We must acknowledge the irq before we move it or the acknowledge will
  1237. * not propagate properly.
  1238. */
  1239. ack_APIC_irq();
  1240. /* Now we can move and renable the irq */
  1241. if (unlikely(do_unmask_irq)) {
  1242. /* Only migrate the irq if the ack has been received.
  1243. *
  1244. * On rare occasions the broadcast level triggered ack gets
  1245. * delayed going to ioapics, and if we reprogram the
  1246. * vector while Remote IRR is still set the irq will never
  1247. * fire again.
  1248. *
  1249. * To prevent this scenario we read the Remote IRR bit
  1250. * of the ioapic. This has two effects.
  1251. * - On any sane system the read of the ioapic will
  1252. * flush writes (and acks) going to the ioapic from
  1253. * this cpu.
  1254. * - We get to see if the ACK has actually been delivered.
  1255. *
  1256. * Based on failed experiments of reprogramming the
  1257. * ioapic entry from outside of irq context starting
  1258. * with masking the ioapic entry and then polling until
  1259. * Remote IRR was clear before reprogramming the
  1260. * ioapic I don't trust the Remote IRR bit to be
  1261. * completey accurate.
  1262. *
  1263. * However there appears to be no other way to plug
  1264. * this race, so if the Remote IRR bit is not
  1265. * accurate and is causing problems then it is a hardware bug
  1266. * and you can go talk to the chipset vendor about it.
  1267. */
  1268. if (!io_apic_level_ack_pending(irq))
  1269. move_masked_irq(irq);
  1270. unmask_IO_APIC_irq(irq);
  1271. }
  1272. }
  1273. static struct irq_chip ioapic_chip __read_mostly = {
  1274. .name = "IO-APIC",
  1275. .startup = startup_ioapic_irq,
  1276. .mask = mask_IO_APIC_irq,
  1277. .unmask = unmask_IO_APIC_irq,
  1278. .ack = ack_apic_edge,
  1279. .eoi = ack_apic_level,
  1280. #ifdef CONFIG_SMP
  1281. .set_affinity = set_ioapic_affinity_irq,
  1282. #endif
  1283. .retrigger = ioapic_retrigger_irq,
  1284. };
  1285. static inline void init_IO_APIC_traps(void)
  1286. {
  1287. int irq;
  1288. /*
  1289. * NOTE! The local APIC isn't very good at handling
  1290. * multiple interrupts at the same interrupt level.
  1291. * As the interrupt level is determined by taking the
  1292. * vector number and shifting that right by 4, we
  1293. * want to spread these out a bit so that they don't
  1294. * all fall in the same interrupt level.
  1295. *
  1296. * Also, we've got to be careful not to trash gate
  1297. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1298. */
  1299. for (irq = 0; irq < NR_IRQS ; irq++) {
  1300. if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
  1301. /*
  1302. * Hmm.. We don't have an entry for this,
  1303. * so default to an old-fashioned 8259
  1304. * interrupt if we can..
  1305. */
  1306. if (irq < 16)
  1307. make_8259A_irq(irq);
  1308. else
  1309. /* Strange. Oh, well.. */
  1310. irq_desc[irq].chip = &no_irq_chip;
  1311. }
  1312. }
  1313. }
  1314. static void enable_lapic_irq (unsigned int irq)
  1315. {
  1316. unsigned long v;
  1317. v = apic_read(APIC_LVT0);
  1318. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1319. }
  1320. static void disable_lapic_irq (unsigned int irq)
  1321. {
  1322. unsigned long v;
  1323. v = apic_read(APIC_LVT0);
  1324. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1325. }
  1326. static void ack_lapic_irq (unsigned int irq)
  1327. {
  1328. ack_APIC_irq();
  1329. }
  1330. static void end_lapic_irq (unsigned int i) { /* nothing */ }
  1331. static struct hw_interrupt_type lapic_irq_type __read_mostly = {
  1332. .name = "local-APIC",
  1333. .typename = "local-APIC-edge",
  1334. .startup = NULL, /* startup_irq() not used for IRQ0 */
  1335. .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
  1336. .enable = enable_lapic_irq,
  1337. .disable = disable_lapic_irq,
  1338. .ack = ack_lapic_irq,
  1339. .end = end_lapic_irq,
  1340. };
  1341. static void __init setup_nmi(void)
  1342. {
  1343. /*
  1344. * Dirty trick to enable the NMI watchdog ...
  1345. * We put the 8259A master into AEOI mode and
  1346. * unmask on all local APICs LVT0 as NMI.
  1347. *
  1348. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1349. * is from Maciej W. Rozycki - so we do not have to EOI from
  1350. * the NMI handler or the timer interrupt.
  1351. */
  1352. printk(KERN_INFO "activating NMI Watchdog ...");
  1353. enable_NMI_through_LVT0();
  1354. printk(" done.\n");
  1355. }
  1356. /*
  1357. * This looks a bit hackish but it's about the only one way of sending
  1358. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1359. * not support the ExtINT mode, unfortunately. We need to send these
  1360. * cycles as some i82489DX-based boards have glue logic that keeps the
  1361. * 8259A interrupt line asserted until INTA. --macro
  1362. */
  1363. static inline void unlock_ExtINT_logic(void)
  1364. {
  1365. int apic, pin, i;
  1366. struct IO_APIC_route_entry entry0, entry1;
  1367. unsigned char save_control, save_freq_select;
  1368. pin = find_isa_irq_pin(8, mp_INT);
  1369. apic = find_isa_irq_apic(8, mp_INT);
  1370. if (pin == -1)
  1371. return;
  1372. entry0 = ioapic_read_entry(apic, pin);
  1373. clear_IO_APIC_pin(apic, pin);
  1374. memset(&entry1, 0, sizeof(entry1));
  1375. entry1.dest_mode = 0; /* physical delivery */
  1376. entry1.mask = 0; /* unmask IRQ now */
  1377. entry1.dest = hard_smp_processor_id();
  1378. entry1.delivery_mode = dest_ExtINT;
  1379. entry1.polarity = entry0.polarity;
  1380. entry1.trigger = 0;
  1381. entry1.vector = 0;
  1382. ioapic_write_entry(apic, pin, entry1);
  1383. save_control = CMOS_READ(RTC_CONTROL);
  1384. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1385. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1386. RTC_FREQ_SELECT);
  1387. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1388. i = 100;
  1389. while (i-- > 0) {
  1390. mdelay(10);
  1391. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1392. i -= 10;
  1393. }
  1394. CMOS_WRITE(save_control, RTC_CONTROL);
  1395. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1396. clear_IO_APIC_pin(apic, pin);
  1397. ioapic_write_entry(apic, pin, entry0);
  1398. }
  1399. /*
  1400. * This code may look a bit paranoid, but it's supposed to cooperate with
  1401. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1402. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1403. * fanatically on his truly buggy board.
  1404. *
  1405. * FIXME: really need to revamp this for modern platforms only.
  1406. */
  1407. static inline void __init check_timer(void)
  1408. {
  1409. struct irq_cfg *cfg = irq_cfg + 0;
  1410. int apic1, pin1, apic2, pin2;
  1411. unsigned long flags;
  1412. local_irq_save(flags);
  1413. /*
  1414. * get/set the timer IRQ vector:
  1415. */
  1416. disable_8259A_irq(0);
  1417. assign_irq_vector(0, TARGET_CPUS);
  1418. /*
  1419. * Subtle, code in do_timer_interrupt() expects an AEOI
  1420. * mode for the 8259A whenever interrupts are routed
  1421. * through I/O APICs. Also IRQ0 has to be enabled in
  1422. * the 8259A which implies the virtual wire has to be
  1423. * disabled in the local APIC.
  1424. */
  1425. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1426. init_8259A(1);
  1427. if (timer_over_8254 > 0)
  1428. enable_8259A_irq(0);
  1429. pin1 = find_isa_irq_pin(0, mp_INT);
  1430. apic1 = find_isa_irq_apic(0, mp_INT);
  1431. pin2 = ioapic_i8259.pin;
  1432. apic2 = ioapic_i8259.apic;
  1433. apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1434. cfg->vector, apic1, pin1, apic2, pin2);
  1435. if (pin1 != -1) {
  1436. /*
  1437. * Ok, does IRQ0 through the IOAPIC work?
  1438. */
  1439. unmask_IO_APIC_irq(0);
  1440. if (!no_timer_check && timer_irq_works()) {
  1441. nmi_watchdog_default();
  1442. if (nmi_watchdog == NMI_IO_APIC) {
  1443. disable_8259A_irq(0);
  1444. setup_nmi();
  1445. enable_8259A_irq(0);
  1446. }
  1447. if (disable_timer_pin_1 > 0)
  1448. clear_IO_APIC_pin(0, pin1);
  1449. goto out;
  1450. }
  1451. clear_IO_APIC_pin(apic1, pin1);
  1452. apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
  1453. "connected to IO-APIC\n");
  1454. }
  1455. apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
  1456. "through the 8259A ... ");
  1457. if (pin2 != -1) {
  1458. apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
  1459. apic2, pin2);
  1460. /*
  1461. * legacy devices should be connected to IO APIC #0
  1462. */
  1463. setup_ExtINT_IRQ0_pin(apic2, pin2, cfg->vector);
  1464. if (timer_irq_works()) {
  1465. apic_printk(APIC_VERBOSE," works.\n");
  1466. nmi_watchdog_default();
  1467. if (nmi_watchdog == NMI_IO_APIC) {
  1468. setup_nmi();
  1469. }
  1470. goto out;
  1471. }
  1472. /*
  1473. * Cleanup, just in case ...
  1474. */
  1475. clear_IO_APIC_pin(apic2, pin2);
  1476. }
  1477. apic_printk(APIC_VERBOSE," failed.\n");
  1478. if (nmi_watchdog == NMI_IO_APIC) {
  1479. printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
  1480. nmi_watchdog = 0;
  1481. }
  1482. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1483. disable_8259A_irq(0);
  1484. irq_desc[0].chip = &lapic_irq_type;
  1485. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  1486. enable_8259A_irq(0);
  1487. if (timer_irq_works()) {
  1488. apic_printk(APIC_VERBOSE," works.\n");
  1489. goto out;
  1490. }
  1491. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  1492. apic_printk(APIC_VERBOSE," failed.\n");
  1493. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1494. init_8259A(0);
  1495. make_8259A_irq(0);
  1496. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1497. unlock_ExtINT_logic();
  1498. if (timer_irq_works()) {
  1499. apic_printk(APIC_VERBOSE," works.\n");
  1500. goto out;
  1501. }
  1502. apic_printk(APIC_VERBOSE," failed :(.\n");
  1503. panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
  1504. out:
  1505. local_irq_restore(flags);
  1506. }
  1507. static int __init notimercheck(char *s)
  1508. {
  1509. no_timer_check = 1;
  1510. return 1;
  1511. }
  1512. __setup("no_timer_check", notimercheck);
  1513. /*
  1514. *
  1515. * IRQs that are handled by the PIC in the MPS IOAPIC case.
  1516. * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1517. * Linux doesn't really care, as it's not actually used
  1518. * for any interrupt handling anyway.
  1519. */
  1520. #define PIC_IRQS (1<<2)
  1521. void __init setup_IO_APIC(void)
  1522. {
  1523. /*
  1524. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  1525. */
  1526. if (acpi_ioapic)
  1527. io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
  1528. else
  1529. io_apic_irqs = ~PIC_IRQS;
  1530. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1531. sync_Arb_IDs();
  1532. setup_IO_APIC_irqs();
  1533. init_IO_APIC_traps();
  1534. check_timer();
  1535. if (!acpi_ioapic)
  1536. print_IO_APIC();
  1537. }
  1538. struct sysfs_ioapic_data {
  1539. struct sys_device dev;
  1540. struct IO_APIC_route_entry entry[0];
  1541. };
  1542. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1543. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1544. {
  1545. struct IO_APIC_route_entry *entry;
  1546. struct sysfs_ioapic_data *data;
  1547. int i;
  1548. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1549. entry = data->entry;
  1550. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  1551. *entry = ioapic_read_entry(dev->id, i);
  1552. return 0;
  1553. }
  1554. static int ioapic_resume(struct sys_device *dev)
  1555. {
  1556. struct IO_APIC_route_entry *entry;
  1557. struct sysfs_ioapic_data *data;
  1558. unsigned long flags;
  1559. union IO_APIC_reg_00 reg_00;
  1560. int i;
  1561. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1562. entry = data->entry;
  1563. spin_lock_irqsave(&ioapic_lock, flags);
  1564. reg_00.raw = io_apic_read(dev->id, 0);
  1565. if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
  1566. reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
  1567. io_apic_write(dev->id, 0, reg_00.raw);
  1568. }
  1569. spin_unlock_irqrestore(&ioapic_lock, flags);
  1570. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1571. ioapic_write_entry(dev->id, i, entry[i]);
  1572. return 0;
  1573. }
  1574. static struct sysdev_class ioapic_sysdev_class = {
  1575. .name = "ioapic",
  1576. .suspend = ioapic_suspend,
  1577. .resume = ioapic_resume,
  1578. };
  1579. static int __init ioapic_init_sysfs(void)
  1580. {
  1581. struct sys_device * dev;
  1582. int i, size, error;
  1583. error = sysdev_class_register(&ioapic_sysdev_class);
  1584. if (error)
  1585. return error;
  1586. for (i = 0; i < nr_ioapics; i++ ) {
  1587. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1588. * sizeof(struct IO_APIC_route_entry);
  1589. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  1590. if (!mp_ioapic_data[i]) {
  1591. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1592. continue;
  1593. }
  1594. dev = &mp_ioapic_data[i]->dev;
  1595. dev->id = i;
  1596. dev->cls = &ioapic_sysdev_class;
  1597. error = sysdev_register(dev);
  1598. if (error) {
  1599. kfree(mp_ioapic_data[i]);
  1600. mp_ioapic_data[i] = NULL;
  1601. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1602. continue;
  1603. }
  1604. }
  1605. return 0;
  1606. }
  1607. device_initcall(ioapic_init_sysfs);
  1608. /*
  1609. * Dynamic irq allocate and deallocation
  1610. */
  1611. int create_irq(void)
  1612. {
  1613. /* Allocate an unused irq */
  1614. int irq;
  1615. int new;
  1616. unsigned long flags;
  1617. irq = -ENOSPC;
  1618. spin_lock_irqsave(&vector_lock, flags);
  1619. for (new = (NR_IRQS - 1); new >= 0; new--) {
  1620. if (platform_legacy_irq(new))
  1621. continue;
  1622. if (irq_cfg[new].vector != 0)
  1623. continue;
  1624. if (__assign_irq_vector(new, TARGET_CPUS) == 0)
  1625. irq = new;
  1626. break;
  1627. }
  1628. spin_unlock_irqrestore(&vector_lock, flags);
  1629. if (irq >= 0) {
  1630. dynamic_irq_init(irq);
  1631. }
  1632. return irq;
  1633. }
  1634. void destroy_irq(unsigned int irq)
  1635. {
  1636. unsigned long flags;
  1637. dynamic_irq_cleanup(irq);
  1638. spin_lock_irqsave(&vector_lock, flags);
  1639. __clear_irq_vector(irq);
  1640. spin_unlock_irqrestore(&vector_lock, flags);
  1641. }
  1642. /*
  1643. * MSI message composition
  1644. */
  1645. #ifdef CONFIG_PCI_MSI
  1646. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1647. {
  1648. struct irq_cfg *cfg = irq_cfg + irq;
  1649. int err;
  1650. unsigned dest;
  1651. cpumask_t tmp;
  1652. tmp = TARGET_CPUS;
  1653. err = assign_irq_vector(irq, tmp);
  1654. if (!err) {
  1655. cpus_and(tmp, cfg->domain, tmp);
  1656. dest = cpu_mask_to_apicid(tmp);
  1657. msg->address_hi = MSI_ADDR_BASE_HI;
  1658. msg->address_lo =
  1659. MSI_ADDR_BASE_LO |
  1660. ((INT_DEST_MODE == 0) ?
  1661. MSI_ADDR_DEST_MODE_PHYSICAL:
  1662. MSI_ADDR_DEST_MODE_LOGICAL) |
  1663. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1664. MSI_ADDR_REDIRECTION_CPU:
  1665. MSI_ADDR_REDIRECTION_LOWPRI) |
  1666. MSI_ADDR_DEST_ID(dest);
  1667. msg->data =
  1668. MSI_DATA_TRIGGER_EDGE |
  1669. MSI_DATA_LEVEL_ASSERT |
  1670. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1671. MSI_DATA_DELIVERY_FIXED:
  1672. MSI_DATA_DELIVERY_LOWPRI) |
  1673. MSI_DATA_VECTOR(cfg->vector);
  1674. }
  1675. return err;
  1676. }
  1677. #ifdef CONFIG_SMP
  1678. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  1679. {
  1680. struct irq_cfg *cfg = irq_cfg + irq;
  1681. struct msi_msg msg;
  1682. unsigned int dest;
  1683. cpumask_t tmp;
  1684. cpus_and(tmp, mask, cpu_online_map);
  1685. if (cpus_empty(tmp))
  1686. return;
  1687. if (assign_irq_vector(irq, mask))
  1688. return;
  1689. cpus_and(tmp, cfg->domain, mask);
  1690. dest = cpu_mask_to_apicid(tmp);
  1691. read_msi_msg(irq, &msg);
  1692. msg.data &= ~MSI_DATA_VECTOR_MASK;
  1693. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  1694. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  1695. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  1696. write_msi_msg(irq, &msg);
  1697. irq_desc[irq].affinity = mask;
  1698. }
  1699. #endif /* CONFIG_SMP */
  1700. /*
  1701. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  1702. * which implement the MSI or MSI-X Capability Structure.
  1703. */
  1704. static struct irq_chip msi_chip = {
  1705. .name = "PCI-MSI",
  1706. .unmask = unmask_msi_irq,
  1707. .mask = mask_msi_irq,
  1708. .ack = ack_apic_edge,
  1709. #ifdef CONFIG_SMP
  1710. .set_affinity = set_msi_irq_affinity,
  1711. #endif
  1712. .retrigger = ioapic_retrigger_irq,
  1713. };
  1714. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  1715. {
  1716. struct msi_msg msg;
  1717. int irq, ret;
  1718. irq = create_irq();
  1719. if (irq < 0)
  1720. return irq;
  1721. ret = msi_compose_msg(dev, irq, &msg);
  1722. if (ret < 0) {
  1723. destroy_irq(irq);
  1724. return ret;
  1725. }
  1726. set_irq_msi(irq, desc);
  1727. write_msi_msg(irq, &msg);
  1728. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  1729. return 0;
  1730. }
  1731. void arch_teardown_msi_irq(unsigned int irq)
  1732. {
  1733. destroy_irq(irq);
  1734. }
  1735. #ifdef CONFIG_DMAR
  1736. #ifdef CONFIG_SMP
  1737. static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
  1738. {
  1739. struct irq_cfg *cfg = irq_cfg + irq;
  1740. struct msi_msg msg;
  1741. unsigned int dest;
  1742. cpumask_t tmp;
  1743. cpus_and(tmp, mask, cpu_online_map);
  1744. if (cpus_empty(tmp))
  1745. return;
  1746. if (assign_irq_vector(irq, mask))
  1747. return;
  1748. cpus_and(tmp, cfg->domain, mask);
  1749. dest = cpu_mask_to_apicid(tmp);
  1750. dmar_msi_read(irq, &msg);
  1751. msg.data &= ~MSI_DATA_VECTOR_MASK;
  1752. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  1753. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  1754. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  1755. dmar_msi_write(irq, &msg);
  1756. irq_desc[irq].affinity = mask;
  1757. }
  1758. #endif /* CONFIG_SMP */
  1759. struct irq_chip dmar_msi_type = {
  1760. .name = "DMAR_MSI",
  1761. .unmask = dmar_msi_unmask,
  1762. .mask = dmar_msi_mask,
  1763. .ack = ack_apic_edge,
  1764. #ifdef CONFIG_SMP
  1765. .set_affinity = dmar_msi_set_affinity,
  1766. #endif
  1767. .retrigger = ioapic_retrigger_irq,
  1768. };
  1769. int arch_setup_dmar_msi(unsigned int irq)
  1770. {
  1771. int ret;
  1772. struct msi_msg msg;
  1773. ret = msi_compose_msg(NULL, irq, &msg);
  1774. if (ret < 0)
  1775. return ret;
  1776. dmar_msi_write(irq, &msg);
  1777. set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  1778. "edge");
  1779. return 0;
  1780. }
  1781. #endif
  1782. #endif /* CONFIG_PCI_MSI */
  1783. /*
  1784. * Hypertransport interrupt support
  1785. */
  1786. #ifdef CONFIG_HT_IRQ
  1787. #ifdef CONFIG_SMP
  1788. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  1789. {
  1790. struct ht_irq_msg msg;
  1791. fetch_ht_irq_msg(irq, &msg);
  1792. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  1793. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  1794. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  1795. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  1796. write_ht_irq_msg(irq, &msg);
  1797. }
  1798. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  1799. {
  1800. struct irq_cfg *cfg = irq_cfg + irq;
  1801. unsigned int dest;
  1802. cpumask_t tmp;
  1803. cpus_and(tmp, mask, cpu_online_map);
  1804. if (cpus_empty(tmp))
  1805. return;
  1806. if (assign_irq_vector(irq, mask))
  1807. return;
  1808. cpus_and(tmp, cfg->domain, mask);
  1809. dest = cpu_mask_to_apicid(tmp);
  1810. target_ht_irq(irq, dest, cfg->vector);
  1811. irq_desc[irq].affinity = mask;
  1812. }
  1813. #endif
  1814. static struct irq_chip ht_irq_chip = {
  1815. .name = "PCI-HT",
  1816. .mask = mask_ht_irq,
  1817. .unmask = unmask_ht_irq,
  1818. .ack = ack_apic_edge,
  1819. #ifdef CONFIG_SMP
  1820. .set_affinity = set_ht_irq_affinity,
  1821. #endif
  1822. .retrigger = ioapic_retrigger_irq,
  1823. };
  1824. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  1825. {
  1826. struct irq_cfg *cfg = irq_cfg + irq;
  1827. int err;
  1828. cpumask_t tmp;
  1829. tmp = TARGET_CPUS;
  1830. err = assign_irq_vector(irq, tmp);
  1831. if (!err) {
  1832. struct ht_irq_msg msg;
  1833. unsigned dest;
  1834. cpus_and(tmp, cfg->domain, tmp);
  1835. dest = cpu_mask_to_apicid(tmp);
  1836. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  1837. msg.address_lo =
  1838. HT_IRQ_LOW_BASE |
  1839. HT_IRQ_LOW_DEST_ID(dest) |
  1840. HT_IRQ_LOW_VECTOR(cfg->vector) |
  1841. ((INT_DEST_MODE == 0) ?
  1842. HT_IRQ_LOW_DM_PHYSICAL :
  1843. HT_IRQ_LOW_DM_LOGICAL) |
  1844. HT_IRQ_LOW_RQEOI_EDGE |
  1845. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1846. HT_IRQ_LOW_MT_FIXED :
  1847. HT_IRQ_LOW_MT_ARBITRATED) |
  1848. HT_IRQ_LOW_IRQ_MASKED;
  1849. write_ht_irq_msg(irq, &msg);
  1850. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  1851. handle_edge_irq, "edge");
  1852. }
  1853. return err;
  1854. }
  1855. #endif /* CONFIG_HT_IRQ */
  1856. /* --------------------------------------------------------------------------
  1857. ACPI-based IOAPIC Configuration
  1858. -------------------------------------------------------------------------- */
  1859. #ifdef CONFIG_ACPI
  1860. #define IO_APIC_MAX_ID 0xFE
  1861. int __init io_apic_get_redir_entries (int ioapic)
  1862. {
  1863. union IO_APIC_reg_01 reg_01;
  1864. unsigned long flags;
  1865. spin_lock_irqsave(&ioapic_lock, flags);
  1866. reg_01.raw = io_apic_read(ioapic, 1);
  1867. spin_unlock_irqrestore(&ioapic_lock, flags);
  1868. return reg_01.bits.entries;
  1869. }
  1870. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  1871. {
  1872. if (!IO_APIC_IRQ(irq)) {
  1873. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  1874. ioapic);
  1875. return -EINVAL;
  1876. }
  1877. /*
  1878. * IRQs < 16 are already in the irq_2_pin[] map
  1879. */
  1880. if (irq >= 16)
  1881. add_pin_to_irq(irq, ioapic, pin);
  1882. setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
  1883. return 0;
  1884. }
  1885. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  1886. {
  1887. int i;
  1888. if (skip_ioapic_setup)
  1889. return -1;
  1890. for (i = 0; i < mp_irq_entries; i++)
  1891. if (mp_irqs[i].mpc_irqtype == mp_INT &&
  1892. mp_irqs[i].mpc_srcbusirq == bus_irq)
  1893. break;
  1894. if (i >= mp_irq_entries)
  1895. return -1;
  1896. *trigger = irq_trigger(i);
  1897. *polarity = irq_polarity(i);
  1898. return 0;
  1899. }
  1900. #endif /* CONFIG_ACPI */
  1901. /*
  1902. * This function currently is only a helper for the i386 smp boot process where
  1903. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  1904. * so mask in all cases should simply be TARGET_CPUS
  1905. */
  1906. #ifdef CONFIG_SMP
  1907. void __init setup_ioapic_dest(void)
  1908. {
  1909. int pin, ioapic, irq, irq_entry;
  1910. if (skip_ioapic_setup == 1)
  1911. return;
  1912. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  1913. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  1914. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  1915. if (irq_entry == -1)
  1916. continue;
  1917. irq = pin_2_irq(irq_entry, ioapic, pin);
  1918. /* setup_IO_APIC_irqs could fail to get vector for some device
  1919. * when you have too many devices, because at that time only boot
  1920. * cpu is online.
  1921. */
  1922. if (!irq_cfg[irq].vector)
  1923. setup_IO_APIC_irq(ioapic, pin, irq,
  1924. irq_trigger(irq_entry),
  1925. irq_polarity(irq_entry));
  1926. else
  1927. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  1928. }
  1929. }
  1930. }
  1931. #endif
  1932. #define IOAPIC_RESOURCE_NAME_SIZE 11
  1933. static struct resource *ioapic_resources;
  1934. static struct resource * __init ioapic_setup_resources(void)
  1935. {
  1936. unsigned long n;
  1937. struct resource *res;
  1938. char *mem;
  1939. int i;
  1940. if (nr_ioapics <= 0)
  1941. return NULL;
  1942. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  1943. n *= nr_ioapics;
  1944. mem = alloc_bootmem(n);
  1945. res = (void *)mem;
  1946. if (mem != NULL) {
  1947. mem += sizeof(struct resource) * nr_ioapics;
  1948. for (i = 0; i < nr_ioapics; i++) {
  1949. res[i].name = mem;
  1950. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  1951. sprintf(mem, "IOAPIC %u", i);
  1952. mem += IOAPIC_RESOURCE_NAME_SIZE;
  1953. }
  1954. }
  1955. ioapic_resources = res;
  1956. return res;
  1957. }
  1958. void __init ioapic_init_mappings(void)
  1959. {
  1960. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  1961. struct resource *ioapic_res;
  1962. int i;
  1963. ioapic_res = ioapic_setup_resources();
  1964. for (i = 0; i < nr_ioapics; i++) {
  1965. if (smp_found_config) {
  1966. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  1967. } else {
  1968. ioapic_phys = (unsigned long)
  1969. alloc_bootmem_pages(PAGE_SIZE);
  1970. ioapic_phys = __pa(ioapic_phys);
  1971. }
  1972. set_fixmap_nocache(idx, ioapic_phys);
  1973. apic_printk(APIC_VERBOSE,
  1974. "mapped IOAPIC to %016lx (%016lx)\n",
  1975. __fix_to_virt(idx), ioapic_phys);
  1976. idx++;
  1977. if (ioapic_res != NULL) {
  1978. ioapic_res->start = ioapic_phys;
  1979. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  1980. ioapic_res++;
  1981. }
  1982. }
  1983. }
  1984. static int __init ioapic_insert_resources(void)
  1985. {
  1986. int i;
  1987. struct resource *r = ioapic_resources;
  1988. if (!r) {
  1989. printk(KERN_ERR
  1990. "IO APIC resources could be not be allocated.\n");
  1991. return -1;
  1992. }
  1993. for (i = 0; i < nr_ioapics; i++) {
  1994. insert_resource(&iomem_resource, r);
  1995. r++;
  1996. }
  1997. return 0;
  1998. }
  1999. /* Insert the IO APIC resources after PCI initialization has occured to handle
  2000. * IO APICS that are mapped in on a BAR in PCI space. */
  2001. late_initcall(ioapic_insert_resources);