io_apic.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041
  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/compiler.h>
  30. #include <linux/acpi.h>
  31. #include <linux/module.h>
  32. #include <linux/sysdev.h>
  33. #include <linux/msi.h>
  34. #include <linux/htirq.h>
  35. #include <linux/freezer.h>
  36. #include <linux/kthread.h>
  37. #include <linux/jiffies.h> /* time_after() */
  38. #ifdef CONFIG_ACPI
  39. #include <acpi/acpi_bus.h>
  40. #endif
  41. #include <linux/bootmem.h>
  42. #include <linux/dmar.h>
  43. #include <linux/hpet.h>
  44. #include <asm/idle.h>
  45. #include <asm/io.h>
  46. #include <asm/smp.h>
  47. #include <asm/desc.h>
  48. #include <asm/proto.h>
  49. #include <asm/acpi.h>
  50. #include <asm/dma.h>
  51. #include <asm/timer.h>
  52. #include <asm/i8259.h>
  53. #include <asm/nmi.h>
  54. #include <asm/msidef.h>
  55. #include <asm/hypertransport.h>
  56. #include <asm/setup.h>
  57. #include <asm/irq_remapping.h>
  58. #include <asm/hpet.h>
  59. #include <asm/uv/uv_hub.h>
  60. #include <asm/uv/uv_irq.h>
  61. #include <mach_ipi.h>
  62. #include <mach_apic.h>
  63. #include <mach_apicdef.h>
  64. #define __apicdebuginit(type) static type __init
  65. /*
  66. * Is the SiS APIC rmw bug present ?
  67. * -1 = don't know, 0 = no, 1 = yes
  68. */
  69. int sis_apic_bug = -1;
  70. static DEFINE_SPINLOCK(ioapic_lock);
  71. static DEFINE_SPINLOCK(vector_lock);
  72. /*
  73. * # of IRQ routing registers
  74. */
  75. int nr_ioapic_registers[MAX_IO_APICS];
  76. /* I/O APIC entries */
  77. struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
  78. int nr_ioapics;
  79. /* MP IRQ source entries */
  80. struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  81. /* # of MP IRQ source entries */
  82. int mp_irq_entries;
  83. #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
  84. int mp_bus_id_to_type[MAX_MP_BUSSES];
  85. #endif
  86. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  87. int skip_ioapic_setup;
  88. static int __init parse_noapic(char *str)
  89. {
  90. /* disable IO-APIC */
  91. disable_ioapic_setup();
  92. return 0;
  93. }
  94. early_param("noapic", parse_noapic);
  95. struct irq_pin_list;
  96. /*
  97. * This is performance-critical, we want to do it O(1)
  98. *
  99. * the indexing order of this array favors 1:1 mappings
  100. * between pins and IRQs.
  101. */
  102. struct irq_pin_list {
  103. int apic, pin;
  104. struct irq_pin_list *next;
  105. };
  106. static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
  107. {
  108. struct irq_pin_list *pin;
  109. int node;
  110. node = cpu_to_node(cpu);
  111. pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
  112. printk(KERN_DEBUG " alloc irq_2_pin on cpu %d node %d\n", cpu, node);
  113. return pin;
  114. }
  115. struct irq_cfg {
  116. struct irq_pin_list *irq_2_pin;
  117. cpumask_t domain;
  118. cpumask_t old_domain;
  119. unsigned move_cleanup_count;
  120. u8 vector;
  121. u8 move_in_progress : 1;
  122. };
  123. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  124. #ifdef CONFIG_SPARSE_IRQ
  125. static struct irq_cfg irq_cfgx[] = {
  126. #else
  127. static struct irq_cfg irq_cfgx[NR_IRQS] = {
  128. #endif
  129. [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
  130. [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
  131. [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
  132. [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
  133. [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
  134. [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
  135. [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
  136. [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
  137. [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
  138. [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
  139. [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
  140. [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
  141. [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
  142. [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
  143. [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
  144. [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
  145. };
  146. void __init arch_early_irq_init(void)
  147. {
  148. struct irq_cfg *cfg;
  149. struct irq_desc *desc;
  150. int count;
  151. int i;
  152. cfg = irq_cfgx;
  153. count = ARRAY_SIZE(irq_cfgx);
  154. for (i = 0; i < count; i++) {
  155. desc = irq_to_desc(i);
  156. desc->chip_data = &cfg[i];
  157. }
  158. }
  159. #ifdef CONFIG_SPARSE_IRQ
  160. static struct irq_cfg *irq_cfg(unsigned int irq)
  161. {
  162. struct irq_cfg *cfg = NULL;
  163. struct irq_desc *desc;
  164. desc = irq_to_desc(irq);
  165. if (desc)
  166. cfg = desc->chip_data;
  167. return cfg;
  168. }
  169. static struct irq_cfg *get_one_free_irq_cfg(int cpu)
  170. {
  171. struct irq_cfg *cfg;
  172. int node;
  173. node = cpu_to_node(cpu);
  174. cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
  175. printk(KERN_DEBUG " alloc irq_cfg on cpu %d node %d\n", cpu, node);
  176. return cfg;
  177. }
  178. void arch_init_chip_data(struct irq_desc *desc, int cpu)
  179. {
  180. struct irq_cfg *cfg;
  181. cfg = desc->chip_data;
  182. if (!cfg) {
  183. desc->chip_data = get_one_free_irq_cfg(cpu);
  184. if (!desc->chip_data) {
  185. printk(KERN_ERR "can not alloc irq_cfg\n");
  186. BUG_ON(1);
  187. }
  188. }
  189. }
  190. #else
  191. static struct irq_cfg *irq_cfg(unsigned int irq)
  192. {
  193. return irq < nr_irqs ? irq_cfgx + irq : NULL;
  194. }
  195. #endif
  196. static inline void set_extra_move_desc(struct irq_desc *desc, cpumask_t mask)
  197. {
  198. }
  199. struct io_apic {
  200. unsigned int index;
  201. unsigned int unused[3];
  202. unsigned int data;
  203. };
  204. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  205. {
  206. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  207. + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
  208. }
  209. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  210. {
  211. struct io_apic __iomem *io_apic = io_apic_base(apic);
  212. writel(reg, &io_apic->index);
  213. return readl(&io_apic->data);
  214. }
  215. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  216. {
  217. struct io_apic __iomem *io_apic = io_apic_base(apic);
  218. writel(reg, &io_apic->index);
  219. writel(value, &io_apic->data);
  220. }
  221. /*
  222. * Re-write a value: to be used for read-modify-write
  223. * cycles where the read already set up the index register.
  224. *
  225. * Older SiS APIC requires we rewrite the index register
  226. */
  227. static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
  228. {
  229. struct io_apic __iomem *io_apic = io_apic_base(apic);
  230. if (sis_apic_bug)
  231. writel(reg, &io_apic->index);
  232. writel(value, &io_apic->data);
  233. }
  234. static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
  235. {
  236. struct irq_pin_list *entry;
  237. unsigned long flags;
  238. spin_lock_irqsave(&ioapic_lock, flags);
  239. entry = cfg->irq_2_pin;
  240. for (;;) {
  241. unsigned int reg;
  242. int pin;
  243. if (!entry)
  244. break;
  245. pin = entry->pin;
  246. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  247. /* Is the remote IRR bit set? */
  248. if (reg & IO_APIC_REDIR_REMOTE_IRR) {
  249. spin_unlock_irqrestore(&ioapic_lock, flags);
  250. return true;
  251. }
  252. if (!entry->next)
  253. break;
  254. entry = entry->next;
  255. }
  256. spin_unlock_irqrestore(&ioapic_lock, flags);
  257. return false;
  258. }
  259. union entry_union {
  260. struct { u32 w1, w2; };
  261. struct IO_APIC_route_entry entry;
  262. };
  263. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  264. {
  265. union entry_union eu;
  266. unsigned long flags;
  267. spin_lock_irqsave(&ioapic_lock, flags);
  268. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  269. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  270. spin_unlock_irqrestore(&ioapic_lock, flags);
  271. return eu.entry;
  272. }
  273. /*
  274. * When we write a new IO APIC routing entry, we need to write the high
  275. * word first! If the mask bit in the low word is clear, we will enable
  276. * the interrupt, and we need to make sure the entry is fully populated
  277. * before that happens.
  278. */
  279. static void
  280. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  281. {
  282. union entry_union eu;
  283. eu.entry = e;
  284. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  285. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  286. }
  287. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  288. {
  289. unsigned long flags;
  290. spin_lock_irqsave(&ioapic_lock, flags);
  291. __ioapic_write_entry(apic, pin, e);
  292. spin_unlock_irqrestore(&ioapic_lock, flags);
  293. }
  294. /*
  295. * When we mask an IO APIC routing entry, we need to write the low
  296. * word first, in order to set the mask bit before we change the
  297. * high bits!
  298. */
  299. static void ioapic_mask_entry(int apic, int pin)
  300. {
  301. unsigned long flags;
  302. union entry_union eu = { .entry.mask = 1 };
  303. spin_lock_irqsave(&ioapic_lock, flags);
  304. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  305. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  306. spin_unlock_irqrestore(&ioapic_lock, flags);
  307. }
  308. #ifdef CONFIG_SMP
  309. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
  310. {
  311. int apic, pin;
  312. struct irq_pin_list *entry;
  313. u8 vector = cfg->vector;
  314. entry = cfg->irq_2_pin;
  315. for (;;) {
  316. unsigned int reg;
  317. if (!entry)
  318. break;
  319. apic = entry->apic;
  320. pin = entry->pin;
  321. #ifdef CONFIG_INTR_REMAP
  322. /*
  323. * With interrupt-remapping, destination information comes
  324. * from interrupt-remapping table entry.
  325. */
  326. if (!irq_remapped(irq))
  327. io_apic_write(apic, 0x11 + pin*2, dest);
  328. #else
  329. io_apic_write(apic, 0x11 + pin*2, dest);
  330. #endif
  331. reg = io_apic_read(apic, 0x10 + pin*2);
  332. reg &= ~IO_APIC_REDIR_VECTOR_MASK;
  333. reg |= vector;
  334. io_apic_modify(apic, 0x10 + pin*2, reg);
  335. if (!entry->next)
  336. break;
  337. entry = entry->next;
  338. }
  339. }
  340. static int assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask);
  341. static void set_ioapic_affinity_irq_desc(struct irq_desc *desc, cpumask_t mask)
  342. {
  343. struct irq_cfg *cfg;
  344. unsigned long flags;
  345. unsigned int dest;
  346. cpumask_t tmp;
  347. unsigned int irq;
  348. cpus_and(tmp, mask, cpu_online_map);
  349. if (cpus_empty(tmp))
  350. return;
  351. irq = desc->irq;
  352. cfg = desc->chip_data;
  353. if (assign_irq_vector(irq, cfg, mask))
  354. return;
  355. set_extra_move_desc(desc, mask);
  356. cpus_and(tmp, cfg->domain, mask);
  357. dest = cpu_mask_to_apicid(tmp);
  358. /*
  359. * Only the high 8 bits are valid.
  360. */
  361. dest = SET_APIC_LOGICAL_ID(dest);
  362. spin_lock_irqsave(&ioapic_lock, flags);
  363. __target_IO_APIC_irq(irq, dest, cfg);
  364. desc->affinity = mask;
  365. spin_unlock_irqrestore(&ioapic_lock, flags);
  366. }
  367. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  368. {
  369. struct irq_desc *desc;
  370. desc = irq_to_desc(irq);
  371. set_ioapic_affinity_irq_desc(desc, mask);
  372. }
  373. #endif /* CONFIG_SMP */
  374. /*
  375. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  376. * shared ISA-space IRQs, so we have to support them. We are super
  377. * fast in the common case, and fast for shared ISA-space IRQs.
  378. */
  379. static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
  380. {
  381. struct irq_pin_list *entry;
  382. entry = cfg->irq_2_pin;
  383. if (!entry) {
  384. entry = get_one_free_irq_2_pin(cpu);
  385. if (!entry) {
  386. printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
  387. apic, pin);
  388. return;
  389. }
  390. cfg->irq_2_pin = entry;
  391. entry->apic = apic;
  392. entry->pin = pin;
  393. return;
  394. }
  395. while (entry->next) {
  396. /* not again, please */
  397. if (entry->apic == apic && entry->pin == pin)
  398. return;
  399. entry = entry->next;
  400. }
  401. entry->next = get_one_free_irq_2_pin(cpu);
  402. entry = entry->next;
  403. entry->apic = apic;
  404. entry->pin = pin;
  405. }
  406. /*
  407. * Reroute an IRQ to a different pin.
  408. */
  409. static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
  410. int oldapic, int oldpin,
  411. int newapic, int newpin)
  412. {
  413. struct irq_pin_list *entry = cfg->irq_2_pin;
  414. int replaced = 0;
  415. while (entry) {
  416. if (entry->apic == oldapic && entry->pin == oldpin) {
  417. entry->apic = newapic;
  418. entry->pin = newpin;
  419. replaced = 1;
  420. /* every one is different, right? */
  421. break;
  422. }
  423. entry = entry->next;
  424. }
  425. /* why? call replace before add? */
  426. if (!replaced)
  427. add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
  428. }
  429. static inline void io_apic_modify_irq(struct irq_cfg *cfg,
  430. int mask_and, int mask_or,
  431. void (*final)(struct irq_pin_list *entry))
  432. {
  433. int pin;
  434. struct irq_pin_list *entry;
  435. for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
  436. unsigned int reg;
  437. pin = entry->pin;
  438. reg = io_apic_read(entry->apic, 0x10 + pin * 2);
  439. reg &= mask_and;
  440. reg |= mask_or;
  441. io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
  442. if (final)
  443. final(entry);
  444. }
  445. }
  446. static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
  447. {
  448. io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
  449. }
  450. #ifdef CONFIG_X86_64
  451. void io_apic_sync(struct irq_pin_list *entry)
  452. {
  453. /*
  454. * Synchronize the IO-APIC and the CPU by doing
  455. * a dummy read from the IO-APIC
  456. */
  457. struct io_apic __iomem *io_apic;
  458. io_apic = io_apic_base(entry->apic);
  459. readl(&io_apic->data);
  460. }
  461. static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
  462. {
  463. io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
  464. }
  465. #else /* CONFIG_X86_32 */
  466. static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
  467. {
  468. io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
  469. }
  470. static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
  471. {
  472. io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
  473. IO_APIC_REDIR_MASKED, NULL);
  474. }
  475. static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
  476. {
  477. io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
  478. IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
  479. }
  480. #endif /* CONFIG_X86_32 */
  481. static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
  482. {
  483. struct irq_cfg *cfg = desc->chip_data;
  484. unsigned long flags;
  485. BUG_ON(!cfg);
  486. spin_lock_irqsave(&ioapic_lock, flags);
  487. __mask_IO_APIC_irq(cfg);
  488. spin_unlock_irqrestore(&ioapic_lock, flags);
  489. }
  490. static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
  491. {
  492. struct irq_cfg *cfg = desc->chip_data;
  493. unsigned long flags;
  494. spin_lock_irqsave(&ioapic_lock, flags);
  495. __unmask_IO_APIC_irq(cfg);
  496. spin_unlock_irqrestore(&ioapic_lock, flags);
  497. }
  498. static void mask_IO_APIC_irq(unsigned int irq)
  499. {
  500. struct irq_desc *desc = irq_to_desc(irq);
  501. mask_IO_APIC_irq_desc(desc);
  502. }
  503. static void unmask_IO_APIC_irq(unsigned int irq)
  504. {
  505. struct irq_desc *desc = irq_to_desc(irq);
  506. unmask_IO_APIC_irq_desc(desc);
  507. }
  508. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  509. {
  510. struct IO_APIC_route_entry entry;
  511. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  512. entry = ioapic_read_entry(apic, pin);
  513. if (entry.delivery_mode == dest_SMI)
  514. return;
  515. /*
  516. * Disable it in the IO-APIC irq-routing table:
  517. */
  518. ioapic_mask_entry(apic, pin);
  519. }
  520. static void clear_IO_APIC (void)
  521. {
  522. int apic, pin;
  523. for (apic = 0; apic < nr_ioapics; apic++)
  524. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  525. clear_IO_APIC_pin(apic, pin);
  526. }
  527. #if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
  528. void send_IPI_self(int vector)
  529. {
  530. unsigned int cfg;
  531. /*
  532. * Wait for idle.
  533. */
  534. apic_wait_icr_idle();
  535. cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
  536. /*
  537. * Send the IPI. The write to APIC_ICR fires this off.
  538. */
  539. apic_write(APIC_ICR, cfg);
  540. }
  541. #endif /* !CONFIG_SMP && CONFIG_X86_32*/
  542. #ifdef CONFIG_X86_32
  543. /*
  544. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  545. * specific CPU-side IRQs.
  546. */
  547. #define MAX_PIRQS 8
  548. static int pirq_entries [MAX_PIRQS];
  549. static int pirqs_enabled;
  550. static int __init ioapic_pirq_setup(char *str)
  551. {
  552. int i, max;
  553. int ints[MAX_PIRQS+1];
  554. get_options(str, ARRAY_SIZE(ints), ints);
  555. for (i = 0; i < MAX_PIRQS; i++)
  556. pirq_entries[i] = -1;
  557. pirqs_enabled = 1;
  558. apic_printk(APIC_VERBOSE, KERN_INFO
  559. "PIRQ redirection, working around broken MP-BIOS.\n");
  560. max = MAX_PIRQS;
  561. if (ints[0] < MAX_PIRQS)
  562. max = ints[0];
  563. for (i = 0; i < max; i++) {
  564. apic_printk(APIC_VERBOSE, KERN_DEBUG
  565. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  566. /*
  567. * PIRQs are mapped upside down, usually.
  568. */
  569. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  570. }
  571. return 1;
  572. }
  573. __setup("pirq=", ioapic_pirq_setup);
  574. #endif /* CONFIG_X86_32 */
  575. #ifdef CONFIG_INTR_REMAP
  576. /* I/O APIC RTE contents at the OS boot up */
  577. static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
  578. /*
  579. * Saves and masks all the unmasked IO-APIC RTE's
  580. */
  581. int save_mask_IO_APIC_setup(void)
  582. {
  583. union IO_APIC_reg_01 reg_01;
  584. unsigned long flags;
  585. int apic, pin;
  586. /*
  587. * The number of IO-APIC IRQ registers (== #pins):
  588. */
  589. for (apic = 0; apic < nr_ioapics; apic++) {
  590. spin_lock_irqsave(&ioapic_lock, flags);
  591. reg_01.raw = io_apic_read(apic, 1);
  592. spin_unlock_irqrestore(&ioapic_lock, flags);
  593. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  594. }
  595. for (apic = 0; apic < nr_ioapics; apic++) {
  596. early_ioapic_entries[apic] =
  597. kzalloc(sizeof(struct IO_APIC_route_entry) *
  598. nr_ioapic_registers[apic], GFP_KERNEL);
  599. if (!early_ioapic_entries[apic])
  600. goto nomem;
  601. }
  602. for (apic = 0; apic < nr_ioapics; apic++)
  603. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  604. struct IO_APIC_route_entry entry;
  605. entry = early_ioapic_entries[apic][pin] =
  606. ioapic_read_entry(apic, pin);
  607. if (!entry.mask) {
  608. entry.mask = 1;
  609. ioapic_write_entry(apic, pin, entry);
  610. }
  611. }
  612. return 0;
  613. nomem:
  614. while (apic >= 0)
  615. kfree(early_ioapic_entries[apic--]);
  616. memset(early_ioapic_entries, 0,
  617. ARRAY_SIZE(early_ioapic_entries));
  618. return -ENOMEM;
  619. }
  620. void restore_IO_APIC_setup(void)
  621. {
  622. int apic, pin;
  623. for (apic = 0; apic < nr_ioapics; apic++) {
  624. if (!early_ioapic_entries[apic])
  625. break;
  626. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  627. ioapic_write_entry(apic, pin,
  628. early_ioapic_entries[apic][pin]);
  629. kfree(early_ioapic_entries[apic]);
  630. early_ioapic_entries[apic] = NULL;
  631. }
  632. }
  633. void reinit_intr_remapped_IO_APIC(int intr_remapping)
  634. {
  635. /*
  636. * for now plain restore of previous settings.
  637. * TBD: In the case of OS enabling interrupt-remapping,
  638. * IO-APIC RTE's need to be setup to point to interrupt-remapping
  639. * table entries. for now, do a plain restore, and wait for
  640. * the setup_IO_APIC_irqs() to do proper initialization.
  641. */
  642. restore_IO_APIC_setup();
  643. }
  644. #endif
  645. /*
  646. * Find the IRQ entry number of a certain pin.
  647. */
  648. static int find_irq_entry(int apic, int pin, int type)
  649. {
  650. int i;
  651. for (i = 0; i < mp_irq_entries; i++)
  652. if (mp_irqs[i].mp_irqtype == type &&
  653. (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
  654. mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
  655. mp_irqs[i].mp_dstirq == pin)
  656. return i;
  657. return -1;
  658. }
  659. /*
  660. * Find the pin to which IRQ[irq] (ISA) is connected
  661. */
  662. static int __init find_isa_irq_pin(int irq, int type)
  663. {
  664. int i;
  665. for (i = 0; i < mp_irq_entries; i++) {
  666. int lbus = mp_irqs[i].mp_srcbus;
  667. if (test_bit(lbus, mp_bus_not_pci) &&
  668. (mp_irqs[i].mp_irqtype == type) &&
  669. (mp_irqs[i].mp_srcbusirq == irq))
  670. return mp_irqs[i].mp_dstirq;
  671. }
  672. return -1;
  673. }
  674. static int __init find_isa_irq_apic(int irq, int type)
  675. {
  676. int i;
  677. for (i = 0; i < mp_irq_entries; i++) {
  678. int lbus = mp_irqs[i].mp_srcbus;
  679. if (test_bit(lbus, mp_bus_not_pci) &&
  680. (mp_irqs[i].mp_irqtype == type) &&
  681. (mp_irqs[i].mp_srcbusirq == irq))
  682. break;
  683. }
  684. if (i < mp_irq_entries) {
  685. int apic;
  686. for(apic = 0; apic < nr_ioapics; apic++) {
  687. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
  688. return apic;
  689. }
  690. }
  691. return -1;
  692. }
  693. /*
  694. * Find a specific PCI IRQ entry.
  695. * Not an __init, possibly needed by modules
  696. */
  697. static int pin_2_irq(int idx, int apic, int pin);
  698. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  699. {
  700. int apic, i, best_guess = -1;
  701. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  702. bus, slot, pin);
  703. if (test_bit(bus, mp_bus_not_pci)) {
  704. apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  705. return -1;
  706. }
  707. for (i = 0; i < mp_irq_entries; i++) {
  708. int lbus = mp_irqs[i].mp_srcbus;
  709. for (apic = 0; apic < nr_ioapics; apic++)
  710. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
  711. mp_irqs[i].mp_dstapic == MP_APIC_ALL)
  712. break;
  713. if (!test_bit(lbus, mp_bus_not_pci) &&
  714. !mp_irqs[i].mp_irqtype &&
  715. (bus == lbus) &&
  716. (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
  717. int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
  718. if (!(apic || IO_APIC_IRQ(irq)))
  719. continue;
  720. if (pin == (mp_irqs[i].mp_srcbusirq & 3))
  721. return irq;
  722. /*
  723. * Use the first all-but-pin matching entry as a
  724. * best-guess fuzzy result for broken mptables.
  725. */
  726. if (best_guess < 0)
  727. best_guess = irq;
  728. }
  729. }
  730. return best_guess;
  731. }
  732. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  733. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  734. /*
  735. * EISA Edge/Level control register, ELCR
  736. */
  737. static int EISA_ELCR(unsigned int irq)
  738. {
  739. if (irq < NR_IRQS_LEGACY) {
  740. unsigned int port = 0x4d0 + (irq >> 3);
  741. return (inb(port) >> (irq & 7)) & 1;
  742. }
  743. apic_printk(APIC_VERBOSE, KERN_INFO
  744. "Broken MPtable reports ISA irq %d\n", irq);
  745. return 0;
  746. }
  747. #endif
  748. /* ISA interrupts are always polarity zero edge triggered,
  749. * when listed as conforming in the MP table. */
  750. #define default_ISA_trigger(idx) (0)
  751. #define default_ISA_polarity(idx) (0)
  752. /* EISA interrupts are always polarity zero and can be edge or level
  753. * trigger depending on the ELCR value. If an interrupt is listed as
  754. * EISA conforming in the MP table, that means its trigger type must
  755. * be read in from the ELCR */
  756. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
  757. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  758. /* PCI interrupts are always polarity one level triggered,
  759. * when listed as conforming in the MP table. */
  760. #define default_PCI_trigger(idx) (1)
  761. #define default_PCI_polarity(idx) (1)
  762. /* MCA interrupts are always polarity zero level triggered,
  763. * when listed as conforming in the MP table. */
  764. #define default_MCA_trigger(idx) (1)
  765. #define default_MCA_polarity(idx) default_ISA_polarity(idx)
  766. static int MPBIOS_polarity(int idx)
  767. {
  768. int bus = mp_irqs[idx].mp_srcbus;
  769. int polarity;
  770. /*
  771. * Determine IRQ line polarity (high active or low active):
  772. */
  773. switch (mp_irqs[idx].mp_irqflag & 3)
  774. {
  775. case 0: /* conforms, ie. bus-type dependent polarity */
  776. if (test_bit(bus, mp_bus_not_pci))
  777. polarity = default_ISA_polarity(idx);
  778. else
  779. polarity = default_PCI_polarity(idx);
  780. break;
  781. case 1: /* high active */
  782. {
  783. polarity = 0;
  784. break;
  785. }
  786. case 2: /* reserved */
  787. {
  788. printk(KERN_WARNING "broken BIOS!!\n");
  789. polarity = 1;
  790. break;
  791. }
  792. case 3: /* low active */
  793. {
  794. polarity = 1;
  795. break;
  796. }
  797. default: /* invalid */
  798. {
  799. printk(KERN_WARNING "broken BIOS!!\n");
  800. polarity = 1;
  801. break;
  802. }
  803. }
  804. return polarity;
  805. }
  806. static int MPBIOS_trigger(int idx)
  807. {
  808. int bus = mp_irqs[idx].mp_srcbus;
  809. int trigger;
  810. /*
  811. * Determine IRQ trigger mode (edge or level sensitive):
  812. */
  813. switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
  814. {
  815. case 0: /* conforms, ie. bus-type dependent */
  816. if (test_bit(bus, mp_bus_not_pci))
  817. trigger = default_ISA_trigger(idx);
  818. else
  819. trigger = default_PCI_trigger(idx);
  820. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  821. switch (mp_bus_id_to_type[bus]) {
  822. case MP_BUS_ISA: /* ISA pin */
  823. {
  824. /* set before the switch */
  825. break;
  826. }
  827. case MP_BUS_EISA: /* EISA pin */
  828. {
  829. trigger = default_EISA_trigger(idx);
  830. break;
  831. }
  832. case MP_BUS_PCI: /* PCI pin */
  833. {
  834. /* set before the switch */
  835. break;
  836. }
  837. case MP_BUS_MCA: /* MCA pin */
  838. {
  839. trigger = default_MCA_trigger(idx);
  840. break;
  841. }
  842. default:
  843. {
  844. printk(KERN_WARNING "broken BIOS!!\n");
  845. trigger = 1;
  846. break;
  847. }
  848. }
  849. #endif
  850. break;
  851. case 1: /* edge */
  852. {
  853. trigger = 0;
  854. break;
  855. }
  856. case 2: /* reserved */
  857. {
  858. printk(KERN_WARNING "broken BIOS!!\n");
  859. trigger = 1;
  860. break;
  861. }
  862. case 3: /* level */
  863. {
  864. trigger = 1;
  865. break;
  866. }
  867. default: /* invalid */
  868. {
  869. printk(KERN_WARNING "broken BIOS!!\n");
  870. trigger = 0;
  871. break;
  872. }
  873. }
  874. return trigger;
  875. }
  876. static inline int irq_polarity(int idx)
  877. {
  878. return MPBIOS_polarity(idx);
  879. }
  880. static inline int irq_trigger(int idx)
  881. {
  882. return MPBIOS_trigger(idx);
  883. }
  884. int (*ioapic_renumber_irq)(int ioapic, int irq);
  885. static int pin_2_irq(int idx, int apic, int pin)
  886. {
  887. int irq, i;
  888. int bus = mp_irqs[idx].mp_srcbus;
  889. /*
  890. * Debugging check, we are in big trouble if this message pops up!
  891. */
  892. if (mp_irqs[idx].mp_dstirq != pin)
  893. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  894. if (test_bit(bus, mp_bus_not_pci)) {
  895. irq = mp_irqs[idx].mp_srcbusirq;
  896. } else {
  897. /*
  898. * PCI IRQs are mapped in order
  899. */
  900. i = irq = 0;
  901. while (i < apic)
  902. irq += nr_ioapic_registers[i++];
  903. irq += pin;
  904. /*
  905. * For MPS mode, so far only needed by ES7000 platform
  906. */
  907. if (ioapic_renumber_irq)
  908. irq = ioapic_renumber_irq(apic, irq);
  909. }
  910. #ifdef CONFIG_X86_32
  911. /*
  912. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  913. */
  914. if ((pin >= 16) && (pin <= 23)) {
  915. if (pirq_entries[pin-16] != -1) {
  916. if (!pirq_entries[pin-16]) {
  917. apic_printk(APIC_VERBOSE, KERN_DEBUG
  918. "disabling PIRQ%d\n", pin-16);
  919. } else {
  920. irq = pirq_entries[pin-16];
  921. apic_printk(APIC_VERBOSE, KERN_DEBUG
  922. "using PIRQ%d -> IRQ %d\n",
  923. pin-16, irq);
  924. }
  925. }
  926. }
  927. #endif
  928. return irq;
  929. }
  930. void lock_vector_lock(void)
  931. {
  932. /* Used to the online set of cpus does not change
  933. * during assign_irq_vector.
  934. */
  935. spin_lock(&vector_lock);
  936. }
  937. void unlock_vector_lock(void)
  938. {
  939. spin_unlock(&vector_lock);
  940. }
  941. static int __assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
  942. {
  943. /*
  944. * NOTE! The local APIC isn't very good at handling
  945. * multiple interrupts at the same interrupt level.
  946. * As the interrupt level is determined by taking the
  947. * vector number and shifting that right by 4, we
  948. * want to spread these out a bit so that they don't
  949. * all fall in the same interrupt level.
  950. *
  951. * Also, we've got to be careful not to trash gate
  952. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  953. */
  954. static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  955. unsigned int old_vector;
  956. int cpu;
  957. if ((cfg->move_in_progress) || cfg->move_cleanup_count)
  958. return -EBUSY;
  959. /* Only try and allocate irqs on cpus that are present */
  960. cpus_and(mask, mask, cpu_online_map);
  961. old_vector = cfg->vector;
  962. if (old_vector) {
  963. cpumask_t tmp;
  964. cpus_and(tmp, cfg->domain, mask);
  965. if (!cpus_empty(tmp))
  966. return 0;
  967. }
  968. for_each_cpu_mask_nr(cpu, mask) {
  969. cpumask_t domain, new_mask;
  970. int new_cpu;
  971. int vector, offset;
  972. domain = vector_allocation_domain(cpu);
  973. cpus_and(new_mask, domain, cpu_online_map);
  974. vector = current_vector;
  975. offset = current_offset;
  976. next:
  977. vector += 8;
  978. if (vector >= first_system_vector) {
  979. /* If we run out of vectors on large boxen, must share them. */
  980. offset = (offset + 1) % 8;
  981. vector = FIRST_DEVICE_VECTOR + offset;
  982. }
  983. if (unlikely(current_vector == vector))
  984. continue;
  985. #ifdef CONFIG_X86_64
  986. if (vector == IA32_SYSCALL_VECTOR)
  987. goto next;
  988. #else
  989. if (vector == SYSCALL_VECTOR)
  990. goto next;
  991. #endif
  992. for_each_cpu_mask_nr(new_cpu, new_mask)
  993. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  994. goto next;
  995. /* Found one! */
  996. current_vector = vector;
  997. current_offset = offset;
  998. if (old_vector) {
  999. cfg->move_in_progress = 1;
  1000. cfg->old_domain = cfg->domain;
  1001. }
  1002. for_each_cpu_mask_nr(new_cpu, new_mask)
  1003. per_cpu(vector_irq, new_cpu)[vector] = irq;
  1004. cfg->vector = vector;
  1005. cfg->domain = domain;
  1006. return 0;
  1007. }
  1008. return -ENOSPC;
  1009. }
  1010. static int assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
  1011. {
  1012. int err;
  1013. unsigned long flags;
  1014. spin_lock_irqsave(&vector_lock, flags);
  1015. err = __assign_irq_vector(irq, cfg, mask);
  1016. spin_unlock_irqrestore(&vector_lock, flags);
  1017. return err;
  1018. }
  1019. static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
  1020. {
  1021. cpumask_t mask;
  1022. int cpu, vector;
  1023. BUG_ON(!cfg->vector);
  1024. vector = cfg->vector;
  1025. cpus_and(mask, cfg->domain, cpu_online_map);
  1026. for_each_cpu_mask_nr(cpu, mask)
  1027. per_cpu(vector_irq, cpu)[vector] = -1;
  1028. cfg->vector = 0;
  1029. cpus_clear(cfg->domain);
  1030. if (likely(!cfg->move_in_progress))
  1031. return;
  1032. cpus_and(mask, cfg->old_domain, cpu_online_map);
  1033. for_each_cpu_mask_nr(cpu, mask) {
  1034. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
  1035. vector++) {
  1036. if (per_cpu(vector_irq, cpu)[vector] != irq)
  1037. continue;
  1038. per_cpu(vector_irq, cpu)[vector] = -1;
  1039. break;
  1040. }
  1041. }
  1042. cfg->move_in_progress = 0;
  1043. }
  1044. void __setup_vector_irq(int cpu)
  1045. {
  1046. /* Initialize vector_irq on a new cpu */
  1047. /* This function must be called with vector_lock held */
  1048. int irq, vector;
  1049. struct irq_cfg *cfg;
  1050. struct irq_desc *desc;
  1051. /* Mark the inuse vectors */
  1052. for_each_irq_desc(irq, desc) {
  1053. if (!desc)
  1054. continue;
  1055. cfg = desc->chip_data;
  1056. if (!cpu_isset(cpu, cfg->domain))
  1057. continue;
  1058. vector = cfg->vector;
  1059. per_cpu(vector_irq, cpu)[vector] = irq;
  1060. }
  1061. /* Mark the free vectors */
  1062. for (vector = 0; vector < NR_VECTORS; ++vector) {
  1063. irq = per_cpu(vector_irq, cpu)[vector];
  1064. if (irq < 0)
  1065. continue;
  1066. cfg = irq_cfg(irq);
  1067. if (!cpu_isset(cpu, cfg->domain))
  1068. per_cpu(vector_irq, cpu)[vector] = -1;
  1069. }
  1070. }
  1071. static struct irq_chip ioapic_chip;
  1072. #ifdef CONFIG_INTR_REMAP
  1073. static struct irq_chip ir_ioapic_chip;
  1074. #endif
  1075. #define IOAPIC_AUTO -1
  1076. #define IOAPIC_EDGE 0
  1077. #define IOAPIC_LEVEL 1
  1078. #ifdef CONFIG_X86_32
  1079. static inline int IO_APIC_irq_trigger(int irq)
  1080. {
  1081. int apic, idx, pin;
  1082. for (apic = 0; apic < nr_ioapics; apic++) {
  1083. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1084. idx = find_irq_entry(apic, pin, mp_INT);
  1085. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
  1086. return irq_trigger(idx);
  1087. }
  1088. }
  1089. /*
  1090. * nonexistent IRQs are edge default
  1091. */
  1092. return 0;
  1093. }
  1094. #else
  1095. static inline int IO_APIC_irq_trigger(int irq)
  1096. {
  1097. return 1;
  1098. }
  1099. #endif
  1100. static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
  1101. {
  1102. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1103. trigger == IOAPIC_LEVEL)
  1104. desc->status |= IRQ_LEVEL;
  1105. else
  1106. desc->status &= ~IRQ_LEVEL;
  1107. #ifdef CONFIG_INTR_REMAP
  1108. if (irq_remapped(irq)) {
  1109. desc->status |= IRQ_MOVE_PCNTXT;
  1110. if (trigger)
  1111. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  1112. handle_fasteoi_irq,
  1113. "fasteoi");
  1114. else
  1115. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  1116. handle_edge_irq, "edge");
  1117. return;
  1118. }
  1119. #endif
  1120. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1121. trigger == IOAPIC_LEVEL)
  1122. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1123. handle_fasteoi_irq,
  1124. "fasteoi");
  1125. else
  1126. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1127. handle_edge_irq, "edge");
  1128. }
  1129. static int setup_ioapic_entry(int apic, int irq,
  1130. struct IO_APIC_route_entry *entry,
  1131. unsigned int destination, int trigger,
  1132. int polarity, int vector)
  1133. {
  1134. /*
  1135. * add it to the IO-APIC irq-routing table:
  1136. */
  1137. memset(entry,0,sizeof(*entry));
  1138. #ifdef CONFIG_INTR_REMAP
  1139. if (intr_remapping_enabled) {
  1140. struct intel_iommu *iommu = map_ioapic_to_ir(apic);
  1141. struct irte irte;
  1142. struct IR_IO_APIC_route_entry *ir_entry =
  1143. (struct IR_IO_APIC_route_entry *) entry;
  1144. int index;
  1145. if (!iommu)
  1146. panic("No mapping iommu for ioapic %d\n", apic);
  1147. index = alloc_irte(iommu, irq, 1);
  1148. if (index < 0)
  1149. panic("Failed to allocate IRTE for ioapic %d\n", apic);
  1150. memset(&irte, 0, sizeof(irte));
  1151. irte.present = 1;
  1152. irte.dst_mode = INT_DEST_MODE;
  1153. irte.trigger_mode = trigger;
  1154. irte.dlvry_mode = INT_DELIVERY_MODE;
  1155. irte.vector = vector;
  1156. irte.dest_id = IRTE_DEST(destination);
  1157. modify_irte(irq, &irte);
  1158. ir_entry->index2 = (index >> 15) & 0x1;
  1159. ir_entry->zero = 0;
  1160. ir_entry->format = 1;
  1161. ir_entry->index = (index & 0x7fff);
  1162. } else
  1163. #endif
  1164. {
  1165. entry->delivery_mode = INT_DELIVERY_MODE;
  1166. entry->dest_mode = INT_DEST_MODE;
  1167. entry->dest = destination;
  1168. }
  1169. entry->mask = 0; /* enable IRQ */
  1170. entry->trigger = trigger;
  1171. entry->polarity = polarity;
  1172. entry->vector = vector;
  1173. /* Mask level triggered irqs.
  1174. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  1175. */
  1176. if (trigger)
  1177. entry->mask = 1;
  1178. return 0;
  1179. }
  1180. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
  1181. int trigger, int polarity)
  1182. {
  1183. struct irq_cfg *cfg;
  1184. struct IO_APIC_route_entry entry;
  1185. cpumask_t mask;
  1186. if (!IO_APIC_IRQ(irq))
  1187. return;
  1188. cfg = desc->chip_data;
  1189. mask = TARGET_CPUS;
  1190. if (assign_irq_vector(irq, cfg, mask))
  1191. return;
  1192. cpus_and(mask, cfg->domain, mask);
  1193. apic_printk(APIC_VERBOSE,KERN_DEBUG
  1194. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  1195. "IRQ %d Mode:%i Active:%i)\n",
  1196. apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
  1197. irq, trigger, polarity);
  1198. if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
  1199. cpu_mask_to_apicid(mask), trigger, polarity,
  1200. cfg->vector)) {
  1201. printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
  1202. mp_ioapics[apic].mp_apicid, pin);
  1203. __clear_irq_vector(irq, cfg);
  1204. return;
  1205. }
  1206. ioapic_register_intr(irq, desc, trigger);
  1207. if (irq < NR_IRQS_LEGACY)
  1208. disable_8259A_irq(irq);
  1209. ioapic_write_entry(apic, pin, entry);
  1210. }
  1211. static void __init setup_IO_APIC_irqs(void)
  1212. {
  1213. int apic, pin, idx, irq;
  1214. int notcon = 0;
  1215. struct irq_desc *desc;
  1216. struct irq_cfg *cfg;
  1217. int cpu = boot_cpu_id;
  1218. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1219. for (apic = 0; apic < nr_ioapics; apic++) {
  1220. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1221. idx = find_irq_entry(apic, pin, mp_INT);
  1222. if (idx == -1) {
  1223. if (!notcon) {
  1224. notcon = 1;
  1225. apic_printk(APIC_VERBOSE,
  1226. KERN_DEBUG " %d-%d",
  1227. mp_ioapics[apic].mp_apicid,
  1228. pin);
  1229. } else
  1230. apic_printk(APIC_VERBOSE, " %d-%d",
  1231. mp_ioapics[apic].mp_apicid,
  1232. pin);
  1233. continue;
  1234. }
  1235. if (notcon) {
  1236. apic_printk(APIC_VERBOSE,
  1237. " (apicid-pin) not connected\n");
  1238. notcon = 0;
  1239. }
  1240. irq = pin_2_irq(idx, apic, pin);
  1241. #ifdef CONFIG_X86_32
  1242. if (multi_timer_check(apic, irq))
  1243. continue;
  1244. #endif
  1245. desc = irq_to_desc_alloc_cpu(irq, cpu);
  1246. if (!desc) {
  1247. printk(KERN_INFO "can not get irq_desc for %d\n", irq);
  1248. continue;
  1249. }
  1250. cfg = desc->chip_data;
  1251. add_pin_to_irq_cpu(cfg, cpu, apic, pin);
  1252. setup_IO_APIC_irq(apic, pin, irq, desc,
  1253. irq_trigger(idx), irq_polarity(idx));
  1254. }
  1255. }
  1256. if (notcon)
  1257. apic_printk(APIC_VERBOSE,
  1258. " (apicid-pin) not connected\n");
  1259. }
  1260. /*
  1261. * Set up the timer pin, possibly with the 8259A-master behind.
  1262. */
  1263. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  1264. int vector)
  1265. {
  1266. struct IO_APIC_route_entry entry;
  1267. #ifdef CONFIG_INTR_REMAP
  1268. if (intr_remapping_enabled)
  1269. return;
  1270. #endif
  1271. memset(&entry, 0, sizeof(entry));
  1272. /*
  1273. * We use logical delivery to get the timer IRQ
  1274. * to the first CPU.
  1275. */
  1276. entry.dest_mode = INT_DEST_MODE;
  1277. entry.mask = 1; /* mask IRQ now */
  1278. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  1279. entry.delivery_mode = INT_DELIVERY_MODE;
  1280. entry.polarity = 0;
  1281. entry.trigger = 0;
  1282. entry.vector = vector;
  1283. /*
  1284. * The timer IRQ doesn't have to know that behind the
  1285. * scene we may have a 8259A-master in AEOI mode ...
  1286. */
  1287. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  1288. /*
  1289. * Add it to the IO-APIC irq-routing table:
  1290. */
  1291. ioapic_write_entry(apic, pin, entry);
  1292. }
  1293. __apicdebuginit(void) print_IO_APIC(void)
  1294. {
  1295. int apic, i;
  1296. union IO_APIC_reg_00 reg_00;
  1297. union IO_APIC_reg_01 reg_01;
  1298. union IO_APIC_reg_02 reg_02;
  1299. union IO_APIC_reg_03 reg_03;
  1300. unsigned long flags;
  1301. struct irq_cfg *cfg;
  1302. struct irq_desc *desc;
  1303. unsigned int irq;
  1304. if (apic_verbosity == APIC_QUIET)
  1305. return;
  1306. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1307. for (i = 0; i < nr_ioapics; i++)
  1308. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1309. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  1310. /*
  1311. * We are a bit conservative about what we expect. We have to
  1312. * know about every hardware change ASAP.
  1313. */
  1314. printk(KERN_INFO "testing the IO APIC.......................\n");
  1315. for (apic = 0; apic < nr_ioapics; apic++) {
  1316. spin_lock_irqsave(&ioapic_lock, flags);
  1317. reg_00.raw = io_apic_read(apic, 0);
  1318. reg_01.raw = io_apic_read(apic, 1);
  1319. if (reg_01.bits.version >= 0x10)
  1320. reg_02.raw = io_apic_read(apic, 2);
  1321. if (reg_01.bits.version >= 0x20)
  1322. reg_03.raw = io_apic_read(apic, 3);
  1323. spin_unlock_irqrestore(&ioapic_lock, flags);
  1324. printk("\n");
  1325. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  1326. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1327. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1328. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1329. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1330. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  1331. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  1332. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1333. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  1334. /*
  1335. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1336. * but the value of reg_02 is read as the previous read register
  1337. * value, so ignore it if reg_02 == reg_01.
  1338. */
  1339. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1340. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1341. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1342. }
  1343. /*
  1344. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1345. * or reg_03, but the value of reg_0[23] is read as the previous read
  1346. * register value, so ignore it if reg_03 == reg_0[12].
  1347. */
  1348. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1349. reg_03.raw != reg_01.raw) {
  1350. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1351. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1352. }
  1353. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1354. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  1355. " Stat Dmod Deli Vect: \n");
  1356. for (i = 0; i <= reg_01.bits.entries; i++) {
  1357. struct IO_APIC_route_entry entry;
  1358. entry = ioapic_read_entry(apic, i);
  1359. printk(KERN_DEBUG " %02x %03X ",
  1360. i,
  1361. entry.dest
  1362. );
  1363. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1364. entry.mask,
  1365. entry.trigger,
  1366. entry.irr,
  1367. entry.polarity,
  1368. entry.delivery_status,
  1369. entry.dest_mode,
  1370. entry.delivery_mode,
  1371. entry.vector
  1372. );
  1373. }
  1374. }
  1375. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1376. for_each_irq_desc(irq, desc) {
  1377. struct irq_pin_list *entry;
  1378. if (!desc)
  1379. continue;
  1380. cfg = desc->chip_data;
  1381. entry = cfg->irq_2_pin;
  1382. if (!entry)
  1383. continue;
  1384. printk(KERN_DEBUG "IRQ%d ", irq);
  1385. for (;;) {
  1386. printk("-> %d:%d", entry->apic, entry->pin);
  1387. if (!entry->next)
  1388. break;
  1389. entry = entry->next;
  1390. }
  1391. printk("\n");
  1392. }
  1393. printk(KERN_INFO ".................................... done.\n");
  1394. return;
  1395. }
  1396. __apicdebuginit(void) print_APIC_bitfield(int base)
  1397. {
  1398. unsigned int v;
  1399. int i, j;
  1400. if (apic_verbosity == APIC_QUIET)
  1401. return;
  1402. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1403. for (i = 0; i < 8; i++) {
  1404. v = apic_read(base + i*0x10);
  1405. for (j = 0; j < 32; j++) {
  1406. if (v & (1<<j))
  1407. printk("1");
  1408. else
  1409. printk("0");
  1410. }
  1411. printk("\n");
  1412. }
  1413. }
  1414. __apicdebuginit(void) print_local_APIC(void *dummy)
  1415. {
  1416. unsigned int v, ver, maxlvt;
  1417. u64 icr;
  1418. if (apic_verbosity == APIC_QUIET)
  1419. return;
  1420. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1421. smp_processor_id(), hard_smp_processor_id());
  1422. v = apic_read(APIC_ID);
  1423. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
  1424. v = apic_read(APIC_LVR);
  1425. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1426. ver = GET_APIC_VERSION(v);
  1427. maxlvt = lapic_get_maxlvt();
  1428. v = apic_read(APIC_TASKPRI);
  1429. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1430. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1431. if (!APIC_XAPIC(ver)) {
  1432. v = apic_read(APIC_ARBPRI);
  1433. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1434. v & APIC_ARBPRI_MASK);
  1435. }
  1436. v = apic_read(APIC_PROCPRI);
  1437. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1438. }
  1439. /*
  1440. * Remote read supported only in the 82489DX and local APIC for
  1441. * Pentium processors.
  1442. */
  1443. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  1444. v = apic_read(APIC_RRR);
  1445. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1446. }
  1447. v = apic_read(APIC_LDR);
  1448. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1449. if (!x2apic_enabled()) {
  1450. v = apic_read(APIC_DFR);
  1451. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1452. }
  1453. v = apic_read(APIC_SPIV);
  1454. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1455. printk(KERN_DEBUG "... APIC ISR field:\n");
  1456. print_APIC_bitfield(APIC_ISR);
  1457. printk(KERN_DEBUG "... APIC TMR field:\n");
  1458. print_APIC_bitfield(APIC_TMR);
  1459. printk(KERN_DEBUG "... APIC IRR field:\n");
  1460. print_APIC_bitfield(APIC_IRR);
  1461. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1462. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1463. apic_write(APIC_ESR, 0);
  1464. v = apic_read(APIC_ESR);
  1465. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1466. }
  1467. icr = apic_icr_read();
  1468. printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
  1469. printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
  1470. v = apic_read(APIC_LVTT);
  1471. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1472. if (maxlvt > 3) { /* PC is LVT#4. */
  1473. v = apic_read(APIC_LVTPC);
  1474. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1475. }
  1476. v = apic_read(APIC_LVT0);
  1477. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1478. v = apic_read(APIC_LVT1);
  1479. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1480. if (maxlvt > 2) { /* ERR is LVT#3. */
  1481. v = apic_read(APIC_LVTERR);
  1482. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1483. }
  1484. v = apic_read(APIC_TMICT);
  1485. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1486. v = apic_read(APIC_TMCCT);
  1487. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1488. v = apic_read(APIC_TDCR);
  1489. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1490. printk("\n");
  1491. }
  1492. __apicdebuginit(void) print_all_local_APICs(void)
  1493. {
  1494. int cpu;
  1495. preempt_disable();
  1496. for_each_online_cpu(cpu)
  1497. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  1498. preempt_enable();
  1499. }
  1500. __apicdebuginit(void) print_PIC(void)
  1501. {
  1502. unsigned int v;
  1503. unsigned long flags;
  1504. if (apic_verbosity == APIC_QUIET)
  1505. return;
  1506. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1507. spin_lock_irqsave(&i8259A_lock, flags);
  1508. v = inb(0xa1) << 8 | inb(0x21);
  1509. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1510. v = inb(0xa0) << 8 | inb(0x20);
  1511. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1512. outb(0x0b,0xa0);
  1513. outb(0x0b,0x20);
  1514. v = inb(0xa0) << 8 | inb(0x20);
  1515. outb(0x0a,0xa0);
  1516. outb(0x0a,0x20);
  1517. spin_unlock_irqrestore(&i8259A_lock, flags);
  1518. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1519. v = inb(0x4d1) << 8 | inb(0x4d0);
  1520. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1521. }
  1522. __apicdebuginit(int) print_all_ICs(void)
  1523. {
  1524. print_PIC();
  1525. print_all_local_APICs();
  1526. print_IO_APIC();
  1527. return 0;
  1528. }
  1529. fs_initcall(print_all_ICs);
  1530. /* Where if anywhere is the i8259 connect in external int mode */
  1531. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  1532. void __init enable_IO_APIC(void)
  1533. {
  1534. union IO_APIC_reg_01 reg_01;
  1535. int i8259_apic, i8259_pin;
  1536. int apic;
  1537. unsigned long flags;
  1538. #ifdef CONFIG_X86_32
  1539. int i;
  1540. if (!pirqs_enabled)
  1541. for (i = 0; i < MAX_PIRQS; i++)
  1542. pirq_entries[i] = -1;
  1543. #endif
  1544. /*
  1545. * The number of IO-APIC IRQ registers (== #pins):
  1546. */
  1547. for (apic = 0; apic < nr_ioapics; apic++) {
  1548. spin_lock_irqsave(&ioapic_lock, flags);
  1549. reg_01.raw = io_apic_read(apic, 1);
  1550. spin_unlock_irqrestore(&ioapic_lock, flags);
  1551. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1552. }
  1553. for(apic = 0; apic < nr_ioapics; apic++) {
  1554. int pin;
  1555. /* See if any of the pins is in ExtINT mode */
  1556. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1557. struct IO_APIC_route_entry entry;
  1558. entry = ioapic_read_entry(apic, pin);
  1559. /* If the interrupt line is enabled and in ExtInt mode
  1560. * I have found the pin where the i8259 is connected.
  1561. */
  1562. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1563. ioapic_i8259.apic = apic;
  1564. ioapic_i8259.pin = pin;
  1565. goto found_i8259;
  1566. }
  1567. }
  1568. }
  1569. found_i8259:
  1570. /* Look to see what if the MP table has reported the ExtINT */
  1571. /* If we could not find the appropriate pin by looking at the ioapic
  1572. * the i8259 probably is not connected the ioapic but give the
  1573. * mptable a chance anyway.
  1574. */
  1575. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1576. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1577. /* Trust the MP table if nothing is setup in the hardware */
  1578. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1579. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1580. ioapic_i8259.pin = i8259_pin;
  1581. ioapic_i8259.apic = i8259_apic;
  1582. }
  1583. /* Complain if the MP table and the hardware disagree */
  1584. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1585. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1586. {
  1587. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1588. }
  1589. /*
  1590. * Do not trust the IO-APIC being empty at bootup
  1591. */
  1592. clear_IO_APIC();
  1593. }
  1594. /*
  1595. * Not an __init, needed by the reboot code
  1596. */
  1597. void disable_IO_APIC(void)
  1598. {
  1599. /*
  1600. * Clear the IO-APIC before rebooting:
  1601. */
  1602. clear_IO_APIC();
  1603. /*
  1604. * If the i8259 is routed through an IOAPIC
  1605. * Put that IOAPIC in virtual wire mode
  1606. * so legacy interrupts can be delivered.
  1607. */
  1608. if (ioapic_i8259.pin != -1) {
  1609. struct IO_APIC_route_entry entry;
  1610. memset(&entry, 0, sizeof(entry));
  1611. entry.mask = 0; /* Enabled */
  1612. entry.trigger = 0; /* Edge */
  1613. entry.irr = 0;
  1614. entry.polarity = 0; /* High */
  1615. entry.delivery_status = 0;
  1616. entry.dest_mode = 0; /* Physical */
  1617. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1618. entry.vector = 0;
  1619. entry.dest = read_apic_id();
  1620. /*
  1621. * Add it to the IO-APIC irq-routing table:
  1622. */
  1623. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1624. }
  1625. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1626. }
  1627. #ifdef CONFIG_X86_32
  1628. /*
  1629. * function to set the IO-APIC physical IDs based on the
  1630. * values stored in the MPC table.
  1631. *
  1632. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1633. */
  1634. static void __init setup_ioapic_ids_from_mpc(void)
  1635. {
  1636. union IO_APIC_reg_00 reg_00;
  1637. physid_mask_t phys_id_present_map;
  1638. int apic;
  1639. int i;
  1640. unsigned char old_id;
  1641. unsigned long flags;
  1642. if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
  1643. return;
  1644. /*
  1645. * Don't check I/O APIC IDs for xAPIC systems. They have
  1646. * no meaning without the serial APIC bus.
  1647. */
  1648. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1649. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1650. return;
  1651. /*
  1652. * This is broken; anything with a real cpu count has to
  1653. * circumvent this idiocy regardless.
  1654. */
  1655. phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
  1656. /*
  1657. * Set the IOAPIC ID to the value stored in the MPC table.
  1658. */
  1659. for (apic = 0; apic < nr_ioapics; apic++) {
  1660. /* Read the register 0 value */
  1661. spin_lock_irqsave(&ioapic_lock, flags);
  1662. reg_00.raw = io_apic_read(apic, 0);
  1663. spin_unlock_irqrestore(&ioapic_lock, flags);
  1664. old_id = mp_ioapics[apic].mp_apicid;
  1665. if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
  1666. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1667. apic, mp_ioapics[apic].mp_apicid);
  1668. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1669. reg_00.bits.ID);
  1670. mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
  1671. }
  1672. /*
  1673. * Sanity check, is the ID really free? Every APIC in a
  1674. * system must have a unique ID or we get lots of nice
  1675. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1676. */
  1677. if (check_apicid_used(phys_id_present_map,
  1678. mp_ioapics[apic].mp_apicid)) {
  1679. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1680. apic, mp_ioapics[apic].mp_apicid);
  1681. for (i = 0; i < get_physical_broadcast(); i++)
  1682. if (!physid_isset(i, phys_id_present_map))
  1683. break;
  1684. if (i >= get_physical_broadcast())
  1685. panic("Max APIC ID exceeded!\n");
  1686. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1687. i);
  1688. physid_set(i, phys_id_present_map);
  1689. mp_ioapics[apic].mp_apicid = i;
  1690. } else {
  1691. physid_mask_t tmp;
  1692. tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
  1693. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1694. "phys_id_present_map\n",
  1695. mp_ioapics[apic].mp_apicid);
  1696. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1697. }
  1698. /*
  1699. * We need to adjust the IRQ routing table
  1700. * if the ID changed.
  1701. */
  1702. if (old_id != mp_ioapics[apic].mp_apicid)
  1703. for (i = 0; i < mp_irq_entries; i++)
  1704. if (mp_irqs[i].mp_dstapic == old_id)
  1705. mp_irqs[i].mp_dstapic
  1706. = mp_ioapics[apic].mp_apicid;
  1707. /*
  1708. * Read the right value from the MPC table and
  1709. * write it into the ID register.
  1710. */
  1711. apic_printk(APIC_VERBOSE, KERN_INFO
  1712. "...changing IO-APIC physical APIC ID to %d ...",
  1713. mp_ioapics[apic].mp_apicid);
  1714. reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
  1715. spin_lock_irqsave(&ioapic_lock, flags);
  1716. io_apic_write(apic, 0, reg_00.raw);
  1717. spin_unlock_irqrestore(&ioapic_lock, flags);
  1718. /*
  1719. * Sanity check
  1720. */
  1721. spin_lock_irqsave(&ioapic_lock, flags);
  1722. reg_00.raw = io_apic_read(apic, 0);
  1723. spin_unlock_irqrestore(&ioapic_lock, flags);
  1724. if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
  1725. printk("could not set ID!\n");
  1726. else
  1727. apic_printk(APIC_VERBOSE, " ok.\n");
  1728. }
  1729. }
  1730. #endif
  1731. int no_timer_check __initdata;
  1732. static int __init notimercheck(char *s)
  1733. {
  1734. no_timer_check = 1;
  1735. return 1;
  1736. }
  1737. __setup("no_timer_check", notimercheck);
  1738. /*
  1739. * There is a nasty bug in some older SMP boards, their mptable lies
  1740. * about the timer IRQ. We do the following to work around the situation:
  1741. *
  1742. * - timer IRQ defaults to IO-APIC IRQ
  1743. * - if this function detects that timer IRQs are defunct, then we fall
  1744. * back to ISA timer IRQs
  1745. */
  1746. static int __init timer_irq_works(void)
  1747. {
  1748. unsigned long t1 = jiffies;
  1749. unsigned long flags;
  1750. if (no_timer_check)
  1751. return 1;
  1752. local_save_flags(flags);
  1753. local_irq_enable();
  1754. /* Let ten ticks pass... */
  1755. mdelay((10 * 1000) / HZ);
  1756. local_irq_restore(flags);
  1757. /*
  1758. * Expect a few ticks at least, to be sure some possible
  1759. * glue logic does not lock up after one or two first
  1760. * ticks in a non-ExtINT mode. Also the local APIC
  1761. * might have cached one ExtINT interrupt. Finally, at
  1762. * least one tick may be lost due to delays.
  1763. */
  1764. /* jiffies wrap? */
  1765. if (time_after(jiffies, t1 + 4))
  1766. return 1;
  1767. return 0;
  1768. }
  1769. /*
  1770. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1771. * number of pending IRQ events unhandled. These cases are very rare,
  1772. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1773. * better to do it this way as thus we do not have to be aware of
  1774. * 'pending' interrupts in the IRQ path, except at this point.
  1775. */
  1776. /*
  1777. * Edge triggered needs to resend any interrupt
  1778. * that was delayed but this is now handled in the device
  1779. * independent code.
  1780. */
  1781. /*
  1782. * Starting up a edge-triggered IO-APIC interrupt is
  1783. * nasty - we need to make sure that we get the edge.
  1784. * If it is already asserted for some reason, we need
  1785. * return 1 to indicate that is was pending.
  1786. *
  1787. * This is not complete - we should be able to fake
  1788. * an edge even if it isn't on the 8259A...
  1789. */
  1790. static unsigned int startup_ioapic_irq(unsigned int irq)
  1791. {
  1792. int was_pending = 0;
  1793. unsigned long flags;
  1794. struct irq_cfg *cfg;
  1795. spin_lock_irqsave(&ioapic_lock, flags);
  1796. if (irq < NR_IRQS_LEGACY) {
  1797. disable_8259A_irq(irq);
  1798. if (i8259A_irq_pending(irq))
  1799. was_pending = 1;
  1800. }
  1801. cfg = irq_cfg(irq);
  1802. __unmask_IO_APIC_irq(cfg);
  1803. spin_unlock_irqrestore(&ioapic_lock, flags);
  1804. return was_pending;
  1805. }
  1806. #ifdef CONFIG_X86_64
  1807. static int ioapic_retrigger_irq(unsigned int irq)
  1808. {
  1809. struct irq_cfg *cfg = irq_cfg(irq);
  1810. unsigned long flags;
  1811. spin_lock_irqsave(&vector_lock, flags);
  1812. send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
  1813. spin_unlock_irqrestore(&vector_lock, flags);
  1814. return 1;
  1815. }
  1816. #else
  1817. static int ioapic_retrigger_irq(unsigned int irq)
  1818. {
  1819. send_IPI_self(irq_cfg(irq)->vector);
  1820. return 1;
  1821. }
  1822. #endif
  1823. /*
  1824. * Level and edge triggered IO-APIC interrupts need different handling,
  1825. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1826. * handled with the level-triggered descriptor, but that one has slightly
  1827. * more overhead. Level-triggered interrupts cannot be handled with the
  1828. * edge-triggered handler, without risking IRQ storms and other ugly
  1829. * races.
  1830. */
  1831. #ifdef CONFIG_SMP
  1832. #ifdef CONFIG_INTR_REMAP
  1833. static void ir_irq_migration(struct work_struct *work);
  1834. static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
  1835. /*
  1836. * Migrate the IO-APIC irq in the presence of intr-remapping.
  1837. *
  1838. * For edge triggered, irq migration is a simple atomic update(of vector
  1839. * and cpu destination) of IRTE and flush the hardware cache.
  1840. *
  1841. * For level triggered, we need to modify the io-apic RTE aswell with the update
  1842. * vector information, along with modifying IRTE with vector and destination.
  1843. * So irq migration for level triggered is little bit more complex compared to
  1844. * edge triggered migration. But the good news is, we use the same algorithm
  1845. * for level triggered migration as we have today, only difference being,
  1846. * we now initiate the irq migration from process context instead of the
  1847. * interrupt context.
  1848. *
  1849. * In future, when we do a directed EOI (combined with cpu EOI broadcast
  1850. * suppression) to the IO-APIC, level triggered irq migration will also be
  1851. * as simple as edge triggered migration and we can do the irq migration
  1852. * with a simple atomic update to IO-APIC RTE.
  1853. */
  1854. static void migrate_ioapic_irq_desc(struct irq_desc *desc, cpumask_t mask)
  1855. {
  1856. struct irq_cfg *cfg;
  1857. cpumask_t tmp, cleanup_mask;
  1858. struct irte irte;
  1859. int modify_ioapic_rte;
  1860. unsigned int dest;
  1861. unsigned long flags;
  1862. unsigned int irq;
  1863. cpus_and(tmp, mask, cpu_online_map);
  1864. if (cpus_empty(tmp))
  1865. return;
  1866. irq = desc->irq;
  1867. if (get_irte(irq, &irte))
  1868. return;
  1869. cfg = desc->chip_data;
  1870. if (assign_irq_vector(irq, cfg, mask))
  1871. return;
  1872. set_extra_move_desc(desc, mask);
  1873. cpus_and(tmp, cfg->domain, mask);
  1874. dest = cpu_mask_to_apicid(tmp);
  1875. modify_ioapic_rte = desc->status & IRQ_LEVEL;
  1876. if (modify_ioapic_rte) {
  1877. spin_lock_irqsave(&ioapic_lock, flags);
  1878. __target_IO_APIC_irq(irq, dest, cfg);
  1879. spin_unlock_irqrestore(&ioapic_lock, flags);
  1880. }
  1881. irte.vector = cfg->vector;
  1882. irte.dest_id = IRTE_DEST(dest);
  1883. /*
  1884. * Modified the IRTE and flushes the Interrupt entry cache.
  1885. */
  1886. modify_irte(irq, &irte);
  1887. if (cfg->move_in_progress) {
  1888. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1889. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1890. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1891. cfg->move_in_progress = 0;
  1892. }
  1893. desc->affinity = mask;
  1894. }
  1895. static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
  1896. {
  1897. int ret = -1;
  1898. struct irq_cfg *cfg = desc->chip_data;
  1899. mask_IO_APIC_irq_desc(desc);
  1900. if (io_apic_level_ack_pending(cfg)) {
  1901. /*
  1902. * Interrupt in progress. Migrating irq now will change the
  1903. * vector information in the IO-APIC RTE and that will confuse
  1904. * the EOI broadcast performed by cpu.
  1905. * So, delay the irq migration to the next instance.
  1906. */
  1907. schedule_delayed_work(&ir_migration_work, 1);
  1908. goto unmask;
  1909. }
  1910. /* everthing is clear. we have right of way */
  1911. migrate_ioapic_irq_desc(desc, desc->pending_mask);
  1912. ret = 0;
  1913. desc->status &= ~IRQ_MOVE_PENDING;
  1914. cpus_clear(desc->pending_mask);
  1915. unmask:
  1916. unmask_IO_APIC_irq_desc(desc);
  1917. return ret;
  1918. }
  1919. static void ir_irq_migration(struct work_struct *work)
  1920. {
  1921. unsigned int irq;
  1922. struct irq_desc *desc;
  1923. for_each_irq_desc(irq, desc) {
  1924. if (!desc)
  1925. continue;
  1926. if (desc->status & IRQ_MOVE_PENDING) {
  1927. unsigned long flags;
  1928. spin_lock_irqsave(&desc->lock, flags);
  1929. if (!desc->chip->set_affinity ||
  1930. !(desc->status & IRQ_MOVE_PENDING)) {
  1931. desc->status &= ~IRQ_MOVE_PENDING;
  1932. spin_unlock_irqrestore(&desc->lock, flags);
  1933. continue;
  1934. }
  1935. desc->chip->set_affinity(irq, desc->pending_mask);
  1936. spin_unlock_irqrestore(&desc->lock, flags);
  1937. }
  1938. }
  1939. }
  1940. /*
  1941. * Migrates the IRQ destination in the process context.
  1942. */
  1943. static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc, cpumask_t mask)
  1944. {
  1945. if (desc->status & IRQ_LEVEL) {
  1946. desc->status |= IRQ_MOVE_PENDING;
  1947. desc->pending_mask = mask;
  1948. migrate_irq_remapped_level_desc(desc);
  1949. return;
  1950. }
  1951. migrate_ioapic_irq_desc(desc, mask);
  1952. }
  1953. static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  1954. {
  1955. struct irq_desc *desc = irq_to_desc(irq);
  1956. set_ir_ioapic_affinity_irq_desc(desc, mask);
  1957. }
  1958. #endif
  1959. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1960. {
  1961. unsigned vector, me;
  1962. ack_APIC_irq();
  1963. #ifdef CONFIG_X86_64
  1964. exit_idle();
  1965. #endif
  1966. irq_enter();
  1967. me = smp_processor_id();
  1968. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1969. unsigned int irq;
  1970. struct irq_desc *desc;
  1971. struct irq_cfg *cfg;
  1972. irq = __get_cpu_var(vector_irq)[vector];
  1973. if (irq == -1)
  1974. continue;
  1975. desc = irq_to_desc(irq);
  1976. if (!desc)
  1977. continue;
  1978. cfg = irq_cfg(irq);
  1979. spin_lock(&desc->lock);
  1980. if (!cfg->move_cleanup_count)
  1981. goto unlock;
  1982. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  1983. goto unlock;
  1984. __get_cpu_var(vector_irq)[vector] = -1;
  1985. cfg->move_cleanup_count--;
  1986. unlock:
  1987. spin_unlock(&desc->lock);
  1988. }
  1989. irq_exit();
  1990. }
  1991. static void irq_complete_move(struct irq_desc **descp)
  1992. {
  1993. struct irq_desc *desc = *descp;
  1994. struct irq_cfg *cfg = desc->chip_data;
  1995. unsigned vector, me;
  1996. if (likely(!cfg->move_in_progress))
  1997. return;
  1998. vector = ~get_irq_regs()->orig_ax;
  1999. me = smp_processor_id();
  2000. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  2001. cpumask_t cleanup_mask;
  2002. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2003. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2004. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2005. cfg->move_in_progress = 0;
  2006. }
  2007. }
  2008. #else
  2009. static inline void irq_complete_move(struct irq_desc **descp) {}
  2010. #endif
  2011. #ifdef CONFIG_INTR_REMAP
  2012. static void ack_x2apic_level(unsigned int irq)
  2013. {
  2014. ack_x2APIC_irq();
  2015. }
  2016. static void ack_x2apic_edge(unsigned int irq)
  2017. {
  2018. ack_x2APIC_irq();
  2019. }
  2020. #endif
  2021. static void ack_apic_edge(unsigned int irq)
  2022. {
  2023. struct irq_desc *desc = irq_to_desc(irq);
  2024. irq_complete_move(&desc);
  2025. move_native_irq(irq);
  2026. ack_APIC_irq();
  2027. }
  2028. atomic_t irq_mis_count;
  2029. static void ack_apic_level(unsigned int irq)
  2030. {
  2031. struct irq_desc *desc = irq_to_desc(irq);
  2032. #ifdef CONFIG_X86_32
  2033. unsigned long v;
  2034. int i;
  2035. #endif
  2036. struct irq_cfg *cfg;
  2037. int do_unmask_irq = 0;
  2038. irq_complete_move(&desc);
  2039. #ifdef CONFIG_GENERIC_PENDING_IRQ
  2040. /* If we are moving the irq we need to mask it */
  2041. if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
  2042. do_unmask_irq = 1;
  2043. mask_IO_APIC_irq_desc(desc);
  2044. }
  2045. #endif
  2046. #ifdef CONFIG_X86_32
  2047. /*
  2048. * It appears there is an erratum which affects at least version 0x11
  2049. * of I/O APIC (that's the 82093AA and cores integrated into various
  2050. * chipsets). Under certain conditions a level-triggered interrupt is
  2051. * erroneously delivered as edge-triggered one but the respective IRR
  2052. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  2053. * message but it will never arrive and further interrupts are blocked
  2054. * from the source. The exact reason is so far unknown, but the
  2055. * phenomenon was observed when two consecutive interrupt requests
  2056. * from a given source get delivered to the same CPU and the source is
  2057. * temporarily disabled in between.
  2058. *
  2059. * A workaround is to simulate an EOI message manually. We achieve it
  2060. * by setting the trigger mode to edge and then to level when the edge
  2061. * trigger mode gets detected in the TMR of a local APIC for a
  2062. * level-triggered interrupt. We mask the source for the time of the
  2063. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  2064. * The idea is from Manfred Spraul. --macro
  2065. */
  2066. cfg = desc->chip_data;
  2067. i = cfg->vector;
  2068. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  2069. #endif
  2070. /*
  2071. * We must acknowledge the irq before we move it or the acknowledge will
  2072. * not propagate properly.
  2073. */
  2074. ack_APIC_irq();
  2075. /* Now we can move and renable the irq */
  2076. if (unlikely(do_unmask_irq)) {
  2077. /* Only migrate the irq if the ack has been received.
  2078. *
  2079. * On rare occasions the broadcast level triggered ack gets
  2080. * delayed going to ioapics, and if we reprogram the
  2081. * vector while Remote IRR is still set the irq will never
  2082. * fire again.
  2083. *
  2084. * To prevent this scenario we read the Remote IRR bit
  2085. * of the ioapic. This has two effects.
  2086. * - On any sane system the read of the ioapic will
  2087. * flush writes (and acks) going to the ioapic from
  2088. * this cpu.
  2089. * - We get to see if the ACK has actually been delivered.
  2090. *
  2091. * Based on failed experiments of reprogramming the
  2092. * ioapic entry from outside of irq context starting
  2093. * with masking the ioapic entry and then polling until
  2094. * Remote IRR was clear before reprogramming the
  2095. * ioapic I don't trust the Remote IRR bit to be
  2096. * completey accurate.
  2097. *
  2098. * However there appears to be no other way to plug
  2099. * this race, so if the Remote IRR bit is not
  2100. * accurate and is causing problems then it is a hardware bug
  2101. * and you can go talk to the chipset vendor about it.
  2102. */
  2103. cfg = desc->chip_data;
  2104. if (!io_apic_level_ack_pending(cfg))
  2105. move_masked_irq(irq);
  2106. unmask_IO_APIC_irq_desc(desc);
  2107. }
  2108. #ifdef CONFIG_X86_32
  2109. if (!(v & (1 << (i & 0x1f)))) {
  2110. atomic_inc(&irq_mis_count);
  2111. spin_lock(&ioapic_lock);
  2112. __mask_and_edge_IO_APIC_irq(cfg);
  2113. __unmask_and_level_IO_APIC_irq(cfg);
  2114. spin_unlock(&ioapic_lock);
  2115. }
  2116. #endif
  2117. }
  2118. static struct irq_chip ioapic_chip __read_mostly = {
  2119. .name = "IO-APIC",
  2120. .startup = startup_ioapic_irq,
  2121. .mask = mask_IO_APIC_irq,
  2122. .unmask = unmask_IO_APIC_irq,
  2123. .ack = ack_apic_edge,
  2124. .eoi = ack_apic_level,
  2125. #ifdef CONFIG_SMP
  2126. .set_affinity = set_ioapic_affinity_irq,
  2127. #endif
  2128. .retrigger = ioapic_retrigger_irq,
  2129. };
  2130. #ifdef CONFIG_INTR_REMAP
  2131. static struct irq_chip ir_ioapic_chip __read_mostly = {
  2132. .name = "IR-IO-APIC",
  2133. .startup = startup_ioapic_irq,
  2134. .mask = mask_IO_APIC_irq,
  2135. .unmask = unmask_IO_APIC_irq,
  2136. .ack = ack_x2apic_edge,
  2137. .eoi = ack_x2apic_level,
  2138. #ifdef CONFIG_SMP
  2139. .set_affinity = set_ir_ioapic_affinity_irq,
  2140. #endif
  2141. .retrigger = ioapic_retrigger_irq,
  2142. };
  2143. #endif
  2144. static inline void init_IO_APIC_traps(void)
  2145. {
  2146. int irq;
  2147. struct irq_desc *desc;
  2148. struct irq_cfg *cfg;
  2149. /*
  2150. * NOTE! The local APIC isn't very good at handling
  2151. * multiple interrupts at the same interrupt level.
  2152. * As the interrupt level is determined by taking the
  2153. * vector number and shifting that right by 4, we
  2154. * want to spread these out a bit so that they don't
  2155. * all fall in the same interrupt level.
  2156. *
  2157. * Also, we've got to be careful not to trash gate
  2158. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  2159. */
  2160. for_each_irq_desc(irq, desc) {
  2161. if (!desc)
  2162. continue;
  2163. cfg = desc->chip_data;
  2164. if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
  2165. /*
  2166. * Hmm.. We don't have an entry for this,
  2167. * so default to an old-fashioned 8259
  2168. * interrupt if we can..
  2169. */
  2170. if (irq < NR_IRQS_LEGACY)
  2171. make_8259A_irq(irq);
  2172. else
  2173. /* Strange. Oh, well.. */
  2174. desc->chip = &no_irq_chip;
  2175. }
  2176. }
  2177. }
  2178. /*
  2179. * The local APIC irq-chip implementation:
  2180. */
  2181. static void mask_lapic_irq(unsigned int irq)
  2182. {
  2183. unsigned long v;
  2184. v = apic_read(APIC_LVT0);
  2185. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  2186. }
  2187. static void unmask_lapic_irq(unsigned int irq)
  2188. {
  2189. unsigned long v;
  2190. v = apic_read(APIC_LVT0);
  2191. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  2192. }
  2193. static void ack_lapic_irq(unsigned int irq)
  2194. {
  2195. ack_APIC_irq();
  2196. }
  2197. static struct irq_chip lapic_chip __read_mostly = {
  2198. .name = "local-APIC",
  2199. .mask = mask_lapic_irq,
  2200. .unmask = unmask_lapic_irq,
  2201. .ack = ack_lapic_irq,
  2202. };
  2203. static void lapic_register_intr(int irq, struct irq_desc *desc)
  2204. {
  2205. desc->status &= ~IRQ_LEVEL;
  2206. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  2207. "edge");
  2208. }
  2209. static void __init setup_nmi(void)
  2210. {
  2211. /*
  2212. * Dirty trick to enable the NMI watchdog ...
  2213. * We put the 8259A master into AEOI mode and
  2214. * unmask on all local APICs LVT0 as NMI.
  2215. *
  2216. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  2217. * is from Maciej W. Rozycki - so we do not have to EOI from
  2218. * the NMI handler or the timer interrupt.
  2219. */
  2220. apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
  2221. enable_NMI_through_LVT0();
  2222. apic_printk(APIC_VERBOSE, " done.\n");
  2223. }
  2224. /*
  2225. * This looks a bit hackish but it's about the only one way of sending
  2226. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  2227. * not support the ExtINT mode, unfortunately. We need to send these
  2228. * cycles as some i82489DX-based boards have glue logic that keeps the
  2229. * 8259A interrupt line asserted until INTA. --macro
  2230. */
  2231. static inline void __init unlock_ExtINT_logic(void)
  2232. {
  2233. int apic, pin, i;
  2234. struct IO_APIC_route_entry entry0, entry1;
  2235. unsigned char save_control, save_freq_select;
  2236. pin = find_isa_irq_pin(8, mp_INT);
  2237. if (pin == -1) {
  2238. WARN_ON_ONCE(1);
  2239. return;
  2240. }
  2241. apic = find_isa_irq_apic(8, mp_INT);
  2242. if (apic == -1) {
  2243. WARN_ON_ONCE(1);
  2244. return;
  2245. }
  2246. entry0 = ioapic_read_entry(apic, pin);
  2247. clear_IO_APIC_pin(apic, pin);
  2248. memset(&entry1, 0, sizeof(entry1));
  2249. entry1.dest_mode = 0; /* physical delivery */
  2250. entry1.mask = 0; /* unmask IRQ now */
  2251. entry1.dest = hard_smp_processor_id();
  2252. entry1.delivery_mode = dest_ExtINT;
  2253. entry1.polarity = entry0.polarity;
  2254. entry1.trigger = 0;
  2255. entry1.vector = 0;
  2256. ioapic_write_entry(apic, pin, entry1);
  2257. save_control = CMOS_READ(RTC_CONTROL);
  2258. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  2259. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  2260. RTC_FREQ_SELECT);
  2261. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  2262. i = 100;
  2263. while (i-- > 0) {
  2264. mdelay(10);
  2265. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  2266. i -= 10;
  2267. }
  2268. CMOS_WRITE(save_control, RTC_CONTROL);
  2269. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  2270. clear_IO_APIC_pin(apic, pin);
  2271. ioapic_write_entry(apic, pin, entry0);
  2272. }
  2273. static int disable_timer_pin_1 __initdata;
  2274. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  2275. static int __init disable_timer_pin_setup(char *arg)
  2276. {
  2277. disable_timer_pin_1 = 1;
  2278. return 0;
  2279. }
  2280. early_param("disable_timer_pin_1", disable_timer_pin_setup);
  2281. int timer_through_8259 __initdata;
  2282. /*
  2283. * This code may look a bit paranoid, but it's supposed to cooperate with
  2284. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  2285. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  2286. * fanatically on his truly buggy board.
  2287. *
  2288. * FIXME: really need to revamp this for all platforms.
  2289. */
  2290. static inline void __init check_timer(void)
  2291. {
  2292. struct irq_desc *desc = irq_to_desc(0);
  2293. struct irq_cfg *cfg = desc->chip_data;
  2294. int cpu = boot_cpu_id;
  2295. int apic1, pin1, apic2, pin2;
  2296. unsigned long flags;
  2297. unsigned int ver;
  2298. int no_pin1 = 0;
  2299. local_irq_save(flags);
  2300. ver = apic_read(APIC_LVR);
  2301. ver = GET_APIC_VERSION(ver);
  2302. /*
  2303. * get/set the timer IRQ vector:
  2304. */
  2305. disable_8259A_irq(0);
  2306. assign_irq_vector(0, cfg, TARGET_CPUS);
  2307. /*
  2308. * As IRQ0 is to be enabled in the 8259A, the virtual
  2309. * wire has to be disabled in the local APIC. Also
  2310. * timer interrupts need to be acknowledged manually in
  2311. * the 8259A for the i82489DX when using the NMI
  2312. * watchdog as that APIC treats NMIs as level-triggered.
  2313. * The AEOI mode will finish them in the 8259A
  2314. * automatically.
  2315. */
  2316. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  2317. init_8259A(1);
  2318. #ifdef CONFIG_X86_32
  2319. timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
  2320. #endif
  2321. pin1 = find_isa_irq_pin(0, mp_INT);
  2322. apic1 = find_isa_irq_apic(0, mp_INT);
  2323. pin2 = ioapic_i8259.pin;
  2324. apic2 = ioapic_i8259.apic;
  2325. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  2326. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  2327. cfg->vector, apic1, pin1, apic2, pin2);
  2328. /*
  2329. * Some BIOS writers are clueless and report the ExtINTA
  2330. * I/O APIC input from the cascaded 8259A as the timer
  2331. * interrupt input. So just in case, if only one pin
  2332. * was found above, try it both directly and through the
  2333. * 8259A.
  2334. */
  2335. if (pin1 == -1) {
  2336. #ifdef CONFIG_INTR_REMAP
  2337. if (intr_remapping_enabled)
  2338. panic("BIOS bug: timer not connected to IO-APIC");
  2339. #endif
  2340. pin1 = pin2;
  2341. apic1 = apic2;
  2342. no_pin1 = 1;
  2343. } else if (pin2 == -1) {
  2344. pin2 = pin1;
  2345. apic2 = apic1;
  2346. }
  2347. if (pin1 != -1) {
  2348. /*
  2349. * Ok, does IRQ0 through the IOAPIC work?
  2350. */
  2351. if (no_pin1) {
  2352. add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
  2353. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  2354. }
  2355. unmask_IO_APIC_irq_desc(desc);
  2356. if (timer_irq_works()) {
  2357. if (nmi_watchdog == NMI_IO_APIC) {
  2358. setup_nmi();
  2359. enable_8259A_irq(0);
  2360. }
  2361. if (disable_timer_pin_1 > 0)
  2362. clear_IO_APIC_pin(0, pin1);
  2363. goto out;
  2364. }
  2365. #ifdef CONFIG_INTR_REMAP
  2366. if (intr_remapping_enabled)
  2367. panic("timer doesn't work through Interrupt-remapped IO-APIC");
  2368. #endif
  2369. clear_IO_APIC_pin(apic1, pin1);
  2370. if (!no_pin1)
  2371. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  2372. "8254 timer not connected to IO-APIC\n");
  2373. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  2374. "(IRQ0) through the 8259A ...\n");
  2375. apic_printk(APIC_QUIET, KERN_INFO
  2376. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  2377. /*
  2378. * legacy devices should be connected to IO APIC #0
  2379. */
  2380. replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
  2381. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  2382. unmask_IO_APIC_irq_desc(desc);
  2383. enable_8259A_irq(0);
  2384. if (timer_irq_works()) {
  2385. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  2386. timer_through_8259 = 1;
  2387. if (nmi_watchdog == NMI_IO_APIC) {
  2388. disable_8259A_irq(0);
  2389. setup_nmi();
  2390. enable_8259A_irq(0);
  2391. }
  2392. goto out;
  2393. }
  2394. /*
  2395. * Cleanup, just in case ...
  2396. */
  2397. disable_8259A_irq(0);
  2398. clear_IO_APIC_pin(apic2, pin2);
  2399. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  2400. }
  2401. if (nmi_watchdog == NMI_IO_APIC) {
  2402. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  2403. "through the IO-APIC - disabling NMI Watchdog!\n");
  2404. nmi_watchdog = NMI_NONE;
  2405. }
  2406. #ifdef CONFIG_X86_32
  2407. timer_ack = 0;
  2408. #endif
  2409. apic_printk(APIC_QUIET, KERN_INFO
  2410. "...trying to set up timer as Virtual Wire IRQ...\n");
  2411. lapic_register_intr(0, desc);
  2412. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  2413. enable_8259A_irq(0);
  2414. if (timer_irq_works()) {
  2415. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2416. goto out;
  2417. }
  2418. disable_8259A_irq(0);
  2419. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  2420. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  2421. apic_printk(APIC_QUIET, KERN_INFO
  2422. "...trying to set up timer as ExtINT IRQ...\n");
  2423. init_8259A(0);
  2424. make_8259A_irq(0);
  2425. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  2426. unlock_ExtINT_logic();
  2427. if (timer_irq_works()) {
  2428. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2429. goto out;
  2430. }
  2431. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  2432. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  2433. "report. Then try booting with the 'noapic' option.\n");
  2434. out:
  2435. local_irq_restore(flags);
  2436. }
  2437. /*
  2438. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  2439. * to devices. However there may be an I/O APIC pin available for
  2440. * this interrupt regardless. The pin may be left unconnected, but
  2441. * typically it will be reused as an ExtINT cascade interrupt for
  2442. * the master 8259A. In the MPS case such a pin will normally be
  2443. * reported as an ExtINT interrupt in the MP table. With ACPI
  2444. * there is no provision for ExtINT interrupts, and in the absence
  2445. * of an override it would be treated as an ordinary ISA I/O APIC
  2446. * interrupt, that is edge-triggered and unmasked by default. We
  2447. * used to do this, but it caused problems on some systems because
  2448. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  2449. * the same ExtINT cascade interrupt to drive the local APIC of the
  2450. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  2451. * the I/O APIC in all cases now. No actual device should request
  2452. * it anyway. --macro
  2453. */
  2454. #define PIC_IRQS (1 << PIC_CASCADE_IR)
  2455. void __init setup_IO_APIC(void)
  2456. {
  2457. #ifdef CONFIG_X86_32
  2458. enable_IO_APIC();
  2459. #else
  2460. /*
  2461. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  2462. */
  2463. #endif
  2464. io_apic_irqs = ~PIC_IRQS;
  2465. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  2466. /*
  2467. * Set up IO-APIC IRQ routing.
  2468. */
  2469. #ifdef CONFIG_X86_32
  2470. if (!acpi_ioapic)
  2471. setup_ioapic_ids_from_mpc();
  2472. #endif
  2473. sync_Arb_IDs();
  2474. setup_IO_APIC_irqs();
  2475. init_IO_APIC_traps();
  2476. check_timer();
  2477. }
  2478. /*
  2479. * Called after all the initialization is done. If we didnt find any
  2480. * APIC bugs then we can allow the modify fast path
  2481. */
  2482. static int __init io_apic_bug_finalize(void)
  2483. {
  2484. if (sis_apic_bug == -1)
  2485. sis_apic_bug = 0;
  2486. return 0;
  2487. }
  2488. late_initcall(io_apic_bug_finalize);
  2489. struct sysfs_ioapic_data {
  2490. struct sys_device dev;
  2491. struct IO_APIC_route_entry entry[0];
  2492. };
  2493. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  2494. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  2495. {
  2496. struct IO_APIC_route_entry *entry;
  2497. struct sysfs_ioapic_data *data;
  2498. int i;
  2499. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2500. entry = data->entry;
  2501. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  2502. *entry = ioapic_read_entry(dev->id, i);
  2503. return 0;
  2504. }
  2505. static int ioapic_resume(struct sys_device *dev)
  2506. {
  2507. struct IO_APIC_route_entry *entry;
  2508. struct sysfs_ioapic_data *data;
  2509. unsigned long flags;
  2510. union IO_APIC_reg_00 reg_00;
  2511. int i;
  2512. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2513. entry = data->entry;
  2514. spin_lock_irqsave(&ioapic_lock, flags);
  2515. reg_00.raw = io_apic_read(dev->id, 0);
  2516. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  2517. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  2518. io_apic_write(dev->id, 0, reg_00.raw);
  2519. }
  2520. spin_unlock_irqrestore(&ioapic_lock, flags);
  2521. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2522. ioapic_write_entry(dev->id, i, entry[i]);
  2523. return 0;
  2524. }
  2525. static struct sysdev_class ioapic_sysdev_class = {
  2526. .name = "ioapic",
  2527. .suspend = ioapic_suspend,
  2528. .resume = ioapic_resume,
  2529. };
  2530. static int __init ioapic_init_sysfs(void)
  2531. {
  2532. struct sys_device * dev;
  2533. int i, size, error;
  2534. error = sysdev_class_register(&ioapic_sysdev_class);
  2535. if (error)
  2536. return error;
  2537. for (i = 0; i < nr_ioapics; i++ ) {
  2538. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  2539. * sizeof(struct IO_APIC_route_entry);
  2540. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  2541. if (!mp_ioapic_data[i]) {
  2542. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2543. continue;
  2544. }
  2545. dev = &mp_ioapic_data[i]->dev;
  2546. dev->id = i;
  2547. dev->cls = &ioapic_sysdev_class;
  2548. error = sysdev_register(dev);
  2549. if (error) {
  2550. kfree(mp_ioapic_data[i]);
  2551. mp_ioapic_data[i] = NULL;
  2552. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2553. continue;
  2554. }
  2555. }
  2556. return 0;
  2557. }
  2558. device_initcall(ioapic_init_sysfs);
  2559. /*
  2560. * Dynamic irq allocate and deallocation
  2561. */
  2562. unsigned int create_irq_nr(unsigned int irq_want)
  2563. {
  2564. /* Allocate an unused irq */
  2565. unsigned int irq;
  2566. unsigned int new;
  2567. unsigned long flags;
  2568. struct irq_cfg *cfg_new = NULL;
  2569. int cpu = boot_cpu_id;
  2570. struct irq_desc *desc_new = NULL;
  2571. irq = 0;
  2572. spin_lock_irqsave(&vector_lock, flags);
  2573. for (new = irq_want; new < NR_IRQS; new++) {
  2574. if (platform_legacy_irq(new))
  2575. continue;
  2576. desc_new = irq_to_desc_alloc_cpu(new, cpu);
  2577. if (!desc_new) {
  2578. printk(KERN_INFO "can not get irq_desc for %d\n", new);
  2579. continue;
  2580. }
  2581. cfg_new = desc_new->chip_data;
  2582. if (cfg_new->vector != 0)
  2583. continue;
  2584. if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
  2585. irq = new;
  2586. break;
  2587. }
  2588. spin_unlock_irqrestore(&vector_lock, flags);
  2589. if (irq > 0) {
  2590. dynamic_irq_init(irq);
  2591. /* restore it, in case dynamic_irq_init clear it */
  2592. if (desc_new)
  2593. desc_new->chip_data = cfg_new;
  2594. }
  2595. return irq;
  2596. }
  2597. static int nr_irqs_gsi = NR_IRQS_LEGACY;
  2598. int create_irq(void)
  2599. {
  2600. unsigned int irq_want;
  2601. int irq;
  2602. irq_want = nr_irqs_gsi;
  2603. irq = create_irq_nr(irq_want);
  2604. if (irq == 0)
  2605. irq = -1;
  2606. return irq;
  2607. }
  2608. void destroy_irq(unsigned int irq)
  2609. {
  2610. unsigned long flags;
  2611. struct irq_cfg *cfg;
  2612. struct irq_desc *desc;
  2613. /* store it, in case dynamic_irq_cleanup clear it */
  2614. desc = irq_to_desc(irq);
  2615. cfg = desc->chip_data;
  2616. dynamic_irq_cleanup(irq);
  2617. /* connect back irq_cfg */
  2618. if (desc)
  2619. desc->chip_data = cfg;
  2620. #ifdef CONFIG_INTR_REMAP
  2621. free_irte(irq);
  2622. #endif
  2623. spin_lock_irqsave(&vector_lock, flags);
  2624. __clear_irq_vector(irq, cfg);
  2625. spin_unlock_irqrestore(&vector_lock, flags);
  2626. }
  2627. /*
  2628. * MSI message composition
  2629. */
  2630. #ifdef CONFIG_PCI_MSI
  2631. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  2632. {
  2633. struct irq_cfg *cfg;
  2634. int err;
  2635. unsigned dest;
  2636. cpumask_t tmp;
  2637. cfg = irq_cfg(irq);
  2638. tmp = TARGET_CPUS;
  2639. err = assign_irq_vector(irq, cfg, tmp);
  2640. if (err)
  2641. return err;
  2642. cpus_and(tmp, cfg->domain, tmp);
  2643. dest = cpu_mask_to_apicid(tmp);
  2644. #ifdef CONFIG_INTR_REMAP
  2645. if (irq_remapped(irq)) {
  2646. struct irte irte;
  2647. int ir_index;
  2648. u16 sub_handle;
  2649. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  2650. BUG_ON(ir_index == -1);
  2651. memset (&irte, 0, sizeof(irte));
  2652. irte.present = 1;
  2653. irte.dst_mode = INT_DEST_MODE;
  2654. irte.trigger_mode = 0; /* edge */
  2655. irte.dlvry_mode = INT_DELIVERY_MODE;
  2656. irte.vector = cfg->vector;
  2657. irte.dest_id = IRTE_DEST(dest);
  2658. modify_irte(irq, &irte);
  2659. msg->address_hi = MSI_ADDR_BASE_HI;
  2660. msg->data = sub_handle;
  2661. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  2662. MSI_ADDR_IR_SHV |
  2663. MSI_ADDR_IR_INDEX1(ir_index) |
  2664. MSI_ADDR_IR_INDEX2(ir_index);
  2665. } else
  2666. #endif
  2667. {
  2668. msg->address_hi = MSI_ADDR_BASE_HI;
  2669. msg->address_lo =
  2670. MSI_ADDR_BASE_LO |
  2671. ((INT_DEST_MODE == 0) ?
  2672. MSI_ADDR_DEST_MODE_PHYSICAL:
  2673. MSI_ADDR_DEST_MODE_LOGICAL) |
  2674. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2675. MSI_ADDR_REDIRECTION_CPU:
  2676. MSI_ADDR_REDIRECTION_LOWPRI) |
  2677. MSI_ADDR_DEST_ID(dest);
  2678. msg->data =
  2679. MSI_DATA_TRIGGER_EDGE |
  2680. MSI_DATA_LEVEL_ASSERT |
  2681. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2682. MSI_DATA_DELIVERY_FIXED:
  2683. MSI_DATA_DELIVERY_LOWPRI) |
  2684. MSI_DATA_VECTOR(cfg->vector);
  2685. }
  2686. return err;
  2687. }
  2688. #ifdef CONFIG_SMP
  2689. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2690. {
  2691. struct irq_desc *desc = irq_to_desc(irq);
  2692. struct irq_cfg *cfg;
  2693. struct msi_msg msg;
  2694. unsigned int dest;
  2695. cpumask_t tmp;
  2696. cpus_and(tmp, mask, cpu_online_map);
  2697. if (cpus_empty(tmp))
  2698. return;
  2699. cfg = desc->chip_data;
  2700. if (assign_irq_vector(irq, cfg, mask))
  2701. return;
  2702. set_extra_move_desc(desc, mask);
  2703. cpus_and(tmp, cfg->domain, mask);
  2704. dest = cpu_mask_to_apicid(tmp);
  2705. read_msi_msg_desc(desc, &msg);
  2706. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2707. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2708. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2709. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2710. write_msi_msg_desc(desc, &msg);
  2711. desc->affinity = mask;
  2712. }
  2713. #ifdef CONFIG_INTR_REMAP
  2714. /*
  2715. * Migrate the MSI irq to another cpumask. This migration is
  2716. * done in the process context using interrupt-remapping hardware.
  2717. */
  2718. static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2719. {
  2720. struct irq_desc *desc = irq_to_desc(irq);
  2721. struct irq_cfg *cfg;
  2722. unsigned int dest;
  2723. cpumask_t tmp, cleanup_mask;
  2724. struct irte irte;
  2725. cpus_and(tmp, mask, cpu_online_map);
  2726. if (cpus_empty(tmp))
  2727. return;
  2728. if (get_irte(irq, &irte))
  2729. return;
  2730. cfg = desc->chip_data;
  2731. if (assign_irq_vector(irq, cfg, mask))
  2732. return;
  2733. set_extra_move_desc(desc, mask);
  2734. cpus_and(tmp, cfg->domain, mask);
  2735. dest = cpu_mask_to_apicid(tmp);
  2736. irte.vector = cfg->vector;
  2737. irte.dest_id = IRTE_DEST(dest);
  2738. /*
  2739. * atomically update the IRTE with the new destination and vector.
  2740. */
  2741. modify_irte(irq, &irte);
  2742. /*
  2743. * After this point, all the interrupts will start arriving
  2744. * at the new destination. So, time to cleanup the previous
  2745. * vector allocation.
  2746. */
  2747. if (cfg->move_in_progress) {
  2748. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2749. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2750. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2751. cfg->move_in_progress = 0;
  2752. }
  2753. desc->affinity = mask;
  2754. }
  2755. #endif
  2756. #endif /* CONFIG_SMP */
  2757. /*
  2758. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2759. * which implement the MSI or MSI-X Capability Structure.
  2760. */
  2761. static struct irq_chip msi_chip = {
  2762. .name = "PCI-MSI",
  2763. .unmask = unmask_msi_irq,
  2764. .mask = mask_msi_irq,
  2765. .ack = ack_apic_edge,
  2766. #ifdef CONFIG_SMP
  2767. .set_affinity = set_msi_irq_affinity,
  2768. #endif
  2769. .retrigger = ioapic_retrigger_irq,
  2770. };
  2771. #ifdef CONFIG_INTR_REMAP
  2772. static struct irq_chip msi_ir_chip = {
  2773. .name = "IR-PCI-MSI",
  2774. .unmask = unmask_msi_irq,
  2775. .mask = mask_msi_irq,
  2776. .ack = ack_x2apic_edge,
  2777. #ifdef CONFIG_SMP
  2778. .set_affinity = ir_set_msi_irq_affinity,
  2779. #endif
  2780. .retrigger = ioapic_retrigger_irq,
  2781. };
  2782. /*
  2783. * Map the PCI dev to the corresponding remapping hardware unit
  2784. * and allocate 'nvec' consecutive interrupt-remapping table entries
  2785. * in it.
  2786. */
  2787. static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
  2788. {
  2789. struct intel_iommu *iommu;
  2790. int index;
  2791. iommu = map_dev_to_ir(dev);
  2792. if (!iommu) {
  2793. printk(KERN_ERR
  2794. "Unable to map PCI %s to iommu\n", pci_name(dev));
  2795. return -ENOENT;
  2796. }
  2797. index = alloc_irte(iommu, irq, nvec);
  2798. if (index < 0) {
  2799. printk(KERN_ERR
  2800. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  2801. pci_name(dev));
  2802. return -ENOSPC;
  2803. }
  2804. return index;
  2805. }
  2806. #endif
  2807. static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
  2808. {
  2809. int ret;
  2810. struct msi_msg msg;
  2811. ret = msi_compose_msg(dev, irq, &msg);
  2812. if (ret < 0)
  2813. return ret;
  2814. set_irq_msi(irq, msidesc);
  2815. write_msi_msg(irq, &msg);
  2816. #ifdef CONFIG_INTR_REMAP
  2817. if (irq_remapped(irq)) {
  2818. struct irq_desc *desc = irq_to_desc(irq);
  2819. /*
  2820. * irq migration in process context
  2821. */
  2822. desc->status |= IRQ_MOVE_PCNTXT;
  2823. set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
  2824. } else
  2825. #endif
  2826. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  2827. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
  2828. return 0;
  2829. }
  2830. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
  2831. {
  2832. unsigned int irq;
  2833. int ret;
  2834. unsigned int irq_want;
  2835. irq_want = nr_irqs_gsi;
  2836. irq = create_irq_nr(irq_want);
  2837. if (irq == 0)
  2838. return -1;
  2839. #ifdef CONFIG_INTR_REMAP
  2840. if (!intr_remapping_enabled)
  2841. goto no_ir;
  2842. ret = msi_alloc_irte(dev, irq, 1);
  2843. if (ret < 0)
  2844. goto error;
  2845. no_ir:
  2846. #endif
  2847. ret = setup_msi_irq(dev, msidesc, irq);
  2848. if (ret < 0) {
  2849. destroy_irq(irq);
  2850. return ret;
  2851. }
  2852. return 0;
  2853. #ifdef CONFIG_INTR_REMAP
  2854. error:
  2855. destroy_irq(irq);
  2856. return ret;
  2857. #endif
  2858. }
  2859. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  2860. {
  2861. unsigned int irq;
  2862. int ret, sub_handle;
  2863. struct msi_desc *msidesc;
  2864. unsigned int irq_want;
  2865. #ifdef CONFIG_INTR_REMAP
  2866. struct intel_iommu *iommu = 0;
  2867. int index = 0;
  2868. #endif
  2869. irq_want = nr_irqs_gsi;
  2870. sub_handle = 0;
  2871. list_for_each_entry(msidesc, &dev->msi_list, list) {
  2872. irq = create_irq_nr(irq_want);
  2873. irq_want++;
  2874. if (irq == 0)
  2875. return -1;
  2876. #ifdef CONFIG_INTR_REMAP
  2877. if (!intr_remapping_enabled)
  2878. goto no_ir;
  2879. if (!sub_handle) {
  2880. /*
  2881. * allocate the consecutive block of IRTE's
  2882. * for 'nvec'
  2883. */
  2884. index = msi_alloc_irte(dev, irq, nvec);
  2885. if (index < 0) {
  2886. ret = index;
  2887. goto error;
  2888. }
  2889. } else {
  2890. iommu = map_dev_to_ir(dev);
  2891. if (!iommu) {
  2892. ret = -ENOENT;
  2893. goto error;
  2894. }
  2895. /*
  2896. * setup the mapping between the irq and the IRTE
  2897. * base index, the sub_handle pointing to the
  2898. * appropriate interrupt remap table entry.
  2899. */
  2900. set_irte_irq(irq, iommu, index, sub_handle);
  2901. }
  2902. no_ir:
  2903. #endif
  2904. ret = setup_msi_irq(dev, msidesc, irq);
  2905. if (ret < 0)
  2906. goto error;
  2907. sub_handle++;
  2908. }
  2909. return 0;
  2910. error:
  2911. destroy_irq(irq);
  2912. return ret;
  2913. }
  2914. void arch_teardown_msi_irq(unsigned int irq)
  2915. {
  2916. destroy_irq(irq);
  2917. }
  2918. #ifdef CONFIG_DMAR
  2919. #ifdef CONFIG_SMP
  2920. static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2921. {
  2922. struct irq_desc *desc = irq_to_desc(irq);
  2923. struct irq_cfg *cfg;
  2924. struct msi_msg msg;
  2925. unsigned int dest;
  2926. cpumask_t tmp;
  2927. cpus_and(tmp, mask, cpu_online_map);
  2928. if (cpus_empty(tmp))
  2929. return;
  2930. cfg = desc->chip_data;
  2931. if (assign_irq_vector(irq, cfg, mask))
  2932. return;
  2933. set_extra_move_desc(desc, mask);
  2934. cpus_and(tmp, cfg->domain, mask);
  2935. dest = cpu_mask_to_apicid(tmp);
  2936. dmar_msi_read(irq, &msg);
  2937. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2938. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2939. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2940. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2941. dmar_msi_write(irq, &msg);
  2942. desc->affinity = mask;
  2943. }
  2944. #endif /* CONFIG_SMP */
  2945. struct irq_chip dmar_msi_type = {
  2946. .name = "DMAR_MSI",
  2947. .unmask = dmar_msi_unmask,
  2948. .mask = dmar_msi_mask,
  2949. .ack = ack_apic_edge,
  2950. #ifdef CONFIG_SMP
  2951. .set_affinity = dmar_msi_set_affinity,
  2952. #endif
  2953. .retrigger = ioapic_retrigger_irq,
  2954. };
  2955. int arch_setup_dmar_msi(unsigned int irq)
  2956. {
  2957. int ret;
  2958. struct msi_msg msg;
  2959. ret = msi_compose_msg(NULL, irq, &msg);
  2960. if (ret < 0)
  2961. return ret;
  2962. dmar_msi_write(irq, &msg);
  2963. set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  2964. "edge");
  2965. return 0;
  2966. }
  2967. #endif
  2968. #ifdef CONFIG_HPET_TIMER
  2969. #ifdef CONFIG_SMP
  2970. static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2971. {
  2972. struct irq_desc *desc = irq_to_desc(irq);
  2973. struct irq_cfg *cfg;
  2974. struct msi_msg msg;
  2975. unsigned int dest;
  2976. cpumask_t tmp;
  2977. cpus_and(tmp, mask, cpu_online_map);
  2978. if (cpus_empty(tmp))
  2979. return;
  2980. cfg = desc->chip_data;
  2981. if (assign_irq_vector(irq, cfg, mask))
  2982. return;
  2983. set_extra_move_desc(desc, mask);
  2984. cpus_and(tmp, cfg->domain, mask);
  2985. dest = cpu_mask_to_apicid(tmp);
  2986. hpet_msi_read(irq, &msg);
  2987. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2988. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2989. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2990. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2991. hpet_msi_write(irq, &msg);
  2992. desc->affinity = mask;
  2993. }
  2994. #endif /* CONFIG_SMP */
  2995. struct irq_chip hpet_msi_type = {
  2996. .name = "HPET_MSI",
  2997. .unmask = hpet_msi_unmask,
  2998. .mask = hpet_msi_mask,
  2999. .ack = ack_apic_edge,
  3000. #ifdef CONFIG_SMP
  3001. .set_affinity = hpet_msi_set_affinity,
  3002. #endif
  3003. .retrigger = ioapic_retrigger_irq,
  3004. };
  3005. int arch_setup_hpet_msi(unsigned int irq)
  3006. {
  3007. int ret;
  3008. struct msi_msg msg;
  3009. ret = msi_compose_msg(NULL, irq, &msg);
  3010. if (ret < 0)
  3011. return ret;
  3012. hpet_msi_write(irq, &msg);
  3013. set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
  3014. "edge");
  3015. return 0;
  3016. }
  3017. #endif
  3018. #endif /* CONFIG_PCI_MSI */
  3019. /*
  3020. * Hypertransport interrupt support
  3021. */
  3022. #ifdef CONFIG_HT_IRQ
  3023. #ifdef CONFIG_SMP
  3024. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  3025. {
  3026. struct ht_irq_msg msg;
  3027. fetch_ht_irq_msg(irq, &msg);
  3028. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  3029. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  3030. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  3031. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  3032. write_ht_irq_msg(irq, &msg);
  3033. }
  3034. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  3035. {
  3036. struct irq_desc *desc = irq_to_desc(irq);
  3037. struct irq_cfg *cfg;
  3038. unsigned int dest;
  3039. cpumask_t tmp;
  3040. cpus_and(tmp, mask, cpu_online_map);
  3041. if (cpus_empty(tmp))
  3042. return;
  3043. cfg = desc->chip_data;
  3044. if (assign_irq_vector(irq, cfg, mask))
  3045. return;
  3046. set_extra_move_desc(desc, mask);
  3047. cpus_and(tmp, cfg->domain, mask);
  3048. dest = cpu_mask_to_apicid(tmp);
  3049. target_ht_irq(irq, dest, cfg->vector);
  3050. desc->affinity = mask;
  3051. }
  3052. #endif
  3053. static struct irq_chip ht_irq_chip = {
  3054. .name = "PCI-HT",
  3055. .mask = mask_ht_irq,
  3056. .unmask = unmask_ht_irq,
  3057. .ack = ack_apic_edge,
  3058. #ifdef CONFIG_SMP
  3059. .set_affinity = set_ht_irq_affinity,
  3060. #endif
  3061. .retrigger = ioapic_retrigger_irq,
  3062. };
  3063. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  3064. {
  3065. struct irq_cfg *cfg;
  3066. int err;
  3067. cpumask_t tmp;
  3068. cfg = irq_cfg(irq);
  3069. tmp = TARGET_CPUS;
  3070. err = assign_irq_vector(irq, cfg, tmp);
  3071. if (!err) {
  3072. struct ht_irq_msg msg;
  3073. unsigned dest;
  3074. cpus_and(tmp, cfg->domain, tmp);
  3075. dest = cpu_mask_to_apicid(tmp);
  3076. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  3077. msg.address_lo =
  3078. HT_IRQ_LOW_BASE |
  3079. HT_IRQ_LOW_DEST_ID(dest) |
  3080. HT_IRQ_LOW_VECTOR(cfg->vector) |
  3081. ((INT_DEST_MODE == 0) ?
  3082. HT_IRQ_LOW_DM_PHYSICAL :
  3083. HT_IRQ_LOW_DM_LOGICAL) |
  3084. HT_IRQ_LOW_RQEOI_EDGE |
  3085. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  3086. HT_IRQ_LOW_MT_FIXED :
  3087. HT_IRQ_LOW_MT_ARBITRATED) |
  3088. HT_IRQ_LOW_IRQ_MASKED;
  3089. write_ht_irq_msg(irq, &msg);
  3090. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  3091. handle_edge_irq, "edge");
  3092. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
  3093. }
  3094. return err;
  3095. }
  3096. #endif /* CONFIG_HT_IRQ */
  3097. #ifdef CONFIG_X86_64
  3098. /*
  3099. * Re-target the irq to the specified CPU and enable the specified MMR located
  3100. * on the specified blade to allow the sending of MSIs to the specified CPU.
  3101. */
  3102. int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
  3103. unsigned long mmr_offset)
  3104. {
  3105. const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
  3106. struct irq_cfg *cfg;
  3107. int mmr_pnode;
  3108. unsigned long mmr_value;
  3109. struct uv_IO_APIC_route_entry *entry;
  3110. unsigned long flags;
  3111. int err;
  3112. cfg = irq_cfg(irq);
  3113. err = assign_irq_vector(irq, cfg, *eligible_cpu);
  3114. if (err != 0)
  3115. return err;
  3116. spin_lock_irqsave(&vector_lock, flags);
  3117. set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
  3118. irq_name);
  3119. spin_unlock_irqrestore(&vector_lock, flags);
  3120. mmr_value = 0;
  3121. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  3122. BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
  3123. entry->vector = cfg->vector;
  3124. entry->delivery_mode = INT_DELIVERY_MODE;
  3125. entry->dest_mode = INT_DEST_MODE;
  3126. entry->polarity = 0;
  3127. entry->trigger = 0;
  3128. entry->mask = 0;
  3129. entry->dest = cpu_mask_to_apicid(*eligible_cpu);
  3130. mmr_pnode = uv_blade_to_pnode(mmr_blade);
  3131. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  3132. return irq;
  3133. }
  3134. /*
  3135. * Disable the specified MMR located on the specified blade so that MSIs are
  3136. * longer allowed to be sent.
  3137. */
  3138. void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
  3139. {
  3140. unsigned long mmr_value;
  3141. struct uv_IO_APIC_route_entry *entry;
  3142. int mmr_pnode;
  3143. mmr_value = 0;
  3144. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  3145. BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
  3146. entry->mask = 1;
  3147. mmr_pnode = uv_blade_to_pnode(mmr_blade);
  3148. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  3149. }
  3150. #endif /* CONFIG_X86_64 */
  3151. int __init io_apic_get_redir_entries (int ioapic)
  3152. {
  3153. union IO_APIC_reg_01 reg_01;
  3154. unsigned long flags;
  3155. spin_lock_irqsave(&ioapic_lock, flags);
  3156. reg_01.raw = io_apic_read(ioapic, 1);
  3157. spin_unlock_irqrestore(&ioapic_lock, flags);
  3158. return reg_01.bits.entries;
  3159. }
  3160. void __init probe_nr_irqs_gsi(void)
  3161. {
  3162. int idx;
  3163. int nr = 0;
  3164. for (idx = 0; idx < nr_ioapics; idx++)
  3165. nr += io_apic_get_redir_entries(idx) + 1;
  3166. if (nr > nr_irqs_gsi)
  3167. nr_irqs_gsi = nr;
  3168. }
  3169. /* --------------------------------------------------------------------------
  3170. ACPI-based IOAPIC Configuration
  3171. -------------------------------------------------------------------------- */
  3172. #ifdef CONFIG_ACPI
  3173. #ifdef CONFIG_X86_32
  3174. int __init io_apic_get_unique_id(int ioapic, int apic_id)
  3175. {
  3176. union IO_APIC_reg_00 reg_00;
  3177. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  3178. physid_mask_t tmp;
  3179. unsigned long flags;
  3180. int i = 0;
  3181. /*
  3182. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  3183. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  3184. * supports up to 16 on one shared APIC bus.
  3185. *
  3186. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  3187. * advantage of new APIC bus architecture.
  3188. */
  3189. if (physids_empty(apic_id_map))
  3190. apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
  3191. spin_lock_irqsave(&ioapic_lock, flags);
  3192. reg_00.raw = io_apic_read(ioapic, 0);
  3193. spin_unlock_irqrestore(&ioapic_lock, flags);
  3194. if (apic_id >= get_physical_broadcast()) {
  3195. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  3196. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  3197. apic_id = reg_00.bits.ID;
  3198. }
  3199. /*
  3200. * Every APIC in a system must have a unique ID or we get lots of nice
  3201. * 'stuck on smp_invalidate_needed IPI wait' messages.
  3202. */
  3203. if (check_apicid_used(apic_id_map, apic_id)) {
  3204. for (i = 0; i < get_physical_broadcast(); i++) {
  3205. if (!check_apicid_used(apic_id_map, i))
  3206. break;
  3207. }
  3208. if (i == get_physical_broadcast())
  3209. panic("Max apic_id exceeded!\n");
  3210. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  3211. "trying %d\n", ioapic, apic_id, i);
  3212. apic_id = i;
  3213. }
  3214. tmp = apicid_to_cpu_present(apic_id);
  3215. physids_or(apic_id_map, apic_id_map, tmp);
  3216. if (reg_00.bits.ID != apic_id) {
  3217. reg_00.bits.ID = apic_id;
  3218. spin_lock_irqsave(&ioapic_lock, flags);
  3219. io_apic_write(ioapic, 0, reg_00.raw);
  3220. reg_00.raw = io_apic_read(ioapic, 0);
  3221. spin_unlock_irqrestore(&ioapic_lock, flags);
  3222. /* Sanity check */
  3223. if (reg_00.bits.ID != apic_id) {
  3224. printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
  3225. return -1;
  3226. }
  3227. }
  3228. apic_printk(APIC_VERBOSE, KERN_INFO
  3229. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  3230. return apic_id;
  3231. }
  3232. int __init io_apic_get_version(int ioapic)
  3233. {
  3234. union IO_APIC_reg_01 reg_01;
  3235. unsigned long flags;
  3236. spin_lock_irqsave(&ioapic_lock, flags);
  3237. reg_01.raw = io_apic_read(ioapic, 1);
  3238. spin_unlock_irqrestore(&ioapic_lock, flags);
  3239. return reg_01.bits.version;
  3240. }
  3241. #endif
  3242. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  3243. {
  3244. struct irq_desc *desc;
  3245. struct irq_cfg *cfg;
  3246. int cpu = boot_cpu_id;
  3247. if (!IO_APIC_IRQ(irq)) {
  3248. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  3249. ioapic);
  3250. return -EINVAL;
  3251. }
  3252. desc = irq_to_desc_alloc_cpu(irq, cpu);
  3253. if (!desc) {
  3254. printk(KERN_INFO "can not get irq_desc %d\n", irq);
  3255. return 0;
  3256. }
  3257. /*
  3258. * IRQs < 16 are already in the irq_2_pin[] map
  3259. */
  3260. if (irq >= NR_IRQS_LEGACY) {
  3261. cfg = desc->chip_data;
  3262. add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
  3263. }
  3264. setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
  3265. return 0;
  3266. }
  3267. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  3268. {
  3269. int i;
  3270. if (skip_ioapic_setup)
  3271. return -1;
  3272. for (i = 0; i < mp_irq_entries; i++)
  3273. if (mp_irqs[i].mp_irqtype == mp_INT &&
  3274. mp_irqs[i].mp_srcbusirq == bus_irq)
  3275. break;
  3276. if (i >= mp_irq_entries)
  3277. return -1;
  3278. *trigger = irq_trigger(i);
  3279. *polarity = irq_polarity(i);
  3280. return 0;
  3281. }
  3282. #endif /* CONFIG_ACPI */
  3283. /*
  3284. * This function currently is only a helper for the i386 smp boot process where
  3285. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  3286. * so mask in all cases should simply be TARGET_CPUS
  3287. */
  3288. #ifdef CONFIG_SMP
  3289. void __init setup_ioapic_dest(void)
  3290. {
  3291. int pin, ioapic, irq, irq_entry;
  3292. struct irq_desc *desc;
  3293. struct irq_cfg *cfg;
  3294. cpumask_t mask;
  3295. if (skip_ioapic_setup == 1)
  3296. return;
  3297. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  3298. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  3299. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  3300. if (irq_entry == -1)
  3301. continue;
  3302. irq = pin_2_irq(irq_entry, ioapic, pin);
  3303. /* setup_IO_APIC_irqs could fail to get vector for some device
  3304. * when you have too many devices, because at that time only boot
  3305. * cpu is online.
  3306. */
  3307. desc = irq_to_desc(irq);
  3308. cfg = desc->chip_data;
  3309. if (!cfg->vector) {
  3310. setup_IO_APIC_irq(ioapic, pin, irq, desc,
  3311. irq_trigger(irq_entry),
  3312. irq_polarity(irq_entry));
  3313. continue;
  3314. }
  3315. /*
  3316. * Honour affinities which have been set in early boot
  3317. */
  3318. if (desc->status &
  3319. (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
  3320. mask = desc->affinity;
  3321. else
  3322. mask = TARGET_CPUS;
  3323. #ifdef CONFIG_INTR_REMAP
  3324. if (intr_remapping_enabled)
  3325. set_ir_ioapic_affinity_irq_desc(desc, mask);
  3326. else
  3327. #endif
  3328. set_ioapic_affinity_irq_desc(desc, mask);
  3329. }
  3330. }
  3331. }
  3332. #endif
  3333. #define IOAPIC_RESOURCE_NAME_SIZE 11
  3334. static struct resource *ioapic_resources;
  3335. static struct resource * __init ioapic_setup_resources(void)
  3336. {
  3337. unsigned long n;
  3338. struct resource *res;
  3339. char *mem;
  3340. int i;
  3341. if (nr_ioapics <= 0)
  3342. return NULL;
  3343. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  3344. n *= nr_ioapics;
  3345. mem = alloc_bootmem(n);
  3346. res = (void *)mem;
  3347. if (mem != NULL) {
  3348. mem += sizeof(struct resource) * nr_ioapics;
  3349. for (i = 0; i < nr_ioapics; i++) {
  3350. res[i].name = mem;
  3351. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  3352. sprintf(mem, "IOAPIC %u", i);
  3353. mem += IOAPIC_RESOURCE_NAME_SIZE;
  3354. }
  3355. }
  3356. ioapic_resources = res;
  3357. return res;
  3358. }
  3359. void __init ioapic_init_mappings(void)
  3360. {
  3361. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  3362. struct resource *ioapic_res;
  3363. int i;
  3364. ioapic_res = ioapic_setup_resources();
  3365. for (i = 0; i < nr_ioapics; i++) {
  3366. if (smp_found_config) {
  3367. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  3368. #ifdef CONFIG_X86_32
  3369. if (!ioapic_phys) {
  3370. printk(KERN_ERR
  3371. "WARNING: bogus zero IO-APIC "
  3372. "address found in MPTABLE, "
  3373. "disabling IO/APIC support!\n");
  3374. smp_found_config = 0;
  3375. skip_ioapic_setup = 1;
  3376. goto fake_ioapic_page;
  3377. }
  3378. #endif
  3379. } else {
  3380. #ifdef CONFIG_X86_32
  3381. fake_ioapic_page:
  3382. #endif
  3383. ioapic_phys = (unsigned long)
  3384. alloc_bootmem_pages(PAGE_SIZE);
  3385. ioapic_phys = __pa(ioapic_phys);
  3386. }
  3387. set_fixmap_nocache(idx, ioapic_phys);
  3388. apic_printk(APIC_VERBOSE,
  3389. "mapped IOAPIC to %08lx (%08lx)\n",
  3390. __fix_to_virt(idx), ioapic_phys);
  3391. idx++;
  3392. if (ioapic_res != NULL) {
  3393. ioapic_res->start = ioapic_phys;
  3394. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  3395. ioapic_res++;
  3396. }
  3397. }
  3398. }
  3399. static int __init ioapic_insert_resources(void)
  3400. {
  3401. int i;
  3402. struct resource *r = ioapic_resources;
  3403. if (!r) {
  3404. printk(KERN_ERR
  3405. "IO APIC resources could be not be allocated.\n");
  3406. return -1;
  3407. }
  3408. for (i = 0; i < nr_ioapics; i++) {
  3409. insert_resource(&iomem_resource, r);
  3410. r++;
  3411. }
  3412. return 0;
  3413. }
  3414. /* Insert the IO APIC resources after PCI initialization has occured to handle
  3415. * IO APICS that are mapped in on a BAR in PCI space. */
  3416. late_initcall(ioapic_insert_resources);