io_apic.c 99 KB

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