io_apic.c 95 KB

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