io_apic_64.c 57 KB

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