io_apic_64.c 60 KB

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