io_apic.c 92 KB

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