io_apic.c 94 KB

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