io_apic.c 71 KB

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