io_apic.c 95 KB

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