io_apic.c 49 KB

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