io_apic.c 100 KB

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