io_apic_32.c 72 KB

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