io_apic_32.c 71 KB

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