io_apic.c 68 KB

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