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