io_apic.c 98 KB

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