io_apic.c 69 KB

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