io_apic.c 99 KB

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