io_apic_32.c 71 KB

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