io_apic_64.c 77 KB

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