io_apic_64.c 72 KB

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