io_apic_64.c 58 KB

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