io_apic.c 98 KB

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