io_apic.c 68 KB

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