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