io_apic.c 68 KB

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