io_apic.c 98 KB

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