io_apic.c 68 KB

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