io_apic.c 99 KB

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