io_apic.c 95 KB

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