io_apic_32.c 62 KB

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