io_apic_32.c 69 KB

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