io_apic.c 47 KB

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