io_apic.c 49 KB

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