io_apic.c 52 KB

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