io_apic_64.c 58 KB

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