io_apic.c 96 KB

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