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