io_apic_32.c 71 KB

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