io_apic.c 98 KB

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