io_apic.c 71 KB

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