io_apic.c 97 KB

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