io_apic_32.c 71 KB

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