io_apic.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896
  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, cpumask_t mask)
  314. {
  315. struct irq_cfg *cfg;
  316. unsigned long flags;
  317. unsigned int dest;
  318. cpumask_t tmp;
  319. struct irq_desc *desc;
  320. cpus_and(tmp, mask, cpu_online_map);
  321. if (cpus_empty(tmp))
  322. return;
  323. cfg = irq_cfg(irq);
  324. if (assign_irq_vector(irq, mask))
  325. return;
  326. cpus_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. 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. }
  988. void __setup_vector_irq(int cpu)
  989. {
  990. /* Initialize vector_irq on a new cpu */
  991. /* This function must be called with vector_lock held */
  992. int irq, vector;
  993. struct irq_cfg *cfg;
  994. /* Mark the inuse vectors */
  995. for_each_irq_cfg(irq, cfg) {
  996. if (!cpu_isset(cpu, cfg->domain))
  997. continue;
  998. vector = cfg->vector;
  999. per_cpu(vector_irq, cpu)[vector] = irq;
  1000. }
  1001. /* Mark the free vectors */
  1002. for (vector = 0; vector < NR_VECTORS; ++vector) {
  1003. irq = per_cpu(vector_irq, cpu)[vector];
  1004. if (irq < 0)
  1005. continue;
  1006. cfg = irq_cfg(irq);
  1007. if (!cpu_isset(cpu, cfg->domain))
  1008. per_cpu(vector_irq, cpu)[vector] = -1;
  1009. }
  1010. }
  1011. static struct irq_chip ioapic_chip;
  1012. #ifdef CONFIG_INTR_REMAP
  1013. static struct irq_chip ir_ioapic_chip;
  1014. #endif
  1015. #define IOAPIC_AUTO -1
  1016. #define IOAPIC_EDGE 0
  1017. #define IOAPIC_LEVEL 1
  1018. #ifdef CONFIG_X86_32
  1019. static inline int IO_APIC_irq_trigger(int irq)
  1020. {
  1021. int apic, idx, pin;
  1022. for (apic = 0; apic < nr_ioapics; apic++) {
  1023. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1024. idx = find_irq_entry(apic, pin, mp_INT);
  1025. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
  1026. return irq_trigger(idx);
  1027. }
  1028. }
  1029. /*
  1030. * nonexistent IRQs are edge default
  1031. */
  1032. return 0;
  1033. }
  1034. #else
  1035. static inline int IO_APIC_irq_trigger(int irq)
  1036. {
  1037. return 1;
  1038. }
  1039. #endif
  1040. static void ioapic_register_intr(int irq, unsigned long trigger)
  1041. {
  1042. struct irq_desc *desc;
  1043. desc = irq_to_desc(irq);
  1044. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1045. trigger == IOAPIC_LEVEL)
  1046. desc->status |= IRQ_LEVEL;
  1047. else
  1048. desc->status &= ~IRQ_LEVEL;
  1049. #ifdef CONFIG_INTR_REMAP
  1050. if (irq_remapped(irq)) {
  1051. desc->status |= IRQ_MOVE_PCNTXT;
  1052. if (trigger)
  1053. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  1054. handle_fasteoi_irq,
  1055. "fasteoi");
  1056. else
  1057. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  1058. handle_edge_irq, "edge");
  1059. return;
  1060. }
  1061. #endif
  1062. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1063. trigger == IOAPIC_LEVEL)
  1064. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1065. handle_fasteoi_irq,
  1066. "fasteoi");
  1067. else
  1068. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1069. handle_edge_irq, "edge");
  1070. }
  1071. static int setup_ioapic_entry(int apic, int irq,
  1072. struct IO_APIC_route_entry *entry,
  1073. unsigned int destination, int trigger,
  1074. int polarity, int vector)
  1075. {
  1076. /*
  1077. * add it to the IO-APIC irq-routing table:
  1078. */
  1079. memset(entry,0,sizeof(*entry));
  1080. #ifdef CONFIG_INTR_REMAP
  1081. if (intr_remapping_enabled) {
  1082. struct intel_iommu *iommu = map_ioapic_to_ir(apic);
  1083. struct irte irte;
  1084. struct IR_IO_APIC_route_entry *ir_entry =
  1085. (struct IR_IO_APIC_route_entry *) entry;
  1086. int index;
  1087. if (!iommu)
  1088. panic("No mapping iommu for ioapic %d\n", apic);
  1089. index = alloc_irte(iommu, irq, 1);
  1090. if (index < 0)
  1091. panic("Failed to allocate IRTE for ioapic %d\n", apic);
  1092. memset(&irte, 0, sizeof(irte));
  1093. irte.present = 1;
  1094. irte.dst_mode = INT_DEST_MODE;
  1095. irte.trigger_mode = trigger;
  1096. irte.dlvry_mode = INT_DELIVERY_MODE;
  1097. irte.vector = vector;
  1098. irte.dest_id = IRTE_DEST(destination);
  1099. modify_irte(irq, &irte);
  1100. ir_entry->index2 = (index >> 15) & 0x1;
  1101. ir_entry->zero = 0;
  1102. ir_entry->format = 1;
  1103. ir_entry->index = (index & 0x7fff);
  1104. } else
  1105. #endif
  1106. {
  1107. entry->delivery_mode = INT_DELIVERY_MODE;
  1108. entry->dest_mode = INT_DEST_MODE;
  1109. entry->dest = destination;
  1110. }
  1111. entry->mask = 0; /* enable IRQ */
  1112. entry->trigger = trigger;
  1113. entry->polarity = polarity;
  1114. entry->vector = vector;
  1115. /* Mask level triggered irqs.
  1116. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  1117. */
  1118. if (trigger)
  1119. entry->mask = 1;
  1120. return 0;
  1121. }
  1122. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
  1123. int trigger, int polarity)
  1124. {
  1125. struct irq_cfg *cfg;
  1126. struct IO_APIC_route_entry entry;
  1127. cpumask_t mask;
  1128. if (!IO_APIC_IRQ(irq))
  1129. return;
  1130. cfg = irq_cfg(irq);
  1131. mask = TARGET_CPUS;
  1132. if (assign_irq_vector(irq, mask))
  1133. return;
  1134. cpus_and(mask, cfg->domain, mask);
  1135. apic_printk(APIC_VERBOSE,KERN_DEBUG
  1136. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  1137. "IRQ %d Mode:%i Active:%i)\n",
  1138. apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
  1139. irq, trigger, polarity);
  1140. if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
  1141. cpu_mask_to_apicid(mask), trigger, polarity,
  1142. cfg->vector)) {
  1143. printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
  1144. mp_ioapics[apic].mp_apicid, pin);
  1145. __clear_irq_vector(irq);
  1146. return;
  1147. }
  1148. ioapic_register_intr(irq, trigger);
  1149. if (irq < 16)
  1150. disable_8259A_irq(irq);
  1151. ioapic_write_entry(apic, pin, entry);
  1152. }
  1153. static void __init setup_IO_APIC_irqs(void)
  1154. {
  1155. int apic, pin, idx, irq;
  1156. int notcon = 0;
  1157. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1158. for (apic = 0; apic < nr_ioapics; apic++) {
  1159. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1160. idx = find_irq_entry(apic, pin, mp_INT);
  1161. if (idx == -1) {
  1162. if (!notcon) {
  1163. notcon = 1;
  1164. apic_printk(APIC_VERBOSE,
  1165. KERN_DEBUG " %d-%d",
  1166. mp_ioapics[apic].mp_apicid,
  1167. pin);
  1168. } else
  1169. apic_printk(APIC_VERBOSE, " %d-%d",
  1170. mp_ioapics[apic].mp_apicid,
  1171. pin);
  1172. continue;
  1173. }
  1174. if (notcon) {
  1175. apic_printk(APIC_VERBOSE,
  1176. " (apicid-pin) not connected\n");
  1177. notcon = 0;
  1178. }
  1179. irq = pin_2_irq(idx, apic, pin);
  1180. #ifdef CONFIG_X86_32
  1181. if (multi_timer_check(apic, irq))
  1182. continue;
  1183. #endif
  1184. add_pin_to_irq(irq, apic, pin);
  1185. setup_IO_APIC_irq(apic, pin, irq,
  1186. irq_trigger(idx), irq_polarity(idx));
  1187. }
  1188. }
  1189. if (notcon)
  1190. apic_printk(APIC_VERBOSE,
  1191. " (apicid-pin) not connected\n");
  1192. }
  1193. /*
  1194. * Set up the timer pin, possibly with the 8259A-master behind.
  1195. */
  1196. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  1197. int vector)
  1198. {
  1199. struct IO_APIC_route_entry entry;
  1200. #ifdef CONFIG_INTR_REMAP
  1201. if (intr_remapping_enabled)
  1202. return;
  1203. #endif
  1204. memset(&entry, 0, sizeof(entry));
  1205. /*
  1206. * We use logical delivery to get the timer IRQ
  1207. * to the first CPU.
  1208. */
  1209. entry.dest_mode = INT_DEST_MODE;
  1210. entry.mask = 1; /* mask IRQ now */
  1211. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  1212. entry.delivery_mode = INT_DELIVERY_MODE;
  1213. entry.polarity = 0;
  1214. entry.trigger = 0;
  1215. entry.vector = vector;
  1216. /*
  1217. * The timer IRQ doesn't have to know that behind the
  1218. * scene we may have a 8259A-master in AEOI mode ...
  1219. */
  1220. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  1221. /*
  1222. * Add it to the IO-APIC irq-routing table:
  1223. */
  1224. ioapic_write_entry(apic, pin, entry);
  1225. }
  1226. __apicdebuginit(void) print_IO_APIC(void)
  1227. {
  1228. int apic, i;
  1229. union IO_APIC_reg_00 reg_00;
  1230. union IO_APIC_reg_01 reg_01;
  1231. union IO_APIC_reg_02 reg_02;
  1232. union IO_APIC_reg_03 reg_03;
  1233. unsigned long flags;
  1234. struct irq_cfg *cfg;
  1235. unsigned int irq;
  1236. if (apic_verbosity == APIC_QUIET)
  1237. return;
  1238. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1239. for (i = 0; i < nr_ioapics; i++)
  1240. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1241. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  1242. /*
  1243. * We are a bit conservative about what we expect. We have to
  1244. * know about every hardware change ASAP.
  1245. */
  1246. printk(KERN_INFO "testing the IO APIC.......................\n");
  1247. for (apic = 0; apic < nr_ioapics; apic++) {
  1248. spin_lock_irqsave(&ioapic_lock, flags);
  1249. reg_00.raw = io_apic_read(apic, 0);
  1250. reg_01.raw = io_apic_read(apic, 1);
  1251. if (reg_01.bits.version >= 0x10)
  1252. reg_02.raw = io_apic_read(apic, 2);
  1253. if (reg_01.bits.version >= 0x20)
  1254. reg_03.raw = io_apic_read(apic, 3);
  1255. spin_unlock_irqrestore(&ioapic_lock, flags);
  1256. printk("\n");
  1257. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  1258. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1259. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1260. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1261. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1262. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  1263. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  1264. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1265. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  1266. /*
  1267. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1268. * but the value of reg_02 is read as the previous read register
  1269. * value, so ignore it if reg_02 == reg_01.
  1270. */
  1271. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1272. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1273. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1274. }
  1275. /*
  1276. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1277. * or reg_03, but the value of reg_0[23] is read as the previous read
  1278. * register value, so ignore it if reg_03 == reg_0[12].
  1279. */
  1280. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1281. reg_03.raw != reg_01.raw) {
  1282. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1283. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1284. }
  1285. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1286. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  1287. " Stat Dmod Deli Vect: \n");
  1288. for (i = 0; i <= reg_01.bits.entries; i++) {
  1289. struct IO_APIC_route_entry entry;
  1290. entry = ioapic_read_entry(apic, i);
  1291. printk(KERN_DEBUG " %02x %03X ",
  1292. i,
  1293. entry.dest
  1294. );
  1295. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1296. entry.mask,
  1297. entry.trigger,
  1298. entry.irr,
  1299. entry.polarity,
  1300. entry.delivery_status,
  1301. entry.dest_mode,
  1302. entry.delivery_mode,
  1303. entry.vector
  1304. );
  1305. }
  1306. }
  1307. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1308. for_each_irq_cfg(irq, cfg) {
  1309. struct irq_pin_list *entry = cfg->irq_2_pin;
  1310. if (!entry)
  1311. continue;
  1312. printk(KERN_DEBUG "IRQ%d ", irq);
  1313. for (;;) {
  1314. printk("-> %d:%d", entry->apic, entry->pin);
  1315. if (!entry->next)
  1316. break;
  1317. entry = entry->next;
  1318. }
  1319. printk("\n");
  1320. }
  1321. printk(KERN_INFO ".................................... done.\n");
  1322. return;
  1323. }
  1324. __apicdebuginit(void) print_APIC_bitfield(int base)
  1325. {
  1326. unsigned int v;
  1327. int i, j;
  1328. if (apic_verbosity == APIC_QUIET)
  1329. return;
  1330. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1331. for (i = 0; i < 8; i++) {
  1332. v = apic_read(base + i*0x10);
  1333. for (j = 0; j < 32; j++) {
  1334. if (v & (1<<j))
  1335. printk("1");
  1336. else
  1337. printk("0");
  1338. }
  1339. printk("\n");
  1340. }
  1341. }
  1342. __apicdebuginit(void) print_local_APIC(void *dummy)
  1343. {
  1344. unsigned int v, ver, maxlvt;
  1345. u64 icr;
  1346. if (apic_verbosity == APIC_QUIET)
  1347. return;
  1348. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1349. smp_processor_id(), hard_smp_processor_id());
  1350. v = apic_read(APIC_ID);
  1351. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
  1352. v = apic_read(APIC_LVR);
  1353. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1354. ver = GET_APIC_VERSION(v);
  1355. maxlvt = lapic_get_maxlvt();
  1356. v = apic_read(APIC_TASKPRI);
  1357. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1358. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1359. if (!APIC_XAPIC(ver)) {
  1360. v = apic_read(APIC_ARBPRI);
  1361. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1362. v & APIC_ARBPRI_MASK);
  1363. }
  1364. v = apic_read(APIC_PROCPRI);
  1365. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1366. }
  1367. /*
  1368. * Remote read supported only in the 82489DX and local APIC for
  1369. * Pentium processors.
  1370. */
  1371. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  1372. v = apic_read(APIC_RRR);
  1373. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1374. }
  1375. v = apic_read(APIC_LDR);
  1376. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1377. if (!x2apic_enabled()) {
  1378. v = apic_read(APIC_DFR);
  1379. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1380. }
  1381. v = apic_read(APIC_SPIV);
  1382. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1383. printk(KERN_DEBUG "... APIC ISR field:\n");
  1384. print_APIC_bitfield(APIC_ISR);
  1385. printk(KERN_DEBUG "... APIC TMR field:\n");
  1386. print_APIC_bitfield(APIC_TMR);
  1387. printk(KERN_DEBUG "... APIC IRR field:\n");
  1388. print_APIC_bitfield(APIC_IRR);
  1389. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1390. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1391. apic_write(APIC_ESR, 0);
  1392. v = apic_read(APIC_ESR);
  1393. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1394. }
  1395. icr = apic_icr_read();
  1396. printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
  1397. printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
  1398. v = apic_read(APIC_LVTT);
  1399. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1400. if (maxlvt > 3) { /* PC is LVT#4. */
  1401. v = apic_read(APIC_LVTPC);
  1402. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1403. }
  1404. v = apic_read(APIC_LVT0);
  1405. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1406. v = apic_read(APIC_LVT1);
  1407. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1408. if (maxlvt > 2) { /* ERR is LVT#3. */
  1409. v = apic_read(APIC_LVTERR);
  1410. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1411. }
  1412. v = apic_read(APIC_TMICT);
  1413. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1414. v = apic_read(APIC_TMCCT);
  1415. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1416. v = apic_read(APIC_TDCR);
  1417. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1418. printk("\n");
  1419. }
  1420. __apicdebuginit(void) print_all_local_APICs(void)
  1421. {
  1422. int cpu;
  1423. preempt_disable();
  1424. for_each_online_cpu(cpu)
  1425. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  1426. preempt_enable();
  1427. }
  1428. __apicdebuginit(void) print_PIC(void)
  1429. {
  1430. unsigned int v;
  1431. unsigned long flags;
  1432. if (apic_verbosity == APIC_QUIET)
  1433. return;
  1434. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1435. spin_lock_irqsave(&i8259A_lock, flags);
  1436. v = inb(0xa1) << 8 | inb(0x21);
  1437. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1438. v = inb(0xa0) << 8 | inb(0x20);
  1439. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1440. outb(0x0b,0xa0);
  1441. outb(0x0b,0x20);
  1442. v = inb(0xa0) << 8 | inb(0x20);
  1443. outb(0x0a,0xa0);
  1444. outb(0x0a,0x20);
  1445. spin_unlock_irqrestore(&i8259A_lock, flags);
  1446. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1447. v = inb(0x4d1) << 8 | inb(0x4d0);
  1448. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1449. }
  1450. __apicdebuginit(int) print_all_ICs(void)
  1451. {
  1452. print_PIC();
  1453. print_all_local_APICs();
  1454. print_IO_APIC();
  1455. return 0;
  1456. }
  1457. fs_initcall(print_all_ICs);
  1458. /* Where if anywhere is the i8259 connect in external int mode */
  1459. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  1460. void __init enable_IO_APIC(void)
  1461. {
  1462. union IO_APIC_reg_01 reg_01;
  1463. int i8259_apic, i8259_pin;
  1464. int apic;
  1465. unsigned long flags;
  1466. #ifdef CONFIG_X86_32
  1467. int i;
  1468. if (!pirqs_enabled)
  1469. for (i = 0; i < MAX_PIRQS; i++)
  1470. pirq_entries[i] = -1;
  1471. #endif
  1472. /*
  1473. * The number of IO-APIC IRQ registers (== #pins):
  1474. */
  1475. for (apic = 0; apic < nr_ioapics; apic++) {
  1476. spin_lock_irqsave(&ioapic_lock, flags);
  1477. reg_01.raw = io_apic_read(apic, 1);
  1478. spin_unlock_irqrestore(&ioapic_lock, flags);
  1479. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1480. }
  1481. for(apic = 0; apic < nr_ioapics; apic++) {
  1482. int pin;
  1483. /* See if any of the pins is in ExtINT mode */
  1484. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1485. struct IO_APIC_route_entry entry;
  1486. entry = ioapic_read_entry(apic, pin);
  1487. /* If the interrupt line is enabled and in ExtInt mode
  1488. * I have found the pin where the i8259 is connected.
  1489. */
  1490. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1491. ioapic_i8259.apic = apic;
  1492. ioapic_i8259.pin = pin;
  1493. goto found_i8259;
  1494. }
  1495. }
  1496. }
  1497. found_i8259:
  1498. /* Look to see what if the MP table has reported the ExtINT */
  1499. /* If we could not find the appropriate pin by looking at the ioapic
  1500. * the i8259 probably is not connected the ioapic but give the
  1501. * mptable a chance anyway.
  1502. */
  1503. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1504. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1505. /* Trust the MP table if nothing is setup in the hardware */
  1506. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1507. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1508. ioapic_i8259.pin = i8259_pin;
  1509. ioapic_i8259.apic = i8259_apic;
  1510. }
  1511. /* Complain if the MP table and the hardware disagree */
  1512. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1513. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1514. {
  1515. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1516. }
  1517. /*
  1518. * Do not trust the IO-APIC being empty at bootup
  1519. */
  1520. clear_IO_APIC();
  1521. }
  1522. /*
  1523. * Not an __init, needed by the reboot code
  1524. */
  1525. void disable_IO_APIC(void)
  1526. {
  1527. /*
  1528. * Clear the IO-APIC before rebooting:
  1529. */
  1530. clear_IO_APIC();
  1531. /*
  1532. * If the i8259 is routed through an IOAPIC
  1533. * Put that IOAPIC in virtual wire mode
  1534. * so legacy interrupts can be delivered.
  1535. */
  1536. if (ioapic_i8259.pin != -1) {
  1537. struct IO_APIC_route_entry entry;
  1538. memset(&entry, 0, sizeof(entry));
  1539. entry.mask = 0; /* Enabled */
  1540. entry.trigger = 0; /* Edge */
  1541. entry.irr = 0;
  1542. entry.polarity = 0; /* High */
  1543. entry.delivery_status = 0;
  1544. entry.dest_mode = 0; /* Physical */
  1545. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1546. entry.vector = 0;
  1547. entry.dest = read_apic_id();
  1548. /*
  1549. * Add it to the IO-APIC irq-routing table:
  1550. */
  1551. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1552. }
  1553. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1554. }
  1555. #ifdef CONFIG_X86_32
  1556. /*
  1557. * function to set the IO-APIC physical IDs based on the
  1558. * values stored in the MPC table.
  1559. *
  1560. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1561. */
  1562. static void __init setup_ioapic_ids_from_mpc(void)
  1563. {
  1564. union IO_APIC_reg_00 reg_00;
  1565. physid_mask_t phys_id_present_map;
  1566. int apic;
  1567. int i;
  1568. unsigned char old_id;
  1569. unsigned long flags;
  1570. if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
  1571. return;
  1572. /*
  1573. * Don't check I/O APIC IDs for xAPIC systems. They have
  1574. * no meaning without the serial APIC bus.
  1575. */
  1576. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1577. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1578. return;
  1579. /*
  1580. * This is broken; anything with a real cpu count has to
  1581. * circumvent this idiocy regardless.
  1582. */
  1583. phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
  1584. /*
  1585. * Set the IOAPIC ID to the value stored in the MPC table.
  1586. */
  1587. for (apic = 0; apic < nr_ioapics; apic++) {
  1588. /* Read the register 0 value */
  1589. spin_lock_irqsave(&ioapic_lock, flags);
  1590. reg_00.raw = io_apic_read(apic, 0);
  1591. spin_unlock_irqrestore(&ioapic_lock, flags);
  1592. old_id = mp_ioapics[apic].mp_apicid;
  1593. if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
  1594. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1595. apic, mp_ioapics[apic].mp_apicid);
  1596. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1597. reg_00.bits.ID);
  1598. mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
  1599. }
  1600. /*
  1601. * Sanity check, is the ID really free? Every APIC in a
  1602. * system must have a unique ID or we get lots of nice
  1603. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1604. */
  1605. if (check_apicid_used(phys_id_present_map,
  1606. mp_ioapics[apic].mp_apicid)) {
  1607. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1608. apic, mp_ioapics[apic].mp_apicid);
  1609. for (i = 0; i < get_physical_broadcast(); i++)
  1610. if (!physid_isset(i, phys_id_present_map))
  1611. break;
  1612. if (i >= get_physical_broadcast())
  1613. panic("Max APIC ID exceeded!\n");
  1614. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1615. i);
  1616. physid_set(i, phys_id_present_map);
  1617. mp_ioapics[apic].mp_apicid = i;
  1618. } else {
  1619. physid_mask_t tmp;
  1620. tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
  1621. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1622. "phys_id_present_map\n",
  1623. mp_ioapics[apic].mp_apicid);
  1624. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1625. }
  1626. /*
  1627. * We need to adjust the IRQ routing table
  1628. * if the ID changed.
  1629. */
  1630. if (old_id != mp_ioapics[apic].mp_apicid)
  1631. for (i = 0; i < mp_irq_entries; i++)
  1632. if (mp_irqs[i].mp_dstapic == old_id)
  1633. mp_irqs[i].mp_dstapic
  1634. = mp_ioapics[apic].mp_apicid;
  1635. /*
  1636. * Read the right value from the MPC table and
  1637. * write it into the ID register.
  1638. */
  1639. apic_printk(APIC_VERBOSE, KERN_INFO
  1640. "...changing IO-APIC physical APIC ID to %d ...",
  1641. mp_ioapics[apic].mp_apicid);
  1642. reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
  1643. spin_lock_irqsave(&ioapic_lock, flags);
  1644. io_apic_write(apic, 0, reg_00.raw);
  1645. spin_unlock_irqrestore(&ioapic_lock, flags);
  1646. /*
  1647. * Sanity check
  1648. */
  1649. spin_lock_irqsave(&ioapic_lock, flags);
  1650. reg_00.raw = io_apic_read(apic, 0);
  1651. spin_unlock_irqrestore(&ioapic_lock, flags);
  1652. if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
  1653. printk("could not set ID!\n");
  1654. else
  1655. apic_printk(APIC_VERBOSE, " ok.\n");
  1656. }
  1657. }
  1658. #endif
  1659. int no_timer_check __initdata;
  1660. static int __init notimercheck(char *s)
  1661. {
  1662. no_timer_check = 1;
  1663. return 1;
  1664. }
  1665. __setup("no_timer_check", notimercheck);
  1666. /*
  1667. * There is a nasty bug in some older SMP boards, their mptable lies
  1668. * about the timer IRQ. We do the following to work around the situation:
  1669. *
  1670. * - timer IRQ defaults to IO-APIC IRQ
  1671. * - if this function detects that timer IRQs are defunct, then we fall
  1672. * back to ISA timer IRQs
  1673. */
  1674. static int __init timer_irq_works(void)
  1675. {
  1676. unsigned long t1 = jiffies;
  1677. unsigned long flags;
  1678. if (no_timer_check)
  1679. return 1;
  1680. local_save_flags(flags);
  1681. local_irq_enable();
  1682. /* Let ten ticks pass... */
  1683. mdelay((10 * 1000) / HZ);
  1684. local_irq_restore(flags);
  1685. /*
  1686. * Expect a few ticks at least, to be sure some possible
  1687. * glue logic does not lock up after one or two first
  1688. * ticks in a non-ExtINT mode. Also the local APIC
  1689. * might have cached one ExtINT interrupt. Finally, at
  1690. * least one tick may be lost due to delays.
  1691. */
  1692. /* jiffies wrap? */
  1693. if (time_after(jiffies, t1 + 4))
  1694. return 1;
  1695. return 0;
  1696. }
  1697. /*
  1698. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1699. * number of pending IRQ events unhandled. These cases are very rare,
  1700. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1701. * better to do it this way as thus we do not have to be aware of
  1702. * 'pending' interrupts in the IRQ path, except at this point.
  1703. */
  1704. /*
  1705. * Edge triggered needs to resend any interrupt
  1706. * that was delayed but this is now handled in the device
  1707. * independent code.
  1708. */
  1709. /*
  1710. * Starting up a edge-triggered IO-APIC interrupt is
  1711. * nasty - we need to make sure that we get the edge.
  1712. * If it is already asserted for some reason, we need
  1713. * return 1 to indicate that is was pending.
  1714. *
  1715. * This is not complete - we should be able to fake
  1716. * an edge even if it isn't on the 8259A...
  1717. */
  1718. static unsigned int startup_ioapic_irq(unsigned int irq)
  1719. {
  1720. int was_pending = 0;
  1721. unsigned long flags;
  1722. spin_lock_irqsave(&ioapic_lock, flags);
  1723. if (irq < 16) {
  1724. disable_8259A_irq(irq);
  1725. if (i8259A_irq_pending(irq))
  1726. was_pending = 1;
  1727. }
  1728. __unmask_IO_APIC_irq(irq);
  1729. spin_unlock_irqrestore(&ioapic_lock, flags);
  1730. return was_pending;
  1731. }
  1732. #ifdef CONFIG_X86_64
  1733. static int ioapic_retrigger_irq(unsigned int irq)
  1734. {
  1735. struct irq_cfg *cfg = irq_cfg(irq);
  1736. unsigned long flags;
  1737. spin_lock_irqsave(&vector_lock, flags);
  1738. send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
  1739. spin_unlock_irqrestore(&vector_lock, flags);
  1740. return 1;
  1741. }
  1742. #else
  1743. static int ioapic_retrigger_irq(unsigned int irq)
  1744. {
  1745. send_IPI_self(irq_cfg(irq)->vector);
  1746. return 1;
  1747. }
  1748. #endif
  1749. /*
  1750. * Level and edge triggered IO-APIC interrupts need different handling,
  1751. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1752. * handled with the level-triggered descriptor, but that one has slightly
  1753. * more overhead. Level-triggered interrupts cannot be handled with the
  1754. * edge-triggered handler, without risking IRQ storms and other ugly
  1755. * races.
  1756. */
  1757. #ifdef CONFIG_SMP
  1758. #ifdef CONFIG_INTR_REMAP
  1759. static void ir_irq_migration(struct work_struct *work);
  1760. static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
  1761. /*
  1762. * Migrate the IO-APIC irq in the presence of intr-remapping.
  1763. *
  1764. * For edge triggered, irq migration is a simple atomic update(of vector
  1765. * and cpu destination) of IRTE and flush the hardware cache.
  1766. *
  1767. * For level triggered, we need to modify the io-apic RTE aswell with the update
  1768. * vector information, along with modifying IRTE with vector and destination.
  1769. * So irq migration for level triggered is little bit more complex compared to
  1770. * edge triggered migration. But the good news is, we use the same algorithm
  1771. * for level triggered migration as we have today, only difference being,
  1772. * we now initiate the irq migration from process context instead of the
  1773. * interrupt context.
  1774. *
  1775. * In future, when we do a directed EOI (combined with cpu EOI broadcast
  1776. * suppression) to the IO-APIC, level triggered irq migration will also be
  1777. * as simple as edge triggered migration and we can do the irq migration
  1778. * with a simple atomic update to IO-APIC RTE.
  1779. */
  1780. static void migrate_ioapic_irq(int irq, cpumask_t mask)
  1781. {
  1782. struct irq_cfg *cfg;
  1783. struct irq_desc *desc;
  1784. cpumask_t tmp, cleanup_mask;
  1785. struct irte irte;
  1786. int modify_ioapic_rte;
  1787. unsigned int dest;
  1788. unsigned long flags;
  1789. cpus_and(tmp, mask, cpu_online_map);
  1790. if (cpus_empty(tmp))
  1791. return;
  1792. if (get_irte(irq, &irte))
  1793. return;
  1794. if (assign_irq_vector(irq, mask))
  1795. return;
  1796. cfg = irq_cfg(irq);
  1797. cpus_and(tmp, cfg->domain, mask);
  1798. dest = cpu_mask_to_apicid(tmp);
  1799. desc = irq_to_desc(irq);
  1800. modify_ioapic_rte = desc->status & IRQ_LEVEL;
  1801. if (modify_ioapic_rte) {
  1802. spin_lock_irqsave(&ioapic_lock, flags);
  1803. __target_IO_APIC_irq(irq, dest, cfg->vector);
  1804. spin_unlock_irqrestore(&ioapic_lock, flags);
  1805. }
  1806. irte.vector = cfg->vector;
  1807. irte.dest_id = IRTE_DEST(dest);
  1808. /*
  1809. * Modified the IRTE and flushes the Interrupt entry cache.
  1810. */
  1811. modify_irte(irq, &irte);
  1812. if (cfg->move_in_progress) {
  1813. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1814. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1815. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1816. cfg->move_in_progress = 0;
  1817. }
  1818. desc->affinity = mask;
  1819. }
  1820. static int migrate_irq_remapped_level(int irq)
  1821. {
  1822. int ret = -1;
  1823. struct irq_desc *desc = irq_to_desc(irq);
  1824. mask_IO_APIC_irq(irq);
  1825. if (io_apic_level_ack_pending(irq)) {
  1826. /*
  1827. * Interrupt in progress. Migrating irq now will change the
  1828. * vector information in the IO-APIC RTE and that will confuse
  1829. * the EOI broadcast performed by cpu.
  1830. * So, delay the irq migration to the next instance.
  1831. */
  1832. schedule_delayed_work(&ir_migration_work, 1);
  1833. goto unmask;
  1834. }
  1835. /* everthing is clear. we have right of way */
  1836. migrate_ioapic_irq(irq, desc->pending_mask);
  1837. ret = 0;
  1838. desc->status &= ~IRQ_MOVE_PENDING;
  1839. cpus_clear(desc->pending_mask);
  1840. unmask:
  1841. unmask_IO_APIC_irq(irq);
  1842. return ret;
  1843. }
  1844. static void ir_irq_migration(struct work_struct *work)
  1845. {
  1846. unsigned int irq;
  1847. struct irq_desc *desc;
  1848. for_each_irq_desc(irq, desc) {
  1849. if (desc->status & IRQ_MOVE_PENDING) {
  1850. unsigned long flags;
  1851. spin_lock_irqsave(&desc->lock, flags);
  1852. if (!desc->chip->set_affinity ||
  1853. !(desc->status & IRQ_MOVE_PENDING)) {
  1854. desc->status &= ~IRQ_MOVE_PENDING;
  1855. spin_unlock_irqrestore(&desc->lock, flags);
  1856. continue;
  1857. }
  1858. desc->chip->set_affinity(irq, desc->pending_mask);
  1859. spin_unlock_irqrestore(&desc->lock, flags);
  1860. }
  1861. }
  1862. }
  1863. /*
  1864. * Migrates the IRQ destination in the process context.
  1865. */
  1866. static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  1867. {
  1868. struct irq_desc *desc = irq_to_desc(irq);
  1869. if (desc->status & IRQ_LEVEL) {
  1870. desc->status |= IRQ_MOVE_PENDING;
  1871. desc->pending_mask = mask;
  1872. migrate_irq_remapped_level(irq);
  1873. return;
  1874. }
  1875. migrate_ioapic_irq(irq, mask);
  1876. }
  1877. #endif
  1878. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1879. {
  1880. unsigned vector, me;
  1881. ack_APIC_irq();
  1882. #ifdef CONFIG_X86_64
  1883. exit_idle();
  1884. #endif
  1885. irq_enter();
  1886. me = smp_processor_id();
  1887. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1888. unsigned int irq;
  1889. struct irq_desc *desc;
  1890. struct irq_cfg *cfg;
  1891. irq = __get_cpu_var(vector_irq)[vector];
  1892. desc = irq_to_desc(irq);
  1893. if (!desc)
  1894. continue;
  1895. cfg = irq_cfg(irq);
  1896. spin_lock(&desc->lock);
  1897. if (!cfg->move_cleanup_count)
  1898. goto unlock;
  1899. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  1900. goto unlock;
  1901. __get_cpu_var(vector_irq)[vector] = -1;
  1902. cfg->move_cleanup_count--;
  1903. unlock:
  1904. spin_unlock(&desc->lock);
  1905. }
  1906. irq_exit();
  1907. }
  1908. static void irq_complete_move(unsigned int irq)
  1909. {
  1910. struct irq_cfg *cfg = irq_cfg(irq);
  1911. unsigned vector, me;
  1912. if (likely(!cfg->move_in_progress))
  1913. return;
  1914. vector = ~get_irq_regs()->orig_ax;
  1915. me = smp_processor_id();
  1916. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  1917. cpumask_t cleanup_mask;
  1918. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1919. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1920. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1921. cfg->move_in_progress = 0;
  1922. }
  1923. }
  1924. #else
  1925. static inline void irq_complete_move(unsigned int irq) {}
  1926. #endif
  1927. #ifdef CONFIG_INTR_REMAP
  1928. static void ack_x2apic_level(unsigned int irq)
  1929. {
  1930. ack_x2APIC_irq();
  1931. }
  1932. static void ack_x2apic_edge(unsigned int irq)
  1933. {
  1934. ack_x2APIC_irq();
  1935. }
  1936. #endif
  1937. static void ack_apic_edge(unsigned int irq)
  1938. {
  1939. irq_complete_move(irq);
  1940. move_native_irq(irq);
  1941. ack_APIC_irq();
  1942. }
  1943. atomic_t irq_mis_count;
  1944. static void ack_apic_level(unsigned int irq)
  1945. {
  1946. #ifdef CONFIG_X86_32
  1947. unsigned long v;
  1948. int i;
  1949. #endif
  1950. int do_unmask_irq = 0;
  1951. irq_complete_move(irq);
  1952. #ifdef CONFIG_GENERIC_PENDING_IRQ
  1953. /* If we are moving the irq we need to mask it */
  1954. if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
  1955. do_unmask_irq = 1;
  1956. mask_IO_APIC_irq(irq);
  1957. }
  1958. #endif
  1959. #ifdef CONFIG_X86_32
  1960. /*
  1961. * It appears there is an erratum which affects at least version 0x11
  1962. * of I/O APIC (that's the 82093AA and cores integrated into various
  1963. * chipsets). Under certain conditions a level-triggered interrupt is
  1964. * erroneously delivered as edge-triggered one but the respective IRR
  1965. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  1966. * message but it will never arrive and further interrupts are blocked
  1967. * from the source. The exact reason is so far unknown, but the
  1968. * phenomenon was observed when two consecutive interrupt requests
  1969. * from a given source get delivered to the same CPU and the source is
  1970. * temporarily disabled in between.
  1971. *
  1972. * A workaround is to simulate an EOI message manually. We achieve it
  1973. * by setting the trigger mode to edge and then to level when the edge
  1974. * trigger mode gets detected in the TMR of a local APIC for a
  1975. * level-triggered interrupt. We mask the source for the time of the
  1976. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  1977. * The idea is from Manfred Spraul. --macro
  1978. */
  1979. i = irq_cfg(irq)->vector;
  1980. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  1981. #endif
  1982. /*
  1983. * We must acknowledge the irq before we move it or the acknowledge will
  1984. * not propagate properly.
  1985. */
  1986. ack_APIC_irq();
  1987. /* Now we can move and renable the irq */
  1988. if (unlikely(do_unmask_irq)) {
  1989. /* Only migrate the irq if the ack has been received.
  1990. *
  1991. * On rare occasions the broadcast level triggered ack gets
  1992. * delayed going to ioapics, and if we reprogram the
  1993. * vector while Remote IRR is still set the irq will never
  1994. * fire again.
  1995. *
  1996. * To prevent this scenario we read the Remote IRR bit
  1997. * of the ioapic. This has two effects.
  1998. * - On any sane system the read of the ioapic will
  1999. * flush writes (and acks) going to the ioapic from
  2000. * this cpu.
  2001. * - We get to see if the ACK has actually been delivered.
  2002. *
  2003. * Based on failed experiments of reprogramming the
  2004. * ioapic entry from outside of irq context starting
  2005. * with masking the ioapic entry and then polling until
  2006. * Remote IRR was clear before reprogramming the
  2007. * ioapic I don't trust the Remote IRR bit to be
  2008. * completey accurate.
  2009. *
  2010. * However there appears to be no other way to plug
  2011. * this race, so if the Remote IRR bit is not
  2012. * accurate and is causing problems then it is a hardware bug
  2013. * and you can go talk to the chipset vendor about it.
  2014. */
  2015. if (!io_apic_level_ack_pending(irq))
  2016. move_masked_irq(irq);
  2017. unmask_IO_APIC_irq(irq);
  2018. }
  2019. #ifdef CONFIG_X86_32
  2020. if (!(v & (1 << (i & 0x1f)))) {
  2021. atomic_inc(&irq_mis_count);
  2022. spin_lock(&ioapic_lock);
  2023. __mask_and_edge_IO_APIC_irq(irq);
  2024. __unmask_and_level_IO_APIC_irq(irq);
  2025. spin_unlock(&ioapic_lock);
  2026. }
  2027. #endif
  2028. }
  2029. static struct irq_chip ioapic_chip __read_mostly = {
  2030. .name = "IO-APIC",
  2031. .startup = startup_ioapic_irq,
  2032. .mask = mask_IO_APIC_irq,
  2033. .unmask = unmask_IO_APIC_irq,
  2034. .ack = ack_apic_edge,
  2035. .eoi = ack_apic_level,
  2036. #ifdef CONFIG_SMP
  2037. .set_affinity = set_ioapic_affinity_irq,
  2038. #endif
  2039. .retrigger = ioapic_retrigger_irq,
  2040. };
  2041. #ifdef CONFIG_INTR_REMAP
  2042. static struct irq_chip ir_ioapic_chip __read_mostly = {
  2043. .name = "IR-IO-APIC",
  2044. .startup = startup_ioapic_irq,
  2045. .mask = mask_IO_APIC_irq,
  2046. .unmask = unmask_IO_APIC_irq,
  2047. .ack = ack_x2apic_edge,
  2048. .eoi = ack_x2apic_level,
  2049. #ifdef CONFIG_SMP
  2050. .set_affinity = set_ir_ioapic_affinity_irq,
  2051. #endif
  2052. .retrigger = ioapic_retrigger_irq,
  2053. };
  2054. #endif
  2055. static inline void init_IO_APIC_traps(void)
  2056. {
  2057. int irq;
  2058. struct irq_desc *desc;
  2059. struct irq_cfg *cfg;
  2060. /*
  2061. * NOTE! The local APIC isn't very good at handling
  2062. * multiple interrupts at the same interrupt level.
  2063. * As the interrupt level is determined by taking the
  2064. * vector number and shifting that right by 4, we
  2065. * want to spread these out a bit so that they don't
  2066. * all fall in the same interrupt level.
  2067. *
  2068. * Also, we've got to be careful not to trash gate
  2069. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  2070. */
  2071. for_each_irq_cfg(irq, cfg) {
  2072. if (IO_APIC_IRQ(irq) && !cfg->vector) {
  2073. /*
  2074. * Hmm.. We don't have an entry for this,
  2075. * so default to an old-fashioned 8259
  2076. * interrupt if we can..
  2077. */
  2078. if (irq < 16)
  2079. make_8259A_irq(irq);
  2080. else {
  2081. desc = irq_to_desc(irq);
  2082. /* Strange. Oh, well.. */
  2083. desc->chip = &no_irq_chip;
  2084. }
  2085. }
  2086. }
  2087. }
  2088. /*
  2089. * The local APIC irq-chip implementation:
  2090. */
  2091. static void mask_lapic_irq(unsigned int irq)
  2092. {
  2093. unsigned long v;
  2094. v = apic_read(APIC_LVT0);
  2095. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  2096. }
  2097. static void unmask_lapic_irq(unsigned int irq)
  2098. {
  2099. unsigned long v;
  2100. v = apic_read(APIC_LVT0);
  2101. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  2102. }
  2103. static void ack_lapic_irq (unsigned int irq)
  2104. {
  2105. ack_APIC_irq();
  2106. }
  2107. static struct irq_chip lapic_chip __read_mostly = {
  2108. .name = "local-APIC",
  2109. .mask = mask_lapic_irq,
  2110. .unmask = unmask_lapic_irq,
  2111. .ack = ack_lapic_irq,
  2112. };
  2113. static void lapic_register_intr(int irq)
  2114. {
  2115. struct irq_desc *desc;
  2116. desc = irq_to_desc(irq);
  2117. desc->status &= ~IRQ_LEVEL;
  2118. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  2119. "edge");
  2120. }
  2121. static void __init setup_nmi(void)
  2122. {
  2123. /*
  2124. * Dirty trick to enable the NMI watchdog ...
  2125. * We put the 8259A master into AEOI mode and
  2126. * unmask on all local APICs LVT0 as NMI.
  2127. *
  2128. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  2129. * is from Maciej W. Rozycki - so we do not have to EOI from
  2130. * the NMI handler or the timer interrupt.
  2131. */
  2132. apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
  2133. enable_NMI_through_LVT0();
  2134. apic_printk(APIC_VERBOSE, " done.\n");
  2135. }
  2136. /*
  2137. * This looks a bit hackish but it's about the only one way of sending
  2138. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  2139. * not support the ExtINT mode, unfortunately. We need to send these
  2140. * cycles as some i82489DX-based boards have glue logic that keeps the
  2141. * 8259A interrupt line asserted until INTA. --macro
  2142. */
  2143. static inline void __init unlock_ExtINT_logic(void)
  2144. {
  2145. int apic, pin, i;
  2146. struct IO_APIC_route_entry entry0, entry1;
  2147. unsigned char save_control, save_freq_select;
  2148. pin = find_isa_irq_pin(8, mp_INT);
  2149. if (pin == -1) {
  2150. WARN_ON_ONCE(1);
  2151. return;
  2152. }
  2153. apic = find_isa_irq_apic(8, mp_INT);
  2154. if (apic == -1) {
  2155. WARN_ON_ONCE(1);
  2156. return;
  2157. }
  2158. entry0 = ioapic_read_entry(apic, pin);
  2159. clear_IO_APIC_pin(apic, pin);
  2160. memset(&entry1, 0, sizeof(entry1));
  2161. entry1.dest_mode = 0; /* physical delivery */
  2162. entry1.mask = 0; /* unmask IRQ now */
  2163. entry1.dest = hard_smp_processor_id();
  2164. entry1.delivery_mode = dest_ExtINT;
  2165. entry1.polarity = entry0.polarity;
  2166. entry1.trigger = 0;
  2167. entry1.vector = 0;
  2168. ioapic_write_entry(apic, pin, entry1);
  2169. save_control = CMOS_READ(RTC_CONTROL);
  2170. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  2171. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  2172. RTC_FREQ_SELECT);
  2173. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  2174. i = 100;
  2175. while (i-- > 0) {
  2176. mdelay(10);
  2177. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  2178. i -= 10;
  2179. }
  2180. CMOS_WRITE(save_control, RTC_CONTROL);
  2181. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  2182. clear_IO_APIC_pin(apic, pin);
  2183. ioapic_write_entry(apic, pin, entry0);
  2184. }
  2185. static int disable_timer_pin_1 __initdata;
  2186. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  2187. static int __init disable_timer_pin_setup(char *arg)
  2188. {
  2189. disable_timer_pin_1 = 1;
  2190. return 0;
  2191. }
  2192. early_param("disable_timer_pin_1", disable_timer_pin_setup);
  2193. int timer_through_8259 __initdata;
  2194. /*
  2195. * This code may look a bit paranoid, but it's supposed to cooperate with
  2196. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  2197. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  2198. * fanatically on his truly buggy board.
  2199. *
  2200. * FIXME: really need to revamp this for all platforms.
  2201. */
  2202. static inline void __init check_timer(void)
  2203. {
  2204. struct irq_cfg *cfg = irq_cfg(0);
  2205. int apic1, pin1, apic2, pin2;
  2206. unsigned long flags;
  2207. unsigned int ver;
  2208. int no_pin1 = 0;
  2209. local_irq_save(flags);
  2210. ver = apic_read(APIC_LVR);
  2211. ver = GET_APIC_VERSION(ver);
  2212. /*
  2213. * get/set the timer IRQ vector:
  2214. */
  2215. disable_8259A_irq(0);
  2216. assign_irq_vector(0, TARGET_CPUS);
  2217. /*
  2218. * As IRQ0 is to be enabled in the 8259A, the virtual
  2219. * wire has to be disabled in the local APIC. Also
  2220. * timer interrupts need to be acknowledged manually in
  2221. * the 8259A for the i82489DX when using the NMI
  2222. * watchdog as that APIC treats NMIs as level-triggered.
  2223. * The AEOI mode will finish them in the 8259A
  2224. * automatically.
  2225. */
  2226. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  2227. init_8259A(1);
  2228. #ifdef CONFIG_X86_32
  2229. timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
  2230. #endif
  2231. pin1 = find_isa_irq_pin(0, mp_INT);
  2232. apic1 = find_isa_irq_apic(0, mp_INT);
  2233. pin2 = ioapic_i8259.pin;
  2234. apic2 = ioapic_i8259.apic;
  2235. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  2236. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  2237. cfg->vector, apic1, pin1, apic2, pin2);
  2238. /*
  2239. * Some BIOS writers are clueless and report the ExtINTA
  2240. * I/O APIC input from the cascaded 8259A as the timer
  2241. * interrupt input. So just in case, if only one pin
  2242. * was found above, try it both directly and through the
  2243. * 8259A.
  2244. */
  2245. if (pin1 == -1) {
  2246. #ifdef CONFIG_INTR_REMAP
  2247. if (intr_remapping_enabled)
  2248. panic("BIOS bug: timer not connected to IO-APIC");
  2249. #endif
  2250. pin1 = pin2;
  2251. apic1 = apic2;
  2252. no_pin1 = 1;
  2253. } else if (pin2 == -1) {
  2254. pin2 = pin1;
  2255. apic2 = apic1;
  2256. }
  2257. if (pin1 != -1) {
  2258. /*
  2259. * Ok, does IRQ0 through the IOAPIC work?
  2260. */
  2261. if (no_pin1) {
  2262. add_pin_to_irq(0, apic1, pin1);
  2263. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  2264. }
  2265. unmask_IO_APIC_irq(0);
  2266. if (timer_irq_works()) {
  2267. if (nmi_watchdog == NMI_IO_APIC) {
  2268. setup_nmi();
  2269. enable_8259A_irq(0);
  2270. }
  2271. if (disable_timer_pin_1 > 0)
  2272. clear_IO_APIC_pin(0, pin1);
  2273. goto out;
  2274. }
  2275. #ifdef CONFIG_INTR_REMAP
  2276. if (intr_remapping_enabled)
  2277. panic("timer doesn't work through Interrupt-remapped IO-APIC");
  2278. #endif
  2279. clear_IO_APIC_pin(apic1, pin1);
  2280. if (!no_pin1)
  2281. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  2282. "8254 timer not connected to IO-APIC\n");
  2283. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  2284. "(IRQ0) through the 8259A ...\n");
  2285. apic_printk(APIC_QUIET, KERN_INFO
  2286. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  2287. /*
  2288. * legacy devices should be connected to IO APIC #0
  2289. */
  2290. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  2291. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  2292. unmask_IO_APIC_irq(0);
  2293. enable_8259A_irq(0);
  2294. if (timer_irq_works()) {
  2295. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  2296. timer_through_8259 = 1;
  2297. if (nmi_watchdog == NMI_IO_APIC) {
  2298. disable_8259A_irq(0);
  2299. setup_nmi();
  2300. enable_8259A_irq(0);
  2301. }
  2302. goto out;
  2303. }
  2304. /*
  2305. * Cleanup, just in case ...
  2306. */
  2307. disable_8259A_irq(0);
  2308. clear_IO_APIC_pin(apic2, pin2);
  2309. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  2310. }
  2311. if (nmi_watchdog == NMI_IO_APIC) {
  2312. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  2313. "through the IO-APIC - disabling NMI Watchdog!\n");
  2314. nmi_watchdog = NMI_NONE;
  2315. }
  2316. #ifdef CONFIG_X86_32
  2317. timer_ack = 0;
  2318. #endif
  2319. apic_printk(APIC_QUIET, KERN_INFO
  2320. "...trying to set up timer as Virtual Wire IRQ...\n");
  2321. lapic_register_intr(0);
  2322. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  2323. enable_8259A_irq(0);
  2324. if (timer_irq_works()) {
  2325. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2326. goto out;
  2327. }
  2328. disable_8259A_irq(0);
  2329. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  2330. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  2331. apic_printk(APIC_QUIET, KERN_INFO
  2332. "...trying to set up timer as ExtINT IRQ...\n");
  2333. init_8259A(0);
  2334. make_8259A_irq(0);
  2335. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  2336. unlock_ExtINT_logic();
  2337. if (timer_irq_works()) {
  2338. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2339. goto out;
  2340. }
  2341. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  2342. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  2343. "report. Then try booting with the 'noapic' option.\n");
  2344. out:
  2345. local_irq_restore(flags);
  2346. }
  2347. /*
  2348. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  2349. * to devices. However there may be an I/O APIC pin available for
  2350. * this interrupt regardless. The pin may be left unconnected, but
  2351. * typically it will be reused as an ExtINT cascade interrupt for
  2352. * the master 8259A. In the MPS case such a pin will normally be
  2353. * reported as an ExtINT interrupt in the MP table. With ACPI
  2354. * there is no provision for ExtINT interrupts, and in the absence
  2355. * of an override it would be treated as an ordinary ISA I/O APIC
  2356. * interrupt, that is edge-triggered and unmasked by default. We
  2357. * used to do this, but it caused problems on some systems because
  2358. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  2359. * the same ExtINT cascade interrupt to drive the local APIC of the
  2360. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  2361. * the I/O APIC in all cases now. No actual device should request
  2362. * it anyway. --macro
  2363. */
  2364. #define PIC_IRQS (1 << PIC_CASCADE_IR)
  2365. void __init setup_IO_APIC(void)
  2366. {
  2367. #ifdef CONFIG_X86_32
  2368. enable_IO_APIC();
  2369. #else
  2370. /*
  2371. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  2372. */
  2373. #endif
  2374. io_apic_irqs = ~PIC_IRQS;
  2375. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  2376. /*
  2377. * Set up IO-APIC IRQ routing.
  2378. */
  2379. #ifdef CONFIG_X86_32
  2380. if (!acpi_ioapic)
  2381. setup_ioapic_ids_from_mpc();
  2382. #endif
  2383. sync_Arb_IDs();
  2384. setup_IO_APIC_irqs();
  2385. init_IO_APIC_traps();
  2386. check_timer();
  2387. }
  2388. /*
  2389. * Called after all the initialization is done. If we didnt find any
  2390. * APIC bugs then we can allow the modify fast path
  2391. */
  2392. static int __init io_apic_bug_finalize(void)
  2393. {
  2394. if (sis_apic_bug == -1)
  2395. sis_apic_bug = 0;
  2396. return 0;
  2397. }
  2398. late_initcall(io_apic_bug_finalize);
  2399. struct sysfs_ioapic_data {
  2400. struct sys_device dev;
  2401. struct IO_APIC_route_entry entry[0];
  2402. };
  2403. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  2404. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  2405. {
  2406. struct IO_APIC_route_entry *entry;
  2407. struct sysfs_ioapic_data *data;
  2408. int i;
  2409. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2410. entry = data->entry;
  2411. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  2412. *entry = ioapic_read_entry(dev->id, i);
  2413. return 0;
  2414. }
  2415. static int ioapic_resume(struct sys_device *dev)
  2416. {
  2417. struct IO_APIC_route_entry *entry;
  2418. struct sysfs_ioapic_data *data;
  2419. unsigned long flags;
  2420. union IO_APIC_reg_00 reg_00;
  2421. int i;
  2422. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2423. entry = data->entry;
  2424. spin_lock_irqsave(&ioapic_lock, flags);
  2425. reg_00.raw = io_apic_read(dev->id, 0);
  2426. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  2427. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  2428. io_apic_write(dev->id, 0, reg_00.raw);
  2429. }
  2430. spin_unlock_irqrestore(&ioapic_lock, flags);
  2431. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2432. ioapic_write_entry(dev->id, i, entry[i]);
  2433. return 0;
  2434. }
  2435. static struct sysdev_class ioapic_sysdev_class = {
  2436. .name = "ioapic",
  2437. .suspend = ioapic_suspend,
  2438. .resume = ioapic_resume,
  2439. };
  2440. static int __init ioapic_init_sysfs(void)
  2441. {
  2442. struct sys_device * dev;
  2443. int i, size, error;
  2444. error = sysdev_class_register(&ioapic_sysdev_class);
  2445. if (error)
  2446. return error;
  2447. for (i = 0; i < nr_ioapics; i++ ) {
  2448. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  2449. * sizeof(struct IO_APIC_route_entry);
  2450. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  2451. if (!mp_ioapic_data[i]) {
  2452. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2453. continue;
  2454. }
  2455. dev = &mp_ioapic_data[i]->dev;
  2456. dev->id = i;
  2457. dev->cls = &ioapic_sysdev_class;
  2458. error = sysdev_register(dev);
  2459. if (error) {
  2460. kfree(mp_ioapic_data[i]);
  2461. mp_ioapic_data[i] = NULL;
  2462. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2463. continue;
  2464. }
  2465. }
  2466. return 0;
  2467. }
  2468. device_initcall(ioapic_init_sysfs);
  2469. /*
  2470. * Dynamic irq allocate and deallocation
  2471. */
  2472. unsigned int create_irq_nr(unsigned int irq_want)
  2473. {
  2474. /* Allocate an unused irq */
  2475. unsigned int irq;
  2476. unsigned int new;
  2477. unsigned long flags;
  2478. struct irq_cfg *cfg_new;
  2479. irq_want = nr_irqs - 1;
  2480. irq = 0;
  2481. spin_lock_irqsave(&vector_lock, flags);
  2482. for (new = irq_want; new > 0; new--) {
  2483. if (platform_legacy_irq(new))
  2484. continue;
  2485. cfg_new = irq_cfg(new);
  2486. if (cfg_new && cfg_new->vector != 0)
  2487. continue;
  2488. /* check if need to create one */
  2489. if (!cfg_new)
  2490. cfg_new = irq_cfg_alloc(new);
  2491. if (__assign_irq_vector(new, TARGET_CPUS) == 0)
  2492. irq = new;
  2493. break;
  2494. }
  2495. spin_unlock_irqrestore(&vector_lock, flags);
  2496. if (irq > 0) {
  2497. dynamic_irq_init(irq);
  2498. }
  2499. return irq;
  2500. }
  2501. int create_irq(void)
  2502. {
  2503. int irq;
  2504. irq = create_irq_nr(nr_irqs - 1);
  2505. if (irq == 0)
  2506. irq = -1;
  2507. return irq;
  2508. }
  2509. void destroy_irq(unsigned int irq)
  2510. {
  2511. unsigned long flags;
  2512. dynamic_irq_cleanup(irq);
  2513. #ifdef CONFIG_INTR_REMAP
  2514. free_irte(irq);
  2515. #endif
  2516. spin_lock_irqsave(&vector_lock, flags);
  2517. __clear_irq_vector(irq);
  2518. spin_unlock_irqrestore(&vector_lock, flags);
  2519. }
  2520. /*
  2521. * MSI message composition
  2522. */
  2523. #ifdef CONFIG_PCI_MSI
  2524. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  2525. {
  2526. struct irq_cfg *cfg;
  2527. int err;
  2528. unsigned dest;
  2529. cpumask_t tmp;
  2530. tmp = TARGET_CPUS;
  2531. err = assign_irq_vector(irq, tmp);
  2532. if (err)
  2533. return err;
  2534. cfg = irq_cfg(irq);
  2535. cpus_and(tmp, cfg->domain, tmp);
  2536. dest = cpu_mask_to_apicid(tmp);
  2537. #ifdef CONFIG_INTR_REMAP
  2538. if (irq_remapped(irq)) {
  2539. struct irte irte;
  2540. int ir_index;
  2541. u16 sub_handle;
  2542. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  2543. BUG_ON(ir_index == -1);
  2544. memset (&irte, 0, sizeof(irte));
  2545. irte.present = 1;
  2546. irte.dst_mode = INT_DEST_MODE;
  2547. irte.trigger_mode = 0; /* edge */
  2548. irte.dlvry_mode = INT_DELIVERY_MODE;
  2549. irte.vector = cfg->vector;
  2550. irte.dest_id = IRTE_DEST(dest);
  2551. modify_irte(irq, &irte);
  2552. msg->address_hi = MSI_ADDR_BASE_HI;
  2553. msg->data = sub_handle;
  2554. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  2555. MSI_ADDR_IR_SHV |
  2556. MSI_ADDR_IR_INDEX1(ir_index) |
  2557. MSI_ADDR_IR_INDEX2(ir_index);
  2558. } else
  2559. #endif
  2560. {
  2561. msg->address_hi = MSI_ADDR_BASE_HI;
  2562. msg->address_lo =
  2563. MSI_ADDR_BASE_LO |
  2564. ((INT_DEST_MODE == 0) ?
  2565. MSI_ADDR_DEST_MODE_PHYSICAL:
  2566. MSI_ADDR_DEST_MODE_LOGICAL) |
  2567. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2568. MSI_ADDR_REDIRECTION_CPU:
  2569. MSI_ADDR_REDIRECTION_LOWPRI) |
  2570. MSI_ADDR_DEST_ID(dest);
  2571. msg->data =
  2572. MSI_DATA_TRIGGER_EDGE |
  2573. MSI_DATA_LEVEL_ASSERT |
  2574. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2575. MSI_DATA_DELIVERY_FIXED:
  2576. MSI_DATA_DELIVERY_LOWPRI) |
  2577. MSI_DATA_VECTOR(cfg->vector);
  2578. }
  2579. return err;
  2580. }
  2581. #ifdef CONFIG_SMP
  2582. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2583. {
  2584. struct irq_cfg *cfg;
  2585. struct msi_msg msg;
  2586. unsigned int dest;
  2587. cpumask_t tmp;
  2588. struct irq_desc *desc;
  2589. cpus_and(tmp, mask, cpu_online_map);
  2590. if (cpus_empty(tmp))
  2591. return;
  2592. if (assign_irq_vector(irq, mask))
  2593. return;
  2594. cfg = irq_cfg(irq);
  2595. cpus_and(tmp, cfg->domain, mask);
  2596. dest = cpu_mask_to_apicid(tmp);
  2597. read_msi_msg(irq, &msg);
  2598. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2599. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2600. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2601. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2602. write_msi_msg(irq, &msg);
  2603. desc = irq_to_desc(irq);
  2604. desc->affinity = mask;
  2605. }
  2606. #ifdef CONFIG_INTR_REMAP
  2607. /*
  2608. * Migrate the MSI irq to another cpumask. This migration is
  2609. * done in the process context using interrupt-remapping hardware.
  2610. */
  2611. static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2612. {
  2613. struct irq_cfg *cfg;
  2614. unsigned int dest;
  2615. cpumask_t tmp, cleanup_mask;
  2616. struct irte irte;
  2617. struct irq_desc *desc;
  2618. cpus_and(tmp, mask, cpu_online_map);
  2619. if (cpus_empty(tmp))
  2620. return;
  2621. if (get_irte(irq, &irte))
  2622. return;
  2623. if (assign_irq_vector(irq, mask))
  2624. return;
  2625. cfg = irq_cfg(irq);
  2626. cpus_and(tmp, cfg->domain, mask);
  2627. dest = cpu_mask_to_apicid(tmp);
  2628. irte.vector = cfg->vector;
  2629. irte.dest_id = IRTE_DEST(dest);
  2630. /*
  2631. * atomically update the IRTE with the new destination and vector.
  2632. */
  2633. modify_irte(irq, &irte);
  2634. /*
  2635. * After this point, all the interrupts will start arriving
  2636. * at the new destination. So, time to cleanup the previous
  2637. * vector allocation.
  2638. */
  2639. if (cfg->move_in_progress) {
  2640. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2641. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2642. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2643. cfg->move_in_progress = 0;
  2644. }
  2645. desc = irq_to_desc(irq);
  2646. desc->affinity = mask;
  2647. }
  2648. #endif
  2649. #endif /* CONFIG_SMP */
  2650. /*
  2651. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2652. * which implement the MSI or MSI-X Capability Structure.
  2653. */
  2654. static struct irq_chip msi_chip = {
  2655. .name = "PCI-MSI",
  2656. .unmask = unmask_msi_irq,
  2657. .mask = mask_msi_irq,
  2658. .ack = ack_apic_edge,
  2659. #ifdef CONFIG_SMP
  2660. .set_affinity = set_msi_irq_affinity,
  2661. #endif
  2662. .retrigger = ioapic_retrigger_irq,
  2663. };
  2664. #ifdef CONFIG_INTR_REMAP
  2665. static struct irq_chip msi_ir_chip = {
  2666. .name = "IR-PCI-MSI",
  2667. .unmask = unmask_msi_irq,
  2668. .mask = mask_msi_irq,
  2669. .ack = ack_x2apic_edge,
  2670. #ifdef CONFIG_SMP
  2671. .set_affinity = ir_set_msi_irq_affinity,
  2672. #endif
  2673. .retrigger = ioapic_retrigger_irq,
  2674. };
  2675. /*
  2676. * Map the PCI dev to the corresponding remapping hardware unit
  2677. * and allocate 'nvec' consecutive interrupt-remapping table entries
  2678. * in it.
  2679. */
  2680. static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
  2681. {
  2682. struct intel_iommu *iommu;
  2683. int index;
  2684. iommu = map_dev_to_ir(dev);
  2685. if (!iommu) {
  2686. printk(KERN_ERR
  2687. "Unable to map PCI %s to iommu\n", pci_name(dev));
  2688. return -ENOENT;
  2689. }
  2690. index = alloc_irte(iommu, irq, nvec);
  2691. if (index < 0) {
  2692. printk(KERN_ERR
  2693. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  2694. pci_name(dev));
  2695. return -ENOSPC;
  2696. }
  2697. return index;
  2698. }
  2699. #endif
  2700. static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
  2701. {
  2702. int ret;
  2703. struct msi_msg msg;
  2704. ret = msi_compose_msg(dev, irq, &msg);
  2705. if (ret < 0)
  2706. return ret;
  2707. set_irq_msi(irq, desc);
  2708. write_msi_msg(irq, &msg);
  2709. #ifdef CONFIG_INTR_REMAP
  2710. if (irq_remapped(irq)) {
  2711. struct irq_desc *desc = irq_to_desc(irq);
  2712. /*
  2713. * irq migration in process context
  2714. */
  2715. desc->status |= IRQ_MOVE_PCNTXT;
  2716. set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
  2717. } else
  2718. #endif
  2719. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  2720. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
  2721. return 0;
  2722. }
  2723. static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
  2724. {
  2725. unsigned int irq;
  2726. irq = dev->bus->number;
  2727. irq <<= 8;
  2728. irq |= dev->devfn;
  2729. irq <<= 12;
  2730. return irq;
  2731. }
  2732. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  2733. {
  2734. unsigned int irq;
  2735. int ret;
  2736. unsigned int irq_want;
  2737. irq_want = build_irq_for_pci_dev(dev) + 0x100;
  2738. irq = create_irq_nr(irq_want);
  2739. if (irq == 0)
  2740. return -1;
  2741. #ifdef CONFIG_INTR_REMAP
  2742. if (!intr_remapping_enabled)
  2743. goto no_ir;
  2744. ret = msi_alloc_irte(dev, irq, 1);
  2745. if (ret < 0)
  2746. goto error;
  2747. no_ir:
  2748. #endif
  2749. ret = setup_msi_irq(dev, desc, irq);
  2750. if (ret < 0) {
  2751. destroy_irq(irq);
  2752. return ret;
  2753. }
  2754. return 0;
  2755. #ifdef CONFIG_INTR_REMAP
  2756. error:
  2757. destroy_irq(irq);
  2758. return ret;
  2759. #endif
  2760. }
  2761. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  2762. {
  2763. unsigned int irq;
  2764. int ret, sub_handle;
  2765. struct msi_desc *desc;
  2766. unsigned int irq_want;
  2767. #ifdef CONFIG_INTR_REMAP
  2768. struct intel_iommu *iommu = 0;
  2769. int index = 0;
  2770. #endif
  2771. irq_want = build_irq_for_pci_dev(dev) + 0x100;
  2772. sub_handle = 0;
  2773. list_for_each_entry(desc, &dev->msi_list, list) {
  2774. irq = create_irq_nr(irq_want--);
  2775. if (irq == 0)
  2776. return -1;
  2777. #ifdef CONFIG_INTR_REMAP
  2778. if (!intr_remapping_enabled)
  2779. goto no_ir;
  2780. if (!sub_handle) {
  2781. /*
  2782. * allocate the consecutive block of IRTE's
  2783. * for 'nvec'
  2784. */
  2785. index = msi_alloc_irte(dev, irq, nvec);
  2786. if (index < 0) {
  2787. ret = index;
  2788. goto error;
  2789. }
  2790. } else {
  2791. iommu = map_dev_to_ir(dev);
  2792. if (!iommu) {
  2793. ret = -ENOENT;
  2794. goto error;
  2795. }
  2796. /*
  2797. * setup the mapping between the irq and the IRTE
  2798. * base index, the sub_handle pointing to the
  2799. * appropriate interrupt remap table entry.
  2800. */
  2801. set_irte_irq(irq, iommu, index, sub_handle);
  2802. }
  2803. no_ir:
  2804. #endif
  2805. ret = setup_msi_irq(dev, desc, irq);
  2806. if (ret < 0)
  2807. goto error;
  2808. sub_handle++;
  2809. }
  2810. return 0;
  2811. error:
  2812. destroy_irq(irq);
  2813. return ret;
  2814. }
  2815. void arch_teardown_msi_irq(unsigned int irq)
  2816. {
  2817. destroy_irq(irq);
  2818. }
  2819. #ifdef CONFIG_DMAR
  2820. #ifdef CONFIG_SMP
  2821. static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2822. {
  2823. struct irq_cfg *cfg;
  2824. struct msi_msg msg;
  2825. unsigned int dest;
  2826. cpumask_t tmp;
  2827. struct irq_desc *desc;
  2828. cpus_and(tmp, mask, cpu_online_map);
  2829. if (cpus_empty(tmp))
  2830. return;
  2831. if (assign_irq_vector(irq, mask))
  2832. return;
  2833. cfg = irq_cfg(irq);
  2834. cpus_and(tmp, cfg->domain, mask);
  2835. dest = cpu_mask_to_apicid(tmp);
  2836. dmar_msi_read(irq, &msg);
  2837. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2838. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2839. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2840. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2841. dmar_msi_write(irq, &msg);
  2842. desc = irq_to_desc(irq);
  2843. desc->affinity = mask;
  2844. }
  2845. #endif /* CONFIG_SMP */
  2846. struct irq_chip dmar_msi_type = {
  2847. .name = "DMAR_MSI",
  2848. .unmask = dmar_msi_unmask,
  2849. .mask = dmar_msi_mask,
  2850. .ack = ack_apic_edge,
  2851. #ifdef CONFIG_SMP
  2852. .set_affinity = dmar_msi_set_affinity,
  2853. #endif
  2854. .retrigger = ioapic_retrigger_irq,
  2855. };
  2856. int arch_setup_dmar_msi(unsigned int irq)
  2857. {
  2858. int ret;
  2859. struct msi_msg msg;
  2860. ret = msi_compose_msg(NULL, irq, &msg);
  2861. if (ret < 0)
  2862. return ret;
  2863. dmar_msi_write(irq, &msg);
  2864. set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  2865. "edge");
  2866. return 0;
  2867. }
  2868. #endif
  2869. #ifdef CONFIG_HPET_TIMER
  2870. #ifdef CONFIG_SMP
  2871. static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2872. {
  2873. struct irq_cfg *cfg;
  2874. struct irq_desc *desc;
  2875. struct msi_msg msg;
  2876. unsigned int dest;
  2877. cpumask_t tmp;
  2878. cpus_and(tmp, mask, cpu_online_map);
  2879. if (cpus_empty(tmp))
  2880. return;
  2881. if (assign_irq_vector(irq, mask))
  2882. return;
  2883. cfg = irq_cfg(irq);
  2884. cpus_and(tmp, cfg->domain, mask);
  2885. dest = cpu_mask_to_apicid(tmp);
  2886. hpet_msi_read(irq, &msg);
  2887. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2888. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2889. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2890. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2891. hpet_msi_write(irq, &msg);
  2892. desc = irq_to_desc(irq);
  2893. desc->affinity = mask;
  2894. }
  2895. #endif /* CONFIG_SMP */
  2896. struct irq_chip hpet_msi_type = {
  2897. .name = "HPET_MSI",
  2898. .unmask = hpet_msi_unmask,
  2899. .mask = hpet_msi_mask,
  2900. .ack = ack_apic_edge,
  2901. #ifdef CONFIG_SMP
  2902. .set_affinity = hpet_msi_set_affinity,
  2903. #endif
  2904. .retrigger = ioapic_retrigger_irq,
  2905. };
  2906. int arch_setup_hpet_msi(unsigned int irq)
  2907. {
  2908. int ret;
  2909. struct msi_msg msg;
  2910. ret = msi_compose_msg(NULL, irq, &msg);
  2911. if (ret < 0)
  2912. return ret;
  2913. hpet_msi_write(irq, &msg);
  2914. set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
  2915. "edge");
  2916. return 0;
  2917. }
  2918. #endif
  2919. #endif /* CONFIG_PCI_MSI */
  2920. /*
  2921. * Hypertransport interrupt support
  2922. */
  2923. #ifdef CONFIG_HT_IRQ
  2924. #ifdef CONFIG_SMP
  2925. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  2926. {
  2927. struct ht_irq_msg msg;
  2928. fetch_ht_irq_msg(irq, &msg);
  2929. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  2930. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  2931. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  2932. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  2933. write_ht_irq_msg(irq, &msg);
  2934. }
  2935. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  2936. {
  2937. struct irq_cfg *cfg;
  2938. unsigned int dest;
  2939. cpumask_t tmp;
  2940. struct irq_desc *desc;
  2941. cpus_and(tmp, mask, cpu_online_map);
  2942. if (cpus_empty(tmp))
  2943. return;
  2944. if (assign_irq_vector(irq, mask))
  2945. return;
  2946. cfg = irq_cfg(irq);
  2947. cpus_and(tmp, cfg->domain, mask);
  2948. dest = cpu_mask_to_apicid(tmp);
  2949. target_ht_irq(irq, dest, cfg->vector);
  2950. desc = irq_to_desc(irq);
  2951. desc->affinity = mask;
  2952. }
  2953. #endif
  2954. static struct irq_chip ht_irq_chip = {
  2955. .name = "PCI-HT",
  2956. .mask = mask_ht_irq,
  2957. .unmask = unmask_ht_irq,
  2958. .ack = ack_apic_edge,
  2959. #ifdef CONFIG_SMP
  2960. .set_affinity = set_ht_irq_affinity,
  2961. #endif
  2962. .retrigger = ioapic_retrigger_irq,
  2963. };
  2964. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  2965. {
  2966. struct irq_cfg *cfg;
  2967. int err;
  2968. cpumask_t tmp;
  2969. tmp = TARGET_CPUS;
  2970. err = assign_irq_vector(irq, tmp);
  2971. if (!err) {
  2972. struct ht_irq_msg msg;
  2973. unsigned dest;
  2974. cfg = irq_cfg(irq);
  2975. cpus_and(tmp, cfg->domain, tmp);
  2976. dest = cpu_mask_to_apicid(tmp);
  2977. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  2978. msg.address_lo =
  2979. HT_IRQ_LOW_BASE |
  2980. HT_IRQ_LOW_DEST_ID(dest) |
  2981. HT_IRQ_LOW_VECTOR(cfg->vector) |
  2982. ((INT_DEST_MODE == 0) ?
  2983. HT_IRQ_LOW_DM_PHYSICAL :
  2984. HT_IRQ_LOW_DM_LOGICAL) |
  2985. HT_IRQ_LOW_RQEOI_EDGE |
  2986. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2987. HT_IRQ_LOW_MT_FIXED :
  2988. HT_IRQ_LOW_MT_ARBITRATED) |
  2989. HT_IRQ_LOW_IRQ_MASKED;
  2990. write_ht_irq_msg(irq, &msg);
  2991. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  2992. handle_edge_irq, "edge");
  2993. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
  2994. }
  2995. return err;
  2996. }
  2997. #endif /* CONFIG_HT_IRQ */
  2998. #ifdef CONFIG_X86_64
  2999. /*
  3000. * Re-target the irq to the specified CPU and enable the specified MMR located
  3001. * on the specified blade to allow the sending of MSIs to the specified CPU.
  3002. */
  3003. int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
  3004. unsigned long mmr_offset)
  3005. {
  3006. const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
  3007. struct irq_cfg *cfg;
  3008. int mmr_pnode;
  3009. unsigned long mmr_value;
  3010. struct uv_IO_APIC_route_entry *entry;
  3011. unsigned long flags;
  3012. int err;
  3013. err = assign_irq_vector(irq, *eligible_cpu);
  3014. if (err != 0)
  3015. return err;
  3016. spin_lock_irqsave(&vector_lock, flags);
  3017. set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
  3018. irq_name);
  3019. spin_unlock_irqrestore(&vector_lock, flags);
  3020. cfg = irq_cfg(irq);
  3021. mmr_value = 0;
  3022. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  3023. BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
  3024. entry->vector = cfg->vector;
  3025. entry->delivery_mode = INT_DELIVERY_MODE;
  3026. entry->dest_mode = INT_DEST_MODE;
  3027. entry->polarity = 0;
  3028. entry->trigger = 0;
  3029. entry->mask = 0;
  3030. entry->dest = cpu_mask_to_apicid(*eligible_cpu);
  3031. mmr_pnode = uv_blade_to_pnode(mmr_blade);
  3032. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  3033. return irq;
  3034. }
  3035. /*
  3036. * Disable the specified MMR located on the specified blade so that MSIs are
  3037. * longer allowed to be sent.
  3038. */
  3039. void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
  3040. {
  3041. unsigned long mmr_value;
  3042. struct uv_IO_APIC_route_entry *entry;
  3043. int mmr_pnode;
  3044. mmr_value = 0;
  3045. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  3046. BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
  3047. entry->mask = 1;
  3048. mmr_pnode = uv_blade_to_pnode(mmr_blade);
  3049. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  3050. }
  3051. #endif /* CONFIG_X86_64 */
  3052. int __init io_apic_get_redir_entries (int ioapic)
  3053. {
  3054. union IO_APIC_reg_01 reg_01;
  3055. unsigned long flags;
  3056. spin_lock_irqsave(&ioapic_lock, flags);
  3057. reg_01.raw = io_apic_read(ioapic, 1);
  3058. spin_unlock_irqrestore(&ioapic_lock, flags);
  3059. return reg_01.bits.entries;
  3060. }
  3061. int __init probe_nr_irqs(void)
  3062. {
  3063. int idx;
  3064. int nr = 0;
  3065. #ifndef CONFIG_XEN
  3066. int nr_min = 32;
  3067. #else
  3068. int nr_min = NR_IRQS;
  3069. #endif
  3070. for (idx = 0; idx < nr_ioapics; idx++)
  3071. nr += io_apic_get_redir_entries(idx) + 1;
  3072. /* double it for hotplug and msi and nmi */
  3073. nr <<= 1;
  3074. /* something wrong ? */
  3075. if (nr < nr_min)
  3076. nr = nr_min;
  3077. return nr;
  3078. }
  3079. /* --------------------------------------------------------------------------
  3080. ACPI-based IOAPIC Configuration
  3081. -------------------------------------------------------------------------- */
  3082. #ifdef CONFIG_ACPI
  3083. #ifdef CONFIG_X86_32
  3084. int __init io_apic_get_unique_id(int ioapic, int apic_id)
  3085. {
  3086. union IO_APIC_reg_00 reg_00;
  3087. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  3088. physid_mask_t tmp;
  3089. unsigned long flags;
  3090. int i = 0;
  3091. /*
  3092. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  3093. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  3094. * supports up to 16 on one shared APIC bus.
  3095. *
  3096. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  3097. * advantage of new APIC bus architecture.
  3098. */
  3099. if (physids_empty(apic_id_map))
  3100. apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
  3101. spin_lock_irqsave(&ioapic_lock, flags);
  3102. reg_00.raw = io_apic_read(ioapic, 0);
  3103. spin_unlock_irqrestore(&ioapic_lock, flags);
  3104. if (apic_id >= get_physical_broadcast()) {
  3105. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  3106. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  3107. apic_id = reg_00.bits.ID;
  3108. }
  3109. /*
  3110. * Every APIC in a system must have a unique ID or we get lots of nice
  3111. * 'stuck on smp_invalidate_needed IPI wait' messages.
  3112. */
  3113. if (check_apicid_used(apic_id_map, apic_id)) {
  3114. for (i = 0; i < get_physical_broadcast(); i++) {
  3115. if (!check_apicid_used(apic_id_map, i))
  3116. break;
  3117. }
  3118. if (i == get_physical_broadcast())
  3119. panic("Max apic_id exceeded!\n");
  3120. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  3121. "trying %d\n", ioapic, apic_id, i);
  3122. apic_id = i;
  3123. }
  3124. tmp = apicid_to_cpu_present(apic_id);
  3125. physids_or(apic_id_map, apic_id_map, tmp);
  3126. if (reg_00.bits.ID != apic_id) {
  3127. reg_00.bits.ID = apic_id;
  3128. spin_lock_irqsave(&ioapic_lock, flags);
  3129. io_apic_write(ioapic, 0, reg_00.raw);
  3130. reg_00.raw = io_apic_read(ioapic, 0);
  3131. spin_unlock_irqrestore(&ioapic_lock, flags);
  3132. /* Sanity check */
  3133. if (reg_00.bits.ID != apic_id) {
  3134. printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
  3135. return -1;
  3136. }
  3137. }
  3138. apic_printk(APIC_VERBOSE, KERN_INFO
  3139. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  3140. return apic_id;
  3141. }
  3142. int __init io_apic_get_version(int ioapic)
  3143. {
  3144. union IO_APIC_reg_01 reg_01;
  3145. unsigned long flags;
  3146. spin_lock_irqsave(&ioapic_lock, flags);
  3147. reg_01.raw = io_apic_read(ioapic, 1);
  3148. spin_unlock_irqrestore(&ioapic_lock, flags);
  3149. return reg_01.bits.version;
  3150. }
  3151. #endif
  3152. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  3153. {
  3154. if (!IO_APIC_IRQ(irq)) {
  3155. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  3156. ioapic);
  3157. return -EINVAL;
  3158. }
  3159. /*
  3160. * IRQs < 16 are already in the irq_2_pin[] map
  3161. */
  3162. if (irq >= 16)
  3163. add_pin_to_irq(irq, ioapic, pin);
  3164. setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
  3165. return 0;
  3166. }
  3167. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  3168. {
  3169. int i;
  3170. if (skip_ioapic_setup)
  3171. return -1;
  3172. for (i = 0; i < mp_irq_entries; i++)
  3173. if (mp_irqs[i].mp_irqtype == mp_INT &&
  3174. mp_irqs[i].mp_srcbusirq == bus_irq)
  3175. break;
  3176. if (i >= mp_irq_entries)
  3177. return -1;
  3178. *trigger = irq_trigger(i);
  3179. *polarity = irq_polarity(i);
  3180. return 0;
  3181. }
  3182. #endif /* CONFIG_ACPI */
  3183. /*
  3184. * This function currently is only a helper for the i386 smp boot process where
  3185. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  3186. * so mask in all cases should simply be TARGET_CPUS
  3187. */
  3188. #ifdef CONFIG_SMP
  3189. void __init setup_ioapic_dest(void)
  3190. {
  3191. int pin, ioapic, irq, irq_entry;
  3192. struct irq_cfg *cfg;
  3193. if (skip_ioapic_setup == 1)
  3194. return;
  3195. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  3196. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  3197. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  3198. if (irq_entry == -1)
  3199. continue;
  3200. irq = pin_2_irq(irq_entry, ioapic, pin);
  3201. /* setup_IO_APIC_irqs could fail to get vector for some device
  3202. * when you have too many devices, because at that time only boot
  3203. * cpu is online.
  3204. */
  3205. cfg = irq_cfg(irq);
  3206. if (!cfg->vector)
  3207. setup_IO_APIC_irq(ioapic, pin, irq,
  3208. irq_trigger(irq_entry),
  3209. irq_polarity(irq_entry));
  3210. #ifdef CONFIG_INTR_REMAP
  3211. else if (intr_remapping_enabled)
  3212. set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
  3213. #endif
  3214. else
  3215. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  3216. }
  3217. }
  3218. }
  3219. #endif
  3220. #define IOAPIC_RESOURCE_NAME_SIZE 11
  3221. static struct resource *ioapic_resources;
  3222. static struct resource * __init ioapic_setup_resources(void)
  3223. {
  3224. unsigned long n;
  3225. struct resource *res;
  3226. char *mem;
  3227. int i;
  3228. if (nr_ioapics <= 0)
  3229. return NULL;
  3230. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  3231. n *= nr_ioapics;
  3232. mem = alloc_bootmem(n);
  3233. res = (void *)mem;
  3234. if (mem != NULL) {
  3235. mem += sizeof(struct resource) * nr_ioapics;
  3236. for (i = 0; i < nr_ioapics; i++) {
  3237. res[i].name = mem;
  3238. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  3239. sprintf(mem, "IOAPIC %u", i);
  3240. mem += IOAPIC_RESOURCE_NAME_SIZE;
  3241. }
  3242. }
  3243. ioapic_resources = res;
  3244. return res;
  3245. }
  3246. void __init ioapic_init_mappings(void)
  3247. {
  3248. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  3249. struct resource *ioapic_res;
  3250. int i;
  3251. irq_2_pin_init();
  3252. ioapic_res = ioapic_setup_resources();
  3253. for (i = 0; i < nr_ioapics; i++) {
  3254. if (smp_found_config) {
  3255. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  3256. #ifdef CONFIG_X86_32
  3257. if (!ioapic_phys) {
  3258. printk(KERN_ERR
  3259. "WARNING: bogus zero IO-APIC "
  3260. "address found in MPTABLE, "
  3261. "disabling IO/APIC support!\n");
  3262. smp_found_config = 0;
  3263. skip_ioapic_setup = 1;
  3264. goto fake_ioapic_page;
  3265. }
  3266. #endif
  3267. } else {
  3268. #ifdef CONFIG_X86_32
  3269. fake_ioapic_page:
  3270. #endif
  3271. ioapic_phys = (unsigned long)
  3272. alloc_bootmem_pages(PAGE_SIZE);
  3273. ioapic_phys = __pa(ioapic_phys);
  3274. }
  3275. set_fixmap_nocache(idx, ioapic_phys);
  3276. apic_printk(APIC_VERBOSE,
  3277. "mapped IOAPIC to %08lx (%08lx)\n",
  3278. __fix_to_virt(idx), ioapic_phys);
  3279. idx++;
  3280. if (ioapic_res != NULL) {
  3281. ioapic_res->start = ioapic_phys;
  3282. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  3283. ioapic_res++;
  3284. }
  3285. }
  3286. }
  3287. static int __init ioapic_insert_resources(void)
  3288. {
  3289. int i;
  3290. struct resource *r = ioapic_resources;
  3291. if (!r) {
  3292. printk(KERN_ERR
  3293. "IO APIC resources could be not be allocated.\n");
  3294. return -1;
  3295. }
  3296. for (i = 0; i < nr_ioapics; i++) {
  3297. insert_resource(&iomem_resource, r);
  3298. r++;
  3299. }
  3300. return 0;
  3301. }
  3302. /* Insert the IO APIC resources after PCI initialization has occured to handle
  3303. * IO APICS that are mapped in on a BAR in PCI space. */
  3304. late_initcall(ioapic_insert_resources);