io_apic_32.c 69 KB

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