io_apic.c 99 KB

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