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