io_apic_64.c 71 KB

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