io_apic.c 54 KB

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