io_apic.c 93 KB

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