io_apic.c 95 KB

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