io_apic.c 98 KB

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