io_apic_32.c 70 KB

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