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