io_apic.c 69 KB

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