io_apic_32.c 62 KB

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