io_apic.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  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/smp_lock.h>
  28. #include <linux/pci.h>
  29. #include <linux/mc146818rtc.h>
  30. #include <linux/acpi.h>
  31. #include <linux/sysdev.h>
  32. #include <linux/msi.h>
  33. #include <linux/htirq.h>
  34. #ifdef CONFIG_ACPI
  35. #include <acpi/acpi_bus.h>
  36. #endif
  37. #include <asm/idle.h>
  38. #include <asm/io.h>
  39. #include <asm/smp.h>
  40. #include <asm/desc.h>
  41. #include <asm/proto.h>
  42. #include <asm/mach_apic.h>
  43. #include <asm/acpi.h>
  44. #include <asm/dma.h>
  45. #include <asm/nmi.h>
  46. #include <asm/msidef.h>
  47. #include <asm/hypertransport.h>
  48. struct irq_cfg {
  49. cpumask_t domain;
  50. cpumask_t old_domain;
  51. unsigned move_cleanup_count;
  52. u8 vector;
  53. u8 move_in_progress : 1;
  54. };
  55. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  56. struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
  57. [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
  58. [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
  59. [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
  60. [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
  61. [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
  62. [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
  63. [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
  64. [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
  65. [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
  66. [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
  67. [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
  68. [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
  69. [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
  70. [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
  71. [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
  72. [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
  73. };
  74. static int assign_irq_vector(int irq, cpumask_t mask);
  75. #define __apicdebuginit __init
  76. int sis_apic_bug; /* not actually supported, dummy for compile */
  77. static int no_timer_check;
  78. static int disable_timer_pin_1 __initdata;
  79. int timer_over_8254 __initdata = 1;
  80. /* Where if anywhere is the i8259 connect in external int mode */
  81. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  82. static DEFINE_SPINLOCK(ioapic_lock);
  83. DEFINE_SPINLOCK(vector_lock);
  84. /*
  85. * # of IRQ routing registers
  86. */
  87. int nr_ioapic_registers[MAX_IO_APICS];
  88. /*
  89. * Rough estimation of how many shared IRQs there are, can
  90. * be changed anytime.
  91. */
  92. #define MAX_PLUS_SHARED_IRQS NR_IRQS
  93. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  94. /*
  95. * This is performance-critical, we want to do it O(1)
  96. *
  97. * the indexing order of this array favors 1:1 mappings
  98. * between pins and IRQs.
  99. */
  100. static struct irq_pin_list {
  101. short apic, pin, next;
  102. } irq_2_pin[PIN_MAP_SIZE];
  103. struct io_apic {
  104. unsigned int index;
  105. unsigned int unused[3];
  106. unsigned int data;
  107. };
  108. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  109. {
  110. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  111. + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
  112. }
  113. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  114. {
  115. struct io_apic __iomem *io_apic = io_apic_base(apic);
  116. writel(reg, &io_apic->index);
  117. return readl(&io_apic->data);
  118. }
  119. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  120. {
  121. struct io_apic __iomem *io_apic = io_apic_base(apic);
  122. writel(reg, &io_apic->index);
  123. writel(value, &io_apic->data);
  124. }
  125. /*
  126. * Re-write a value: to be used for read-modify-write
  127. * cycles where the read already set up the index register.
  128. */
  129. static inline void io_apic_modify(unsigned int apic, unsigned int value)
  130. {
  131. struct io_apic __iomem *io_apic = io_apic_base(apic);
  132. writel(value, &io_apic->data);
  133. }
  134. /*
  135. * Synchronize the IO-APIC and the CPU by doing
  136. * a dummy read from the IO-APIC
  137. */
  138. static inline void io_apic_sync(unsigned int apic)
  139. {
  140. struct io_apic __iomem *io_apic = io_apic_base(apic);
  141. readl(&io_apic->data);
  142. }
  143. #define __DO_ACTION(R, ACTION, FINAL) \
  144. \
  145. { \
  146. int pin; \
  147. struct irq_pin_list *entry = irq_2_pin + irq; \
  148. \
  149. BUG_ON(irq >= NR_IRQS); \
  150. for (;;) { \
  151. unsigned int reg; \
  152. pin = entry->pin; \
  153. if (pin == -1) \
  154. break; \
  155. reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
  156. reg ACTION; \
  157. io_apic_modify(entry->apic, reg); \
  158. FINAL; \
  159. if (!entry->next) \
  160. break; \
  161. entry = irq_2_pin + entry->next; \
  162. } \
  163. }
  164. union entry_union {
  165. struct { u32 w1, w2; };
  166. struct IO_APIC_route_entry entry;
  167. };
  168. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  169. {
  170. union entry_union eu;
  171. unsigned long flags;
  172. spin_lock_irqsave(&ioapic_lock, flags);
  173. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  174. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  175. spin_unlock_irqrestore(&ioapic_lock, flags);
  176. return eu.entry;
  177. }
  178. /*
  179. * When we write a new IO APIC routing entry, we need to write the high
  180. * word first! If the mask bit in the low word is clear, we will enable
  181. * the interrupt, and we need to make sure the entry is fully populated
  182. * before that happens.
  183. */
  184. static void
  185. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  186. {
  187. union entry_union eu;
  188. eu.entry = e;
  189. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  190. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  191. }
  192. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  193. {
  194. unsigned long flags;
  195. spin_lock_irqsave(&ioapic_lock, flags);
  196. __ioapic_write_entry(apic, pin, e);
  197. spin_unlock_irqrestore(&ioapic_lock, flags);
  198. }
  199. /*
  200. * When we mask an IO APIC routing entry, we need to write the low
  201. * word first, in order to set the mask bit before we change the
  202. * high bits!
  203. */
  204. static void ioapic_mask_entry(int apic, int pin)
  205. {
  206. unsigned long flags;
  207. union entry_union eu = { .entry.mask = 1 };
  208. spin_lock_irqsave(&ioapic_lock, flags);
  209. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  210. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  211. spin_unlock_irqrestore(&ioapic_lock, flags);
  212. }
  213. #ifdef CONFIG_SMP
  214. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
  215. {
  216. int apic, pin;
  217. struct irq_pin_list *entry = irq_2_pin + irq;
  218. BUG_ON(irq >= NR_IRQS);
  219. for (;;) {
  220. unsigned int reg;
  221. apic = entry->apic;
  222. pin = entry->pin;
  223. if (pin == -1)
  224. break;
  225. io_apic_write(apic, 0x11 + pin*2, dest);
  226. reg = io_apic_read(apic, 0x10 + pin*2);
  227. reg &= ~0x000000ff;
  228. reg |= vector;
  229. io_apic_modify(apic, reg);
  230. if (!entry->next)
  231. break;
  232. entry = irq_2_pin + entry->next;
  233. }
  234. }
  235. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  236. {
  237. struct irq_cfg *cfg = irq_cfg + irq;
  238. unsigned long flags;
  239. unsigned int dest;
  240. cpumask_t tmp;
  241. cpus_and(tmp, mask, cpu_online_map);
  242. if (cpus_empty(tmp))
  243. return;
  244. if (assign_irq_vector(irq, mask))
  245. return;
  246. cpus_and(tmp, cfg->domain, mask);
  247. dest = cpu_mask_to_apicid(tmp);
  248. /*
  249. * Only the high 8 bits are valid.
  250. */
  251. dest = SET_APIC_LOGICAL_ID(dest);
  252. spin_lock_irqsave(&ioapic_lock, flags);
  253. __target_IO_APIC_irq(irq, dest, cfg->vector);
  254. irq_desc[irq].affinity = mask;
  255. spin_unlock_irqrestore(&ioapic_lock, flags);
  256. }
  257. #endif
  258. /*
  259. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  260. * shared ISA-space IRQs, so we have to support them. We are super
  261. * fast in the common case, and fast for shared ISA-space IRQs.
  262. */
  263. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  264. {
  265. static int first_free_entry = NR_IRQS;
  266. struct irq_pin_list *entry = irq_2_pin + irq;
  267. BUG_ON(irq >= NR_IRQS);
  268. while (entry->next)
  269. entry = irq_2_pin + entry->next;
  270. if (entry->pin != -1) {
  271. entry->next = first_free_entry;
  272. entry = irq_2_pin + entry->next;
  273. if (++first_free_entry >= PIN_MAP_SIZE)
  274. panic("io_apic.c: ran out of irq_2_pin entries!");
  275. }
  276. entry->apic = apic;
  277. entry->pin = pin;
  278. }
  279. #define DO_ACTION(name,R,ACTION, FINAL) \
  280. \
  281. static void name##_IO_APIC_irq (unsigned int irq) \
  282. __DO_ACTION(R, ACTION, FINAL)
  283. DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
  284. /* mask = 1 */
  285. DO_ACTION( __unmask, 0, &= 0xfffeffff, )
  286. /* mask = 0 */
  287. static void mask_IO_APIC_irq (unsigned int irq)
  288. {
  289. unsigned long flags;
  290. spin_lock_irqsave(&ioapic_lock, flags);
  291. __mask_IO_APIC_irq(irq);
  292. spin_unlock_irqrestore(&ioapic_lock, flags);
  293. }
  294. static void unmask_IO_APIC_irq (unsigned int irq)
  295. {
  296. unsigned long flags;
  297. spin_lock_irqsave(&ioapic_lock, flags);
  298. __unmask_IO_APIC_irq(irq);
  299. spin_unlock_irqrestore(&ioapic_lock, flags);
  300. }
  301. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  302. {
  303. struct IO_APIC_route_entry entry;
  304. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  305. entry = ioapic_read_entry(apic, pin);
  306. if (entry.delivery_mode == dest_SMI)
  307. return;
  308. /*
  309. * Disable it in the IO-APIC irq-routing table:
  310. */
  311. ioapic_mask_entry(apic, pin);
  312. }
  313. static void clear_IO_APIC (void)
  314. {
  315. int apic, pin;
  316. for (apic = 0; apic < nr_ioapics; apic++)
  317. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  318. clear_IO_APIC_pin(apic, pin);
  319. }
  320. int skip_ioapic_setup;
  321. int ioapic_force;
  322. /* dummy parsing: see setup.c */
  323. static int __init disable_ioapic_setup(char *str)
  324. {
  325. skip_ioapic_setup = 1;
  326. return 0;
  327. }
  328. early_param("noapic", disable_ioapic_setup);
  329. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  330. static int __init disable_timer_pin_setup(char *arg)
  331. {
  332. disable_timer_pin_1 = 1;
  333. return 1;
  334. }
  335. __setup("disable_timer_pin_1", disable_timer_pin_setup);
  336. static int __init setup_disable_8254_timer(char *s)
  337. {
  338. timer_over_8254 = -1;
  339. return 1;
  340. }
  341. static int __init setup_enable_8254_timer(char *s)
  342. {
  343. timer_over_8254 = 2;
  344. return 1;
  345. }
  346. __setup("disable_8254_timer", setup_disable_8254_timer);
  347. __setup("enable_8254_timer", setup_enable_8254_timer);
  348. /*
  349. * Find the IRQ entry number of a certain pin.
  350. */
  351. static int find_irq_entry(int apic, int pin, int type)
  352. {
  353. int i;
  354. for (i = 0; i < mp_irq_entries; i++)
  355. if (mp_irqs[i].mpc_irqtype == type &&
  356. (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
  357. mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
  358. mp_irqs[i].mpc_dstirq == pin)
  359. return i;
  360. return -1;
  361. }
  362. /*
  363. * Find the pin to which IRQ[irq] (ISA) is connected
  364. */
  365. static int __init find_isa_irq_pin(int irq, int type)
  366. {
  367. int i;
  368. for (i = 0; i < mp_irq_entries; i++) {
  369. int lbus = mp_irqs[i].mpc_srcbus;
  370. if (test_bit(lbus, mp_bus_not_pci) &&
  371. (mp_irqs[i].mpc_irqtype == type) &&
  372. (mp_irqs[i].mpc_srcbusirq == irq))
  373. return mp_irqs[i].mpc_dstirq;
  374. }
  375. return -1;
  376. }
  377. static int __init find_isa_irq_apic(int irq, int type)
  378. {
  379. int i;
  380. for (i = 0; i < mp_irq_entries; i++) {
  381. int lbus = mp_irqs[i].mpc_srcbus;
  382. if (test_bit(lbus, mp_bus_not_pci) &&
  383. (mp_irqs[i].mpc_irqtype == type) &&
  384. (mp_irqs[i].mpc_srcbusirq == irq))
  385. break;
  386. }
  387. if (i < mp_irq_entries) {
  388. int apic;
  389. for(apic = 0; apic < nr_ioapics; apic++) {
  390. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
  391. return apic;
  392. }
  393. }
  394. return -1;
  395. }
  396. /*
  397. * Find a specific PCI IRQ entry.
  398. * Not an __init, possibly needed by modules
  399. */
  400. static int pin_2_irq(int idx, int apic, int pin);
  401. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  402. {
  403. int apic, i, best_guess = -1;
  404. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  405. bus, slot, pin);
  406. if (mp_bus_id_to_pci_bus[bus] == -1) {
  407. apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  408. return -1;
  409. }
  410. for (i = 0; i < mp_irq_entries; i++) {
  411. int lbus = mp_irqs[i].mpc_srcbus;
  412. for (apic = 0; apic < nr_ioapics; apic++)
  413. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
  414. mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
  415. break;
  416. if (!test_bit(lbus, mp_bus_not_pci) &&
  417. !mp_irqs[i].mpc_irqtype &&
  418. (bus == lbus) &&
  419. (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
  420. int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
  421. if (!(apic || IO_APIC_IRQ(irq)))
  422. continue;
  423. if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
  424. return irq;
  425. /*
  426. * Use the first all-but-pin matching entry as a
  427. * best-guess fuzzy result for broken mptables.
  428. */
  429. if (best_guess < 0)
  430. best_guess = irq;
  431. }
  432. }
  433. BUG_ON(best_guess >= NR_IRQS);
  434. return best_guess;
  435. }
  436. /* ISA interrupts are always polarity zero edge triggered,
  437. * when listed as conforming in the MP table. */
  438. #define default_ISA_trigger(idx) (0)
  439. #define default_ISA_polarity(idx) (0)
  440. /* PCI interrupts are always polarity one level triggered,
  441. * when listed as conforming in the MP table. */
  442. #define default_PCI_trigger(idx) (1)
  443. #define default_PCI_polarity(idx) (1)
  444. static int __init MPBIOS_polarity(int idx)
  445. {
  446. int bus = mp_irqs[idx].mpc_srcbus;
  447. int polarity;
  448. /*
  449. * Determine IRQ line polarity (high active or low active):
  450. */
  451. switch (mp_irqs[idx].mpc_irqflag & 3)
  452. {
  453. case 0: /* conforms, ie. bus-type dependent polarity */
  454. if (test_bit(bus, mp_bus_not_pci))
  455. polarity = default_ISA_polarity(idx);
  456. else
  457. polarity = default_PCI_polarity(idx);
  458. break;
  459. case 1: /* high active */
  460. {
  461. polarity = 0;
  462. break;
  463. }
  464. case 2: /* reserved */
  465. {
  466. printk(KERN_WARNING "broken BIOS!!\n");
  467. polarity = 1;
  468. break;
  469. }
  470. case 3: /* low active */
  471. {
  472. polarity = 1;
  473. break;
  474. }
  475. default: /* invalid */
  476. {
  477. printk(KERN_WARNING "broken BIOS!!\n");
  478. polarity = 1;
  479. break;
  480. }
  481. }
  482. return polarity;
  483. }
  484. static int MPBIOS_trigger(int idx)
  485. {
  486. int bus = mp_irqs[idx].mpc_srcbus;
  487. int trigger;
  488. /*
  489. * Determine IRQ trigger mode (edge or level sensitive):
  490. */
  491. switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
  492. {
  493. case 0: /* conforms, ie. bus-type dependent */
  494. if (test_bit(bus, mp_bus_not_pci))
  495. trigger = default_ISA_trigger(idx);
  496. else
  497. trigger = default_PCI_trigger(idx);
  498. break;
  499. case 1: /* edge */
  500. {
  501. trigger = 0;
  502. break;
  503. }
  504. case 2: /* reserved */
  505. {
  506. printk(KERN_WARNING "broken BIOS!!\n");
  507. trigger = 1;
  508. break;
  509. }
  510. case 3: /* level */
  511. {
  512. trigger = 1;
  513. break;
  514. }
  515. default: /* invalid */
  516. {
  517. printk(KERN_WARNING "broken BIOS!!\n");
  518. trigger = 0;
  519. break;
  520. }
  521. }
  522. return trigger;
  523. }
  524. static inline int irq_polarity(int idx)
  525. {
  526. return MPBIOS_polarity(idx);
  527. }
  528. static inline int irq_trigger(int idx)
  529. {
  530. return MPBIOS_trigger(idx);
  531. }
  532. static int pin_2_irq(int idx, int apic, int pin)
  533. {
  534. int irq, i;
  535. int bus = mp_irqs[idx].mpc_srcbus;
  536. /*
  537. * Debugging check, we are in big trouble if this message pops up!
  538. */
  539. if (mp_irqs[idx].mpc_dstirq != pin)
  540. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  541. if (test_bit(bus, mp_bus_not_pci)) {
  542. irq = mp_irqs[idx].mpc_srcbusirq;
  543. } else {
  544. /*
  545. * PCI IRQs are mapped in order
  546. */
  547. i = irq = 0;
  548. while (i < apic)
  549. irq += nr_ioapic_registers[i++];
  550. irq += pin;
  551. }
  552. BUG_ON(irq >= NR_IRQS);
  553. return irq;
  554. }
  555. static int __assign_irq_vector(int irq, cpumask_t mask)
  556. {
  557. /*
  558. * NOTE! The local APIC isn't very good at handling
  559. * multiple interrupts at the same interrupt level.
  560. * As the interrupt level is determined by taking the
  561. * vector number and shifting that right by 4, we
  562. * want to spread these out a bit so that they don't
  563. * all fall in the same interrupt level.
  564. *
  565. * Also, we've got to be careful not to trash gate
  566. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  567. */
  568. static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  569. unsigned int old_vector;
  570. int cpu;
  571. struct irq_cfg *cfg;
  572. BUG_ON((unsigned)irq >= NR_IRQS);
  573. cfg = &irq_cfg[irq];
  574. /* Only try and allocate irqs on cpus that are present */
  575. cpus_and(mask, mask, cpu_online_map);
  576. if ((cfg->move_in_progress) || cfg->move_cleanup_count)
  577. return -EBUSY;
  578. old_vector = cfg->vector;
  579. if (old_vector) {
  580. cpumask_t tmp;
  581. cpus_and(tmp, cfg->domain, mask);
  582. if (!cpus_empty(tmp))
  583. return 0;
  584. }
  585. for_each_cpu_mask(cpu, mask) {
  586. cpumask_t domain, new_mask;
  587. int new_cpu;
  588. int vector, offset;
  589. domain = vector_allocation_domain(cpu);
  590. cpus_and(new_mask, domain, cpu_online_map);
  591. vector = current_vector;
  592. offset = current_offset;
  593. next:
  594. vector += 8;
  595. if (vector >= FIRST_SYSTEM_VECTOR) {
  596. /* If we run out of vectors on large boxen, must share them. */
  597. offset = (offset + 1) % 8;
  598. vector = FIRST_DEVICE_VECTOR + offset;
  599. }
  600. if (unlikely(current_vector == vector))
  601. continue;
  602. if (vector == IA32_SYSCALL_VECTOR)
  603. goto next;
  604. for_each_cpu_mask(new_cpu, new_mask)
  605. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  606. goto next;
  607. /* Found one! */
  608. current_vector = vector;
  609. current_offset = offset;
  610. if (old_vector) {
  611. cfg->move_in_progress = 1;
  612. cfg->old_domain = cfg->domain;
  613. }
  614. for_each_cpu_mask(new_cpu, new_mask)
  615. per_cpu(vector_irq, new_cpu)[vector] = irq;
  616. cfg->vector = vector;
  617. cfg->domain = domain;
  618. return 0;
  619. }
  620. return -ENOSPC;
  621. }
  622. static int assign_irq_vector(int irq, cpumask_t mask)
  623. {
  624. int err;
  625. unsigned long flags;
  626. spin_lock_irqsave(&vector_lock, flags);
  627. err = __assign_irq_vector(irq, mask);
  628. spin_unlock_irqrestore(&vector_lock, flags);
  629. return err;
  630. }
  631. static void __clear_irq_vector(int irq)
  632. {
  633. struct irq_cfg *cfg;
  634. cpumask_t mask;
  635. int cpu, vector;
  636. BUG_ON((unsigned)irq >= NR_IRQS);
  637. cfg = &irq_cfg[irq];
  638. BUG_ON(!cfg->vector);
  639. vector = cfg->vector;
  640. cpus_and(mask, cfg->domain, cpu_online_map);
  641. for_each_cpu_mask(cpu, mask)
  642. per_cpu(vector_irq, cpu)[vector] = -1;
  643. cfg->vector = 0;
  644. cfg->domain = CPU_MASK_NONE;
  645. }
  646. void __setup_vector_irq(int cpu)
  647. {
  648. /* Initialize vector_irq on a new cpu */
  649. /* This function must be called with vector_lock held */
  650. int irq, vector;
  651. /* Mark the inuse vectors */
  652. for (irq = 0; irq < NR_IRQS; ++irq) {
  653. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  654. continue;
  655. vector = irq_cfg[irq].vector;
  656. per_cpu(vector_irq, cpu)[vector] = irq;
  657. }
  658. /* Mark the free vectors */
  659. for (vector = 0; vector < NR_VECTORS; ++vector) {
  660. irq = per_cpu(vector_irq, cpu)[vector];
  661. if (irq < 0)
  662. continue;
  663. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  664. per_cpu(vector_irq, cpu)[vector] = -1;
  665. }
  666. }
  667. static struct irq_chip ioapic_chip;
  668. static void ioapic_register_intr(int irq, unsigned long trigger)
  669. {
  670. if (trigger)
  671. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  672. handle_fasteoi_irq, "fasteoi");
  673. else
  674. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  675. handle_edge_irq, "edge");
  676. }
  677. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
  678. int trigger, int polarity)
  679. {
  680. struct irq_cfg *cfg = irq_cfg + irq;
  681. struct IO_APIC_route_entry entry;
  682. cpumask_t mask;
  683. if (!IO_APIC_IRQ(irq))
  684. return;
  685. mask = TARGET_CPUS;
  686. if (assign_irq_vector(irq, mask))
  687. return;
  688. cpus_and(mask, cfg->domain, mask);
  689. apic_printk(APIC_VERBOSE,KERN_DEBUG
  690. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  691. "IRQ %d Mode:%i Active:%i)\n",
  692. apic, mp_ioapics[apic].mpc_apicid, pin, cfg->vector,
  693. irq, trigger, polarity);
  694. /*
  695. * add it to the IO-APIC irq-routing table:
  696. */
  697. memset(&entry,0,sizeof(entry));
  698. entry.delivery_mode = INT_DELIVERY_MODE;
  699. entry.dest_mode = INT_DEST_MODE;
  700. entry.dest = cpu_mask_to_apicid(mask);
  701. entry.mask = 0; /* enable IRQ */
  702. entry.trigger = trigger;
  703. entry.polarity = polarity;
  704. entry.vector = cfg->vector;
  705. /* Mask level triggered irqs.
  706. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  707. */
  708. if (trigger)
  709. entry.mask = 1;
  710. ioapic_register_intr(irq, trigger);
  711. if (irq < 16)
  712. disable_8259A_irq(irq);
  713. ioapic_write_entry(apic, pin, entry);
  714. }
  715. static void __init setup_IO_APIC_irqs(void)
  716. {
  717. int apic, pin, idx, irq, first_notcon = 1;
  718. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  719. for (apic = 0; apic < nr_ioapics; apic++) {
  720. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  721. idx = find_irq_entry(apic,pin,mp_INT);
  722. if (idx == -1) {
  723. if (first_notcon) {
  724. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  725. first_notcon = 0;
  726. } else
  727. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  728. continue;
  729. }
  730. irq = pin_2_irq(idx, apic, pin);
  731. add_pin_to_irq(irq, apic, pin);
  732. setup_IO_APIC_irq(apic, pin, irq,
  733. irq_trigger(idx), irq_polarity(idx));
  734. }
  735. }
  736. if (!first_notcon)
  737. apic_printk(APIC_VERBOSE," not connected.\n");
  738. }
  739. /*
  740. * Set up the 8259A-master output pin as broadcast to all
  741. * CPUs.
  742. */
  743. static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
  744. {
  745. struct IO_APIC_route_entry entry;
  746. unsigned long flags;
  747. memset(&entry,0,sizeof(entry));
  748. disable_8259A_irq(0);
  749. /* mask LVT0 */
  750. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  751. /*
  752. * We use logical delivery to get the timer IRQ
  753. * to the first CPU.
  754. */
  755. entry.dest_mode = INT_DEST_MODE;
  756. entry.mask = 0; /* unmask IRQ now */
  757. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  758. entry.delivery_mode = INT_DELIVERY_MODE;
  759. entry.polarity = 0;
  760. entry.trigger = 0;
  761. entry.vector = vector;
  762. /*
  763. * The timer IRQ doesn't have to know that behind the
  764. * scene we have a 8259A-master in AEOI mode ...
  765. */
  766. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  767. /*
  768. * Add it to the IO-APIC irq-routing table:
  769. */
  770. spin_lock_irqsave(&ioapic_lock, flags);
  771. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  772. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  773. spin_unlock_irqrestore(&ioapic_lock, flags);
  774. enable_8259A_irq(0);
  775. }
  776. void __apicdebuginit print_IO_APIC(void)
  777. {
  778. int apic, i;
  779. union IO_APIC_reg_00 reg_00;
  780. union IO_APIC_reg_01 reg_01;
  781. union IO_APIC_reg_02 reg_02;
  782. unsigned long flags;
  783. if (apic_verbosity == APIC_QUIET)
  784. return;
  785. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  786. for (i = 0; i < nr_ioapics; i++)
  787. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  788. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  789. /*
  790. * We are a bit conservative about what we expect. We have to
  791. * know about every hardware change ASAP.
  792. */
  793. printk(KERN_INFO "testing the IO APIC.......................\n");
  794. for (apic = 0; apic < nr_ioapics; apic++) {
  795. spin_lock_irqsave(&ioapic_lock, flags);
  796. reg_00.raw = io_apic_read(apic, 0);
  797. reg_01.raw = io_apic_read(apic, 1);
  798. if (reg_01.bits.version >= 0x10)
  799. reg_02.raw = io_apic_read(apic, 2);
  800. spin_unlock_irqrestore(&ioapic_lock, flags);
  801. printk("\n");
  802. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  803. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  804. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  805. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  806. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  807. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  808. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  809. if (reg_01.bits.version >= 0x10) {
  810. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  811. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  812. }
  813. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  814. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  815. " Stat Dmod Deli Vect: \n");
  816. for (i = 0; i <= reg_01.bits.entries; i++) {
  817. struct IO_APIC_route_entry entry;
  818. entry = ioapic_read_entry(apic, i);
  819. printk(KERN_DEBUG " %02x %03X ",
  820. i,
  821. entry.dest
  822. );
  823. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  824. entry.mask,
  825. entry.trigger,
  826. entry.irr,
  827. entry.polarity,
  828. entry.delivery_status,
  829. entry.dest_mode,
  830. entry.delivery_mode,
  831. entry.vector
  832. );
  833. }
  834. }
  835. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  836. for (i = 0; i < NR_IRQS; i++) {
  837. struct irq_pin_list *entry = irq_2_pin + i;
  838. if (entry->pin < 0)
  839. continue;
  840. printk(KERN_DEBUG "IRQ%d ", i);
  841. for (;;) {
  842. printk("-> %d:%d", entry->apic, entry->pin);
  843. if (!entry->next)
  844. break;
  845. entry = irq_2_pin + entry->next;
  846. }
  847. printk("\n");
  848. }
  849. printk(KERN_INFO ".................................... done.\n");
  850. return;
  851. }
  852. #if 0
  853. static __apicdebuginit void print_APIC_bitfield (int base)
  854. {
  855. unsigned int v;
  856. int i, j;
  857. if (apic_verbosity == APIC_QUIET)
  858. return;
  859. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  860. for (i = 0; i < 8; i++) {
  861. v = apic_read(base + i*0x10);
  862. for (j = 0; j < 32; j++) {
  863. if (v & (1<<j))
  864. printk("1");
  865. else
  866. printk("0");
  867. }
  868. printk("\n");
  869. }
  870. }
  871. void __apicdebuginit print_local_APIC(void * dummy)
  872. {
  873. unsigned int v, ver, maxlvt;
  874. if (apic_verbosity == APIC_QUIET)
  875. return;
  876. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  877. smp_processor_id(), hard_smp_processor_id());
  878. v = apic_read(APIC_ID);
  879. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
  880. v = apic_read(APIC_LVR);
  881. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  882. ver = GET_APIC_VERSION(v);
  883. maxlvt = get_maxlvt();
  884. v = apic_read(APIC_TASKPRI);
  885. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  886. v = apic_read(APIC_ARBPRI);
  887. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  888. v & APIC_ARBPRI_MASK);
  889. v = apic_read(APIC_PROCPRI);
  890. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  891. v = apic_read(APIC_EOI);
  892. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  893. v = apic_read(APIC_RRR);
  894. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  895. v = apic_read(APIC_LDR);
  896. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  897. v = apic_read(APIC_DFR);
  898. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  899. v = apic_read(APIC_SPIV);
  900. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  901. printk(KERN_DEBUG "... APIC ISR field:\n");
  902. print_APIC_bitfield(APIC_ISR);
  903. printk(KERN_DEBUG "... APIC TMR field:\n");
  904. print_APIC_bitfield(APIC_TMR);
  905. printk(KERN_DEBUG "... APIC IRR field:\n");
  906. print_APIC_bitfield(APIC_IRR);
  907. v = apic_read(APIC_ESR);
  908. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  909. v = apic_read(APIC_ICR);
  910. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  911. v = apic_read(APIC_ICR2);
  912. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  913. v = apic_read(APIC_LVTT);
  914. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  915. if (maxlvt > 3) { /* PC is LVT#4. */
  916. v = apic_read(APIC_LVTPC);
  917. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  918. }
  919. v = apic_read(APIC_LVT0);
  920. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  921. v = apic_read(APIC_LVT1);
  922. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  923. if (maxlvt > 2) { /* ERR is LVT#3. */
  924. v = apic_read(APIC_LVTERR);
  925. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  926. }
  927. v = apic_read(APIC_TMICT);
  928. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  929. v = apic_read(APIC_TMCCT);
  930. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  931. v = apic_read(APIC_TDCR);
  932. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  933. printk("\n");
  934. }
  935. void print_all_local_APICs (void)
  936. {
  937. on_each_cpu(print_local_APIC, NULL, 1, 1);
  938. }
  939. void __apicdebuginit print_PIC(void)
  940. {
  941. unsigned int v;
  942. unsigned long flags;
  943. if (apic_verbosity == APIC_QUIET)
  944. return;
  945. printk(KERN_DEBUG "\nprinting PIC contents\n");
  946. spin_lock_irqsave(&i8259A_lock, flags);
  947. v = inb(0xa1) << 8 | inb(0x21);
  948. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  949. v = inb(0xa0) << 8 | inb(0x20);
  950. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  951. outb(0x0b,0xa0);
  952. outb(0x0b,0x20);
  953. v = inb(0xa0) << 8 | inb(0x20);
  954. outb(0x0a,0xa0);
  955. outb(0x0a,0x20);
  956. spin_unlock_irqrestore(&i8259A_lock, flags);
  957. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  958. v = inb(0x4d1) << 8 | inb(0x4d0);
  959. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  960. }
  961. #endif /* 0 */
  962. static void __init enable_IO_APIC(void)
  963. {
  964. union IO_APIC_reg_01 reg_01;
  965. int i8259_apic, i8259_pin;
  966. int i, apic;
  967. unsigned long flags;
  968. for (i = 0; i < PIN_MAP_SIZE; i++) {
  969. irq_2_pin[i].pin = -1;
  970. irq_2_pin[i].next = 0;
  971. }
  972. /*
  973. * The number of IO-APIC IRQ registers (== #pins):
  974. */
  975. for (apic = 0; apic < nr_ioapics; apic++) {
  976. spin_lock_irqsave(&ioapic_lock, flags);
  977. reg_01.raw = io_apic_read(apic, 1);
  978. spin_unlock_irqrestore(&ioapic_lock, flags);
  979. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  980. }
  981. for(apic = 0; apic < nr_ioapics; apic++) {
  982. int pin;
  983. /* See if any of the pins is in ExtINT mode */
  984. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  985. struct IO_APIC_route_entry entry;
  986. entry = ioapic_read_entry(apic, pin);
  987. /* If the interrupt line is enabled and in ExtInt mode
  988. * I have found the pin where the i8259 is connected.
  989. */
  990. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  991. ioapic_i8259.apic = apic;
  992. ioapic_i8259.pin = pin;
  993. goto found_i8259;
  994. }
  995. }
  996. }
  997. found_i8259:
  998. /* Look to see what if the MP table has reported the ExtINT */
  999. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1000. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1001. /* Trust the MP table if nothing is setup in the hardware */
  1002. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1003. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1004. ioapic_i8259.pin = i8259_pin;
  1005. ioapic_i8259.apic = i8259_apic;
  1006. }
  1007. /* Complain if the MP table and the hardware disagree */
  1008. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1009. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1010. {
  1011. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1012. }
  1013. /*
  1014. * Do not trust the IO-APIC being empty at bootup
  1015. */
  1016. clear_IO_APIC();
  1017. }
  1018. /*
  1019. * Not an __init, needed by the reboot code
  1020. */
  1021. void disable_IO_APIC(void)
  1022. {
  1023. /*
  1024. * Clear the IO-APIC before rebooting:
  1025. */
  1026. clear_IO_APIC();
  1027. /*
  1028. * If the i8259 is routed through an IOAPIC
  1029. * Put that IOAPIC in virtual wire mode
  1030. * so legacy interrupts can be delivered.
  1031. */
  1032. if (ioapic_i8259.pin != -1) {
  1033. struct IO_APIC_route_entry entry;
  1034. memset(&entry, 0, sizeof(entry));
  1035. entry.mask = 0; /* Enabled */
  1036. entry.trigger = 0; /* Edge */
  1037. entry.irr = 0;
  1038. entry.polarity = 0; /* High */
  1039. entry.delivery_status = 0;
  1040. entry.dest_mode = 0; /* Physical */
  1041. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1042. entry.vector = 0;
  1043. entry.dest = GET_APIC_ID(apic_read(APIC_ID));
  1044. /*
  1045. * Add it to the IO-APIC irq-routing table:
  1046. */
  1047. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1048. }
  1049. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1050. }
  1051. /*
  1052. * There is a nasty bug in some older SMP boards, their mptable lies
  1053. * about the timer IRQ. We do the following to work around the situation:
  1054. *
  1055. * - timer IRQ defaults to IO-APIC IRQ
  1056. * - if this function detects that timer IRQs are defunct, then we fall
  1057. * back to ISA timer IRQs
  1058. */
  1059. static int __init timer_irq_works(void)
  1060. {
  1061. unsigned long t1 = jiffies;
  1062. local_irq_enable();
  1063. /* Let ten ticks pass... */
  1064. mdelay((10 * 1000) / HZ);
  1065. /*
  1066. * Expect a few ticks at least, to be sure some possible
  1067. * glue logic does not lock up after one or two first
  1068. * ticks in a non-ExtINT mode. Also the local APIC
  1069. * might have cached one ExtINT interrupt. Finally, at
  1070. * least one tick may be lost due to delays.
  1071. */
  1072. /* jiffies wrap? */
  1073. if (jiffies - t1 > 4)
  1074. return 1;
  1075. return 0;
  1076. }
  1077. /*
  1078. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1079. * number of pending IRQ events unhandled. These cases are very rare,
  1080. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1081. * better to do it this way as thus we do not have to be aware of
  1082. * 'pending' interrupts in the IRQ path, except at this point.
  1083. */
  1084. /*
  1085. * Edge triggered needs to resend any interrupt
  1086. * that was delayed but this is now handled in the device
  1087. * independent code.
  1088. */
  1089. /*
  1090. * Starting up a edge-triggered IO-APIC interrupt is
  1091. * nasty - we need to make sure that we get the edge.
  1092. * If it is already asserted for some reason, we need
  1093. * return 1 to indicate that is was pending.
  1094. *
  1095. * This is not complete - we should be able to fake
  1096. * an edge even if it isn't on the 8259A...
  1097. */
  1098. static unsigned int startup_ioapic_irq(unsigned int irq)
  1099. {
  1100. int was_pending = 0;
  1101. unsigned long flags;
  1102. spin_lock_irqsave(&ioapic_lock, flags);
  1103. if (irq < 16) {
  1104. disable_8259A_irq(irq);
  1105. if (i8259A_irq_pending(irq))
  1106. was_pending = 1;
  1107. }
  1108. __unmask_IO_APIC_irq(irq);
  1109. spin_unlock_irqrestore(&ioapic_lock, flags);
  1110. return was_pending;
  1111. }
  1112. static int ioapic_retrigger_irq(unsigned int irq)
  1113. {
  1114. struct irq_cfg *cfg = &irq_cfg[irq];
  1115. cpumask_t mask;
  1116. unsigned long flags;
  1117. spin_lock_irqsave(&vector_lock, flags);
  1118. cpus_clear(mask);
  1119. cpu_set(first_cpu(cfg->domain), mask);
  1120. send_IPI_mask(mask, cfg->vector);
  1121. spin_unlock_irqrestore(&vector_lock, flags);
  1122. return 1;
  1123. }
  1124. /*
  1125. * Level and edge triggered IO-APIC interrupts need different handling,
  1126. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1127. * handled with the level-triggered descriptor, but that one has slightly
  1128. * more overhead. Level-triggered interrupts cannot be handled with the
  1129. * edge-triggered handler, without risking IRQ storms and other ugly
  1130. * races.
  1131. */
  1132. #ifdef CONFIG_SMP
  1133. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1134. {
  1135. unsigned vector, me;
  1136. ack_APIC_irq();
  1137. exit_idle();
  1138. irq_enter();
  1139. me = smp_processor_id();
  1140. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1141. unsigned int irq;
  1142. struct irq_desc *desc;
  1143. struct irq_cfg *cfg;
  1144. irq = __get_cpu_var(vector_irq)[vector];
  1145. if (irq >= NR_IRQS)
  1146. continue;
  1147. desc = irq_desc + irq;
  1148. cfg = irq_cfg + irq;
  1149. spin_lock(&desc->lock);
  1150. if (!cfg->move_cleanup_count)
  1151. goto unlock;
  1152. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  1153. goto unlock;
  1154. __get_cpu_var(vector_irq)[vector] = -1;
  1155. cfg->move_cleanup_count--;
  1156. unlock:
  1157. spin_unlock(&desc->lock);
  1158. }
  1159. irq_exit();
  1160. }
  1161. static void irq_complete_move(unsigned int irq)
  1162. {
  1163. struct irq_cfg *cfg = irq_cfg + irq;
  1164. unsigned vector, me;
  1165. if (likely(!cfg->move_in_progress))
  1166. return;
  1167. vector = ~get_irq_regs()->orig_rax;
  1168. me = smp_processor_id();
  1169. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  1170. cpumask_t cleanup_mask;
  1171. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1172. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1173. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1174. cfg->move_in_progress = 0;
  1175. }
  1176. }
  1177. #else
  1178. static inline void irq_complete_move(unsigned int irq) {}
  1179. #endif
  1180. static void ack_apic_edge(unsigned int irq)
  1181. {
  1182. irq_complete_move(irq);
  1183. move_native_irq(irq);
  1184. ack_APIC_irq();
  1185. }
  1186. static void ack_apic_level(unsigned int irq)
  1187. {
  1188. int do_unmask_irq = 0;
  1189. irq_complete_move(irq);
  1190. #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
  1191. /* If we are moving the irq we need to mask it */
  1192. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  1193. do_unmask_irq = 1;
  1194. mask_IO_APIC_irq(irq);
  1195. }
  1196. #endif
  1197. /*
  1198. * We must acknowledge the irq before we move it or the acknowledge will
  1199. * not propogate properly.
  1200. */
  1201. ack_APIC_irq();
  1202. /* Now we can move and renable the irq */
  1203. move_masked_irq(irq);
  1204. if (unlikely(do_unmask_irq))
  1205. unmask_IO_APIC_irq(irq);
  1206. }
  1207. static struct irq_chip ioapic_chip __read_mostly = {
  1208. .name = "IO-APIC",
  1209. .startup = startup_ioapic_irq,
  1210. .mask = mask_IO_APIC_irq,
  1211. .unmask = unmask_IO_APIC_irq,
  1212. .ack = ack_apic_edge,
  1213. .eoi = ack_apic_level,
  1214. #ifdef CONFIG_SMP
  1215. .set_affinity = set_ioapic_affinity_irq,
  1216. #endif
  1217. .retrigger = ioapic_retrigger_irq,
  1218. };
  1219. static inline void init_IO_APIC_traps(void)
  1220. {
  1221. int irq;
  1222. /*
  1223. * NOTE! The local APIC isn't very good at handling
  1224. * multiple interrupts at the same interrupt level.
  1225. * As the interrupt level is determined by taking the
  1226. * vector number and shifting that right by 4, we
  1227. * want to spread these out a bit so that they don't
  1228. * all fall in the same interrupt level.
  1229. *
  1230. * Also, we've got to be careful not to trash gate
  1231. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1232. */
  1233. for (irq = 0; irq < NR_IRQS ; irq++) {
  1234. int tmp = irq;
  1235. if (IO_APIC_IRQ(tmp) && !irq_cfg[tmp].vector) {
  1236. /*
  1237. * Hmm.. We don't have an entry for this,
  1238. * so default to an old-fashioned 8259
  1239. * interrupt if we can..
  1240. */
  1241. if (irq < 16)
  1242. make_8259A_irq(irq);
  1243. else
  1244. /* Strange. Oh, well.. */
  1245. irq_desc[irq].chip = &no_irq_chip;
  1246. }
  1247. }
  1248. }
  1249. static void enable_lapic_irq (unsigned int irq)
  1250. {
  1251. unsigned long v;
  1252. v = apic_read(APIC_LVT0);
  1253. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1254. }
  1255. static void disable_lapic_irq (unsigned int irq)
  1256. {
  1257. unsigned long v;
  1258. v = apic_read(APIC_LVT0);
  1259. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1260. }
  1261. static void ack_lapic_irq (unsigned int irq)
  1262. {
  1263. ack_APIC_irq();
  1264. }
  1265. static void end_lapic_irq (unsigned int i) { /* nothing */ }
  1266. static struct hw_interrupt_type lapic_irq_type __read_mostly = {
  1267. .typename = "local-APIC-edge",
  1268. .startup = NULL, /* startup_irq() not used for IRQ0 */
  1269. .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
  1270. .enable = enable_lapic_irq,
  1271. .disable = disable_lapic_irq,
  1272. .ack = ack_lapic_irq,
  1273. .end = end_lapic_irq,
  1274. };
  1275. static void setup_nmi (void)
  1276. {
  1277. /*
  1278. * Dirty trick to enable the NMI watchdog ...
  1279. * We put the 8259A master into AEOI mode and
  1280. * unmask on all local APICs LVT0 as NMI.
  1281. *
  1282. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1283. * is from Maciej W. Rozycki - so we do not have to EOI from
  1284. * the NMI handler or the timer interrupt.
  1285. */
  1286. printk(KERN_INFO "activating NMI Watchdog ...");
  1287. enable_NMI_through_LVT0(NULL);
  1288. printk(" done.\n");
  1289. }
  1290. /*
  1291. * This looks a bit hackish but it's about the only one way of sending
  1292. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1293. * not support the ExtINT mode, unfortunately. We need to send these
  1294. * cycles as some i82489DX-based boards have glue logic that keeps the
  1295. * 8259A interrupt line asserted until INTA. --macro
  1296. */
  1297. static inline void unlock_ExtINT_logic(void)
  1298. {
  1299. int apic, pin, i;
  1300. struct IO_APIC_route_entry entry0, entry1;
  1301. unsigned char save_control, save_freq_select;
  1302. unsigned long flags;
  1303. pin = find_isa_irq_pin(8, mp_INT);
  1304. apic = find_isa_irq_apic(8, mp_INT);
  1305. if (pin == -1)
  1306. return;
  1307. spin_lock_irqsave(&ioapic_lock, flags);
  1308. *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  1309. *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  1310. spin_unlock_irqrestore(&ioapic_lock, flags);
  1311. clear_IO_APIC_pin(apic, pin);
  1312. memset(&entry1, 0, sizeof(entry1));
  1313. entry1.dest_mode = 0; /* physical delivery */
  1314. entry1.mask = 0; /* unmask IRQ now */
  1315. entry1.dest = hard_smp_processor_id();
  1316. entry1.delivery_mode = dest_ExtINT;
  1317. entry1.polarity = entry0.polarity;
  1318. entry1.trigger = 0;
  1319. entry1.vector = 0;
  1320. spin_lock_irqsave(&ioapic_lock, flags);
  1321. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
  1322. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
  1323. spin_unlock_irqrestore(&ioapic_lock, flags);
  1324. save_control = CMOS_READ(RTC_CONTROL);
  1325. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1326. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1327. RTC_FREQ_SELECT);
  1328. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1329. i = 100;
  1330. while (i-- > 0) {
  1331. mdelay(10);
  1332. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1333. i -= 10;
  1334. }
  1335. CMOS_WRITE(save_control, RTC_CONTROL);
  1336. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1337. clear_IO_APIC_pin(apic, pin);
  1338. spin_lock_irqsave(&ioapic_lock, flags);
  1339. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
  1340. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
  1341. spin_unlock_irqrestore(&ioapic_lock, flags);
  1342. }
  1343. /*
  1344. * This code may look a bit paranoid, but it's supposed to cooperate with
  1345. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1346. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1347. * fanatically on his truly buggy board.
  1348. *
  1349. * FIXME: really need to revamp this for modern platforms only.
  1350. */
  1351. static inline void check_timer(void)
  1352. {
  1353. struct irq_cfg *cfg = irq_cfg + 0;
  1354. int apic1, pin1, apic2, pin2;
  1355. /*
  1356. * get/set the timer IRQ vector:
  1357. */
  1358. disable_8259A_irq(0);
  1359. assign_irq_vector(0, TARGET_CPUS);
  1360. /*
  1361. * Subtle, code in do_timer_interrupt() expects an AEOI
  1362. * mode for the 8259A whenever interrupts are routed
  1363. * through I/O APICs. Also IRQ0 has to be enabled in
  1364. * the 8259A which implies the virtual wire has to be
  1365. * disabled in the local APIC.
  1366. */
  1367. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1368. init_8259A(1);
  1369. if (timer_over_8254 > 0)
  1370. enable_8259A_irq(0);
  1371. pin1 = find_isa_irq_pin(0, mp_INT);
  1372. apic1 = find_isa_irq_apic(0, mp_INT);
  1373. pin2 = ioapic_i8259.pin;
  1374. apic2 = ioapic_i8259.apic;
  1375. apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1376. cfg->vector, apic1, pin1, apic2, pin2);
  1377. if (pin1 != -1) {
  1378. /*
  1379. * Ok, does IRQ0 through the IOAPIC work?
  1380. */
  1381. unmask_IO_APIC_irq(0);
  1382. if (!no_timer_check && timer_irq_works()) {
  1383. nmi_watchdog_default();
  1384. if (nmi_watchdog == NMI_IO_APIC) {
  1385. disable_8259A_irq(0);
  1386. setup_nmi();
  1387. enable_8259A_irq(0);
  1388. }
  1389. if (disable_timer_pin_1 > 0)
  1390. clear_IO_APIC_pin(0, pin1);
  1391. return;
  1392. }
  1393. clear_IO_APIC_pin(apic1, pin1);
  1394. apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
  1395. "connected to IO-APIC\n");
  1396. }
  1397. apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
  1398. "through the 8259A ... ");
  1399. if (pin2 != -1) {
  1400. apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
  1401. apic2, pin2);
  1402. /*
  1403. * legacy devices should be connected to IO APIC #0
  1404. */
  1405. setup_ExtINT_IRQ0_pin(apic2, pin2, cfg->vector);
  1406. if (timer_irq_works()) {
  1407. apic_printk(APIC_VERBOSE," works.\n");
  1408. nmi_watchdog_default();
  1409. if (nmi_watchdog == NMI_IO_APIC) {
  1410. setup_nmi();
  1411. }
  1412. return;
  1413. }
  1414. /*
  1415. * Cleanup, just in case ...
  1416. */
  1417. clear_IO_APIC_pin(apic2, pin2);
  1418. }
  1419. apic_printk(APIC_VERBOSE," failed.\n");
  1420. if (nmi_watchdog == NMI_IO_APIC) {
  1421. printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
  1422. nmi_watchdog = 0;
  1423. }
  1424. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1425. disable_8259A_irq(0);
  1426. irq_desc[0].chip = &lapic_irq_type;
  1427. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  1428. enable_8259A_irq(0);
  1429. if (timer_irq_works()) {
  1430. apic_printk(APIC_VERBOSE," works.\n");
  1431. return;
  1432. }
  1433. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  1434. apic_printk(APIC_VERBOSE," failed.\n");
  1435. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1436. init_8259A(0);
  1437. make_8259A_irq(0);
  1438. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1439. unlock_ExtINT_logic();
  1440. if (timer_irq_works()) {
  1441. apic_printk(APIC_VERBOSE," works.\n");
  1442. return;
  1443. }
  1444. apic_printk(APIC_VERBOSE," failed :(.\n");
  1445. panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
  1446. }
  1447. static int __init notimercheck(char *s)
  1448. {
  1449. no_timer_check = 1;
  1450. return 1;
  1451. }
  1452. __setup("no_timer_check", notimercheck);
  1453. /*
  1454. *
  1455. * IRQ's that are handled by the PIC in the MPS IOAPIC case.
  1456. * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1457. * Linux doesn't really care, as it's not actually used
  1458. * for any interrupt handling anyway.
  1459. */
  1460. #define PIC_IRQS (1<<2)
  1461. void __init setup_IO_APIC(void)
  1462. {
  1463. enable_IO_APIC();
  1464. if (acpi_ioapic)
  1465. io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
  1466. else
  1467. io_apic_irqs = ~PIC_IRQS;
  1468. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1469. sync_Arb_IDs();
  1470. setup_IO_APIC_irqs();
  1471. init_IO_APIC_traps();
  1472. check_timer();
  1473. if (!acpi_ioapic)
  1474. print_IO_APIC();
  1475. }
  1476. struct sysfs_ioapic_data {
  1477. struct sys_device dev;
  1478. struct IO_APIC_route_entry entry[0];
  1479. };
  1480. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1481. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1482. {
  1483. struct IO_APIC_route_entry *entry;
  1484. struct sysfs_ioapic_data *data;
  1485. int i;
  1486. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1487. entry = data->entry;
  1488. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  1489. *entry = ioapic_read_entry(dev->id, i);
  1490. return 0;
  1491. }
  1492. static int ioapic_resume(struct sys_device *dev)
  1493. {
  1494. struct IO_APIC_route_entry *entry;
  1495. struct sysfs_ioapic_data *data;
  1496. unsigned long flags;
  1497. union IO_APIC_reg_00 reg_00;
  1498. int i;
  1499. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1500. entry = data->entry;
  1501. spin_lock_irqsave(&ioapic_lock, flags);
  1502. reg_00.raw = io_apic_read(dev->id, 0);
  1503. if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
  1504. reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
  1505. io_apic_write(dev->id, 0, reg_00.raw);
  1506. }
  1507. spin_unlock_irqrestore(&ioapic_lock, flags);
  1508. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1509. ioapic_write_entry(dev->id, i, entry[i]);
  1510. return 0;
  1511. }
  1512. static struct sysdev_class ioapic_sysdev_class = {
  1513. set_kset_name("ioapic"),
  1514. .suspend = ioapic_suspend,
  1515. .resume = ioapic_resume,
  1516. };
  1517. static int __init ioapic_init_sysfs(void)
  1518. {
  1519. struct sys_device * dev;
  1520. int i, size, error = 0;
  1521. error = sysdev_class_register(&ioapic_sysdev_class);
  1522. if (error)
  1523. return error;
  1524. for (i = 0; i < nr_ioapics; i++ ) {
  1525. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1526. * sizeof(struct IO_APIC_route_entry);
  1527. mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
  1528. if (!mp_ioapic_data[i]) {
  1529. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1530. continue;
  1531. }
  1532. memset(mp_ioapic_data[i], 0, size);
  1533. dev = &mp_ioapic_data[i]->dev;
  1534. dev->id = i;
  1535. dev->cls = &ioapic_sysdev_class;
  1536. error = sysdev_register(dev);
  1537. if (error) {
  1538. kfree(mp_ioapic_data[i]);
  1539. mp_ioapic_data[i] = NULL;
  1540. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1541. continue;
  1542. }
  1543. }
  1544. return 0;
  1545. }
  1546. device_initcall(ioapic_init_sysfs);
  1547. /*
  1548. * Dynamic irq allocate and deallocation
  1549. */
  1550. int create_irq(void)
  1551. {
  1552. /* Allocate an unused irq */
  1553. int irq;
  1554. int new;
  1555. unsigned long flags;
  1556. irq = -ENOSPC;
  1557. spin_lock_irqsave(&vector_lock, flags);
  1558. for (new = (NR_IRQS - 1); new >= 0; new--) {
  1559. if (platform_legacy_irq(new))
  1560. continue;
  1561. if (irq_cfg[new].vector != 0)
  1562. continue;
  1563. if (__assign_irq_vector(new, TARGET_CPUS) == 0)
  1564. irq = new;
  1565. break;
  1566. }
  1567. spin_unlock_irqrestore(&vector_lock, flags);
  1568. if (irq >= 0) {
  1569. dynamic_irq_init(irq);
  1570. }
  1571. return irq;
  1572. }
  1573. void destroy_irq(unsigned int irq)
  1574. {
  1575. unsigned long flags;
  1576. dynamic_irq_cleanup(irq);
  1577. spin_lock_irqsave(&vector_lock, flags);
  1578. __clear_irq_vector(irq);
  1579. spin_unlock_irqrestore(&vector_lock, flags);
  1580. }
  1581. /*
  1582. * MSI mesage composition
  1583. */
  1584. #ifdef CONFIG_PCI_MSI
  1585. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1586. {
  1587. struct irq_cfg *cfg = irq_cfg + irq;
  1588. int err;
  1589. unsigned dest;
  1590. cpumask_t tmp;
  1591. tmp = TARGET_CPUS;
  1592. err = assign_irq_vector(irq, tmp);
  1593. if (!err) {
  1594. cpus_and(tmp, cfg->domain, tmp);
  1595. dest = cpu_mask_to_apicid(tmp);
  1596. msg->address_hi = MSI_ADDR_BASE_HI;
  1597. msg->address_lo =
  1598. MSI_ADDR_BASE_LO |
  1599. ((INT_DEST_MODE == 0) ?
  1600. MSI_ADDR_DEST_MODE_PHYSICAL:
  1601. MSI_ADDR_DEST_MODE_LOGICAL) |
  1602. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1603. MSI_ADDR_REDIRECTION_CPU:
  1604. MSI_ADDR_REDIRECTION_LOWPRI) |
  1605. MSI_ADDR_DEST_ID(dest);
  1606. msg->data =
  1607. MSI_DATA_TRIGGER_EDGE |
  1608. MSI_DATA_LEVEL_ASSERT |
  1609. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1610. MSI_DATA_DELIVERY_FIXED:
  1611. MSI_DATA_DELIVERY_LOWPRI) |
  1612. MSI_DATA_VECTOR(cfg->vector);
  1613. }
  1614. return err;
  1615. }
  1616. #ifdef CONFIG_SMP
  1617. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  1618. {
  1619. struct irq_cfg *cfg = irq_cfg + irq;
  1620. struct msi_msg msg;
  1621. unsigned int dest;
  1622. cpumask_t tmp;
  1623. cpus_and(tmp, mask, cpu_online_map);
  1624. if (cpus_empty(tmp))
  1625. return;
  1626. if (assign_irq_vector(irq, mask))
  1627. return;
  1628. cpus_and(tmp, cfg->domain, mask);
  1629. dest = cpu_mask_to_apicid(tmp);
  1630. read_msi_msg(irq, &msg);
  1631. msg.data &= ~MSI_DATA_VECTOR_MASK;
  1632. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  1633. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  1634. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  1635. write_msi_msg(irq, &msg);
  1636. irq_desc[irq].affinity = mask;
  1637. }
  1638. #endif /* CONFIG_SMP */
  1639. /*
  1640. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  1641. * which implement the MSI or MSI-X Capability Structure.
  1642. */
  1643. static struct irq_chip msi_chip = {
  1644. .name = "PCI-MSI",
  1645. .unmask = unmask_msi_irq,
  1646. .mask = mask_msi_irq,
  1647. .ack = ack_apic_edge,
  1648. #ifdef CONFIG_SMP
  1649. .set_affinity = set_msi_irq_affinity,
  1650. #endif
  1651. .retrigger = ioapic_retrigger_irq,
  1652. };
  1653. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  1654. {
  1655. struct msi_msg msg;
  1656. int irq, ret;
  1657. irq = create_irq();
  1658. if (irq < 0)
  1659. return irq;
  1660. set_irq_msi(irq, desc);
  1661. ret = msi_compose_msg(dev, irq, &msg);
  1662. if (ret < 0) {
  1663. destroy_irq(irq);
  1664. return ret;
  1665. }
  1666. write_msi_msg(irq, &msg);
  1667. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  1668. return irq;
  1669. }
  1670. void arch_teardown_msi_irq(unsigned int irq)
  1671. {
  1672. destroy_irq(irq);
  1673. }
  1674. #endif /* CONFIG_PCI_MSI */
  1675. /*
  1676. * Hypertransport interrupt support
  1677. */
  1678. #ifdef CONFIG_HT_IRQ
  1679. #ifdef CONFIG_SMP
  1680. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  1681. {
  1682. struct ht_irq_msg msg;
  1683. fetch_ht_irq_msg(irq, &msg);
  1684. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  1685. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  1686. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  1687. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  1688. write_ht_irq_msg(irq, &msg);
  1689. }
  1690. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  1691. {
  1692. struct irq_cfg *cfg = irq_cfg + irq;
  1693. unsigned int dest;
  1694. cpumask_t tmp;
  1695. cpus_and(tmp, mask, cpu_online_map);
  1696. if (cpus_empty(tmp))
  1697. return;
  1698. if (assign_irq_vector(irq, mask))
  1699. return;
  1700. cpus_and(tmp, cfg->domain, mask);
  1701. dest = cpu_mask_to_apicid(tmp);
  1702. target_ht_irq(irq, dest, cfg->vector);
  1703. irq_desc[irq].affinity = mask;
  1704. }
  1705. #endif
  1706. static struct irq_chip ht_irq_chip = {
  1707. .name = "PCI-HT",
  1708. .mask = mask_ht_irq,
  1709. .unmask = unmask_ht_irq,
  1710. .ack = ack_apic_edge,
  1711. #ifdef CONFIG_SMP
  1712. .set_affinity = set_ht_irq_affinity,
  1713. #endif
  1714. .retrigger = ioapic_retrigger_irq,
  1715. };
  1716. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  1717. {
  1718. struct irq_cfg *cfg = irq_cfg + irq;
  1719. int err;
  1720. cpumask_t tmp;
  1721. tmp = TARGET_CPUS;
  1722. err = assign_irq_vector(irq, tmp);
  1723. if (!err) {
  1724. struct ht_irq_msg msg;
  1725. unsigned dest;
  1726. cpus_and(tmp, cfg->domain, tmp);
  1727. dest = cpu_mask_to_apicid(tmp);
  1728. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  1729. msg.address_lo =
  1730. HT_IRQ_LOW_BASE |
  1731. HT_IRQ_LOW_DEST_ID(dest) |
  1732. HT_IRQ_LOW_VECTOR(cfg->vector) |
  1733. ((INT_DEST_MODE == 0) ?
  1734. HT_IRQ_LOW_DM_PHYSICAL :
  1735. HT_IRQ_LOW_DM_LOGICAL) |
  1736. HT_IRQ_LOW_RQEOI_EDGE |
  1737. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1738. HT_IRQ_LOW_MT_FIXED :
  1739. HT_IRQ_LOW_MT_ARBITRATED) |
  1740. HT_IRQ_LOW_IRQ_MASKED;
  1741. write_ht_irq_msg(irq, &msg);
  1742. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  1743. handle_edge_irq, "edge");
  1744. }
  1745. return err;
  1746. }
  1747. #endif /* CONFIG_HT_IRQ */
  1748. /* --------------------------------------------------------------------------
  1749. ACPI-based IOAPIC Configuration
  1750. -------------------------------------------------------------------------- */
  1751. #ifdef CONFIG_ACPI
  1752. #define IO_APIC_MAX_ID 0xFE
  1753. int __init io_apic_get_redir_entries (int ioapic)
  1754. {
  1755. union IO_APIC_reg_01 reg_01;
  1756. unsigned long flags;
  1757. spin_lock_irqsave(&ioapic_lock, flags);
  1758. reg_01.raw = io_apic_read(ioapic, 1);
  1759. spin_unlock_irqrestore(&ioapic_lock, flags);
  1760. return reg_01.bits.entries;
  1761. }
  1762. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  1763. {
  1764. if (!IO_APIC_IRQ(irq)) {
  1765. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  1766. ioapic);
  1767. return -EINVAL;
  1768. }
  1769. /*
  1770. * IRQs < 16 are already in the irq_2_pin[] map
  1771. */
  1772. if (irq >= 16)
  1773. add_pin_to_irq(irq, ioapic, pin);
  1774. setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
  1775. return 0;
  1776. }
  1777. #endif /* CONFIG_ACPI */
  1778. /*
  1779. * This function currently is only a helper for the i386 smp boot process where
  1780. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  1781. * so mask in all cases should simply be TARGET_CPUS
  1782. */
  1783. #ifdef CONFIG_SMP
  1784. void __init setup_ioapic_dest(void)
  1785. {
  1786. int pin, ioapic, irq, irq_entry;
  1787. if (skip_ioapic_setup == 1)
  1788. return;
  1789. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  1790. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  1791. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  1792. if (irq_entry == -1)
  1793. continue;
  1794. irq = pin_2_irq(irq_entry, ioapic, pin);
  1795. /* setup_IO_APIC_irqs could fail to get vector for some device
  1796. * when you have too many devices, because at that time only boot
  1797. * cpu is online.
  1798. */
  1799. if (!irq_cfg[irq].vector)
  1800. setup_IO_APIC_irq(ioapic, pin, irq,
  1801. irq_trigger(irq_entry),
  1802. irq_polarity(irq_entry));
  1803. else
  1804. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  1805. }
  1806. }
  1807. }
  1808. #endif