io_apic.c 99 KB

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