io_apic.c 53 KB

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