io_apic.c 101 KB

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