io_apic.c 98 KB

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