io_apic.c 53 KB

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