io_apic_64.c 71 KB

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