io_apic.c 93 KB

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