io_apic_32.c 69 KB

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