io_apic.c 97 KB

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