io_apic_32.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  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/bootmem.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <linux/compiler.h>
  30. #include <linux/acpi.h>
  31. #include <linux/module.h>
  32. #include <linux/sysdev.h>
  33. #include <linux/pci.h>
  34. #include <linux/msi.h>
  35. #include <linux/htirq.h>
  36. #include <linux/freezer.h>
  37. #include <linux/kthread.h>
  38. #include <linux/jiffies.h> /* time_after() */
  39. #include <asm/io.h>
  40. #include <asm/smp.h>
  41. #include <asm/desc.h>
  42. #include <asm/timer.h>
  43. #include <asm/i8259.h>
  44. #include <asm/nmi.h>
  45. #include <asm/msidef.h>
  46. #include <asm/hypertransport.h>
  47. #include <asm/setup.h>
  48. #include <mach_apic.h>
  49. #include <mach_apicdef.h>
  50. #define __apicdebuginit(type) static type __init
  51. int (*ioapic_renumber_irq)(int ioapic, int irq);
  52. atomic_t irq_mis_count;
  53. /* Where if anywhere is the i8259 connect in external int mode */
  54. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  55. static DEFINE_SPINLOCK(ioapic_lock);
  56. DEFINE_SPINLOCK(vector_lock);
  57. int timer_through_8259 __initdata;
  58. /*
  59. * Is the SiS APIC rmw bug present ?
  60. * -1 = don't know, 0 = no, 1 = yes
  61. */
  62. int sis_apic_bug = -1;
  63. int first_free_entry;
  64. /*
  65. * # of IRQ routing registers
  66. */
  67. int nr_ioapic_registers[MAX_IO_APICS];
  68. /* I/O APIC entries */
  69. struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
  70. int nr_ioapics;
  71. /* MP IRQ source entries */
  72. struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  73. /* # of MP IRQ source entries */
  74. int mp_irq_entries;
  75. #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
  76. int mp_bus_id_to_type[MAX_MP_BUSSES];
  77. #endif
  78. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  79. static int disable_timer_pin_1 __initdata;
  80. /*
  81. * Rough estimation of how many shared IRQs there are, can
  82. * be changed anytime.
  83. */
  84. int pin_map_size;
  85. /*
  86. * This is performance-critical, we want to do it O(1)
  87. *
  88. * the indexing order of this array favors 1:1 mappings
  89. * between pins and IRQs.
  90. */
  91. static struct irq_pin_list {
  92. int apic, pin, next;
  93. } *irq_2_pin;
  94. DEFINE_DYN_ARRAY(irq_2_pin, sizeof(struct irq_pin_list), pin_map_size, 16, NULL);
  95. struct io_apic {
  96. unsigned int index;
  97. unsigned int unused[3];
  98. unsigned int data;
  99. };
  100. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  101. {
  102. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  103. + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
  104. }
  105. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  106. {
  107. struct io_apic __iomem *io_apic = io_apic_base(apic);
  108. writel(reg, &io_apic->index);
  109. return readl(&io_apic->data);
  110. }
  111. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  112. {
  113. struct io_apic __iomem *io_apic = io_apic_base(apic);
  114. writel(reg, &io_apic->index);
  115. writel(value, &io_apic->data);
  116. }
  117. /*
  118. * Re-write a value: to be used for read-modify-write
  119. * cycles where the read already set up the index register.
  120. *
  121. * Older SiS APIC requires we rewrite the index register
  122. */
  123. static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
  124. {
  125. volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
  126. if (sis_apic_bug)
  127. writel(reg, &io_apic->index);
  128. writel(value, &io_apic->data);
  129. }
  130. union entry_union {
  131. struct { u32 w1, w2; };
  132. struct IO_APIC_route_entry entry;
  133. };
  134. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  135. {
  136. union entry_union eu;
  137. unsigned long flags;
  138. spin_lock_irqsave(&ioapic_lock, flags);
  139. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  140. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  141. spin_unlock_irqrestore(&ioapic_lock, flags);
  142. return eu.entry;
  143. }
  144. /*
  145. * When we write a new IO APIC routing entry, we need to write the high
  146. * word first! If the mask bit in the low word is clear, we will enable
  147. * the interrupt, and we need to make sure the entry is fully populated
  148. * before that happens.
  149. */
  150. static void
  151. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  152. {
  153. union entry_union eu;
  154. eu.entry = e;
  155. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  156. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  157. }
  158. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  159. {
  160. unsigned long flags;
  161. spin_lock_irqsave(&ioapic_lock, flags);
  162. __ioapic_write_entry(apic, pin, e);
  163. spin_unlock_irqrestore(&ioapic_lock, flags);
  164. }
  165. /*
  166. * When we mask an IO APIC routing entry, we need to write the low
  167. * word first, in order to set the mask bit before we change the
  168. * high bits!
  169. */
  170. static void ioapic_mask_entry(int apic, int pin)
  171. {
  172. unsigned long flags;
  173. union entry_union eu = { .entry.mask = 1 };
  174. spin_lock_irqsave(&ioapic_lock, flags);
  175. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  176. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  177. spin_unlock_irqrestore(&ioapic_lock, flags);
  178. }
  179. /*
  180. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  181. * shared ISA-space IRQs, so we have to support them. We are super
  182. * fast in the common case, and fast for shared ISA-space IRQs.
  183. */
  184. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  185. {
  186. struct irq_pin_list *entry = irq_2_pin + irq;
  187. while (entry->next)
  188. entry = irq_2_pin + entry->next;
  189. if (entry->pin != -1) {
  190. entry->next = first_free_entry;
  191. entry = irq_2_pin + entry->next;
  192. if (++first_free_entry >= pin_map_size)
  193. panic("io_apic.c: whoops");
  194. }
  195. entry->apic = apic;
  196. entry->pin = pin;
  197. }
  198. /*
  199. * Reroute an IRQ to a different pin.
  200. */
  201. static void __init replace_pin_at_irq(unsigned int irq,
  202. int oldapic, int oldpin,
  203. int newapic, int newpin)
  204. {
  205. struct irq_pin_list *entry = irq_2_pin + irq;
  206. while (1) {
  207. if (entry->apic == oldapic && entry->pin == oldpin) {
  208. entry->apic = newapic;
  209. entry->pin = newpin;
  210. }
  211. if (!entry->next)
  212. break;
  213. entry = irq_2_pin + entry->next;
  214. }
  215. }
  216. static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
  217. {
  218. struct irq_pin_list *entry = irq_2_pin + irq;
  219. unsigned int pin, reg;
  220. for (;;) {
  221. pin = entry->pin;
  222. if (pin == -1)
  223. break;
  224. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  225. reg &= ~disable;
  226. reg |= enable;
  227. io_apic_modify(entry->apic, 0x10 + pin*2, reg);
  228. if (!entry->next)
  229. break;
  230. entry = irq_2_pin + entry->next;
  231. }
  232. }
  233. /* mask = 1 */
  234. static void __mask_IO_APIC_irq(unsigned int irq)
  235. {
  236. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
  237. }
  238. /* mask = 0 */
  239. static void __unmask_IO_APIC_irq(unsigned int irq)
  240. {
  241. __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
  242. }
  243. /* mask = 1, trigger = 0 */
  244. static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
  245. {
  246. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
  247. IO_APIC_REDIR_LEVEL_TRIGGER);
  248. }
  249. /* mask = 0, trigger = 1 */
  250. static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
  251. {
  252. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
  253. IO_APIC_REDIR_MASKED);
  254. }
  255. static void mask_IO_APIC_irq(unsigned int irq)
  256. {
  257. unsigned long flags;
  258. spin_lock_irqsave(&ioapic_lock, flags);
  259. __mask_IO_APIC_irq(irq);
  260. spin_unlock_irqrestore(&ioapic_lock, flags);
  261. }
  262. static void unmask_IO_APIC_irq(unsigned int irq)
  263. {
  264. unsigned long flags;
  265. spin_lock_irqsave(&ioapic_lock, flags);
  266. __unmask_IO_APIC_irq(irq);
  267. spin_unlock_irqrestore(&ioapic_lock, flags);
  268. }
  269. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  270. {
  271. struct IO_APIC_route_entry entry;
  272. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  273. entry = ioapic_read_entry(apic, pin);
  274. if (entry.delivery_mode == dest_SMI)
  275. return;
  276. /*
  277. * Disable it in the IO-APIC irq-routing table:
  278. */
  279. ioapic_mask_entry(apic, pin);
  280. }
  281. static void clear_IO_APIC(void)
  282. {
  283. int apic, pin;
  284. for (apic = 0; apic < nr_ioapics; apic++)
  285. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  286. clear_IO_APIC_pin(apic, pin);
  287. }
  288. #ifdef CONFIG_SMP
  289. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
  290. {
  291. unsigned long flags;
  292. int pin;
  293. struct irq_pin_list *entry = irq_2_pin + irq;
  294. unsigned int apicid_value;
  295. cpumask_t tmp;
  296. cpus_and(tmp, cpumask, cpu_online_map);
  297. if (cpus_empty(tmp))
  298. tmp = TARGET_CPUS;
  299. cpus_and(cpumask, tmp, CPU_MASK_ALL);
  300. apicid_value = cpu_mask_to_apicid(cpumask);
  301. /* Prepare to do the io_apic_write */
  302. apicid_value = apicid_value << 24;
  303. spin_lock_irqsave(&ioapic_lock, flags);
  304. for (;;) {
  305. pin = entry->pin;
  306. if (pin == -1)
  307. break;
  308. io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
  309. if (!entry->next)
  310. break;
  311. entry = irq_2_pin + entry->next;
  312. }
  313. irq_desc[irq].affinity = cpumask;
  314. spin_unlock_irqrestore(&ioapic_lock, flags);
  315. }
  316. #if defined(CONFIG_IRQBALANCE)
  317. # include <asm/processor.h> /* kernel_thread() */
  318. # include <linux/kernel_stat.h> /* kstat */
  319. # include <linux/slab.h> /* kmalloc() */
  320. # include <linux/timer.h>
  321. #define IRQBALANCE_CHECK_ARCH -999
  322. #define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
  323. #define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
  324. #define BALANCED_IRQ_MORE_DELTA (HZ/10)
  325. #define BALANCED_IRQ_LESS_DELTA (HZ)
  326. static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
  327. static int physical_balance __read_mostly;
  328. static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
  329. static struct irq_cpu_info {
  330. unsigned long *last_irq;
  331. unsigned long *irq_delta;
  332. unsigned long irq;
  333. } irq_cpu_data[NR_CPUS];
  334. #define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
  335. #define LAST_CPU_IRQ(cpu, irq) (irq_cpu_data[cpu].last_irq[irq])
  336. #define IRQ_DELTA(cpu, irq) (irq_cpu_data[cpu].irq_delta[irq])
  337. #define IDLE_ENOUGH(cpu,now) \
  338. (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
  339. #define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
  340. #define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
  341. static cpumask_t balance_irq_affinity_init __initdata = CPU_MASK_ALL;
  342. static cpumask_t *balance_irq_affinity;
  343. static void __init irq_affinity_init_work(void *data)
  344. {
  345. struct dyn_array *da = data;
  346. int i;
  347. struct balance_irq_affinity *affinity;
  348. affinity = *da->name;
  349. for (i = 0; i < *da->nr; i++)
  350. memcpy(&affinity[i], &balance_irq_affinity_init,
  351. sizeof(struct balance_irq_affinity));
  352. }
  353. DEFINE_DYN_ARRAY(balance_irq_affinity, sizeof(struct balance_irq_affinity), nr_irqs, PAGE_SIZE, irq_affinity_init_work);
  354. void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
  355. {
  356. balance_irq_affinity[irq] = mask;
  357. }
  358. static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
  359. unsigned long now, int direction)
  360. {
  361. int search_idle = 1;
  362. int cpu = curr_cpu;
  363. goto inside;
  364. do {
  365. if (unlikely(cpu == curr_cpu))
  366. search_idle = 0;
  367. inside:
  368. if (direction == 1) {
  369. cpu++;
  370. if (cpu >= NR_CPUS)
  371. cpu = 0;
  372. } else {
  373. cpu--;
  374. if (cpu == -1)
  375. cpu = NR_CPUS-1;
  376. }
  377. } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu, allowed_mask) ||
  378. (search_idle && !IDLE_ENOUGH(cpu, now)));
  379. return cpu;
  380. }
  381. static inline void balance_irq(int cpu, int irq)
  382. {
  383. unsigned long now = jiffies;
  384. cpumask_t allowed_mask;
  385. unsigned int new_cpu;
  386. if (irqbalance_disabled)
  387. return;
  388. cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
  389. new_cpu = move(cpu, allowed_mask, now, 1);
  390. if (cpu != new_cpu)
  391. set_pending_irq(irq, cpumask_of_cpu(new_cpu));
  392. }
  393. static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
  394. {
  395. int i, j;
  396. for_each_online_cpu(i) {
  397. for (j = 0; j < nr_irqs; j++) {
  398. if (!irq_desc[j].action)
  399. continue;
  400. /* Is it a significant load ? */
  401. if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i), j) <
  402. useful_load_threshold)
  403. continue;
  404. balance_irq(i, j);
  405. }
  406. }
  407. balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
  408. balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
  409. return;
  410. }
  411. static void do_irq_balance(void)
  412. {
  413. int i, j;
  414. unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
  415. unsigned long move_this_load = 0;
  416. int max_loaded = 0, min_loaded = 0;
  417. int load;
  418. unsigned long useful_load_threshold = balanced_irq_interval + 10;
  419. int selected_irq;
  420. int tmp_loaded, first_attempt = 1;
  421. unsigned long tmp_cpu_irq;
  422. unsigned long imbalance = 0;
  423. cpumask_t allowed_mask, target_cpu_mask, tmp;
  424. for_each_possible_cpu(i) {
  425. int package_index;
  426. CPU_IRQ(i) = 0;
  427. if (!cpu_online(i))
  428. continue;
  429. package_index = CPU_TO_PACKAGEINDEX(i);
  430. for (j = 0; j < nr_irqs; j++) {
  431. unsigned long value_now, delta;
  432. /* Is this an active IRQ or balancing disabled ? */
  433. if (!irq_desc[j].action || irq_balancing_disabled(j))
  434. continue;
  435. if (package_index == i)
  436. IRQ_DELTA(package_index, j) = 0;
  437. /* Determine the total count per processor per IRQ */
  438. value_now = (unsigned long) kstat_cpu(i).irqs[j];
  439. /* Determine the activity per processor per IRQ */
  440. delta = value_now - LAST_CPU_IRQ(i, j);
  441. /* Update last_cpu_irq[][] for the next time */
  442. LAST_CPU_IRQ(i, j) = value_now;
  443. /* Ignore IRQs whose rate is less than the clock */
  444. if (delta < useful_load_threshold)
  445. continue;
  446. /* update the load for the processor or package total */
  447. IRQ_DELTA(package_index, j) += delta;
  448. /* Keep track of the higher numbered sibling as well */
  449. if (i != package_index)
  450. CPU_IRQ(i) += delta;
  451. /*
  452. * We have sibling A and sibling B in the package
  453. *
  454. * cpu_irq[A] = load for cpu A + load for cpu B
  455. * cpu_irq[B] = load for cpu B
  456. */
  457. CPU_IRQ(package_index) += delta;
  458. }
  459. }
  460. /* Find the least loaded processor package */
  461. for_each_online_cpu(i) {
  462. if (i != CPU_TO_PACKAGEINDEX(i))
  463. continue;
  464. if (min_cpu_irq > CPU_IRQ(i)) {
  465. min_cpu_irq = CPU_IRQ(i);
  466. min_loaded = i;
  467. }
  468. }
  469. max_cpu_irq = ULONG_MAX;
  470. tryanothercpu:
  471. /*
  472. * Look for heaviest loaded processor.
  473. * We may come back to get the next heaviest loaded processor.
  474. * Skip processors with trivial loads.
  475. */
  476. tmp_cpu_irq = 0;
  477. tmp_loaded = -1;
  478. for_each_online_cpu(i) {
  479. if (i != CPU_TO_PACKAGEINDEX(i))
  480. continue;
  481. if (max_cpu_irq <= CPU_IRQ(i))
  482. continue;
  483. if (tmp_cpu_irq < CPU_IRQ(i)) {
  484. tmp_cpu_irq = CPU_IRQ(i);
  485. tmp_loaded = i;
  486. }
  487. }
  488. if (tmp_loaded == -1) {
  489. /*
  490. * In the case of small number of heavy interrupt sources,
  491. * loading some of the cpus too much. We use Ingo's original
  492. * approach to rotate them around.
  493. */
  494. if (!first_attempt && imbalance >= useful_load_threshold) {
  495. rotate_irqs_among_cpus(useful_load_threshold);
  496. return;
  497. }
  498. goto not_worth_the_effort;
  499. }
  500. first_attempt = 0; /* heaviest search */
  501. max_cpu_irq = tmp_cpu_irq; /* load */
  502. max_loaded = tmp_loaded; /* processor */
  503. imbalance = (max_cpu_irq - min_cpu_irq) / 2;
  504. /*
  505. * if imbalance is less than approx 10% of max load, then
  506. * observe diminishing returns action. - quit
  507. */
  508. if (imbalance < (max_cpu_irq >> 3))
  509. goto not_worth_the_effort;
  510. tryanotherirq:
  511. /* if we select an IRQ to move that can't go where we want, then
  512. * see if there is another one to try.
  513. */
  514. move_this_load = 0;
  515. selected_irq = -1;
  516. for (j = 0; j < nr_irqs; j++) {
  517. /* Is this an active IRQ? */
  518. if (!irq_desc[j].action)
  519. continue;
  520. if (imbalance <= IRQ_DELTA(max_loaded, j))
  521. continue;
  522. /* Try to find the IRQ that is closest to the imbalance
  523. * without going over.
  524. */
  525. if (move_this_load < IRQ_DELTA(max_loaded, j)) {
  526. move_this_load = IRQ_DELTA(max_loaded, j);
  527. selected_irq = j;
  528. }
  529. }
  530. if (selected_irq == -1)
  531. goto tryanothercpu;
  532. imbalance = move_this_load;
  533. /* For physical_balance case, we accumulated both load
  534. * values in the one of the siblings cpu_irq[],
  535. * to use the same code for physical and logical processors
  536. * as much as possible.
  537. *
  538. * NOTE: the cpu_irq[] array holds the sum of the load for
  539. * sibling A and sibling B in the slot for the lowest numbered
  540. * sibling (A), _AND_ the load for sibling B in the slot for
  541. * the higher numbered sibling.
  542. *
  543. * We seek the least loaded sibling by making the comparison
  544. * (A+B)/2 vs B
  545. */
  546. load = CPU_IRQ(min_loaded) >> 1;
  547. for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
  548. if (load > CPU_IRQ(j)) {
  549. /* This won't change cpu_sibling_map[min_loaded] */
  550. load = CPU_IRQ(j);
  551. min_loaded = j;
  552. }
  553. }
  554. cpus_and(allowed_mask,
  555. cpu_online_map,
  556. balance_irq_affinity[selected_irq]);
  557. target_cpu_mask = cpumask_of_cpu(min_loaded);
  558. cpus_and(tmp, target_cpu_mask, allowed_mask);
  559. if (!cpus_empty(tmp)) {
  560. /* mark for change destination */
  561. set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
  562. /* Since we made a change, come back sooner to
  563. * check for more variation.
  564. */
  565. balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
  566. balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
  567. return;
  568. }
  569. goto tryanotherirq;
  570. not_worth_the_effort:
  571. /*
  572. * if we did not find an IRQ to move, then adjust the time interval
  573. * upward
  574. */
  575. balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
  576. balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
  577. return;
  578. }
  579. static int balanced_irq(void *unused)
  580. {
  581. int i;
  582. unsigned long prev_balance_time = jiffies;
  583. long time_remaining = balanced_irq_interval;
  584. /* push everything to CPU 0 to give us a starting point. */
  585. for (i = 0 ; i < nr_irqs ; i++) {
  586. irq_desc[i].pending_mask = cpumask_of_cpu(0);
  587. set_pending_irq(i, cpumask_of_cpu(0));
  588. }
  589. set_freezable();
  590. for ( ; ; ) {
  591. time_remaining = schedule_timeout_interruptible(time_remaining);
  592. try_to_freeze();
  593. if (time_after(jiffies,
  594. prev_balance_time+balanced_irq_interval)) {
  595. preempt_disable();
  596. do_irq_balance();
  597. prev_balance_time = jiffies;
  598. time_remaining = balanced_irq_interval;
  599. preempt_enable();
  600. }
  601. }
  602. return 0;
  603. }
  604. static int __init balanced_irq_init(void)
  605. {
  606. int i;
  607. struct cpuinfo_x86 *c;
  608. cpumask_t tmp;
  609. cpus_shift_right(tmp, cpu_online_map, 2);
  610. c = &boot_cpu_data;
  611. /* When not overwritten by the command line ask subarchitecture. */
  612. if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
  613. irqbalance_disabled = NO_BALANCE_IRQ;
  614. if (irqbalance_disabled)
  615. return 0;
  616. /* disable irqbalance completely if there is only one processor online */
  617. if (num_online_cpus() < 2) {
  618. irqbalance_disabled = 1;
  619. return 0;
  620. }
  621. /*
  622. * Enable physical balance only if more than 1 physical processor
  623. * is present
  624. */
  625. if (smp_num_siblings > 1 && !cpus_empty(tmp))
  626. physical_balance = 1;
  627. for_each_online_cpu(i) {
  628. irq_cpu_data[i].irq_delta = kzalloc(sizeof(unsigned long) * nr_irqs, GFP_KERNEL);
  629. irq_cpu_data[i].last_irq = kzalloc(sizeof(unsigned long) * nr_irqs, GFP_KERNEL);
  630. if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
  631. printk(KERN_ERR "balanced_irq_init: out of memory");
  632. goto failed;
  633. }
  634. }
  635. printk(KERN_INFO "Starting balanced_irq\n");
  636. if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
  637. return 0;
  638. printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
  639. failed:
  640. for_each_possible_cpu(i) {
  641. kfree(irq_cpu_data[i].irq_delta);
  642. irq_cpu_data[i].irq_delta = NULL;
  643. kfree(irq_cpu_data[i].last_irq);
  644. irq_cpu_data[i].last_irq = NULL;
  645. }
  646. return 0;
  647. }
  648. int __devinit irqbalance_disable(char *str)
  649. {
  650. irqbalance_disabled = 1;
  651. return 1;
  652. }
  653. __setup("noirqbalance", irqbalance_disable);
  654. late_initcall(balanced_irq_init);
  655. #endif /* CONFIG_IRQBALANCE */
  656. #endif /* CONFIG_SMP */
  657. #ifndef CONFIG_SMP
  658. void send_IPI_self(int vector)
  659. {
  660. unsigned int cfg;
  661. /*
  662. * Wait for idle.
  663. */
  664. apic_wait_icr_idle();
  665. cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
  666. /*
  667. * Send the IPI. The write to APIC_ICR fires this off.
  668. */
  669. apic_write(APIC_ICR, cfg);
  670. }
  671. #endif /* !CONFIG_SMP */
  672. /*
  673. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  674. * specific CPU-side IRQs.
  675. */
  676. #define MAX_PIRQS 8
  677. static int pirq_entries [MAX_PIRQS];
  678. static int pirqs_enabled;
  679. int skip_ioapic_setup;
  680. static int __init ioapic_pirq_setup(char *str)
  681. {
  682. int i, max;
  683. int ints[MAX_PIRQS+1];
  684. get_options(str, ARRAY_SIZE(ints), ints);
  685. for (i = 0; i < MAX_PIRQS; i++)
  686. pirq_entries[i] = -1;
  687. pirqs_enabled = 1;
  688. apic_printk(APIC_VERBOSE, KERN_INFO
  689. "PIRQ redirection, working around broken MP-BIOS.\n");
  690. max = MAX_PIRQS;
  691. if (ints[0] < MAX_PIRQS)
  692. max = ints[0];
  693. for (i = 0; i < max; i++) {
  694. apic_printk(APIC_VERBOSE, KERN_DEBUG
  695. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  696. /*
  697. * PIRQs are mapped upside down, usually.
  698. */
  699. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  700. }
  701. return 1;
  702. }
  703. __setup("pirq=", ioapic_pirq_setup);
  704. /*
  705. * Find the IRQ entry number of a certain pin.
  706. */
  707. static int find_irq_entry(int apic, int pin, int type)
  708. {
  709. int i;
  710. for (i = 0; i < mp_irq_entries; i++)
  711. if (mp_irqs[i].mp_irqtype == type &&
  712. (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
  713. mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
  714. mp_irqs[i].mp_dstirq == pin)
  715. return i;
  716. return -1;
  717. }
  718. /*
  719. * Find the pin to which IRQ[irq] (ISA) is connected
  720. */
  721. static int __init find_isa_irq_pin(int irq, int type)
  722. {
  723. int i;
  724. for (i = 0; i < mp_irq_entries; i++) {
  725. int lbus = mp_irqs[i].mp_srcbus;
  726. if (test_bit(lbus, mp_bus_not_pci) &&
  727. (mp_irqs[i].mp_irqtype == type) &&
  728. (mp_irqs[i].mp_srcbusirq == irq))
  729. return mp_irqs[i].mp_dstirq;
  730. }
  731. return -1;
  732. }
  733. static int __init find_isa_irq_apic(int irq, int type)
  734. {
  735. int i;
  736. for (i = 0; i < mp_irq_entries; i++) {
  737. int lbus = mp_irqs[i].mp_srcbus;
  738. if (test_bit(lbus, mp_bus_not_pci) &&
  739. (mp_irqs[i].mp_irqtype == type) &&
  740. (mp_irqs[i].mp_srcbusirq == irq))
  741. break;
  742. }
  743. if (i < mp_irq_entries) {
  744. int apic;
  745. for (apic = 0; apic < nr_ioapics; apic++) {
  746. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
  747. return apic;
  748. }
  749. }
  750. return -1;
  751. }
  752. /*
  753. * Find a specific PCI IRQ entry.
  754. * Not an __init, possibly needed by modules
  755. */
  756. static int pin_2_irq(int idx, int apic, int pin);
  757. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  758. {
  759. int apic, i, best_guess = -1;
  760. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
  761. "slot:%d, pin:%d.\n", bus, slot, pin);
  762. if (test_bit(bus, mp_bus_not_pci)) {
  763. printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  764. return -1;
  765. }
  766. for (i = 0; i < mp_irq_entries; i++) {
  767. int lbus = mp_irqs[i].mp_srcbus;
  768. for (apic = 0; apic < nr_ioapics; apic++)
  769. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
  770. mp_irqs[i].mp_dstapic == MP_APIC_ALL)
  771. break;
  772. if (!test_bit(lbus, mp_bus_not_pci) &&
  773. !mp_irqs[i].mp_irqtype &&
  774. (bus == lbus) &&
  775. (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
  776. int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
  777. if (!(apic || IO_APIC_IRQ(irq)))
  778. continue;
  779. if (pin == (mp_irqs[i].mp_srcbusirq & 3))
  780. return irq;
  781. /*
  782. * Use the first all-but-pin matching entry as a
  783. * best-guess fuzzy result for broken mptables.
  784. */
  785. if (best_guess < 0)
  786. best_guess = irq;
  787. }
  788. }
  789. return best_guess;
  790. }
  791. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  792. /*
  793. * This function currently is only a helper for the i386 smp boot process where
  794. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  795. * so mask in all cases should simply be TARGET_CPUS
  796. */
  797. #ifdef CONFIG_SMP
  798. void __init setup_ioapic_dest(void)
  799. {
  800. int pin, ioapic, irq, irq_entry;
  801. if (skip_ioapic_setup == 1)
  802. return;
  803. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  804. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  805. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  806. if (irq_entry == -1)
  807. continue;
  808. irq = pin_2_irq(irq_entry, ioapic, pin);
  809. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  810. }
  811. }
  812. }
  813. #endif
  814. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  815. /*
  816. * EISA Edge/Level control register, ELCR
  817. */
  818. static int EISA_ELCR(unsigned int irq)
  819. {
  820. if (irq < 16) {
  821. unsigned int port = 0x4d0 + (irq >> 3);
  822. return (inb(port) >> (irq & 7)) & 1;
  823. }
  824. apic_printk(APIC_VERBOSE, KERN_INFO
  825. "Broken MPtable reports ISA irq %d\n", irq);
  826. return 0;
  827. }
  828. #endif
  829. /* ISA interrupts are always polarity zero edge triggered,
  830. * when listed as conforming in the MP table. */
  831. #define default_ISA_trigger(idx) (0)
  832. #define default_ISA_polarity(idx) (0)
  833. /* EISA interrupts are always polarity zero and can be edge or level
  834. * trigger depending on the ELCR value. If an interrupt is listed as
  835. * EISA conforming in the MP table, that means its trigger type must
  836. * be read in from the ELCR */
  837. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
  838. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  839. /* PCI interrupts are always polarity one level triggered,
  840. * when listed as conforming in the MP table. */
  841. #define default_PCI_trigger(idx) (1)
  842. #define default_PCI_polarity(idx) (1)
  843. /* MCA interrupts are always polarity zero level triggered,
  844. * when listed as conforming in the MP table. */
  845. #define default_MCA_trigger(idx) (1)
  846. #define default_MCA_polarity(idx) default_ISA_polarity(idx)
  847. static int MPBIOS_polarity(int idx)
  848. {
  849. int bus = mp_irqs[idx].mp_srcbus;
  850. int polarity;
  851. /*
  852. * Determine IRQ line polarity (high active or low active):
  853. */
  854. switch (mp_irqs[idx].mp_irqflag & 3) {
  855. case 0: /* conforms, ie. bus-type dependent polarity */
  856. {
  857. polarity = test_bit(bus, mp_bus_not_pci)?
  858. default_ISA_polarity(idx):
  859. default_PCI_polarity(idx);
  860. break;
  861. }
  862. case 1: /* high active */
  863. {
  864. polarity = 0;
  865. break;
  866. }
  867. case 2: /* reserved */
  868. {
  869. printk(KERN_WARNING "broken BIOS!!\n");
  870. polarity = 1;
  871. break;
  872. }
  873. case 3: /* low active */
  874. {
  875. polarity = 1;
  876. break;
  877. }
  878. default: /* invalid */
  879. {
  880. printk(KERN_WARNING "broken BIOS!!\n");
  881. polarity = 1;
  882. break;
  883. }
  884. }
  885. return polarity;
  886. }
  887. static int MPBIOS_trigger(int idx)
  888. {
  889. int bus = mp_irqs[idx].mp_srcbus;
  890. int trigger;
  891. /*
  892. * Determine IRQ trigger mode (edge or level sensitive):
  893. */
  894. switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
  895. case 0: /* conforms, ie. bus-type dependent */
  896. {
  897. trigger = test_bit(bus, mp_bus_not_pci)?
  898. default_ISA_trigger(idx):
  899. default_PCI_trigger(idx);
  900. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  901. switch (mp_bus_id_to_type[bus]) {
  902. case MP_BUS_ISA: /* ISA pin */
  903. {
  904. /* set before the switch */
  905. break;
  906. }
  907. case MP_BUS_EISA: /* EISA pin */
  908. {
  909. trigger = default_EISA_trigger(idx);
  910. break;
  911. }
  912. case MP_BUS_PCI: /* PCI pin */
  913. {
  914. /* set before the switch */
  915. break;
  916. }
  917. case MP_BUS_MCA: /* MCA pin */
  918. {
  919. trigger = default_MCA_trigger(idx);
  920. break;
  921. }
  922. default:
  923. {
  924. printk(KERN_WARNING "broken BIOS!!\n");
  925. trigger = 1;
  926. break;
  927. }
  928. }
  929. #endif
  930. break;
  931. }
  932. case 1: /* edge */
  933. {
  934. trigger = 0;
  935. break;
  936. }
  937. case 2: /* reserved */
  938. {
  939. printk(KERN_WARNING "broken BIOS!!\n");
  940. trigger = 1;
  941. break;
  942. }
  943. case 3: /* level */
  944. {
  945. trigger = 1;
  946. break;
  947. }
  948. default: /* invalid */
  949. {
  950. printk(KERN_WARNING "broken BIOS!!\n");
  951. trigger = 0;
  952. break;
  953. }
  954. }
  955. return trigger;
  956. }
  957. static inline int irq_polarity(int idx)
  958. {
  959. return MPBIOS_polarity(idx);
  960. }
  961. static inline int irq_trigger(int idx)
  962. {
  963. return MPBIOS_trigger(idx);
  964. }
  965. static int pin_2_irq(int idx, int apic, int pin)
  966. {
  967. int irq, i;
  968. int bus = mp_irqs[idx].mp_srcbus;
  969. /*
  970. * Debugging check, we are in big trouble if this message pops up!
  971. */
  972. if (mp_irqs[idx].mp_dstirq != pin)
  973. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  974. if (test_bit(bus, mp_bus_not_pci))
  975. irq = mp_irqs[idx].mp_srcbusirq;
  976. else {
  977. /*
  978. * PCI IRQs are mapped in order
  979. */
  980. i = irq = 0;
  981. while (i < apic)
  982. irq += nr_ioapic_registers[i++];
  983. irq += pin;
  984. /*
  985. * For MPS mode, so far only needed by ES7000 platform
  986. */
  987. if (ioapic_renumber_irq)
  988. irq = ioapic_renumber_irq(apic, irq);
  989. }
  990. /*
  991. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  992. */
  993. if ((pin >= 16) && (pin <= 23)) {
  994. if (pirq_entries[pin-16] != -1) {
  995. if (!pirq_entries[pin-16]) {
  996. apic_printk(APIC_VERBOSE, KERN_DEBUG
  997. "disabling PIRQ%d\n", pin-16);
  998. } else {
  999. irq = pirq_entries[pin-16];
  1000. apic_printk(APIC_VERBOSE, KERN_DEBUG
  1001. "using PIRQ%d -> IRQ %d\n",
  1002. pin-16, irq);
  1003. }
  1004. }
  1005. }
  1006. return irq;
  1007. }
  1008. static inline int IO_APIC_irq_trigger(int irq)
  1009. {
  1010. int apic, idx, pin;
  1011. for (apic = 0; apic < nr_ioapics; apic++) {
  1012. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1013. idx = find_irq_entry(apic, pin, mp_INT);
  1014. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
  1015. return irq_trigger(idx);
  1016. }
  1017. }
  1018. /*
  1019. * nonexistent IRQs are edge default
  1020. */
  1021. return 0;
  1022. }
  1023. /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
  1024. static u8 irq_vector_init_first __initdata = FIRST_DEVICE_VECTOR;
  1025. static u8 *irq_vector;
  1026. static void __init irq_vector_init_work(void *data)
  1027. {
  1028. struct dyn_array *da = data;
  1029. u8 *irq_vec;
  1030. irq_vec = *da->name;
  1031. irq_vec[0] = irq_vector_init_first;
  1032. }
  1033. DEFINE_DYN_ARRAY(irq_vector, sizeof(u8), nr_irqs, PAGE_SIZE, irq_vector_init_work);
  1034. static int __assign_irq_vector(int irq)
  1035. {
  1036. static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
  1037. int vector, offset;
  1038. BUG_ON((unsigned)irq >= nr_irqs);
  1039. if (irq_vector[irq] > 0)
  1040. return irq_vector[irq];
  1041. vector = current_vector;
  1042. offset = current_offset;
  1043. next:
  1044. vector += 8;
  1045. if (vector >= first_system_vector) {
  1046. offset = (offset + 1) % 8;
  1047. vector = FIRST_DEVICE_VECTOR + offset;
  1048. }
  1049. if (vector == current_vector)
  1050. return -ENOSPC;
  1051. if (test_and_set_bit(vector, used_vectors))
  1052. goto next;
  1053. current_vector = vector;
  1054. current_offset = offset;
  1055. irq_vector[irq] = vector;
  1056. return vector;
  1057. }
  1058. static int assign_irq_vector(int irq)
  1059. {
  1060. unsigned long flags;
  1061. int vector;
  1062. spin_lock_irqsave(&vector_lock, flags);
  1063. vector = __assign_irq_vector(irq);
  1064. spin_unlock_irqrestore(&vector_lock, flags);
  1065. return vector;
  1066. }
  1067. static struct irq_chip ioapic_chip;
  1068. #define IOAPIC_AUTO -1
  1069. #define IOAPIC_EDGE 0
  1070. #define IOAPIC_LEVEL 1
  1071. static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
  1072. {
  1073. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1074. trigger == IOAPIC_LEVEL) {
  1075. irq_desc[irq].status |= IRQ_LEVEL;
  1076. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1077. handle_fasteoi_irq, "fasteoi");
  1078. } else {
  1079. irq_desc[irq].status &= ~IRQ_LEVEL;
  1080. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1081. handle_edge_irq, "edge");
  1082. }
  1083. set_intr_gate(vector, interrupt[irq]);
  1084. }
  1085. static void __init setup_IO_APIC_irqs(void)
  1086. {
  1087. struct IO_APIC_route_entry entry;
  1088. int apic, pin, idx, irq, first_notcon = 1, vector;
  1089. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1090. for (apic = 0; apic < nr_ioapics; apic++) {
  1091. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1092. /*
  1093. * add it to the IO-APIC irq-routing table:
  1094. */
  1095. memset(&entry, 0, sizeof(entry));
  1096. entry.delivery_mode = INT_DELIVERY_MODE;
  1097. entry.dest_mode = INT_DEST_MODE;
  1098. entry.mask = 0; /* enable IRQ */
  1099. entry.dest.logical.logical_dest =
  1100. cpu_mask_to_apicid(TARGET_CPUS);
  1101. idx = find_irq_entry(apic, pin, mp_INT);
  1102. if (idx == -1) {
  1103. if (first_notcon) {
  1104. apic_printk(APIC_VERBOSE, KERN_DEBUG
  1105. " IO-APIC (apicid-pin) %d-%d",
  1106. mp_ioapics[apic].mp_apicid,
  1107. pin);
  1108. first_notcon = 0;
  1109. } else
  1110. apic_printk(APIC_VERBOSE, ", %d-%d",
  1111. mp_ioapics[apic].mp_apicid, pin);
  1112. continue;
  1113. }
  1114. if (!first_notcon) {
  1115. apic_printk(APIC_VERBOSE, " not connected.\n");
  1116. first_notcon = 1;
  1117. }
  1118. entry.trigger = irq_trigger(idx);
  1119. entry.polarity = irq_polarity(idx);
  1120. if (irq_trigger(idx)) {
  1121. entry.trigger = 1;
  1122. entry.mask = 1;
  1123. }
  1124. irq = pin_2_irq(idx, apic, pin);
  1125. /*
  1126. * skip adding the timer int on secondary nodes, which causes
  1127. * a small but painful rift in the time-space continuum
  1128. */
  1129. if (multi_timer_check(apic, irq))
  1130. continue;
  1131. else
  1132. add_pin_to_irq(irq, apic, pin);
  1133. if (!apic && !IO_APIC_IRQ(irq))
  1134. continue;
  1135. if (IO_APIC_IRQ(irq)) {
  1136. vector = assign_irq_vector(irq);
  1137. entry.vector = vector;
  1138. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  1139. if (!apic && (irq < 16))
  1140. disable_8259A_irq(irq);
  1141. }
  1142. ioapic_write_entry(apic, pin, entry);
  1143. }
  1144. }
  1145. if (!first_notcon)
  1146. apic_printk(APIC_VERBOSE, " not connected.\n");
  1147. }
  1148. /*
  1149. * Set up the timer pin, possibly with the 8259A-master behind.
  1150. */
  1151. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  1152. int vector)
  1153. {
  1154. struct IO_APIC_route_entry entry;
  1155. memset(&entry, 0, sizeof(entry));
  1156. /*
  1157. * We use logical delivery to get the timer IRQ
  1158. * to the first CPU.
  1159. */
  1160. entry.dest_mode = INT_DEST_MODE;
  1161. entry.mask = 1; /* mask IRQ now */
  1162. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  1163. entry.delivery_mode = INT_DELIVERY_MODE;
  1164. entry.polarity = 0;
  1165. entry.trigger = 0;
  1166. entry.vector = vector;
  1167. /*
  1168. * The timer IRQ doesn't have to know that behind the
  1169. * scene we may have a 8259A-master in AEOI mode ...
  1170. */
  1171. ioapic_register_intr(0, vector, IOAPIC_EDGE);
  1172. /*
  1173. * Add it to the IO-APIC irq-routing table:
  1174. */
  1175. ioapic_write_entry(apic, pin, entry);
  1176. }
  1177. __apicdebuginit(void) print_IO_APIC(void)
  1178. {
  1179. int apic, i;
  1180. union IO_APIC_reg_00 reg_00;
  1181. union IO_APIC_reg_01 reg_01;
  1182. union IO_APIC_reg_02 reg_02;
  1183. union IO_APIC_reg_03 reg_03;
  1184. unsigned long flags;
  1185. if (apic_verbosity == APIC_QUIET)
  1186. return;
  1187. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1188. for (i = 0; i < nr_ioapics; i++)
  1189. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1190. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  1191. /*
  1192. * We are a bit conservative about what we expect. We have to
  1193. * know about every hardware change ASAP.
  1194. */
  1195. printk(KERN_INFO "testing the IO APIC.......................\n");
  1196. for (apic = 0; apic < nr_ioapics; apic++) {
  1197. spin_lock_irqsave(&ioapic_lock, flags);
  1198. reg_00.raw = io_apic_read(apic, 0);
  1199. reg_01.raw = io_apic_read(apic, 1);
  1200. if (reg_01.bits.version >= 0x10)
  1201. reg_02.raw = io_apic_read(apic, 2);
  1202. if (reg_01.bits.version >= 0x20)
  1203. reg_03.raw = io_apic_read(apic, 3);
  1204. spin_unlock_irqrestore(&ioapic_lock, flags);
  1205. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  1206. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1207. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1208. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1209. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1210. printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
  1211. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  1212. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1213. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  1214. /*
  1215. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1216. * but the value of reg_02 is read as the previous read register
  1217. * value, so ignore it if reg_02 == reg_01.
  1218. */
  1219. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1220. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1221. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1222. }
  1223. /*
  1224. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1225. * or reg_03, but the value of reg_0[23] is read as the previous read
  1226. * register value, so ignore it if reg_03 == reg_0[12].
  1227. */
  1228. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1229. reg_03.raw != reg_01.raw) {
  1230. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1231. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1232. }
  1233. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1234. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  1235. " Stat Dest Deli Vect: \n");
  1236. for (i = 0; i <= reg_01.bits.entries; i++) {
  1237. struct IO_APIC_route_entry entry;
  1238. entry = ioapic_read_entry(apic, i);
  1239. printk(KERN_DEBUG " %02x %03X %02X ",
  1240. i,
  1241. entry.dest.logical.logical_dest,
  1242. entry.dest.physical.physical_dest
  1243. );
  1244. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1245. entry.mask,
  1246. entry.trigger,
  1247. entry.irr,
  1248. entry.polarity,
  1249. entry.delivery_status,
  1250. entry.dest_mode,
  1251. entry.delivery_mode,
  1252. entry.vector
  1253. );
  1254. }
  1255. }
  1256. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1257. for (i = 0; i < nr_irqs; i++) {
  1258. struct irq_pin_list *entry = irq_2_pin + i;
  1259. if (entry->pin < 0)
  1260. continue;
  1261. printk(KERN_DEBUG "IRQ%d ", i);
  1262. for (;;) {
  1263. printk("-> %d:%d", entry->apic, entry->pin);
  1264. if (!entry->next)
  1265. break;
  1266. entry = irq_2_pin + entry->next;
  1267. }
  1268. printk("\n");
  1269. }
  1270. printk(KERN_INFO ".................................... done.\n");
  1271. return;
  1272. }
  1273. __apicdebuginit(void) print_APIC_bitfield(int base)
  1274. {
  1275. unsigned int v;
  1276. int i, j;
  1277. if (apic_verbosity == APIC_QUIET)
  1278. return;
  1279. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1280. for (i = 0; i < 8; i++) {
  1281. v = apic_read(base + i*0x10);
  1282. for (j = 0; j < 32; j++) {
  1283. if (v & (1<<j))
  1284. printk("1");
  1285. else
  1286. printk("0");
  1287. }
  1288. printk("\n");
  1289. }
  1290. }
  1291. __apicdebuginit(void) print_local_APIC(void *dummy)
  1292. {
  1293. unsigned int v, ver, maxlvt;
  1294. u64 icr;
  1295. if (apic_verbosity == APIC_QUIET)
  1296. return;
  1297. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1298. smp_processor_id(), hard_smp_processor_id());
  1299. v = apic_read(APIC_ID);
  1300. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
  1301. GET_APIC_ID(v));
  1302. v = apic_read(APIC_LVR);
  1303. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1304. ver = GET_APIC_VERSION(v);
  1305. maxlvt = lapic_get_maxlvt();
  1306. v = apic_read(APIC_TASKPRI);
  1307. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1308. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1309. v = apic_read(APIC_ARBPRI);
  1310. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1311. v & APIC_ARBPRI_MASK);
  1312. v = apic_read(APIC_PROCPRI);
  1313. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1314. }
  1315. v = apic_read(APIC_EOI);
  1316. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  1317. v = apic_read(APIC_RRR);
  1318. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1319. v = apic_read(APIC_LDR);
  1320. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1321. v = apic_read(APIC_DFR);
  1322. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1323. v = apic_read(APIC_SPIV);
  1324. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1325. printk(KERN_DEBUG "... APIC ISR field:\n");
  1326. print_APIC_bitfield(APIC_ISR);
  1327. printk(KERN_DEBUG "... APIC TMR field:\n");
  1328. print_APIC_bitfield(APIC_TMR);
  1329. printk(KERN_DEBUG "... APIC IRR field:\n");
  1330. print_APIC_bitfield(APIC_IRR);
  1331. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1332. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1333. apic_write(APIC_ESR, 0);
  1334. v = apic_read(APIC_ESR);
  1335. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1336. }
  1337. icr = apic_icr_read();
  1338. printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
  1339. printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
  1340. v = apic_read(APIC_LVTT);
  1341. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1342. if (maxlvt > 3) { /* PC is LVT#4. */
  1343. v = apic_read(APIC_LVTPC);
  1344. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1345. }
  1346. v = apic_read(APIC_LVT0);
  1347. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1348. v = apic_read(APIC_LVT1);
  1349. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1350. if (maxlvt > 2) { /* ERR is LVT#3. */
  1351. v = apic_read(APIC_LVTERR);
  1352. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1353. }
  1354. v = apic_read(APIC_TMICT);
  1355. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1356. v = apic_read(APIC_TMCCT);
  1357. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1358. v = apic_read(APIC_TDCR);
  1359. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1360. printk("\n");
  1361. }
  1362. __apicdebuginit(void) print_all_local_APICs(void)
  1363. {
  1364. on_each_cpu(print_local_APIC, NULL, 1);
  1365. }
  1366. __apicdebuginit(void) print_PIC(void)
  1367. {
  1368. unsigned int v;
  1369. unsigned long flags;
  1370. if (apic_verbosity == APIC_QUIET)
  1371. return;
  1372. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1373. spin_lock_irqsave(&i8259A_lock, flags);
  1374. v = inb(0xa1) << 8 | inb(0x21);
  1375. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1376. v = inb(0xa0) << 8 | inb(0x20);
  1377. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1378. outb(0x0b, 0xa0);
  1379. outb(0x0b, 0x20);
  1380. v = inb(0xa0) << 8 | inb(0x20);
  1381. outb(0x0a, 0xa0);
  1382. outb(0x0a, 0x20);
  1383. spin_unlock_irqrestore(&i8259A_lock, flags);
  1384. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1385. v = inb(0x4d1) << 8 | inb(0x4d0);
  1386. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1387. }
  1388. __apicdebuginit(int) print_all_ICs(void)
  1389. {
  1390. print_PIC();
  1391. print_all_local_APICs();
  1392. print_IO_APIC();
  1393. return 0;
  1394. }
  1395. fs_initcall(print_all_ICs);
  1396. static void __init enable_IO_APIC(void)
  1397. {
  1398. union IO_APIC_reg_01 reg_01;
  1399. int i8259_apic, i8259_pin;
  1400. int i, apic;
  1401. unsigned long flags;
  1402. for (i = 0; i < pin_map_size; i++) {
  1403. irq_2_pin[i].pin = -1;
  1404. irq_2_pin[i].next = 0;
  1405. }
  1406. if (!pirqs_enabled)
  1407. for (i = 0; i < MAX_PIRQS; i++)
  1408. pirq_entries[i] = -1;
  1409. /*
  1410. * The number of IO-APIC IRQ registers (== #pins):
  1411. */
  1412. for (apic = 0; apic < nr_ioapics; apic++) {
  1413. spin_lock_irqsave(&ioapic_lock, flags);
  1414. reg_01.raw = io_apic_read(apic, 1);
  1415. spin_unlock_irqrestore(&ioapic_lock, flags);
  1416. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1417. }
  1418. for (apic = 0; apic < nr_ioapics; apic++) {
  1419. int pin;
  1420. /* See if any of the pins is in ExtINT mode */
  1421. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1422. struct IO_APIC_route_entry entry;
  1423. entry = ioapic_read_entry(apic, pin);
  1424. /* If the interrupt line is enabled and in ExtInt mode
  1425. * I have found the pin where the i8259 is connected.
  1426. */
  1427. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1428. ioapic_i8259.apic = apic;
  1429. ioapic_i8259.pin = pin;
  1430. goto found_i8259;
  1431. }
  1432. }
  1433. }
  1434. found_i8259:
  1435. /* Look to see what if the MP table has reported the ExtINT */
  1436. /* If we could not find the appropriate pin by looking at the ioapic
  1437. * the i8259 probably is not connected the ioapic but give the
  1438. * mptable a chance anyway.
  1439. */
  1440. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1441. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1442. /* Trust the MP table if nothing is setup in the hardware */
  1443. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1444. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1445. ioapic_i8259.pin = i8259_pin;
  1446. ioapic_i8259.apic = i8259_apic;
  1447. }
  1448. /* Complain if the MP table and the hardware disagree */
  1449. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1450. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1451. {
  1452. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1453. }
  1454. /*
  1455. * Do not trust the IO-APIC being empty at bootup
  1456. */
  1457. clear_IO_APIC();
  1458. }
  1459. /*
  1460. * Not an __init, needed by the reboot code
  1461. */
  1462. void disable_IO_APIC(void)
  1463. {
  1464. /*
  1465. * Clear the IO-APIC before rebooting:
  1466. */
  1467. clear_IO_APIC();
  1468. /*
  1469. * If the i8259 is routed through an IOAPIC
  1470. * Put that IOAPIC in virtual wire mode
  1471. * so legacy interrupts can be delivered.
  1472. */
  1473. if (ioapic_i8259.pin != -1) {
  1474. struct IO_APIC_route_entry entry;
  1475. memset(&entry, 0, sizeof(entry));
  1476. entry.mask = 0; /* Enabled */
  1477. entry.trigger = 0; /* Edge */
  1478. entry.irr = 0;
  1479. entry.polarity = 0; /* High */
  1480. entry.delivery_status = 0;
  1481. entry.dest_mode = 0; /* Physical */
  1482. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1483. entry.vector = 0;
  1484. entry.dest.physical.physical_dest = read_apic_id();
  1485. /*
  1486. * Add it to the IO-APIC irq-routing table:
  1487. */
  1488. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1489. }
  1490. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1491. }
  1492. /*
  1493. * function to set the IO-APIC physical IDs based on the
  1494. * values stored in the MPC table.
  1495. *
  1496. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1497. */
  1498. static void __init setup_ioapic_ids_from_mpc(void)
  1499. {
  1500. union IO_APIC_reg_00 reg_00;
  1501. physid_mask_t phys_id_present_map;
  1502. int apic;
  1503. int i;
  1504. unsigned char old_id;
  1505. unsigned long flags;
  1506. if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
  1507. return;
  1508. /*
  1509. * Don't check I/O APIC IDs for xAPIC systems. They have
  1510. * no meaning without the serial APIC bus.
  1511. */
  1512. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1513. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1514. return;
  1515. /*
  1516. * This is broken; anything with a real cpu count has to
  1517. * circumvent this idiocy regardless.
  1518. */
  1519. phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
  1520. /*
  1521. * Set the IOAPIC ID to the value stored in the MPC table.
  1522. */
  1523. for (apic = 0; apic < nr_ioapics; apic++) {
  1524. /* Read the register 0 value */
  1525. spin_lock_irqsave(&ioapic_lock, flags);
  1526. reg_00.raw = io_apic_read(apic, 0);
  1527. spin_unlock_irqrestore(&ioapic_lock, flags);
  1528. old_id = mp_ioapics[apic].mp_apicid;
  1529. if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
  1530. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1531. apic, mp_ioapics[apic].mp_apicid);
  1532. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1533. reg_00.bits.ID);
  1534. mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
  1535. }
  1536. /*
  1537. * Sanity check, is the ID really free? Every APIC in a
  1538. * system must have a unique ID or we get lots of nice
  1539. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1540. */
  1541. if (check_apicid_used(phys_id_present_map,
  1542. mp_ioapics[apic].mp_apicid)) {
  1543. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1544. apic, mp_ioapics[apic].mp_apicid);
  1545. for (i = 0; i < get_physical_broadcast(); i++)
  1546. if (!physid_isset(i, phys_id_present_map))
  1547. break;
  1548. if (i >= get_physical_broadcast())
  1549. panic("Max APIC ID exceeded!\n");
  1550. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1551. i);
  1552. physid_set(i, phys_id_present_map);
  1553. mp_ioapics[apic].mp_apicid = i;
  1554. } else {
  1555. physid_mask_t tmp;
  1556. tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
  1557. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1558. "phys_id_present_map\n",
  1559. mp_ioapics[apic].mp_apicid);
  1560. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1561. }
  1562. /*
  1563. * We need to adjust the IRQ routing table
  1564. * if the ID changed.
  1565. */
  1566. if (old_id != mp_ioapics[apic].mp_apicid)
  1567. for (i = 0; i < mp_irq_entries; i++)
  1568. if (mp_irqs[i].mp_dstapic == old_id)
  1569. mp_irqs[i].mp_dstapic
  1570. = mp_ioapics[apic].mp_apicid;
  1571. /*
  1572. * Read the right value from the MPC table and
  1573. * write it into the ID register.
  1574. */
  1575. apic_printk(APIC_VERBOSE, KERN_INFO
  1576. "...changing IO-APIC physical APIC ID to %d ...",
  1577. mp_ioapics[apic].mp_apicid);
  1578. reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
  1579. spin_lock_irqsave(&ioapic_lock, flags);
  1580. io_apic_write(apic, 0, reg_00.raw);
  1581. spin_unlock_irqrestore(&ioapic_lock, flags);
  1582. /*
  1583. * Sanity check
  1584. */
  1585. spin_lock_irqsave(&ioapic_lock, flags);
  1586. reg_00.raw = io_apic_read(apic, 0);
  1587. spin_unlock_irqrestore(&ioapic_lock, flags);
  1588. if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
  1589. printk("could not set ID!\n");
  1590. else
  1591. apic_printk(APIC_VERBOSE, " ok.\n");
  1592. }
  1593. }
  1594. int no_timer_check __initdata;
  1595. static int __init notimercheck(char *s)
  1596. {
  1597. no_timer_check = 1;
  1598. return 1;
  1599. }
  1600. __setup("no_timer_check", notimercheck);
  1601. /*
  1602. * There is a nasty bug in some older SMP boards, their mptable lies
  1603. * about the timer IRQ. We do the following to work around the situation:
  1604. *
  1605. * - timer IRQ defaults to IO-APIC IRQ
  1606. * - if this function detects that timer IRQs are defunct, then we fall
  1607. * back to ISA timer IRQs
  1608. */
  1609. static int __init timer_irq_works(void)
  1610. {
  1611. unsigned long t1 = jiffies;
  1612. unsigned long flags;
  1613. if (no_timer_check)
  1614. return 1;
  1615. local_save_flags(flags);
  1616. local_irq_enable();
  1617. /* Let ten ticks pass... */
  1618. mdelay((10 * 1000) / HZ);
  1619. local_irq_restore(flags);
  1620. /*
  1621. * Expect a few ticks at least, to be sure some possible
  1622. * glue logic does not lock up after one or two first
  1623. * ticks in a non-ExtINT mode. Also the local APIC
  1624. * might have cached one ExtINT interrupt. Finally, at
  1625. * least one tick may be lost due to delays.
  1626. */
  1627. if (time_after(jiffies, t1 + 4))
  1628. return 1;
  1629. return 0;
  1630. }
  1631. /*
  1632. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1633. * number of pending IRQ events unhandled. These cases are very rare,
  1634. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1635. * better to do it this way as thus we do not have to be aware of
  1636. * 'pending' interrupts in the IRQ path, except at this point.
  1637. */
  1638. /*
  1639. * Edge triggered needs to resend any interrupt
  1640. * that was delayed but this is now handled in the device
  1641. * independent code.
  1642. */
  1643. /*
  1644. * Startup quirk:
  1645. *
  1646. * Starting up a edge-triggered IO-APIC interrupt is
  1647. * nasty - we need to make sure that we get the edge.
  1648. * If it is already asserted for some reason, we need
  1649. * return 1 to indicate that is was pending.
  1650. *
  1651. * This is not complete - we should be able to fake
  1652. * an edge even if it isn't on the 8259A...
  1653. *
  1654. * (We do this for level-triggered IRQs too - it cannot hurt.)
  1655. */
  1656. static unsigned int startup_ioapic_irq(unsigned int irq)
  1657. {
  1658. int was_pending = 0;
  1659. unsigned long flags;
  1660. spin_lock_irqsave(&ioapic_lock, flags);
  1661. if (irq < 16) {
  1662. disable_8259A_irq(irq);
  1663. if (i8259A_irq_pending(irq))
  1664. was_pending = 1;
  1665. }
  1666. __unmask_IO_APIC_irq(irq);
  1667. spin_unlock_irqrestore(&ioapic_lock, flags);
  1668. return was_pending;
  1669. }
  1670. static void ack_ioapic_irq(unsigned int irq)
  1671. {
  1672. move_native_irq(irq);
  1673. ack_APIC_irq();
  1674. }
  1675. static void ack_ioapic_quirk_irq(unsigned int irq)
  1676. {
  1677. unsigned long v;
  1678. int i;
  1679. move_native_irq(irq);
  1680. /*
  1681. * It appears there is an erratum which affects at least version 0x11
  1682. * of I/O APIC (that's the 82093AA and cores integrated into various
  1683. * chipsets). Under certain conditions a level-triggered interrupt is
  1684. * erroneously delivered as edge-triggered one but the respective IRR
  1685. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  1686. * message but it will never arrive and further interrupts are blocked
  1687. * from the source. The exact reason is so far unknown, but the
  1688. * phenomenon was observed when two consecutive interrupt requests
  1689. * from a given source get delivered to the same CPU and the source is
  1690. * temporarily disabled in between.
  1691. *
  1692. * A workaround is to simulate an EOI message manually. We achieve it
  1693. * by setting the trigger mode to edge and then to level when the edge
  1694. * trigger mode gets detected in the TMR of a local APIC for a
  1695. * level-triggered interrupt. We mask the source for the time of the
  1696. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  1697. * The idea is from Manfred Spraul. --macro
  1698. */
  1699. i = irq_vector[irq];
  1700. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  1701. ack_APIC_irq();
  1702. if (!(v & (1 << (i & 0x1f)))) {
  1703. atomic_inc(&irq_mis_count);
  1704. spin_lock(&ioapic_lock);
  1705. __mask_and_edge_IO_APIC_irq(irq);
  1706. __unmask_and_level_IO_APIC_irq(irq);
  1707. spin_unlock(&ioapic_lock);
  1708. }
  1709. }
  1710. static int ioapic_retrigger_irq(unsigned int irq)
  1711. {
  1712. send_IPI_self(irq_vector[irq]);
  1713. return 1;
  1714. }
  1715. static struct irq_chip ioapic_chip __read_mostly = {
  1716. .name = "IO-APIC",
  1717. .startup = startup_ioapic_irq,
  1718. .mask = mask_IO_APIC_irq,
  1719. .unmask = unmask_IO_APIC_irq,
  1720. .ack = ack_ioapic_irq,
  1721. .eoi = ack_ioapic_quirk_irq,
  1722. #ifdef CONFIG_SMP
  1723. .set_affinity = set_ioapic_affinity_irq,
  1724. #endif
  1725. .retrigger = ioapic_retrigger_irq,
  1726. };
  1727. static inline void init_IO_APIC_traps(void)
  1728. {
  1729. int irq;
  1730. /*
  1731. * NOTE! The local APIC isn't very good at handling
  1732. * multiple interrupts at the same interrupt level.
  1733. * As the interrupt level is determined by taking the
  1734. * vector number and shifting that right by 4, we
  1735. * want to spread these out a bit so that they don't
  1736. * all fall in the same interrupt level.
  1737. *
  1738. * Also, we've got to be careful not to trash gate
  1739. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1740. */
  1741. for (irq = 0; irq < nr_irqs ; irq++) {
  1742. if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
  1743. /*
  1744. * Hmm.. We don't have an entry for this,
  1745. * so default to an old-fashioned 8259
  1746. * interrupt if we can..
  1747. */
  1748. if (irq < 16)
  1749. make_8259A_irq(irq);
  1750. else
  1751. /* Strange. Oh, well.. */
  1752. irq_desc[irq].chip = &no_irq_chip;
  1753. }
  1754. }
  1755. }
  1756. /*
  1757. * The local APIC irq-chip implementation:
  1758. */
  1759. static void ack_lapic_irq(unsigned int irq)
  1760. {
  1761. ack_APIC_irq();
  1762. }
  1763. static void mask_lapic_irq(unsigned int irq)
  1764. {
  1765. unsigned long v;
  1766. v = apic_read(APIC_LVT0);
  1767. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1768. }
  1769. static void unmask_lapic_irq(unsigned int irq)
  1770. {
  1771. unsigned long v;
  1772. v = apic_read(APIC_LVT0);
  1773. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1774. }
  1775. static struct irq_chip lapic_chip __read_mostly = {
  1776. .name = "local-APIC",
  1777. .mask = mask_lapic_irq,
  1778. .unmask = unmask_lapic_irq,
  1779. .ack = ack_lapic_irq,
  1780. };
  1781. static void lapic_register_intr(int irq, int vector)
  1782. {
  1783. irq_desc[irq].status &= ~IRQ_LEVEL;
  1784. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  1785. "edge");
  1786. set_intr_gate(vector, interrupt[irq]);
  1787. }
  1788. static void __init setup_nmi(void)
  1789. {
  1790. /*
  1791. * Dirty trick to enable the NMI watchdog ...
  1792. * We put the 8259A master into AEOI mode and
  1793. * unmask on all local APICs LVT0 as NMI.
  1794. *
  1795. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1796. * is from Maciej W. Rozycki - so we do not have to EOI from
  1797. * the NMI handler or the timer interrupt.
  1798. */
  1799. apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
  1800. enable_NMI_through_LVT0();
  1801. apic_printk(APIC_VERBOSE, " done.\n");
  1802. }
  1803. /*
  1804. * This looks a bit hackish but it's about the only one way of sending
  1805. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1806. * not support the ExtINT mode, unfortunately. We need to send these
  1807. * cycles as some i82489DX-based boards have glue logic that keeps the
  1808. * 8259A interrupt line asserted until INTA. --macro
  1809. */
  1810. static inline void __init unlock_ExtINT_logic(void)
  1811. {
  1812. int apic, pin, i;
  1813. struct IO_APIC_route_entry entry0, entry1;
  1814. unsigned char save_control, save_freq_select;
  1815. pin = find_isa_irq_pin(8, mp_INT);
  1816. if (pin == -1) {
  1817. WARN_ON_ONCE(1);
  1818. return;
  1819. }
  1820. apic = find_isa_irq_apic(8, mp_INT);
  1821. if (apic == -1) {
  1822. WARN_ON_ONCE(1);
  1823. return;
  1824. }
  1825. entry0 = ioapic_read_entry(apic, pin);
  1826. clear_IO_APIC_pin(apic, pin);
  1827. memset(&entry1, 0, sizeof(entry1));
  1828. entry1.dest_mode = 0; /* physical delivery */
  1829. entry1.mask = 0; /* unmask IRQ now */
  1830. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1831. entry1.delivery_mode = dest_ExtINT;
  1832. entry1.polarity = entry0.polarity;
  1833. entry1.trigger = 0;
  1834. entry1.vector = 0;
  1835. ioapic_write_entry(apic, pin, entry1);
  1836. save_control = CMOS_READ(RTC_CONTROL);
  1837. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1838. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1839. RTC_FREQ_SELECT);
  1840. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1841. i = 100;
  1842. while (i-- > 0) {
  1843. mdelay(10);
  1844. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1845. i -= 10;
  1846. }
  1847. CMOS_WRITE(save_control, RTC_CONTROL);
  1848. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1849. clear_IO_APIC_pin(apic, pin);
  1850. ioapic_write_entry(apic, pin, entry0);
  1851. }
  1852. /*
  1853. * This code may look a bit paranoid, but it's supposed to cooperate with
  1854. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1855. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1856. * fanatically on his truly buggy board.
  1857. */
  1858. static inline void __init check_timer(void)
  1859. {
  1860. int apic1, pin1, apic2, pin2;
  1861. int no_pin1 = 0;
  1862. int vector;
  1863. unsigned int ver;
  1864. unsigned long flags;
  1865. local_irq_save(flags);
  1866. ver = apic_read(APIC_LVR);
  1867. ver = GET_APIC_VERSION(ver);
  1868. /*
  1869. * get/set the timer IRQ vector:
  1870. */
  1871. disable_8259A_irq(0);
  1872. vector = assign_irq_vector(0);
  1873. set_intr_gate(vector, interrupt[0]);
  1874. /*
  1875. * As IRQ0 is to be enabled in the 8259A, the virtual
  1876. * wire has to be disabled in the local APIC. Also
  1877. * timer interrupts need to be acknowledged manually in
  1878. * the 8259A for the i82489DX when using the NMI
  1879. * watchdog as that APIC treats NMIs as level-triggered.
  1880. * The AEOI mode will finish them in the 8259A
  1881. * automatically.
  1882. */
  1883. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1884. init_8259A(1);
  1885. timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
  1886. pin1 = find_isa_irq_pin(0, mp_INT);
  1887. apic1 = find_isa_irq_apic(0, mp_INT);
  1888. pin2 = ioapic_i8259.pin;
  1889. apic2 = ioapic_i8259.apic;
  1890. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  1891. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1892. vector, apic1, pin1, apic2, pin2);
  1893. /*
  1894. * Some BIOS writers are clueless and report the ExtINTA
  1895. * I/O APIC input from the cascaded 8259A as the timer
  1896. * interrupt input. So just in case, if only one pin
  1897. * was found above, try it both directly and through the
  1898. * 8259A.
  1899. */
  1900. if (pin1 == -1) {
  1901. pin1 = pin2;
  1902. apic1 = apic2;
  1903. no_pin1 = 1;
  1904. } else if (pin2 == -1) {
  1905. pin2 = pin1;
  1906. apic2 = apic1;
  1907. }
  1908. if (pin1 != -1) {
  1909. /*
  1910. * Ok, does IRQ0 through the IOAPIC work?
  1911. */
  1912. if (no_pin1) {
  1913. add_pin_to_irq(0, apic1, pin1);
  1914. setup_timer_IRQ0_pin(apic1, pin1, vector);
  1915. }
  1916. unmask_IO_APIC_irq(0);
  1917. if (timer_irq_works()) {
  1918. if (nmi_watchdog == NMI_IO_APIC) {
  1919. setup_nmi();
  1920. enable_8259A_irq(0);
  1921. }
  1922. if (disable_timer_pin_1 > 0)
  1923. clear_IO_APIC_pin(0, pin1);
  1924. goto out;
  1925. }
  1926. clear_IO_APIC_pin(apic1, pin1);
  1927. if (!no_pin1)
  1928. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  1929. "8254 timer not connected to IO-APIC\n");
  1930. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  1931. "(IRQ0) through the 8259A ...\n");
  1932. apic_printk(APIC_QUIET, KERN_INFO
  1933. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  1934. /*
  1935. * legacy devices should be connected to IO APIC #0
  1936. */
  1937. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  1938. setup_timer_IRQ0_pin(apic2, pin2, vector);
  1939. unmask_IO_APIC_irq(0);
  1940. enable_8259A_irq(0);
  1941. if (timer_irq_works()) {
  1942. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  1943. timer_through_8259 = 1;
  1944. if (nmi_watchdog == NMI_IO_APIC) {
  1945. disable_8259A_irq(0);
  1946. setup_nmi();
  1947. enable_8259A_irq(0);
  1948. }
  1949. goto out;
  1950. }
  1951. /*
  1952. * Cleanup, just in case ...
  1953. */
  1954. disable_8259A_irq(0);
  1955. clear_IO_APIC_pin(apic2, pin2);
  1956. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  1957. }
  1958. if (nmi_watchdog == NMI_IO_APIC) {
  1959. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  1960. "through the IO-APIC - disabling NMI Watchdog!\n");
  1961. nmi_watchdog = NMI_NONE;
  1962. }
  1963. timer_ack = 0;
  1964. apic_printk(APIC_QUIET, KERN_INFO
  1965. "...trying to set up timer as Virtual Wire IRQ...\n");
  1966. lapic_register_intr(0, vector);
  1967. apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1968. enable_8259A_irq(0);
  1969. if (timer_irq_works()) {
  1970. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1971. goto out;
  1972. }
  1973. disable_8259A_irq(0);
  1974. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1975. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  1976. apic_printk(APIC_QUIET, KERN_INFO
  1977. "...trying to set up timer as ExtINT IRQ...\n");
  1978. init_8259A(0);
  1979. make_8259A_irq(0);
  1980. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1981. unlock_ExtINT_logic();
  1982. if (timer_irq_works()) {
  1983. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1984. goto out;
  1985. }
  1986. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  1987. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1988. "report. Then try booting with the 'noapic' option.\n");
  1989. out:
  1990. local_irq_restore(flags);
  1991. }
  1992. /*
  1993. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  1994. * to devices. However there may be an I/O APIC pin available for
  1995. * this interrupt regardless. The pin may be left unconnected, but
  1996. * typically it will be reused as an ExtINT cascade interrupt for
  1997. * the master 8259A. In the MPS case such a pin will normally be
  1998. * reported as an ExtINT interrupt in the MP table. With ACPI
  1999. * there is no provision for ExtINT interrupts, and in the absence
  2000. * of an override it would be treated as an ordinary ISA I/O APIC
  2001. * interrupt, that is edge-triggered and unmasked by default. We
  2002. * used to do this, but it caused problems on some systems because
  2003. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  2004. * the same ExtINT cascade interrupt to drive the local APIC of the
  2005. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  2006. * the I/O APIC in all cases now. No actual device should request
  2007. * it anyway. --macro
  2008. */
  2009. #define PIC_IRQS (1 << PIC_CASCADE_IR)
  2010. void __init setup_IO_APIC(void)
  2011. {
  2012. int i;
  2013. /* Reserve all the system vectors. */
  2014. for (i = first_system_vector; i < NR_VECTORS; i++)
  2015. set_bit(i, used_vectors);
  2016. enable_IO_APIC();
  2017. io_apic_irqs = ~PIC_IRQS;
  2018. printk("ENABLING IO-APIC IRQs\n");
  2019. /*
  2020. * Set up IO-APIC IRQ routing.
  2021. */
  2022. if (!acpi_ioapic)
  2023. setup_ioapic_ids_from_mpc();
  2024. sync_Arb_IDs();
  2025. setup_IO_APIC_irqs();
  2026. init_IO_APIC_traps();
  2027. check_timer();
  2028. }
  2029. /*
  2030. * Called after all the initialization is done. If we didnt find any
  2031. * APIC bugs then we can allow the modify fast path
  2032. */
  2033. static int __init io_apic_bug_finalize(void)
  2034. {
  2035. if (sis_apic_bug == -1)
  2036. sis_apic_bug = 0;
  2037. return 0;
  2038. }
  2039. late_initcall(io_apic_bug_finalize);
  2040. struct sysfs_ioapic_data {
  2041. struct sys_device dev;
  2042. struct IO_APIC_route_entry entry[0];
  2043. };
  2044. static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
  2045. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  2046. {
  2047. struct IO_APIC_route_entry *entry;
  2048. struct sysfs_ioapic_data *data;
  2049. int i;
  2050. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2051. entry = data->entry;
  2052. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2053. entry[i] = ioapic_read_entry(dev->id, i);
  2054. return 0;
  2055. }
  2056. static int ioapic_resume(struct sys_device *dev)
  2057. {
  2058. struct IO_APIC_route_entry *entry;
  2059. struct sysfs_ioapic_data *data;
  2060. unsigned long flags;
  2061. union IO_APIC_reg_00 reg_00;
  2062. int i;
  2063. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2064. entry = data->entry;
  2065. spin_lock_irqsave(&ioapic_lock, flags);
  2066. reg_00.raw = io_apic_read(dev->id, 0);
  2067. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  2068. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  2069. io_apic_write(dev->id, 0, reg_00.raw);
  2070. }
  2071. spin_unlock_irqrestore(&ioapic_lock, flags);
  2072. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2073. ioapic_write_entry(dev->id, i, entry[i]);
  2074. return 0;
  2075. }
  2076. static struct sysdev_class ioapic_sysdev_class = {
  2077. .name = "ioapic",
  2078. .suspend = ioapic_suspend,
  2079. .resume = ioapic_resume,
  2080. };
  2081. static int __init ioapic_init_sysfs(void)
  2082. {
  2083. struct sys_device *dev;
  2084. int i, size, error = 0;
  2085. error = sysdev_class_register(&ioapic_sysdev_class);
  2086. if (error)
  2087. return error;
  2088. for (i = 0; i < nr_ioapics; i++) {
  2089. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  2090. * sizeof(struct IO_APIC_route_entry);
  2091. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  2092. if (!mp_ioapic_data[i]) {
  2093. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2094. continue;
  2095. }
  2096. dev = &mp_ioapic_data[i]->dev;
  2097. dev->id = i;
  2098. dev->cls = &ioapic_sysdev_class;
  2099. error = sysdev_register(dev);
  2100. if (error) {
  2101. kfree(mp_ioapic_data[i]);
  2102. mp_ioapic_data[i] = NULL;
  2103. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2104. continue;
  2105. }
  2106. }
  2107. return 0;
  2108. }
  2109. device_initcall(ioapic_init_sysfs);
  2110. /*
  2111. * Dynamic irq allocate and deallocation
  2112. */
  2113. int create_irq(void)
  2114. {
  2115. /* Allocate an unused irq */
  2116. int irq, new, vector = 0;
  2117. unsigned long flags;
  2118. irq = -ENOSPC;
  2119. spin_lock_irqsave(&vector_lock, flags);
  2120. for (new = (nr_irqs - 1); new >= 0; new--) {
  2121. if (platform_legacy_irq(new))
  2122. continue;
  2123. if (irq_vector[new] != 0)
  2124. continue;
  2125. vector = __assign_irq_vector(new);
  2126. if (likely(vector > 0))
  2127. irq = new;
  2128. break;
  2129. }
  2130. spin_unlock_irqrestore(&vector_lock, flags);
  2131. if (irq >= 0) {
  2132. set_intr_gate(vector, interrupt[irq]);
  2133. dynamic_irq_init(irq);
  2134. }
  2135. return irq;
  2136. }
  2137. void destroy_irq(unsigned int irq)
  2138. {
  2139. unsigned long flags;
  2140. dynamic_irq_cleanup(irq);
  2141. spin_lock_irqsave(&vector_lock, flags);
  2142. clear_bit(irq_vector[irq], used_vectors);
  2143. irq_vector[irq] = 0;
  2144. spin_unlock_irqrestore(&vector_lock, flags);
  2145. }
  2146. /*
  2147. * MSI message composition
  2148. */
  2149. #ifdef CONFIG_PCI_MSI
  2150. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  2151. {
  2152. int vector;
  2153. unsigned dest;
  2154. vector = assign_irq_vector(irq);
  2155. if (vector >= 0) {
  2156. dest = cpu_mask_to_apicid(TARGET_CPUS);
  2157. msg->address_hi = MSI_ADDR_BASE_HI;
  2158. msg->address_lo =
  2159. MSI_ADDR_BASE_LO |
  2160. ((INT_DEST_MODE == 0) ?
  2161. MSI_ADDR_DEST_MODE_PHYSICAL:
  2162. MSI_ADDR_DEST_MODE_LOGICAL) |
  2163. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2164. MSI_ADDR_REDIRECTION_CPU:
  2165. MSI_ADDR_REDIRECTION_LOWPRI) |
  2166. MSI_ADDR_DEST_ID(dest);
  2167. msg->data =
  2168. MSI_DATA_TRIGGER_EDGE |
  2169. MSI_DATA_LEVEL_ASSERT |
  2170. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2171. MSI_DATA_DELIVERY_FIXED:
  2172. MSI_DATA_DELIVERY_LOWPRI) |
  2173. MSI_DATA_VECTOR(vector);
  2174. }
  2175. return vector;
  2176. }
  2177. #ifdef CONFIG_SMP
  2178. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2179. {
  2180. struct msi_msg msg;
  2181. unsigned int dest;
  2182. cpumask_t tmp;
  2183. int vector;
  2184. cpus_and(tmp, mask, cpu_online_map);
  2185. if (cpus_empty(tmp))
  2186. tmp = TARGET_CPUS;
  2187. vector = assign_irq_vector(irq);
  2188. if (vector < 0)
  2189. return;
  2190. dest = cpu_mask_to_apicid(mask);
  2191. read_msi_msg(irq, &msg);
  2192. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2193. msg.data |= MSI_DATA_VECTOR(vector);
  2194. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2195. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2196. write_msi_msg(irq, &msg);
  2197. irq_desc[irq].affinity = mask;
  2198. }
  2199. #endif /* CONFIG_SMP */
  2200. /*
  2201. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2202. * which implement the MSI or MSI-X Capability Structure.
  2203. */
  2204. static struct irq_chip msi_chip = {
  2205. .name = "PCI-MSI",
  2206. .unmask = unmask_msi_irq,
  2207. .mask = mask_msi_irq,
  2208. .ack = ack_ioapic_irq,
  2209. #ifdef CONFIG_SMP
  2210. .set_affinity = set_msi_irq_affinity,
  2211. #endif
  2212. .retrigger = ioapic_retrigger_irq,
  2213. };
  2214. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  2215. {
  2216. struct msi_msg msg;
  2217. int irq, ret;
  2218. irq = create_irq();
  2219. if (irq < 0)
  2220. return irq;
  2221. ret = msi_compose_msg(dev, irq, &msg);
  2222. if (ret < 0) {
  2223. destroy_irq(irq);
  2224. return ret;
  2225. }
  2226. set_irq_msi(irq, desc);
  2227. write_msi_msg(irq, &msg);
  2228. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
  2229. "edge");
  2230. return 0;
  2231. }
  2232. void arch_teardown_msi_irq(unsigned int irq)
  2233. {
  2234. destroy_irq(irq);
  2235. }
  2236. #endif /* CONFIG_PCI_MSI */
  2237. /*
  2238. * Hypertransport interrupt support
  2239. */
  2240. #ifdef CONFIG_HT_IRQ
  2241. #ifdef CONFIG_SMP
  2242. static void target_ht_irq(unsigned int irq, unsigned int dest)
  2243. {
  2244. struct ht_irq_msg msg;
  2245. fetch_ht_irq_msg(irq, &msg);
  2246. msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
  2247. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  2248. msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
  2249. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  2250. write_ht_irq_msg(irq, &msg);
  2251. }
  2252. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  2253. {
  2254. unsigned int dest;
  2255. cpumask_t tmp;
  2256. cpus_and(tmp, mask, cpu_online_map);
  2257. if (cpus_empty(tmp))
  2258. tmp = TARGET_CPUS;
  2259. cpus_and(mask, tmp, CPU_MASK_ALL);
  2260. dest = cpu_mask_to_apicid(mask);
  2261. target_ht_irq(irq, dest);
  2262. irq_desc[irq].affinity = mask;
  2263. }
  2264. #endif
  2265. static struct irq_chip ht_irq_chip = {
  2266. .name = "PCI-HT",
  2267. .mask = mask_ht_irq,
  2268. .unmask = unmask_ht_irq,
  2269. .ack = ack_ioapic_irq,
  2270. #ifdef CONFIG_SMP
  2271. .set_affinity = set_ht_irq_affinity,
  2272. #endif
  2273. .retrigger = ioapic_retrigger_irq,
  2274. };
  2275. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  2276. {
  2277. int vector;
  2278. vector = assign_irq_vector(irq);
  2279. if (vector >= 0) {
  2280. struct ht_irq_msg msg;
  2281. unsigned dest;
  2282. cpumask_t tmp;
  2283. cpus_clear(tmp);
  2284. cpu_set(vector >> 8, tmp);
  2285. dest = cpu_mask_to_apicid(tmp);
  2286. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  2287. msg.address_lo =
  2288. HT_IRQ_LOW_BASE |
  2289. HT_IRQ_LOW_DEST_ID(dest) |
  2290. HT_IRQ_LOW_VECTOR(vector) |
  2291. ((INT_DEST_MODE == 0) ?
  2292. HT_IRQ_LOW_DM_PHYSICAL :
  2293. HT_IRQ_LOW_DM_LOGICAL) |
  2294. HT_IRQ_LOW_RQEOI_EDGE |
  2295. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2296. HT_IRQ_LOW_MT_FIXED :
  2297. HT_IRQ_LOW_MT_ARBITRATED) |
  2298. HT_IRQ_LOW_IRQ_MASKED;
  2299. write_ht_irq_msg(irq, &msg);
  2300. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  2301. handle_edge_irq, "edge");
  2302. }
  2303. return vector;
  2304. }
  2305. #endif /* CONFIG_HT_IRQ */
  2306. /* --------------------------------------------------------------------------
  2307. ACPI-based IOAPIC Configuration
  2308. -------------------------------------------------------------------------- */
  2309. #ifdef CONFIG_ACPI
  2310. int __init io_apic_get_unique_id(int ioapic, int apic_id)
  2311. {
  2312. union IO_APIC_reg_00 reg_00;
  2313. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  2314. physid_mask_t tmp;
  2315. unsigned long flags;
  2316. int i = 0;
  2317. /*
  2318. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  2319. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  2320. * supports up to 16 on one shared APIC bus.
  2321. *
  2322. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  2323. * advantage of new APIC bus architecture.
  2324. */
  2325. if (physids_empty(apic_id_map))
  2326. apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
  2327. spin_lock_irqsave(&ioapic_lock, flags);
  2328. reg_00.raw = io_apic_read(ioapic, 0);
  2329. spin_unlock_irqrestore(&ioapic_lock, flags);
  2330. if (apic_id >= get_physical_broadcast()) {
  2331. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  2332. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  2333. apic_id = reg_00.bits.ID;
  2334. }
  2335. /*
  2336. * Every APIC in a system must have a unique ID or we get lots of nice
  2337. * 'stuck on smp_invalidate_needed IPI wait' messages.
  2338. */
  2339. if (check_apicid_used(apic_id_map, apic_id)) {
  2340. for (i = 0; i < get_physical_broadcast(); i++) {
  2341. if (!check_apicid_used(apic_id_map, i))
  2342. break;
  2343. }
  2344. if (i == get_physical_broadcast())
  2345. panic("Max apic_id exceeded!\n");
  2346. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  2347. "trying %d\n", ioapic, apic_id, i);
  2348. apic_id = i;
  2349. }
  2350. tmp = apicid_to_cpu_present(apic_id);
  2351. physids_or(apic_id_map, apic_id_map, tmp);
  2352. if (reg_00.bits.ID != apic_id) {
  2353. reg_00.bits.ID = apic_id;
  2354. spin_lock_irqsave(&ioapic_lock, flags);
  2355. io_apic_write(ioapic, 0, reg_00.raw);
  2356. reg_00.raw = io_apic_read(ioapic, 0);
  2357. spin_unlock_irqrestore(&ioapic_lock, flags);
  2358. /* Sanity check */
  2359. if (reg_00.bits.ID != apic_id) {
  2360. printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
  2361. return -1;
  2362. }
  2363. }
  2364. apic_printk(APIC_VERBOSE, KERN_INFO
  2365. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  2366. return apic_id;
  2367. }
  2368. int __init io_apic_get_version(int ioapic)
  2369. {
  2370. union IO_APIC_reg_01 reg_01;
  2371. unsigned long flags;
  2372. spin_lock_irqsave(&ioapic_lock, flags);
  2373. reg_01.raw = io_apic_read(ioapic, 1);
  2374. spin_unlock_irqrestore(&ioapic_lock, flags);
  2375. return reg_01.bits.version;
  2376. }
  2377. int __init io_apic_get_redir_entries(int ioapic)
  2378. {
  2379. union IO_APIC_reg_01 reg_01;
  2380. unsigned long flags;
  2381. spin_lock_irqsave(&ioapic_lock, flags);
  2382. reg_01.raw = io_apic_read(ioapic, 1);
  2383. spin_unlock_irqrestore(&ioapic_lock, flags);
  2384. return reg_01.bits.entries;
  2385. }
  2386. int io_apic_set_pci_routing(int ioapic, int pin, int irq, int edge_level, int active_high_low)
  2387. {
  2388. struct IO_APIC_route_entry entry;
  2389. if (!IO_APIC_IRQ(irq)) {
  2390. printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  2391. ioapic);
  2392. return -EINVAL;
  2393. }
  2394. /*
  2395. * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
  2396. * Note that we mask (disable) IRQs now -- these get enabled when the
  2397. * corresponding device driver registers for this IRQ.
  2398. */
  2399. memset(&entry, 0, sizeof(entry));
  2400. entry.delivery_mode = INT_DELIVERY_MODE;
  2401. entry.dest_mode = INT_DEST_MODE;
  2402. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  2403. entry.trigger = edge_level;
  2404. entry.polarity = active_high_low;
  2405. entry.mask = 1;
  2406. /*
  2407. * IRQs < 16 are already in the irq_2_pin[] map
  2408. */
  2409. if (irq >= 16)
  2410. add_pin_to_irq(irq, ioapic, pin);
  2411. entry.vector = assign_irq_vector(irq);
  2412. apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
  2413. "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
  2414. mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
  2415. edge_level, active_high_low);
  2416. ioapic_register_intr(irq, entry.vector, edge_level);
  2417. if (!ioapic && (irq < 16))
  2418. disable_8259A_irq(irq);
  2419. ioapic_write_entry(ioapic, pin, entry);
  2420. return 0;
  2421. }
  2422. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  2423. {
  2424. int i;
  2425. if (skip_ioapic_setup)
  2426. return -1;
  2427. for (i = 0; i < mp_irq_entries; i++)
  2428. if (mp_irqs[i].mp_irqtype == mp_INT &&
  2429. mp_irqs[i].mp_srcbusirq == bus_irq)
  2430. break;
  2431. if (i >= mp_irq_entries)
  2432. return -1;
  2433. *trigger = irq_trigger(i);
  2434. *polarity = irq_polarity(i);
  2435. return 0;
  2436. }
  2437. #endif /* CONFIG_ACPI */
  2438. static int __init parse_disable_timer_pin_1(char *arg)
  2439. {
  2440. disable_timer_pin_1 = 1;
  2441. return 0;
  2442. }
  2443. early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
  2444. static int __init parse_enable_timer_pin_1(char *arg)
  2445. {
  2446. disable_timer_pin_1 = -1;
  2447. return 0;
  2448. }
  2449. early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
  2450. static int __init parse_noapic(char *arg)
  2451. {
  2452. /* disable IO-APIC */
  2453. disable_ioapic_setup();
  2454. return 0;
  2455. }
  2456. early_param("noapic", parse_noapic);
  2457. void __init ioapic_init_mappings(void)
  2458. {
  2459. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  2460. int i;
  2461. for (i = 0; i < nr_ioapics; i++) {
  2462. if (smp_found_config) {
  2463. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  2464. if (!ioapic_phys) {
  2465. printk(KERN_ERR
  2466. "WARNING: bogus zero IO-APIC "
  2467. "address found in MPTABLE, "
  2468. "disabling IO/APIC support!\n");
  2469. smp_found_config = 0;
  2470. skip_ioapic_setup = 1;
  2471. goto fake_ioapic_page;
  2472. }
  2473. } else {
  2474. fake_ioapic_page:
  2475. ioapic_phys = (unsigned long)
  2476. alloc_bootmem_pages(PAGE_SIZE);
  2477. ioapic_phys = __pa(ioapic_phys);
  2478. }
  2479. set_fixmap_nocache(idx, ioapic_phys);
  2480. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  2481. __fix_to_virt(idx), ioapic_phys);
  2482. idx++;
  2483. }
  2484. }