io_apic.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:41k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  */
  21. #include <linux/mm.h>
  22. #include <linux/irq.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/sched.h>
  27. #include <linux/config.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/mc146818rtc.h>
  30. #include <asm/io.h>
  31. #include <asm/smp.h>
  32. #include <asm/desc.h>
  33. #undef APIC_LOCKUP_DEBUG
  34. #define APIC_LOCKUP_DEBUG
  35. static spinlock_t ioapic_lock = SPIN_LOCK_UNLOCKED;
  36. /*
  37.  * # of IRQ routing registers
  38.  */
  39. int nr_ioapic_registers[MAX_IO_APICS];
  40. /*
  41.  * Rough estimation of how many shared IRQs there are, can
  42.  * be changed anytime.
  43.  */
  44. #define MAX_PLUS_SHARED_IRQS NR_IRQS
  45. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  46. /*
  47.  * This is performance-critical, we want to do it O(1)
  48.  *
  49.  * the indexing order of this array favors 1:1 mappings
  50.  * between pins and IRQs.
  51.  */
  52. static struct irq_pin_list {
  53. int apic, pin, next;
  54. } irq_2_pin[PIN_MAP_SIZE];
  55. /*
  56.  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  57.  * shared ISA-space IRQs, so we have to support them. We are super
  58.  * fast in the common case, and fast for shared ISA-space IRQs.
  59.  */
  60. static void __init add_pin_to_irq(unsigned int irq, int apic, int pin)
  61. {
  62. static int first_free_entry = NR_IRQS;
  63. struct irq_pin_list *entry = irq_2_pin + irq;
  64. while (entry->next)
  65. entry = irq_2_pin + entry->next;
  66. if (entry->pin != -1) {
  67. entry->next = first_free_entry;
  68. entry = irq_2_pin + entry->next;
  69. if (++first_free_entry >= PIN_MAP_SIZE)
  70. panic("io_apic.c: whoops");
  71. }
  72. entry->apic = apic;
  73. entry->pin = pin;
  74. }
  75. /*
  76.  * Reroute an IRQ to a different pin.
  77.  */
  78. static void __init replace_pin_at_irq(unsigned int irq,
  79.       int oldapic, int oldpin,
  80.       int newapic, int newpin)
  81. {
  82. struct irq_pin_list *entry = irq_2_pin + irq;
  83. while (1) {
  84. if (entry->apic == oldapic && entry->pin == oldpin) {
  85. entry->apic = newapic;
  86. entry->pin = newpin;
  87. }
  88. if (!entry->next)
  89. break;
  90. entry = irq_2_pin + entry->next;
  91. }
  92. }
  93. #define __DO_ACTION(R, ACTION, FINAL)
  94. {
  95. int pin;
  96. struct irq_pin_list *entry = irq_2_pin + irq;
  97. for (;;) {
  98. unsigned int reg;
  99. pin = entry->pin;
  100. if (pin == -1)
  101. break;
  102. reg = io_apic_read(entry->apic, 0x10 + R + pin*2);
  103. reg ACTION;
  104. io_apic_modify(entry->apic, reg);
  105. if (!entry->next)
  106. break;
  107. entry = irq_2_pin + entry->next;
  108. }
  109. FINAL;
  110. }
  111. #define DO_ACTION(name,R,ACTION, FINAL)
  112. static void name##_IO_APIC_irq (unsigned int irq)
  113. __DO_ACTION(R, ACTION, FINAL)
  114. DO_ACTION( __mask,             0, |= 0x00010000, io_apic_sync(entry->apic) )
  115. /* mask = 1 */
  116. DO_ACTION( __unmask,           0, &= 0xfffeffff, )
  117. /* mask = 0 */
  118. DO_ACTION( __mask_and_edge,    0, = (reg & 0xffff7fff) | 0x00010000, )
  119. /* mask = 1, trigger = 0 */
  120. DO_ACTION( __unmask_and_level, 0, = (reg & 0xfffeffff) | 0x00008000, )
  121. /* mask = 0, trigger = 1 */
  122. static void mask_IO_APIC_irq (unsigned int irq)
  123. {
  124. unsigned long flags;
  125. spin_lock_irqsave(&ioapic_lock, flags);
  126. __mask_IO_APIC_irq(irq);
  127. spin_unlock_irqrestore(&ioapic_lock, flags);
  128. }
  129. static void unmask_IO_APIC_irq (unsigned int irq)
  130. {
  131. unsigned long flags;
  132. spin_lock_irqsave(&ioapic_lock, flags);
  133. __unmask_IO_APIC_irq(irq);
  134. spin_unlock_irqrestore(&ioapic_lock, flags);
  135. }
  136. void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  137. {
  138. struct IO_APIC_route_entry entry;
  139. unsigned long flags;
  140. /*
  141.  * Disable it in the IO-APIC irq-routing table:
  142.  */
  143. memset(&entry, 0, sizeof(entry));
  144. entry.mask = 1;
  145. spin_lock_irqsave(&ioapic_lock, flags);
  146. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
  147. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
  148. spin_unlock_irqrestore(&ioapic_lock, flags);
  149. }
  150. static void clear_IO_APIC (void)
  151. {
  152. int apic, pin;
  153. for (apic = 0; apic < nr_ioapics; apic++)
  154. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  155. clear_IO_APIC_pin(apic, pin);
  156. }
  157. /*
  158.  * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  159.  * specific CPU-side IRQs.
  160.  */
  161. #define MAX_PIRQS 8
  162. int pirq_entries [MAX_PIRQS];
  163. int pirqs_enabled;
  164. int skip_ioapic_setup;
  165. static int __init noioapic_setup(char *str)
  166. {
  167. skip_ioapic_setup = 1;
  168. return 1;
  169. }
  170. __setup("noapic", noioapic_setup);
  171. static int __init ioapic_setup(char *str)
  172. {
  173. skip_ioapic_setup = 0;
  174. return 1;
  175. }
  176. __setup("apic", ioapic_setup);
  177. static int __init ioapic_pirq_setup(char *str)
  178. {
  179. int i, max;
  180. int ints[MAX_PIRQS+1];
  181. get_options(str, ARRAY_SIZE(ints), ints);
  182. for (i = 0; i < MAX_PIRQS; i++)
  183. pirq_entries[i] = -1;
  184. pirqs_enabled = 1;
  185. printk(KERN_INFO "PIRQ redirection, working around broken MP-BIOS.n");
  186. max = MAX_PIRQS;
  187. if (ints[0] < MAX_PIRQS)
  188. max = ints[0];
  189. for (i = 0; i < max; i++) {
  190. printk(KERN_DEBUG "... PIRQ%d -> IRQ %dn", i, ints[i+1]);
  191. /*
  192.  * PIRQs are mapped upside down, usually.
  193.  */
  194. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  195. }
  196. return 1;
  197. }
  198. __setup("pirq=", ioapic_pirq_setup);
  199. /*
  200.  * Find the IRQ entry number of a certain pin.
  201.  */
  202. static int __init find_irq_entry(int apic, int pin, int type)
  203. {
  204. int i;
  205. for (i = 0; i < mp_irq_entries; i++)
  206. if (mp_irqs[i].mpc_irqtype == type &&
  207.     (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
  208.      mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
  209.     mp_irqs[i].mpc_dstirq == pin)
  210. return i;
  211. return -1;
  212. }
  213. /*
  214.  * Find the pin to which IRQ[irq] (ISA) is connected
  215.  */
  216. static int __init find_isa_irq_pin(int irq, int type)
  217. {
  218. int i;
  219. for (i = 0; i < mp_irq_entries; i++) {
  220. int lbus = mp_irqs[i].mpc_srcbus;
  221. if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
  222.      mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
  223.      mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
  224.     (mp_irqs[i].mpc_irqtype == type) &&
  225.     (mp_irqs[i].mpc_srcbusirq == irq))
  226. return mp_irqs[i].mpc_dstirq;
  227. }
  228. return -1;
  229. }
  230. /*
  231.  * Find a specific PCI IRQ entry.
  232.  * Not an __init, possibly needed by modules
  233.  */
  234. static int pin_2_irq(int idx, int apic, int pin);
  235. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  236. {
  237. int apic, i, best_guess = -1;
  238. Dprintk("querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.n",
  239. bus, slot, pin);
  240. if (mp_bus_id_to_pci_bus[bus] == -1) {
  241. printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!n", bus);
  242. return -1;
  243. }
  244. for (i = 0; i < mp_irq_entries; i++) {
  245. int lbus = mp_irqs[i].mpc_srcbus;
  246. for (apic = 0; apic < nr_ioapics; apic++)
  247. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
  248.     mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
  249. break;
  250. if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
  251.     !mp_irqs[i].mpc_irqtype &&
  252.     (bus == lbus) &&
  253.     (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
  254. int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
  255. if (!(apic || IO_APIC_IRQ(irq)))
  256. continue;
  257. if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
  258. return irq;
  259. /*
  260.  * Use the first all-but-pin matching entry as a
  261.  * best-guess fuzzy result for broken mptables.
  262.  */
  263. if (best_guess < 0)
  264. best_guess = irq;
  265. }
  266. }
  267. return best_guess;
  268. }
  269. /*
  270.  * EISA Edge/Level control register, ELCR
  271.  */
  272. static int __init EISA_ELCR(unsigned int irq)
  273. {
  274. if (irq < 16) {
  275. unsigned int port = 0x4d0 + (irq >> 3);
  276. return (inb(port) >> (irq & 7)) & 1;
  277. }
  278. printk(KERN_INFO "Broken MPtable reports ISA irq %dn", irq);
  279. return 0;
  280. }
  281. /* EISA interrupts are always polarity zero and can be edge or level
  282.  * trigger depending on the ELCR value.  If an interrupt is listed as
  283.  * EISA conforming in the MP table, that means its trigger type must
  284.  * be read in from the ELCR */
  285. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
  286. #define default_EISA_polarity(idx) (0)
  287. /* ISA interrupts are always polarity zero edge triggered,
  288.  * when listed as conforming in the MP table. */
  289. #define default_ISA_trigger(idx) (0)
  290. #define default_ISA_polarity(idx) (0)
  291. /* PCI interrupts are always polarity one level triggered,
  292.  * when listed as conforming in the MP table. */
  293. #define default_PCI_trigger(idx) (1)
  294. #define default_PCI_polarity(idx) (1)
  295. /* MCA interrupts are always polarity zero level triggered,
  296.  * when listed as conforming in the MP table. */
  297. #define default_MCA_trigger(idx) (1)
  298. #define default_MCA_polarity(idx) (0)
  299. static int __init MPBIOS_polarity(int idx)
  300. {
  301. int bus = mp_irqs[idx].mpc_srcbus;
  302. int polarity;
  303. /*
  304.  * Determine IRQ line polarity (high active or low active):
  305.  */
  306. switch (mp_irqs[idx].mpc_irqflag & 3)
  307. {
  308. case 0: /* conforms, ie. bus-type dependent polarity */
  309. {
  310. switch (mp_bus_id_to_type[bus])
  311. {
  312. case MP_BUS_ISA: /* ISA pin */
  313. {
  314. polarity = default_ISA_polarity(idx);
  315. break;
  316. }
  317. case MP_BUS_EISA: /* EISA pin */
  318. {
  319. polarity = default_EISA_polarity(idx);
  320. break;
  321. }
  322. case MP_BUS_PCI: /* PCI pin */
  323. {
  324. polarity = default_PCI_polarity(idx);
  325. break;
  326. }
  327. case MP_BUS_MCA: /* MCA pin */
  328. {
  329. polarity = default_MCA_polarity(idx);
  330. break;
  331. }
  332. default:
  333. {
  334. printk(KERN_WARNING "broken BIOS!!n");
  335. polarity = 1;
  336. break;
  337. }
  338. }
  339. break;
  340. }
  341. case 1: /* high active */
  342. {
  343. polarity = 0;
  344. break;
  345. }
  346. case 2: /* reserved */
  347. {
  348. printk(KERN_WARNING "broken BIOS!!n");
  349. polarity = 1;
  350. break;
  351. }
  352. case 3: /* low active */
  353. {
  354. polarity = 1;
  355. break;
  356. }
  357. default: /* invalid */
  358. {
  359. printk(KERN_WARNING "broken BIOS!!n");
  360. polarity = 1;
  361. break;
  362. }
  363. }
  364. return polarity;
  365. }
  366. static int __init MPBIOS_trigger(int idx)
  367. {
  368. int bus = mp_irqs[idx].mpc_srcbus;
  369. int trigger;
  370. /*
  371.  * Determine IRQ trigger mode (edge or level sensitive):
  372.  */
  373. switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
  374. {
  375. case 0: /* conforms, ie. bus-type dependent */
  376. {
  377. switch (mp_bus_id_to_type[bus])
  378. {
  379. case MP_BUS_ISA: /* ISA pin */
  380. {
  381. trigger = default_ISA_trigger(idx);
  382. break;
  383. }
  384. case MP_BUS_EISA: /* EISA pin */
  385. {
  386. trigger = default_EISA_trigger(idx);
  387. break;
  388. }
  389. case MP_BUS_PCI: /* PCI pin */
  390. {
  391. trigger = default_PCI_trigger(idx);
  392. break;
  393. }
  394. case MP_BUS_MCA: /* MCA pin */
  395. {
  396. trigger = default_MCA_trigger(idx);
  397. break;
  398. }
  399. default:
  400. {
  401. printk(KERN_WARNING "broken BIOS!!n");
  402. trigger = 1;
  403. break;
  404. }
  405. }
  406. break;
  407. }
  408. case 1: /* edge */
  409. {
  410. trigger = 0;
  411. break;
  412. }
  413. case 2: /* reserved */
  414. {
  415. printk(KERN_WARNING "broken BIOS!!n");
  416. trigger = 1;
  417. break;
  418. }
  419. case 3: /* level */
  420. {
  421. trigger = 1;
  422. break;
  423. }
  424. default: /* invalid */
  425. {
  426. printk(KERN_WARNING "broken BIOS!!n");
  427. trigger = 0;
  428. break;
  429. }
  430. }
  431. return trigger;
  432. }
  433. static inline int irq_polarity(int idx)
  434. {
  435. return MPBIOS_polarity(idx);
  436. }
  437. static inline int irq_trigger(int idx)
  438. {
  439. return MPBIOS_trigger(idx);
  440. }
  441. static int pin_2_irq(int idx, int apic, int pin)
  442. {
  443. int irq, i;
  444. int bus = mp_irqs[idx].mpc_srcbus;
  445. /*
  446.  * Debugging check, we are in big trouble if this message pops up!
  447.  */
  448. if (mp_irqs[idx].mpc_dstirq != pin)
  449. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!n");
  450. switch (mp_bus_id_to_type[bus])
  451. {
  452. case MP_BUS_ISA: /* ISA pin */
  453. case MP_BUS_EISA:
  454. case MP_BUS_MCA:
  455. {
  456. irq = mp_irqs[idx].mpc_srcbusirq;
  457. break;
  458. }
  459. case MP_BUS_PCI: /* PCI pin */
  460. {
  461. /*
  462.  * PCI IRQs are mapped in order
  463.  */
  464. i = irq = 0;
  465. while (i < apic)
  466. irq += nr_ioapic_registers[i++];
  467. irq += pin;
  468. break;
  469. }
  470. default:
  471. {
  472. printk(KERN_ERR "unknown bus type %d.n",bus); 
  473. irq = 0;
  474. break;
  475. }
  476. }
  477. /*
  478.  * PCI IRQ command line redirection. Yes, limits are hardcoded.
  479.  */
  480. if ((pin >= 16) && (pin <= 23)) {
  481. if (pirq_entries[pin-16] != -1) {
  482. if (!pirq_entries[pin-16]) {
  483. printk(KERN_DEBUG "disabling PIRQ%dn", pin-16);
  484. } else {
  485. irq = pirq_entries[pin-16];
  486. printk(KERN_DEBUG "using PIRQ%d -> IRQ %dn",
  487. pin-16, irq);
  488. }
  489. }
  490. }
  491. return irq;
  492. }
  493. static inline int IO_APIC_irq_trigger(int irq)
  494. {
  495. int apic, idx, pin;
  496. for (apic = 0; apic < nr_ioapics; apic++) {
  497. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  498. idx = find_irq_entry(apic,pin,mp_INT);
  499. if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
  500. return irq_trigger(idx);
  501. }
  502. }
  503. /*
  504.  * nonexistent IRQs are edge default
  505.  */
  506. return 0;
  507. }
  508. int irq_vector[NR_IRQS] = { FIRST_DEVICE_VECTOR , 0 };
  509. static int __init assign_irq_vector(int irq)
  510. {
  511. static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
  512. if (IO_APIC_VECTOR(irq) > 0)
  513. return IO_APIC_VECTOR(irq);
  514. next:
  515. current_vector += 8;
  516. if (current_vector == IA32_SYSCALL_VECTOR)
  517. goto next;
  518. if (current_vector > FIRST_SYSTEM_VECTOR) {
  519. offset++;
  520. current_vector = FIRST_DEVICE_VECTOR + offset;
  521. }
  522. if (current_vector == FIRST_SYSTEM_VECTOR)
  523. panic("ran out of interrupt sources!");
  524. IO_APIC_VECTOR(irq) = current_vector;
  525. return current_vector;
  526. }
  527. extern void (*interrupt[NR_IRQS])(void);
  528. static struct hw_interrupt_type ioapic_level_irq_type;
  529. static struct hw_interrupt_type ioapic_edge_irq_type;
  530. void __init setup_IO_APIC_irqs(void)
  531. {
  532. struct IO_APIC_route_entry entry;
  533. int apic, pin, idx, irq, first_notcon = 1, vector;
  534. unsigned long flags;
  535. printk(KERN_DEBUG "init IO_APIC IRQsn");
  536. for (apic = 0; apic < nr_ioapics; apic++) {
  537. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  538. /*
  539.  * add it to the IO-APIC irq-routing table:
  540.  */
  541. memset(&entry,0,sizeof(entry));
  542. entry.delivery_mode = dest_LowestPrio;
  543. entry.dest_mode = INT_DELIVERY_MODE;
  544. entry.mask = 0; /* enable IRQ */
  545. entry.dest.logical.logical_dest = TARGET_CPUS;
  546. idx = find_irq_entry(apic,pin,mp_INT);
  547. if (idx == -1) {
  548. if (first_notcon) {
  549. printk(KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  550. first_notcon = 0;
  551. } else
  552. printk(", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  553. continue;
  554. }
  555. entry.trigger = irq_trigger(idx);
  556. entry.polarity = irq_polarity(idx);
  557. if (irq_trigger(idx)) {
  558. entry.trigger = 1;
  559. entry.mask = 1;
  560. entry.dest.logical.logical_dest = TARGET_CPUS;
  561. }
  562. irq = pin_2_irq(idx, apic, pin);
  563. add_pin_to_irq(irq, apic, pin);
  564. if (!apic && !IO_APIC_IRQ(irq))
  565. continue;
  566. if (IO_APIC_IRQ(irq)) {
  567. vector = assign_irq_vector(irq);
  568. entry.vector = vector;
  569. if (IO_APIC_irq_trigger(irq))
  570. irq_desc[irq].handler = &ioapic_level_irq_type;
  571. else
  572. irq_desc[irq].handler = &ioapic_edge_irq_type;
  573. set_intr_gate(vector, interrupt[irq]);
  574. if (!apic && (irq < 16))
  575. disable_8259A_irq(irq);
  576. }
  577. spin_lock_irqsave(&ioapic_lock, flags);
  578. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  579. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  580. spin_unlock_irqrestore(&ioapic_lock, flags);
  581. }
  582. }
  583. if (!first_notcon)
  584. printk(" not connected.n");
  585. }
  586. /*
  587.  * Set up the 8259A-master output pin as broadcast to all
  588.  * CPUs.
  589.  */
  590. void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
  591. {
  592. struct IO_APIC_route_entry entry;
  593. unsigned long flags;
  594. memset(&entry,0,sizeof(entry));
  595. disable_8259A_irq(0);
  596. /* mask LVT0 */
  597. apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  598. /*
  599.  * We use logical delivery to get the timer IRQ
  600.  * to the first CPU.
  601.  */
  602. entry.dest_mode = INT_DELIVERY_MODE;
  603. entry.mask = 0; /* unmask IRQ now */
  604. entry.dest.logical.logical_dest = TARGET_CPUS;
  605. entry.delivery_mode = dest_LowestPrio;
  606. entry.polarity = 0;
  607. entry.trigger = 0;
  608. entry.vector = vector;
  609. /*
  610.  * The timer IRQ doesnt have to know that behind the
  611.  * scene we have a 8259A-master in AEOI mode ...
  612.  */
  613. irq_desc[0].handler = &ioapic_edge_irq_type;
  614. /*
  615.  * Add it to the IO-APIC irq-routing table:
  616.  */
  617. spin_lock_irqsave(&ioapic_lock, flags);
  618. io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
  619. io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
  620. spin_unlock_irqrestore(&ioapic_lock, flags);
  621. enable_8259A_irq(0);
  622. }
  623. void __init UNEXPECTED_IO_APIC(void)
  624. {
  625. printk(KERN_WARNING " WARNING: unexpected IO-APIC, please mailn");
  626. printk(KERN_WARNING "          to linux-smp@vger.kernel.orgn");
  627. }
  628. void __init print_IO_APIC(void)
  629. {
  630. int apic, i;
  631. struct IO_APIC_reg_00 reg_00;
  632. struct IO_APIC_reg_01 reg_01;
  633. struct IO_APIC_reg_02 reg_02;
  634. unsigned long flags;
  635.   printk(KERN_DEBUG "number of MP IRQ sources: %d.n", mp_irq_entries);
  636. for (i = 0; i < nr_ioapics; i++)
  637. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.n",
  638.        mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  639. /*
  640.  * We are a bit conservative about what we expect.  We have to
  641.  * know about every hardware change ASAP.
  642.  */
  643. printk(KERN_INFO "testing the IO APIC.......................n");
  644. for (apic = 0; apic < nr_ioapics; apic++) {
  645. spin_lock_irqsave(&ioapic_lock, flags);
  646. *(int *)&reg_00 = io_apic_read(apic, 0);
  647. *(int *)&reg_01 = io_apic_read(apic, 1);
  648. if (reg_01.version >= 0x10)
  649. *(int *)&reg_02 = io_apic_read(apic, 2);
  650. spin_unlock_irqrestore(&ioapic_lock, flags);
  651. printk("n");
  652. printk(KERN_DEBUG "IO APIC #%d......n", mp_ioapics[apic].mpc_apicid);
  653. printk(KERN_DEBUG ".... register #00: %08Xn", *(int *)&reg_00);
  654. printk(KERN_DEBUG ".......    : physical APIC id: %02Xn", reg_00.ID);
  655. if (reg_00.__reserved_1 || reg_00.__reserved_2)
  656. UNEXPECTED_IO_APIC();
  657. printk(KERN_DEBUG ".... register #01: %08Xn", *(int *)&reg_01);
  658. printk(KERN_DEBUG ".......     : max redirection entries: %04Xn", reg_01.entries);
  659. if ( (reg_01.entries != 0x0f) && /* older (Neptune) boards */
  660. (reg_01.entries != 0x17) && /* typical ISA+PCI boards */
  661. (reg_01.entries != 0x1b) && /* Compaq Proliant boards */
  662. (reg_01.entries != 0x1f) && /* dual Xeon boards */
  663. (reg_01.entries != 0x22) && /* bigger Xeon boards */
  664. (reg_01.entries != 0x2E) &&
  665. (reg_01.entries != 0x3F) &&
  666. (reg_01.entries != 0x03)    /* Golem */
  667. )
  668. UNEXPECTED_IO_APIC();
  669. printk(KERN_DEBUG ".......     : PRQ implemented: %Xn", reg_01.PRQ);
  670. printk(KERN_DEBUG ".......     : IO APIC version: %04Xn", reg_01.version);
  671. if ( (reg_01.version != 0x01) && /* 82489DX IO-APICs */
  672. (reg_01.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
  673. (reg_01.version != 0x10) && /* oldest IO-APICs */
  674. (reg_01.version != 0x11) && /* Pentium/Pro IO-APICs / GOLEM */
  675. (reg_01.version != 0x13) && /* Xeon IO-APICs */
  676. (reg_01.version != 0x20)    /* Intel P64H (82806 AA) */
  677. )
  678. UNEXPECTED_IO_APIC();
  679. if (reg_01.__reserved_1 || reg_01.__reserved_2)
  680. UNEXPECTED_IO_APIC();
  681. if (reg_01.version >= 0x10) {
  682. printk(KERN_DEBUG ".... register #02: %08Xn", *(int *)&reg_02);
  683. printk(KERN_DEBUG ".......     : arbitration: %02Xn", reg_02.arbitration);
  684. if (reg_02.__reserved_1 || reg_02.__reserved_2)
  685. UNEXPECTED_IO_APIC();
  686. }
  687. printk(KERN_DEBUG ".... IRQ redirection table:n");
  688. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  689.   " Stat Dest Deli Vect:   n");
  690. for (i = 0; i <= reg_01.entries; i++) {
  691. struct IO_APIC_route_entry entry;
  692. spin_lock_irqsave(&ioapic_lock, flags);
  693. *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
  694. *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
  695. spin_unlock_irqrestore(&ioapic_lock, flags);
  696. printk(KERN_DEBUG " %02x %03X %02X  ",
  697. i,
  698. entry.dest.logical.logical_dest,
  699. entry.dest.physical.physical_dest
  700. );
  701. printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02Xn",
  702. entry.mask,
  703. entry.trigger,
  704. entry.irr,
  705. entry.polarity,
  706. entry.delivery_status,
  707. entry.dest_mode,
  708. entry.delivery_mode,
  709. entry.vector
  710. );
  711. }
  712. }
  713. printk(KERN_DEBUG "IRQ to pin mappings:n");
  714. for (i = 0; i < NR_IRQS; i++) {
  715. struct irq_pin_list *entry = irq_2_pin + i;
  716. if (entry->pin < 0)
  717. continue;
  718. printk(KERN_DEBUG "IRQ%d ", i);
  719. for (;;) {
  720. printk("-> %d:%d", entry->apic, entry->pin);
  721. if (!entry->next)
  722. break;
  723. entry = irq_2_pin + entry->next;
  724. }
  725. printk("n");
  726. }
  727. printk(KERN_INFO ".................................... done.n");
  728. return;
  729. }
  730. static void print_APIC_bitfield (int base)
  731. {
  732. unsigned int v;
  733. int i, j;
  734. printk(KERN_DEBUG "0123456789abcdef0123456789abcdefn" KERN_DEBUG);
  735. for (i = 0; i < 8; i++) {
  736. v = apic_read(base + i*0x10);
  737. for (j = 0; j < 32; j++) {
  738. if (v & (1<<j))
  739. printk("1");
  740. else
  741. printk("0");
  742. }
  743. printk("n");
  744. }
  745. }
  746. void /*__init*/ print_local_APIC(void * dummy)
  747. {
  748. unsigned int v, ver, maxlvt;
  749. printk("n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:n",
  750. smp_processor_id(), hard_smp_processor_id());
  751. v = apic_read(APIC_ID);
  752. printk(KERN_INFO "... APIC ID:      %08x (%01x)n", v, GET_APIC_ID(v));
  753. v = apic_read(APIC_LVR);
  754. printk(KERN_INFO "... APIC VERSION: %08xn", v);
  755. ver = GET_APIC_VERSION(v);
  756. maxlvt = get_maxlvt();
  757. v = apic_read(APIC_TASKPRI);
  758. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)n", v, v & APIC_TPRI_MASK);
  759. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  760. v = apic_read(APIC_ARBPRI);
  761. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)n", v,
  762. v & APIC_ARBPRI_MASK);
  763. v = apic_read(APIC_PROCPRI);
  764. printk(KERN_DEBUG "... APIC PROCPRI: %08xn", v);
  765. }
  766. v = apic_read(APIC_EOI);
  767. printk(KERN_DEBUG "... APIC EOI: %08xn", v);
  768. v = apic_read(APIC_RRR);
  769. printk(KERN_DEBUG "... APIC RRR: %08xn", v);
  770. v = apic_read(APIC_LDR);
  771. printk(KERN_DEBUG "... APIC LDR: %08xn", v);
  772. v = apic_read(APIC_DFR);
  773. printk(KERN_DEBUG "... APIC DFR: %08xn", v);
  774. v = apic_read(APIC_SPIV);
  775. printk(KERN_DEBUG "... APIC SPIV: %08xn", v);
  776. printk(KERN_DEBUG "... APIC ISR field:n");
  777. print_APIC_bitfield(APIC_ISR);
  778. printk(KERN_DEBUG "... APIC TMR field:n");
  779. print_APIC_bitfield(APIC_TMR);
  780. printk(KERN_DEBUG "... APIC IRR field:n");
  781. print_APIC_bitfield(APIC_IRR);
  782. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  783. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  784. apic_write(APIC_ESR, 0);
  785. v = apic_read(APIC_ESR);
  786. printk(KERN_DEBUG "... APIC ESR: %08xn", v);
  787. }
  788. v = apic_read(APIC_ICR);
  789. printk(KERN_DEBUG "... APIC ICR: %08xn", v);
  790. v = apic_read(APIC_ICR2);
  791. printk(KERN_DEBUG "... APIC ICR2: %08xn", v);
  792. v = apic_read(APIC_LVTT);
  793. printk(KERN_DEBUG "... APIC LVTT: %08xn", v);
  794. if (maxlvt > 3) {                       /* PC is LVT#4. */
  795. v = apic_read(APIC_LVTPC);
  796. printk(KERN_DEBUG "... APIC LVTPC: %08xn", v);
  797. }
  798. v = apic_read(APIC_LVT0);
  799. printk(KERN_DEBUG "... APIC LVT0: %08xn", v);
  800. v = apic_read(APIC_LVT1);
  801. printk(KERN_DEBUG "... APIC LVT1: %08xn", v);
  802. if (maxlvt > 2) { /* ERR is LVT#3. */
  803. v = apic_read(APIC_LVTERR);
  804. printk(KERN_DEBUG "... APIC LVTERR: %08xn", v);
  805. }
  806. v = apic_read(APIC_TMICT);
  807. printk(KERN_DEBUG "... APIC TMICT: %08xn", v);
  808. v = apic_read(APIC_TMCCT);
  809. printk(KERN_DEBUG "... APIC TMCCT: %08xn", v);
  810. v = apic_read(APIC_TDCR);
  811. printk(KERN_DEBUG "... APIC TDCR: %08xn", v);
  812. printk("n");
  813. }
  814. void print_all_local_APICs (void)
  815. {
  816. smp_call_function(print_local_APIC, NULL, 1, 1);
  817. print_local_APIC(NULL);
  818. }
  819. void /*__init*/ print_PIC(void)
  820. {
  821. extern spinlock_t i8259A_lock;
  822. unsigned int v;
  823. unsigned long flags;
  824. printk(KERN_DEBUG "nprinting PIC contentsn");
  825. spin_lock_irqsave(&i8259A_lock, flags);
  826. v = inb(0xa1) << 8 | inb(0x21);
  827. printk(KERN_DEBUG "... PIC  IMR: %04xn", v);
  828. v = inb(0xa0) << 8 | inb(0x20);
  829. printk(KERN_DEBUG "... PIC  IRR: %04xn", v);
  830. outb(0x0b,0xa0);
  831. outb(0x0b,0x20);
  832. v = inb(0xa0) << 8 | inb(0x20);
  833. outb(0x0a,0xa0);
  834. outb(0x0a,0x20);
  835. spin_unlock_irqrestore(&i8259A_lock, flags);
  836. printk(KERN_DEBUG "... PIC  ISR: %04xn", v);
  837. v = inb(0x4d1) << 8 | inb(0x4d0);
  838. printk(KERN_DEBUG "... PIC ELCR: %04xn", v);
  839. }
  840. static void __init enable_IO_APIC(void)
  841. {
  842. struct IO_APIC_reg_01 reg_01;
  843. int i;
  844. unsigned long flags;
  845. for (i = 0; i < PIN_MAP_SIZE; i++) {
  846. irq_2_pin[i].pin = -1;
  847. irq_2_pin[i].next = 0;
  848. }
  849. if (!pirqs_enabled)
  850. for (i = 0; i < MAX_PIRQS; i++)
  851. pirq_entries[i] = -1;
  852. /*
  853.  * The number of IO-APIC IRQ registers (== #pins):
  854.  */
  855. for (i = 0; i < nr_ioapics; i++) {
  856. spin_lock_irqsave(&ioapic_lock, flags);
  857. *(int *)&reg_01 = io_apic_read(i, 1);
  858. spin_unlock_irqrestore(&ioapic_lock, flags);
  859. nr_ioapic_registers[i] = reg_01.entries+1;
  860. }
  861. /*
  862.  * Do not trust the IO-APIC being empty at bootup
  863.  */
  864. clear_IO_APIC();
  865. }
  866. /*
  867.  * Not an __init, needed by the reboot code
  868.  */
  869. void disable_IO_APIC(void)
  870. {
  871. /*
  872.  * Clear the IO-APIC before rebooting:
  873.  */
  874. clear_IO_APIC();
  875. disconnect_bsp_APIC();
  876. }
  877. /*
  878.  * function to set the IO-APIC physical IDs based on the
  879.  * values stored in the MPC table.
  880.  *
  881.  * by Matt Domsch <Matt_Domsch@dell.com>  Tue Dec 21 12:25:05 CST 1999
  882.  */
  883. static void __init setup_ioapic_ids_from_mpc (void)
  884. {
  885. struct IO_APIC_reg_00 reg_00;
  886. unsigned long phys_id_present_map = phys_cpu_present_map;
  887. int apic;
  888. int i;
  889. unsigned char old_id;
  890. unsigned long flags;
  891. /*
  892.  * Set the IOAPIC ID to the value stored in the MPC table.
  893.  */
  894. for (apic = 0; apic < nr_ioapics; apic++) {
  895. /* Read the register 0 value */
  896. spin_lock_irqsave(&ioapic_lock, flags);
  897. *(int *)&reg_00 = io_apic_read(apic, 0);
  898. spin_unlock_irqrestore(&ioapic_lock, flags);
  899. old_id = mp_ioapics[apic].mpc_apicid;
  900. if (mp_ioapics[apic].mpc_apicid >= 0xf) {
  901. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...n",
  902. apic, mp_ioapics[apic].mpc_apicid);
  903. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)n",
  904. reg_00.ID);
  905. mp_ioapics[apic].mpc_apicid = reg_00.ID;
  906. }
  907. /*
  908.  * Sanity check, is the ID really free? Every APIC in a
  909.  * system must have a unique ID or we get lots of nice
  910.  * 'stuck on smp_invalidate_needed IPI wait' messages.
  911.    */
  912. if (phys_id_present_map & (1 << mp_ioapics[apic].mpc_apicid)) {
  913. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...n",
  914. apic, mp_ioapics[apic].mpc_apicid);
  915. for (i = 0; i < 0xf; i++)
  916. if (!(phys_id_present_map & (1 << i)))
  917. break;
  918. if (i >= 0xf)
  919. panic("Max APIC ID exceeded!n");
  920. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)n",
  921. i);
  922. phys_id_present_map |= 1 << i;
  923. mp_ioapics[apic].mpc_apicid = i;
  924. } else {
  925. printk("Setting %d in the phys_id_present_mapn", mp_ioapics[apic].mpc_apicid);
  926. phys_id_present_map |= 1 << mp_ioapics[apic].mpc_apicid;
  927. }
  928. /*
  929.  * We need to adjust the IRQ routing table
  930.  * if the ID changed.
  931.  */
  932. if (old_id != mp_ioapics[apic].mpc_apicid)
  933. for (i = 0; i < mp_irq_entries; i++)
  934. if (mp_irqs[i].mpc_dstapic == old_id)
  935. mp_irqs[i].mpc_dstapic
  936. = mp_ioapics[apic].mpc_apicid;
  937. /*
  938.  * Read the right value from the MPC table and
  939.  * write it into the ID register.
  940.    */
  941. printk(KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
  942. mp_ioapics[apic].mpc_apicid);
  943. reg_00.ID = mp_ioapics[apic].mpc_apicid;
  944. spin_lock_irqsave(&ioapic_lock, flags);
  945. io_apic_write(apic, 0, *(int *)&reg_00);
  946. spin_unlock_irqrestore(&ioapic_lock, flags);
  947. /*
  948.  * Sanity check
  949.  */
  950. spin_lock_irqsave(&ioapic_lock, flags);
  951. *(int *)&reg_00 = io_apic_read(apic, 0);
  952. spin_unlock_irqrestore(&ioapic_lock, flags);
  953. if (reg_00.ID != mp_ioapics[apic].mpc_apicid)
  954. panic("could not set ID!n");
  955. else
  956. printk(" ok.n");
  957. }
  958. }
  959. /*
  960.  * There is a nasty bug in some older SMP boards, their mptable lies
  961.  * about the timer IRQ. We do the following to work around the situation:
  962.  *
  963.  * - timer IRQ defaults to IO-APIC IRQ
  964.  * - if this function detects that timer IRQs are defunct, then we fall
  965.  *   back to ISA timer IRQs
  966.  */
  967. static int __init timer_irq_works(void)
  968. {
  969. unsigned int t1 = jiffies;
  970. sti();
  971. /* Let ten ticks pass... */
  972. mdelay((10 * 1000) / HZ);
  973. /*
  974.  * Expect a few ticks at least, to be sure some possible
  975.  * glue logic does not lock up after one or two first
  976.  * ticks in a non-ExtINT mode.  Also the local APIC
  977.  * might have cached one ExtINT interrupt.  Finally, at
  978.  * least one tick may be lost due to delays.
  979.  */
  980. if (jiffies - t1 > 4)
  981. return 1;
  982. return 0;
  983. }
  984. /*
  985.  * In the SMP+IOAPIC case it might happen that there are an unspecified
  986.  * number of pending IRQ events unhandled. These cases are very rare,
  987.  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  988.  * better to do it this way as thus we do not have to be aware of
  989.  * 'pending' interrupts in the IRQ path, except at this point.
  990.  */
  991. /*
  992.  * Edge triggered needs to resend any interrupt
  993.  * that was delayed but this is now handled in the device
  994.  * independent code.
  995.  */
  996. #define enable_edge_ioapic_irq unmask_IO_APIC_irq
  997. static void disable_edge_ioapic_irq (unsigned int irq) { /* nothing */ }
  998. /*
  999.  * Starting up a edge-triggered IO-APIC interrupt is
  1000.  * nasty - we need to make sure that we get the edge.
  1001.  * If it is already asserted for some reason, we need
  1002.  * return 1 to indicate that is was pending.
  1003.  *
  1004.  * This is not complete - we should be able to fake
  1005.  * an edge even if it isn't on the 8259A...
  1006.  */
  1007. static unsigned int startup_edge_ioapic_irq(unsigned int irq)
  1008. {
  1009. int was_pending = 0;
  1010. unsigned long flags;
  1011. spin_lock_irqsave(&ioapic_lock, flags);
  1012. if (irq < 16) {
  1013. disable_8259A_irq(irq);
  1014. if (i8259A_irq_pending(irq))
  1015. was_pending = 1;
  1016. }
  1017. __unmask_IO_APIC_irq(irq);
  1018. spin_unlock_irqrestore(&ioapic_lock, flags);
  1019. return was_pending;
  1020. }
  1021. #define shutdown_edge_ioapic_irq disable_edge_ioapic_irq
  1022. /*
  1023.  * Once we have recorded IRQ_PENDING already, we can mask the
  1024.  * interrupt for real. This prevents IRQ storms from unhandled
  1025.  * devices.
  1026.  */
  1027. static void ack_edge_ioapic_irq(unsigned int irq)
  1028. {
  1029. if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
  1030. == (IRQ_PENDING | IRQ_DISABLED))
  1031. mask_IO_APIC_irq(irq);
  1032. ack_APIC_irq();
  1033. }
  1034. static void end_edge_ioapic_irq (unsigned int i) { /* nothing */ }
  1035. /*
  1036.  * Level triggered interrupts can just be masked,
  1037.  * and shutting down and starting up the interrupt
  1038.  * is the same as enabling and disabling them -- except
  1039.  * with a startup need to return a "was pending" value.
  1040.  *
  1041.  * Level triggered interrupts are special because we
  1042.  * do not touch any IO-APIC register while handling
  1043.  * them. We ack the APIC in the end-IRQ handler, not
  1044.  * in the start-IRQ-handler. Protection against reentrance
  1045.  * from the same interrupt is still provided, both by the
  1046.  * generic IRQ layer and by the fact that an unacked local
  1047.  * APIC does not accept IRQs.
  1048.  */
  1049. static unsigned int startup_level_ioapic_irq (unsigned int irq)
  1050. {
  1051. unmask_IO_APIC_irq(irq);
  1052. return 0; /* don't check for pending */
  1053. }
  1054. #define shutdown_level_ioapic_irq mask_IO_APIC_irq
  1055. #define enable_level_ioapic_irq unmask_IO_APIC_irq
  1056. #define disable_level_ioapic_irq mask_IO_APIC_irq
  1057. static void end_level_ioapic_irq (unsigned int irq)
  1058. {
  1059. unsigned long v;
  1060. int i;
  1061. /*
  1062.  * It appears there is an erratum which affects at least version 0x11
  1063.  * of I/O APIC (that's the 82093AA and cores integrated into various
  1064.  * chipsets).  Under certain conditions a level-triggered interrupt is
  1065.  * erroneously delivered as edge-triggered one but the respective IRR
  1066.  * bit gets set nevertheless.  As a result the I/O unit expects an EOI
  1067.  * message but it will never arrive and further interrupts are blocked
  1068.  * from the source.  The exact reason is so far unknown, but the
  1069.  * phenomenon was observed when two consecutive interrupt requests
  1070.  * from a given source get delivered to the same CPU and the source is
  1071.  * temporarily disabled in between.
  1072.  *
  1073.  * A workaround is to simulate an EOI message manually.  We achieve it
  1074.  * by setting the trigger mode to edge and then to level when the edge
  1075.  * trigger mode gets detected in the TMR of a local APIC for a
  1076.  * level-triggered interrupt.  We mask the source for the time of the
  1077.  * operation to prevent an edge-triggered interrupt escaping meanwhile.
  1078.  * The idea is from Manfred Spraul.  --macro
  1079.  */
  1080. i = IO_APIC_VECTOR(irq);
  1081. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  1082. ack_APIC_irq();
  1083. if (!(v & (1 << (i & 0x1f)))) {
  1084. #ifdef APIC_LOCKUP_DEBUG
  1085. struct irq_pin_list *entry;
  1086. #endif
  1087. #ifdef APIC_MISMATCH_DEBUG
  1088. atomic_inc(&irq_mis_count);
  1089. #endif
  1090. spin_lock(&ioapic_lock);
  1091. __mask_and_edge_IO_APIC_irq(irq);
  1092. #ifdef APIC_LOCKUP_DEBUG
  1093. for (entry = irq_2_pin + irq;;) {
  1094. unsigned int reg;
  1095. if (entry->pin == -1)
  1096. break;
  1097. reg = io_apic_read(entry->apic, 0x10 + entry->pin * 2);
  1098. if (reg & 0x00004000)
  1099. printk(KERN_CRIT "Aieee!!!  Remote IRR"
  1100. " still set after unlock!n");
  1101. if (!entry->next)
  1102. break;
  1103. entry = irq_2_pin + entry->next;
  1104. }
  1105. #endif
  1106. __unmask_and_level_IO_APIC_irq(irq);
  1107. spin_unlock(&ioapic_lock);
  1108. }
  1109. }
  1110. static void mask_and_ack_level_ioapic_irq (unsigned int irq) { /* nothing */ }
  1111. static void set_ioapic_affinity (unsigned int irq, unsigned long mask)
  1112. {
  1113. unsigned long flags;
  1114. /*
  1115.  * Only the first 8 bits are valid.
  1116.  */
  1117. mask = mask << 24;
  1118. spin_lock_irqsave(&ioapic_lock, flags);
  1119. __DO_ACTION(1, = mask, )
  1120. spin_unlock_irqrestore(&ioapic_lock, flags);
  1121. }
  1122. /*
  1123.  * Level and edge triggered IO-APIC interrupts need different handling,
  1124.  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1125.  * handled with the level-triggered descriptor, but that one has slightly
  1126.  * more overhead. Level-triggered interrupts cannot be handled with the
  1127.  * edge-triggered handler, without risking IRQ storms and other ugly
  1128.  * races.
  1129.  */
  1130. static struct hw_interrupt_type ioapic_edge_irq_type = {
  1131. "IO-APIC-edge",
  1132. startup_edge_ioapic_irq,
  1133. shutdown_edge_ioapic_irq,
  1134. enable_edge_ioapic_irq,
  1135. disable_edge_ioapic_irq,
  1136. ack_edge_ioapic_irq,
  1137. end_edge_ioapic_irq,
  1138. set_ioapic_affinity,
  1139. };
  1140. static struct hw_interrupt_type ioapic_level_irq_type = {
  1141. "IO-APIC-level",
  1142. startup_level_ioapic_irq,
  1143. shutdown_level_ioapic_irq,
  1144. enable_level_ioapic_irq,
  1145. disable_level_ioapic_irq,
  1146. mask_and_ack_level_ioapic_irq,
  1147. end_level_ioapic_irq,
  1148. set_ioapic_affinity,
  1149. };
  1150. static inline void init_IO_APIC_traps(void)
  1151. {
  1152. int irq;
  1153. /*
  1154.  * NOTE! The local APIC isn't very good at handling
  1155.  * multiple interrupts at the same interrupt level.
  1156.  * As the interrupt level is determined by taking the
  1157.  * vector number and shifting that right by 4, we
  1158.  * want to spread these out a bit so that they don't
  1159.  * all fall in the same interrupt level.
  1160.  *
  1161.  * Also, we've got to be careful not to trash gate
  1162.  * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1163.  */
  1164. for (irq = 0; irq < NR_IRQS ; irq++) {
  1165. if (IO_APIC_IRQ(irq) && !IO_APIC_VECTOR(irq)) {
  1166. /*
  1167.  * Hmm.. We don't have an entry for this,
  1168.  * so default to an old-fashioned 8259
  1169.  * interrupt if we can..
  1170.  */
  1171. if (irq < 16)
  1172. make_8259A_irq(irq);
  1173. else
  1174. /* Strange. Oh, well.. */
  1175. irq_desc[irq].handler = &no_irq_type;
  1176. }
  1177. }
  1178. }
  1179. static void enable_lapic_irq (unsigned int irq)
  1180. {
  1181. unsigned long v;
  1182. v = apic_read(APIC_LVT0);
  1183. apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1184. }
  1185. static void disable_lapic_irq (unsigned int irq)
  1186. {
  1187. unsigned long v;
  1188. v = apic_read(APIC_LVT0);
  1189. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  1190. }
  1191. static void ack_lapic_irq (unsigned int irq)
  1192. {
  1193. ack_APIC_irq();
  1194. }
  1195. static void end_lapic_irq (unsigned int i) { /* nothing */ }
  1196. static struct hw_interrupt_type lapic_irq_type = {
  1197. "local-APIC-edge",
  1198. NULL, /* startup_irq() not used for IRQ0 */
  1199. NULL, /* shutdown_irq() not used for IRQ0 */
  1200. enable_lapic_irq,
  1201. disable_lapic_irq,
  1202. ack_lapic_irq,
  1203. end_lapic_irq
  1204. };
  1205. void enable_NMI_through_LVT0 (void * dummy)
  1206. {
  1207. unsigned int v, ver;
  1208. printk("enable NMI through LVT0 on cpu %dn", smp_processor_id());
  1209. ver = apic_read(APIC_LVR);
  1210. ver = GET_APIC_VERSION(ver);
  1211. v = APIC_DM_NMI; /* unmask and set to NMI */
  1212. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  1213. v |= APIC_LVT_LEVEL_TRIGGER;
  1214. apic_write_around(APIC_LVT0, v);
  1215. }
  1216. static void setup_nmi (void)
  1217. {
  1218. /*
  1219.    * Dirty trick to enable the NMI watchdog ...
  1220.  * We put the 8259A master into AEOI mode and
  1221.  * unmask on all local APICs LVT0 as NMI.
  1222.  *
  1223.  * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1224.  * is from Maciej W. Rozycki - so we do not have to EOI from
  1225.  * the NMI handler or the timer interrupt.
  1226.  */ 
  1227. printk(KERN_INFO "activating NMI Watchdog ...");
  1228. smp_call_function(enable_NMI_through_LVT0, NULL, 1, 1);
  1229. enable_NMI_through_LVT0(NULL);
  1230. printk(" done.n");
  1231. }
  1232. /*
  1233.  * This looks a bit hackish but it's about the only one way of sending
  1234.  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
  1235.  * not support the ExtINT mode, unfortunately.  We need to send these
  1236.  * cycles as some i82489DX-based boards have glue logic that keeps the
  1237.  * 8259A interrupt line asserted until INTA.  --macro
  1238.  */
  1239. static inline void unlock_ExtINT_logic(void)
  1240. {
  1241. int pin, i;
  1242. struct IO_APIC_route_entry entry0, entry1;
  1243. unsigned char save_control, save_freq_select;
  1244. unsigned long flags;
  1245. pin = find_isa_irq_pin(8, mp_INT);
  1246. if (pin == -1)
  1247. return;
  1248. spin_lock_irqsave(&ioapic_lock, flags);
  1249. *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
  1250. *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
  1251. spin_unlock_irqrestore(&ioapic_lock, flags);
  1252. clear_IO_APIC_pin(0, pin);
  1253. memset(&entry1, 0, sizeof(entry1));
  1254. entry1.dest_mode = 0; /* physical delivery */
  1255. entry1.mask = 0; /* unmask IRQ now */
  1256. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1257. entry1.delivery_mode = dest_ExtINT;
  1258. entry1.polarity = entry0.polarity;
  1259. entry1.trigger = 0;
  1260. entry1.vector = 0;
  1261. spin_lock_irqsave(&ioapic_lock, flags);
  1262. io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
  1263. io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
  1264. spin_unlock_irqrestore(&ioapic_lock, flags);
  1265. save_control = CMOS_READ(RTC_CONTROL);
  1266. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1267. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1268.    RTC_FREQ_SELECT);
  1269. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1270. i = 100;
  1271. while (i-- > 0) {
  1272. mdelay(10);
  1273. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1274. i -= 10;
  1275. }
  1276. CMOS_WRITE(save_control, RTC_CONTROL);
  1277. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1278. clear_IO_APIC_pin(0, pin);
  1279. spin_lock_irqsave(&ioapic_lock, flags);
  1280. io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
  1281. io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
  1282. spin_unlock_irqrestore(&ioapic_lock, flags);
  1283. }
  1284. /*
  1285.  * This code may look a bit paranoid, but it's supposed to cooperate with
  1286.  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
  1287.  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
  1288.  * fanatically on his truly buggy board.
  1289.  */
  1290. static inline void check_timer(void)
  1291. {
  1292. int pin1, pin2;
  1293. int vector;
  1294. /*
  1295.  * get/set the timer IRQ vector:
  1296.  */
  1297. disable_8259A_irq(0);
  1298. vector = assign_irq_vector(0);
  1299. set_intr_gate(vector, interrupt[0]);
  1300. /*
  1301.  * Subtle, code in do_timer_interrupt() expects an AEOI
  1302.  * mode for the 8259A whenever interrupts are routed
  1303.  * through I/O APICs.  Also IRQ0 has to be enabled in
  1304.  * the 8259A which implies the virtual wire has to be
  1305.  * disabled in the local APIC.
  1306.  */
  1307. apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1308. init_8259A(1);
  1309. enable_8259A_irq(0);
  1310. pin1 = find_isa_irq_pin(0, mp_INT);
  1311. pin2 = find_isa_irq_pin(0, mp_ExtINT);
  1312. printk(KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%dn", vector, pin1, pin2);
  1313. if (pin1 != -1) {
  1314. /*
  1315.  * Ok, does IRQ0 through the IOAPIC work?
  1316.  */
  1317. unmask_IO_APIC_irq(0);
  1318. if (timer_irq_works()) {
  1319. if (nmi_watchdog == NMI_IO_APIC) {
  1320. disable_8259A_irq(0);
  1321. setup_nmi();
  1322. enable_8259A_irq(0);
  1323. check_nmi_watchdog();
  1324. }
  1325. return;
  1326. }
  1327. clear_IO_APIC_pin(0, pin1);
  1328. printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APICn");
  1329. }
  1330. printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
  1331. if (pin2 != -1) {
  1332. printk("n..... (found pin %d) ...", pin2);
  1333. /*
  1334.  * legacy devices should be connected to IO APIC #0
  1335.  */
  1336. setup_ExtINT_IRQ0_pin(pin2, vector);
  1337. if (timer_irq_works()) {
  1338. printk("works.n");
  1339. if (pin1 != -1)
  1340. replace_pin_at_irq(0, 0, pin1, 0, pin2);
  1341. else
  1342. add_pin_to_irq(0, 0, pin2);
  1343. if (nmi_watchdog == NMI_IO_APIC) {
  1344. setup_nmi();
  1345. check_nmi_watchdog();
  1346. }
  1347. return;
  1348. }
  1349. /*
  1350.  * Cleanup, just in case ...
  1351.  */
  1352. clear_IO_APIC_pin(0, pin2);
  1353. }
  1354. printk(" failed.n");
  1355. if (nmi_watchdog) {
  1356. printk(KERN_WARNING "timer doesnt work through the IO-APIC - disabling NMI Watchdog!n");
  1357. nmi_watchdog = 0;
  1358. }
  1359. printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1360. disable_8259A_irq(0);
  1361. irq_desc[0].handler = &lapic_irq_type;
  1362. apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1363. enable_8259A_irq(0);
  1364. if (timer_irq_works()) {
  1365. printk(" works.n");
  1366. return;
  1367. }
  1368. apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1369. printk(" failed.n");
  1370. printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1371. init_8259A(0);
  1372. make_8259A_irq(0);
  1373. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  1374. unlock_ExtINT_logic();
  1375. if (timer_irq_works()) {
  1376. printk(" works.n");
  1377. return;
  1378. }
  1379. printk(" failed :(.n");
  1380. panic("IO-APIC + timer doesn't work! pester mingo@redhat.com");
  1381. }
  1382. /*
  1383.  *
  1384.  * IRQ's that are handled by the old PIC in all cases:
  1385.  * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1386.  *   Linux doesn't really care, as it's not actually used
  1387.  *   for any interrupt handling anyway.
  1388.  * - There used to be IRQ13 here as well, but all
  1389.  *   MPS-compliant must not use it for FPU coupling and we
  1390.  *   want to use exception 16 anyway.  And there are
  1391.  *   systems who connect it to an I/O APIC for other uses.
  1392.  *   Thus we don't mark it special any longer.
  1393.  *
  1394.  * Additionally, something is definitely wrong with irq9
  1395.  * on PIIX4 boards.
  1396.  */
  1397. #define PIC_IRQS (1<<2)
  1398. void __init setup_IO_APIC(void)
  1399. {
  1400. enable_IO_APIC();
  1401. io_apic_irqs = ~PIC_IRQS;
  1402. printk("ENABLING IO-APIC IRQsn");
  1403. /*
  1404.  * Set up the IO-APIC IRQ routing table by parsing the MP-BIOS
  1405.  * mptable:
  1406.  */
  1407. setup_ioapic_ids_from_mpc();
  1408. sync_Arb_IDs();
  1409. setup_IO_APIC_irqs();
  1410. init_IO_APIC_traps();
  1411. check_timer();
  1412. print_IO_APIC();
  1413. }