io_apic.c 101 KB

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