io_apic.c 67 KB

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