io_apic.c 98 KB

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