io_apic.c 100 KB

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