io_apic.c 69 KB

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