io_apic.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  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_irq(int apic, int pin, int idx, int irq)
  687. {
  688. struct IO_APIC_route_entry entry;
  689. int vector;
  690. unsigned long flags;
  691. /*
  692. * add it to the IO-APIC irq-routing table:
  693. */
  694. memset(&entry,0,sizeof(entry));
  695. entry.delivery_mode = INT_DELIVERY_MODE;
  696. entry.dest_mode = INT_DEST_MODE;
  697. entry.mask = 0; /* enable IRQ */
  698. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  699. entry.trigger = irq_trigger(idx);
  700. entry.polarity = irq_polarity(idx);
  701. if (irq_trigger(idx)) {
  702. entry.trigger = 1;
  703. entry.mask = 1;
  704. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  705. }
  706. if (!apic && !IO_APIC_IRQ(irq))
  707. return;
  708. if (IO_APIC_IRQ(irq)) {
  709. cpumask_t mask;
  710. vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
  711. if (vector < 0)
  712. return;
  713. entry.dest.logical.logical_dest = cpu_mask_to_apicid(mask);
  714. entry.vector = vector;
  715. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  716. if (!apic && (irq < 16))
  717. disable_8259A_irq(irq);
  718. }
  719. ioapic_write_entry(apic, pin, entry);
  720. spin_lock_irqsave(&ioapic_lock, flags);
  721. set_native_irq_info(irq, TARGET_CPUS);
  722. spin_unlock_irqrestore(&ioapic_lock, flags);
  723. }
  724. static void __init setup_IO_APIC_irqs(void)
  725. {
  726. int apic, pin, idx, irq, first_notcon = 1;
  727. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  728. for (apic = 0; apic < nr_ioapics; apic++) {
  729. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  730. idx = find_irq_entry(apic,pin,mp_INT);
  731. if (idx == -1) {
  732. if (first_notcon) {
  733. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  734. first_notcon = 0;
  735. } else
  736. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  737. continue;
  738. }
  739. irq = pin_2_irq(idx, apic, pin);
  740. add_pin_to_irq(irq, apic, pin);
  741. setup_IO_APIC_irq(apic, pin, idx, irq);
  742. }
  743. }
  744. if (!first_notcon)
  745. apic_printk(APIC_VERBOSE," not connected.\n");
  746. }
  747. /*
  748. * Set up the 8259A-master output pin as broadcast to all
  749. * CPUs.
  750. */
  751. static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
  752. {
  753. struct IO_APIC_route_entry entry;
  754. unsigned long flags;
  755. memset(&entry,0,sizeof(entry));
  756. disable_8259A_irq(0);
  757. /* mask LVT0 */
  758. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  759. /*
  760. * We use logical delivery to get the timer IRQ
  761. * to the first CPU.
  762. */
  763. entry.dest_mode = INT_DEST_MODE;
  764. entry.mask = 0; /* unmask IRQ now */
  765. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  766. entry.delivery_mode = INT_DELIVERY_MODE;
  767. entry.polarity = 0;
  768. entry.trigger = 0;
  769. entry.vector = vector;
  770. /*
  771. * The timer IRQ doesn't have to know that behind the
  772. * scene we have a 8259A-master in AEOI mode ...
  773. */
  774. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  775. /*
  776. * Add it to the IO-APIC irq-routing table:
  777. */
  778. spin_lock_irqsave(&ioapic_lock, flags);
  779. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  780. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  781. spin_unlock_irqrestore(&ioapic_lock, flags);
  782. enable_8259A_irq(0);
  783. }
  784. void __init UNEXPECTED_IO_APIC(void)
  785. {
  786. }
  787. void __apicdebuginit print_IO_APIC(void)
  788. {
  789. int apic, i;
  790. union IO_APIC_reg_00 reg_00;
  791. union IO_APIC_reg_01 reg_01;
  792. union IO_APIC_reg_02 reg_02;
  793. unsigned long flags;
  794. if (apic_verbosity == APIC_QUIET)
  795. return;
  796. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  797. for (i = 0; i < nr_ioapics; i++)
  798. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  799. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  800. /*
  801. * We are a bit conservative about what we expect. We have to
  802. * know about every hardware change ASAP.
  803. */
  804. printk(KERN_INFO "testing the IO APIC.......................\n");
  805. for (apic = 0; apic < nr_ioapics; apic++) {
  806. spin_lock_irqsave(&ioapic_lock, flags);
  807. reg_00.raw = io_apic_read(apic, 0);
  808. reg_01.raw = io_apic_read(apic, 1);
  809. if (reg_01.bits.version >= 0x10)
  810. reg_02.raw = io_apic_read(apic, 2);
  811. spin_unlock_irqrestore(&ioapic_lock, flags);
  812. printk("\n");
  813. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  814. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  815. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  816. if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
  817. UNEXPECTED_IO_APIC();
  818. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  819. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  820. if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
  821. (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
  822. (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
  823. (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
  824. (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
  825. (reg_01.bits.entries != 0x2E) &&
  826. (reg_01.bits.entries != 0x3F) &&
  827. (reg_01.bits.entries != 0x03)
  828. )
  829. UNEXPECTED_IO_APIC();
  830. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  831. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  832. if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
  833. (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
  834. (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
  835. (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
  836. (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
  837. (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
  838. )
  839. UNEXPECTED_IO_APIC();
  840. if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
  841. UNEXPECTED_IO_APIC();
  842. if (reg_01.bits.version >= 0x10) {
  843. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  844. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  845. if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
  846. UNEXPECTED_IO_APIC();
  847. }
  848. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  849. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  850. " Stat Dest Deli Vect: \n");
  851. for (i = 0; i <= reg_01.bits.entries; i++) {
  852. struct IO_APIC_route_entry entry;
  853. entry = ioapic_read_entry(apic, i);
  854. printk(KERN_DEBUG " %02x %03X %02X ",
  855. i,
  856. entry.dest.logical.logical_dest,
  857. entry.dest.physical.physical_dest
  858. );
  859. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  860. entry.mask,
  861. entry.trigger,
  862. entry.irr,
  863. entry.polarity,
  864. entry.delivery_status,
  865. entry.dest_mode,
  866. entry.delivery_mode,
  867. entry.vector
  868. );
  869. }
  870. }
  871. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  872. for (i = 0; i < NR_IRQS; i++) {
  873. struct irq_pin_list *entry = irq_2_pin + i;
  874. if (entry->pin < 0)
  875. continue;
  876. printk(KERN_DEBUG "IRQ%d ", i);
  877. for (;;) {
  878. printk("-> %d:%d", entry->apic, entry->pin);
  879. if (!entry->next)
  880. break;
  881. entry = irq_2_pin + entry->next;
  882. }
  883. printk("\n");
  884. }
  885. printk(KERN_INFO ".................................... done.\n");
  886. return;
  887. }
  888. #if 0
  889. static __apicdebuginit void print_APIC_bitfield (int base)
  890. {
  891. unsigned int v;
  892. int i, j;
  893. if (apic_verbosity == APIC_QUIET)
  894. return;
  895. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  896. for (i = 0; i < 8; i++) {
  897. v = apic_read(base + i*0x10);
  898. for (j = 0; j < 32; j++) {
  899. if (v & (1<<j))
  900. printk("1");
  901. else
  902. printk("0");
  903. }
  904. printk("\n");
  905. }
  906. }
  907. void __apicdebuginit print_local_APIC(void * dummy)
  908. {
  909. unsigned int v, ver, maxlvt;
  910. if (apic_verbosity == APIC_QUIET)
  911. return;
  912. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  913. smp_processor_id(), hard_smp_processor_id());
  914. v = apic_read(APIC_ID);
  915. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
  916. v = apic_read(APIC_LVR);
  917. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  918. ver = GET_APIC_VERSION(v);
  919. maxlvt = get_maxlvt();
  920. v = apic_read(APIC_TASKPRI);
  921. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  922. v = apic_read(APIC_ARBPRI);
  923. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  924. v & APIC_ARBPRI_MASK);
  925. v = apic_read(APIC_PROCPRI);
  926. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  927. v = apic_read(APIC_EOI);
  928. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  929. v = apic_read(APIC_RRR);
  930. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  931. v = apic_read(APIC_LDR);
  932. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  933. v = apic_read(APIC_DFR);
  934. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  935. v = apic_read(APIC_SPIV);
  936. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  937. printk(KERN_DEBUG "... APIC ISR field:\n");
  938. print_APIC_bitfield(APIC_ISR);
  939. printk(KERN_DEBUG "... APIC TMR field:\n");
  940. print_APIC_bitfield(APIC_TMR);
  941. printk(KERN_DEBUG "... APIC IRR field:\n");
  942. print_APIC_bitfield(APIC_IRR);
  943. v = apic_read(APIC_ESR);
  944. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  945. v = apic_read(APIC_ICR);
  946. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  947. v = apic_read(APIC_ICR2);
  948. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  949. v = apic_read(APIC_LVTT);
  950. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  951. if (maxlvt > 3) { /* PC is LVT#4. */
  952. v = apic_read(APIC_LVTPC);
  953. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  954. }
  955. v = apic_read(APIC_LVT0);
  956. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  957. v = apic_read(APIC_LVT1);
  958. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  959. if (maxlvt > 2) { /* ERR is LVT#3. */
  960. v = apic_read(APIC_LVTERR);
  961. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  962. }
  963. v = apic_read(APIC_TMICT);
  964. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  965. v = apic_read(APIC_TMCCT);
  966. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  967. v = apic_read(APIC_TDCR);
  968. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  969. printk("\n");
  970. }
  971. void print_all_local_APICs (void)
  972. {
  973. on_each_cpu(print_local_APIC, NULL, 1, 1);
  974. }
  975. void __apicdebuginit print_PIC(void)
  976. {
  977. unsigned int v;
  978. unsigned long flags;
  979. if (apic_verbosity == APIC_QUIET)
  980. return;
  981. printk(KERN_DEBUG "\nprinting PIC contents\n");
  982. spin_lock_irqsave(&i8259A_lock, flags);
  983. v = inb(0xa1) << 8 | inb(0x21);
  984. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  985. v = inb(0xa0) << 8 | inb(0x20);
  986. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  987. outb(0x0b,0xa0);
  988. outb(0x0b,0x20);
  989. v = inb(0xa0) << 8 | inb(0x20);
  990. outb(0x0a,0xa0);
  991. outb(0x0a,0x20);
  992. spin_unlock_irqrestore(&i8259A_lock, flags);
  993. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  994. v = inb(0x4d1) << 8 | inb(0x4d0);
  995. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  996. }
  997. #endif /* 0 */
  998. static void __init enable_IO_APIC(void)
  999. {
  1000. union IO_APIC_reg_01 reg_01;
  1001. int i8259_apic, i8259_pin;
  1002. int i, apic;
  1003. unsigned long flags;
  1004. for (i = 0; i < PIN_MAP_SIZE; i++) {
  1005. irq_2_pin[i].pin = -1;
  1006. irq_2_pin[i].next = 0;
  1007. }
  1008. /*
  1009. * The number of IO-APIC IRQ registers (== #pins):
  1010. */
  1011. for (apic = 0; apic < nr_ioapics; apic++) {
  1012. spin_lock_irqsave(&ioapic_lock, flags);
  1013. reg_01.raw = io_apic_read(apic, 1);
  1014. spin_unlock_irqrestore(&ioapic_lock, flags);
  1015. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1016. }
  1017. for(apic = 0; apic < nr_ioapics; apic++) {
  1018. int pin;
  1019. /* See if any of the pins is in ExtINT mode */
  1020. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1021. struct IO_APIC_route_entry entry;
  1022. entry = ioapic_read_entry(apic, pin);
  1023. /* If the interrupt line is enabled and in ExtInt mode
  1024. * I have found the pin where the i8259 is connected.
  1025. */
  1026. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1027. ioapic_i8259.apic = apic;
  1028. ioapic_i8259.pin = pin;
  1029. goto found_i8259;
  1030. }
  1031. }
  1032. }
  1033. found_i8259:
  1034. /* Look to see what if the MP table has reported the ExtINT */
  1035. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1036. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1037. /* Trust the MP table if nothing is setup in the hardware */
  1038. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1039. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1040. ioapic_i8259.pin = i8259_pin;
  1041. ioapic_i8259.apic = i8259_apic;
  1042. }
  1043. /* Complain if the MP table and the hardware disagree */
  1044. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1045. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1046. {
  1047. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1048. }
  1049. /*
  1050. * Do not trust the IO-APIC being empty at bootup
  1051. */
  1052. clear_IO_APIC();
  1053. }
  1054. /*
  1055. * Not an __init, needed by the reboot code
  1056. */
  1057. void disable_IO_APIC(void)
  1058. {
  1059. /*
  1060. * Clear the IO-APIC before rebooting:
  1061. */
  1062. clear_IO_APIC();
  1063. /*
  1064. * If the i8259 is routed through an IOAPIC
  1065. * Put that IOAPIC in virtual wire mode
  1066. * so legacy interrupts can be delivered.
  1067. */
  1068. if (ioapic_i8259.pin != -1) {
  1069. struct IO_APIC_route_entry entry;
  1070. memset(&entry, 0, sizeof(entry));
  1071. entry.mask = 0; /* Enabled */
  1072. entry.trigger = 0; /* Edge */
  1073. entry.irr = 0;
  1074. entry.polarity = 0; /* High */
  1075. entry.delivery_status = 0;
  1076. entry.dest_mode = 0; /* Physical */
  1077. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1078. entry.vector = 0;
  1079. entry.dest.physical.physical_dest =
  1080. GET_APIC_ID(apic_read(APIC_ID));
  1081. /*
  1082. * Add it to the IO-APIC irq-routing table:
  1083. */
  1084. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1085. }
  1086. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1087. }
  1088. /*
  1089. * There is a nasty bug in some older SMP boards, their mptable lies
  1090. * about the timer IRQ. We do the following to work around the situation:
  1091. *
  1092. * - timer IRQ defaults to IO-APIC IRQ
  1093. * - if this function detects that timer IRQs are defunct, then we fall
  1094. * back to ISA timer IRQs
  1095. */
  1096. static int __init timer_irq_works(void)
  1097. {
  1098. unsigned long t1 = jiffies;
  1099. local_irq_enable();
  1100. /* Let ten ticks pass... */
  1101. mdelay((10 * 1000) / HZ);
  1102. /*
  1103. * Expect a few ticks at least, to be sure some possible
  1104. * glue logic does not lock up after one or two first
  1105. * ticks in a non-ExtINT mode. Also the local APIC
  1106. * might have cached one ExtINT interrupt. Finally, at
  1107. * least one tick may be lost due to delays.
  1108. */
  1109. /* jiffies wrap? */
  1110. if (jiffies - t1 > 4)
  1111. return 1;
  1112. return 0;
  1113. }
  1114. /*
  1115. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1116. * number of pending IRQ events unhandled. These cases are very rare,
  1117. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1118. * better to do it this way as thus we do not have to be aware of
  1119. * 'pending' interrupts in the IRQ path, except at this point.
  1120. */
  1121. /*
  1122. * Edge triggered needs to resend any interrupt
  1123. * that was delayed but this is now handled in the device
  1124. * independent code.
  1125. */
  1126. /*
  1127. * Starting up a edge-triggered IO-APIC interrupt is
  1128. * nasty - we need to make sure that we get the edge.
  1129. * If it is already asserted for some reason, we need
  1130. * return 1 to indicate that is was pending.
  1131. *
  1132. * This is not complete - we should be able to fake
  1133. * an edge even if it isn't on the 8259A...
  1134. */
  1135. static unsigned int startup_ioapic_irq(unsigned int irq)
  1136. {
  1137. int was_pending = 0;
  1138. unsigned long flags;
  1139. spin_lock_irqsave(&ioapic_lock, flags);
  1140. if (irq < 16) {
  1141. disable_8259A_irq(irq);
  1142. if (i8259A_irq_pending(irq))
  1143. was_pending = 1;
  1144. }
  1145. __unmask_IO_APIC_irq(irq);
  1146. spin_unlock_irqrestore(&ioapic_lock, flags);
  1147. return was_pending;
  1148. }
  1149. static int ioapic_retrigger_irq(unsigned int irq)
  1150. {
  1151. cpumask_t mask;
  1152. unsigned vector;
  1153. unsigned long flags;
  1154. spin_lock_irqsave(&vector_lock, flags);
  1155. vector = irq_vector[irq];
  1156. cpus_clear(mask);
  1157. cpu_set(first_cpu(irq_domain[irq]), mask);
  1158. send_IPI_mask(mask, vector);
  1159. spin_unlock_irqrestore(&vector_lock, flags);
  1160. return 1;
  1161. }
  1162. /*
  1163. * Level and edge triggered IO-APIC interrupts need different handling,
  1164. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1165. * handled with the level-triggered descriptor, but that one has slightly
  1166. * more overhead. Level-triggered interrupts cannot be handled with the
  1167. * edge-triggered handler, without risking IRQ storms and other ugly
  1168. * races.
  1169. */
  1170. static void ack_apic_edge(unsigned int irq)
  1171. {
  1172. move_native_irq(irq);
  1173. ack_APIC_irq();
  1174. }
  1175. static void ack_apic_level(unsigned int irq)
  1176. {
  1177. int do_unmask_irq = 0;
  1178. #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
  1179. /* If we are moving the irq we need to mask it */
  1180. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  1181. do_unmask_irq = 1;
  1182. mask_IO_APIC_irq(irq);
  1183. }
  1184. #endif
  1185. /*
  1186. * We must acknowledge the irq before we move it or the acknowledge will
  1187. * not propogate properly.
  1188. */
  1189. ack_APIC_irq();
  1190. /* Now we can move and renable the irq */
  1191. move_masked_irq(irq);
  1192. if (unlikely(do_unmask_irq))
  1193. unmask_IO_APIC_irq(irq);
  1194. }
  1195. static struct irq_chip ioapic_chip __read_mostly = {
  1196. .name = "IO-APIC",
  1197. .startup = startup_ioapic_irq,
  1198. .mask = mask_IO_APIC_irq,
  1199. .unmask = unmask_IO_APIC_irq,
  1200. .ack = ack_apic_edge,
  1201. .eoi = ack_apic_level,
  1202. #ifdef CONFIG_SMP
  1203. .set_affinity = set_ioapic_affinity_irq,
  1204. #endif
  1205. .retrigger = ioapic_retrigger_irq,
  1206. };
  1207. static inline void init_IO_APIC_traps(void)
  1208. {
  1209. int irq;
  1210. /*
  1211. * NOTE! The local APIC isn't very good at handling
  1212. * multiple interrupts at the same interrupt level.
  1213. * As the interrupt level is determined by taking the
  1214. * vector number and shifting that right by 4, we
  1215. * want to spread these out a bit so that they don't
  1216. * all fall in the same interrupt level.
  1217. *
  1218. * Also, we've got to be careful not to trash gate
  1219. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1220. */
  1221. for (irq = 0; irq < NR_IRQS ; irq++) {
  1222. int tmp = irq;
  1223. if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
  1224. /*
  1225. * Hmm.. We don't have an entry for this,
  1226. * so default to an old-fashioned 8259
  1227. * interrupt if we can..
  1228. */
  1229. if (irq < 16)
  1230. make_8259A_irq(irq);
  1231. else
  1232. /* Strange. Oh, well.. */
  1233. irq_desc[irq].chip = &no_irq_chip;
  1234. }
  1235. }
  1236. }
  1237. static void enable_lapic_irq (unsigned int irq)
  1238. {
  1239. unsigned long v;
  1240. v = apic_read(APIC_LVT0);
  1241. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1242. }
  1243. static void disable_lapic_irq (unsigned int irq)
  1244. {
  1245. unsigned long v;
  1246. v = apic_read(APIC_LVT0);
  1247. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1248. }
  1249. static void ack_lapic_irq (unsigned int irq)
  1250. {
  1251. ack_APIC_irq();
  1252. }
  1253. static void end_lapic_irq (unsigned int i) { /* nothing */ }
  1254. static struct hw_interrupt_type lapic_irq_type __read_mostly = {
  1255. .typename = "local-APIC-edge",
  1256. .startup = NULL, /* startup_irq() not used for IRQ0 */
  1257. .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
  1258. .enable = enable_lapic_irq,
  1259. .disable = disable_lapic_irq,
  1260. .ack = ack_lapic_irq,
  1261. .end = end_lapic_irq,
  1262. };
  1263. static void setup_nmi (void)
  1264. {
  1265. /*
  1266. * Dirty trick to enable the NMI watchdog ...
  1267. * We put the 8259A master into AEOI mode and
  1268. * unmask on all local APICs LVT0 as NMI.
  1269. *
  1270. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1271. * is from Maciej W. Rozycki - so we do not have to EOI from
  1272. * the NMI handler or the timer interrupt.
  1273. */
  1274. printk(KERN_INFO "activating NMI Watchdog ...");
  1275. enable_NMI_through_LVT0(NULL);
  1276. printk(" done.\n");
  1277. }
  1278. /*
  1279. * This looks a bit hackish but it's about the only one way of sending
  1280. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1281. * not support the ExtINT mode, unfortunately. We need to send these
  1282. * cycles as some i82489DX-based boards have glue logic that keeps the
  1283. * 8259A interrupt line asserted until INTA. --macro
  1284. */
  1285. static inline void unlock_ExtINT_logic(void)
  1286. {
  1287. int apic, pin, i;
  1288. struct IO_APIC_route_entry entry0, entry1;
  1289. unsigned char save_control, save_freq_select;
  1290. unsigned long flags;
  1291. pin = find_isa_irq_pin(8, mp_INT);
  1292. apic = find_isa_irq_apic(8, mp_INT);
  1293. if (pin == -1)
  1294. return;
  1295. spin_lock_irqsave(&ioapic_lock, flags);
  1296. *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  1297. *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  1298. spin_unlock_irqrestore(&ioapic_lock, flags);
  1299. clear_IO_APIC_pin(apic, pin);
  1300. memset(&entry1, 0, sizeof(entry1));
  1301. entry1.dest_mode = 0; /* physical delivery */
  1302. entry1.mask = 0; /* unmask IRQ now */
  1303. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1304. entry1.delivery_mode = dest_ExtINT;
  1305. entry1.polarity = entry0.polarity;
  1306. entry1.trigger = 0;
  1307. entry1.vector = 0;
  1308. spin_lock_irqsave(&ioapic_lock, flags);
  1309. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
  1310. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
  1311. spin_unlock_irqrestore(&ioapic_lock, flags);
  1312. save_control = CMOS_READ(RTC_CONTROL);
  1313. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1314. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1315. RTC_FREQ_SELECT);
  1316. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1317. i = 100;
  1318. while (i-- > 0) {
  1319. mdelay(10);
  1320. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1321. i -= 10;
  1322. }
  1323. CMOS_WRITE(save_control, RTC_CONTROL);
  1324. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1325. clear_IO_APIC_pin(apic, pin);
  1326. spin_lock_irqsave(&ioapic_lock, flags);
  1327. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
  1328. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
  1329. spin_unlock_irqrestore(&ioapic_lock, flags);
  1330. }
  1331. /*
  1332. * This code may look a bit paranoid, but it's supposed to cooperate with
  1333. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1334. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1335. * fanatically on his truly buggy board.
  1336. */
  1337. static int try_apic_pin(int apic, int pin, char *msg)
  1338. {
  1339. apic_printk(APIC_VERBOSE, KERN_INFO
  1340. "..TIMER: trying IO-APIC=%d PIN=%d %s",
  1341. apic, pin, msg);
  1342. /*
  1343. * Ok, does IRQ0 through the IOAPIC work?
  1344. */
  1345. if (!no_timer_check && timer_irq_works()) {
  1346. nmi_watchdog_default();
  1347. if (nmi_watchdog == NMI_IO_APIC) {
  1348. disable_8259A_irq(0);
  1349. setup_nmi();
  1350. enable_8259A_irq(0);
  1351. }
  1352. return 1;
  1353. }
  1354. clear_IO_APIC_pin(apic, pin);
  1355. apic_printk(APIC_QUIET, KERN_ERR " .. failed\n");
  1356. return 0;
  1357. }
  1358. /* The function from hell */
  1359. static void check_timer(void)
  1360. {
  1361. int apic1, pin1, apic2, pin2;
  1362. int vector;
  1363. cpumask_t mask;
  1364. /*
  1365. * get/set the timer IRQ vector:
  1366. */
  1367. disable_8259A_irq(0);
  1368. vector = assign_irq_vector(0, TARGET_CPUS, &mask);
  1369. /*
  1370. * Subtle, code in do_timer_interrupt() expects an AEOI
  1371. * mode for the 8259A whenever interrupts are routed
  1372. * through I/O APICs. Also IRQ0 has to be enabled in
  1373. * the 8259A which implies the virtual wire has to be
  1374. * disabled in the local APIC.
  1375. */
  1376. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1377. init_8259A(1);
  1378. pin1 = find_isa_irq_pin(0, mp_INT);
  1379. apic1 = find_isa_irq_apic(0, mp_INT);
  1380. pin2 = ioapic_i8259.pin;
  1381. apic2 = ioapic_i8259.apic;
  1382. /* Do this first, otherwise we get double interrupts on ATI boards */
  1383. if ((pin1 != -1) && try_apic_pin(apic1, pin1,"with 8259 IRQ0 disabled"))
  1384. return;
  1385. /* Now try again with IRQ0 8259A enabled.
  1386. Assumes timer is on IO-APIC 0 ?!? */
  1387. enable_8259A_irq(0);
  1388. unmask_IO_APIC_irq(0);
  1389. if (try_apic_pin(apic1, pin1, "with 8259 IRQ0 enabled"))
  1390. return;
  1391. disable_8259A_irq(0);
  1392. /* Always try pin0 and pin2 on APIC 0 to handle buggy timer overrides
  1393. on Nvidia boards */
  1394. if (!(apic1 == 0 && pin1 == 0) &&
  1395. try_apic_pin(0, 0, "fallback with 8259 IRQ0 disabled"))
  1396. return;
  1397. if (!(apic1 == 0 && pin1 == 2) &&
  1398. try_apic_pin(0, 2, "fallback with 8259 IRQ0 disabled"))
  1399. return;
  1400. /* Then try pure 8259A routing on the 8259 as reported by BIOS*/
  1401. enable_8259A_irq(0);
  1402. if (pin2 != -1) {
  1403. setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
  1404. if (try_apic_pin(apic2,pin2,"8259A broadcast ExtINT from BIOS"))
  1405. return;
  1406. }
  1407. /* Tried all possibilities to go through the IO-APIC. Now come the
  1408. really cheesy fallbacks. */
  1409. if (nmi_watchdog == NMI_IO_APIC) {
  1410. printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
  1411. nmi_watchdog = 0;
  1412. }
  1413. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1414. disable_8259A_irq(0);
  1415. irq_desc[0].chip = &lapic_irq_type;
  1416. apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1417. enable_8259A_irq(0);
  1418. if (timer_irq_works()) {
  1419. apic_printk(APIC_VERBOSE," works.\n");
  1420. return;
  1421. }
  1422. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1423. apic_printk(APIC_VERBOSE," failed.\n");
  1424. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1425. init_8259A(0);
  1426. make_8259A_irq(0);
  1427. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1428. unlock_ExtINT_logic();
  1429. if (timer_irq_works()) {
  1430. apic_printk(APIC_VERBOSE," works.\n");
  1431. return;
  1432. }
  1433. apic_printk(APIC_VERBOSE," failed :(.\n");
  1434. panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
  1435. }
  1436. static int __init notimercheck(char *s)
  1437. {
  1438. no_timer_check = 1;
  1439. return 1;
  1440. }
  1441. __setup("no_timer_check", notimercheck);
  1442. /*
  1443. *
  1444. * IRQ's that are handled by the PIC in the MPS IOAPIC case.
  1445. * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1446. * Linux doesn't really care, as it's not actually used
  1447. * for any interrupt handling anyway.
  1448. */
  1449. #define PIC_IRQS (1<<2)
  1450. void __init setup_IO_APIC(void)
  1451. {
  1452. enable_IO_APIC();
  1453. if (acpi_ioapic)
  1454. io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
  1455. else
  1456. io_apic_irqs = ~PIC_IRQS;
  1457. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1458. sync_Arb_IDs();
  1459. setup_IO_APIC_irqs();
  1460. init_IO_APIC_traps();
  1461. check_timer();
  1462. if (!acpi_ioapic)
  1463. print_IO_APIC();
  1464. }
  1465. struct sysfs_ioapic_data {
  1466. struct sys_device dev;
  1467. struct IO_APIC_route_entry entry[0];
  1468. };
  1469. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1470. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1471. {
  1472. struct IO_APIC_route_entry *entry;
  1473. struct sysfs_ioapic_data *data;
  1474. int i;
  1475. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1476. entry = data->entry;
  1477. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  1478. *entry = ioapic_read_entry(dev->id, i);
  1479. return 0;
  1480. }
  1481. static int ioapic_resume(struct sys_device *dev)
  1482. {
  1483. struct IO_APIC_route_entry *entry;
  1484. struct sysfs_ioapic_data *data;
  1485. unsigned long flags;
  1486. union IO_APIC_reg_00 reg_00;
  1487. int i;
  1488. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1489. entry = data->entry;
  1490. spin_lock_irqsave(&ioapic_lock, flags);
  1491. reg_00.raw = io_apic_read(dev->id, 0);
  1492. if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
  1493. reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
  1494. io_apic_write(dev->id, 0, reg_00.raw);
  1495. }
  1496. spin_unlock_irqrestore(&ioapic_lock, flags);
  1497. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1498. ioapic_write_entry(dev->id, i, entry[i]);
  1499. return 0;
  1500. }
  1501. static struct sysdev_class ioapic_sysdev_class = {
  1502. set_kset_name("ioapic"),
  1503. .suspend = ioapic_suspend,
  1504. .resume = ioapic_resume,
  1505. };
  1506. static int __init ioapic_init_sysfs(void)
  1507. {
  1508. struct sys_device * dev;
  1509. int i, size, error = 0;
  1510. error = sysdev_class_register(&ioapic_sysdev_class);
  1511. if (error)
  1512. return error;
  1513. for (i = 0; i < nr_ioapics; i++ ) {
  1514. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1515. * sizeof(struct IO_APIC_route_entry);
  1516. mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
  1517. if (!mp_ioapic_data[i]) {
  1518. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1519. continue;
  1520. }
  1521. memset(mp_ioapic_data[i], 0, size);
  1522. dev = &mp_ioapic_data[i]->dev;
  1523. dev->id = i;
  1524. dev->cls = &ioapic_sysdev_class;
  1525. error = sysdev_register(dev);
  1526. if (error) {
  1527. kfree(mp_ioapic_data[i]);
  1528. mp_ioapic_data[i] = NULL;
  1529. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1530. continue;
  1531. }
  1532. }
  1533. return 0;
  1534. }
  1535. device_initcall(ioapic_init_sysfs);
  1536. /*
  1537. * Dynamic irq allocate and deallocation
  1538. */
  1539. int create_irq(void)
  1540. {
  1541. /* Allocate an unused irq */
  1542. int irq;
  1543. int new;
  1544. int vector = 0;
  1545. unsigned long flags;
  1546. cpumask_t mask;
  1547. irq = -ENOSPC;
  1548. spin_lock_irqsave(&vector_lock, flags);
  1549. for (new = (NR_IRQS - 1); new >= 0; new--) {
  1550. if (platform_legacy_irq(new))
  1551. continue;
  1552. if (irq_vector[new] != 0)
  1553. continue;
  1554. vector = __assign_irq_vector(new, TARGET_CPUS, &mask);
  1555. if (likely(vector > 0))
  1556. irq = new;
  1557. break;
  1558. }
  1559. spin_unlock_irqrestore(&vector_lock, flags);
  1560. if (irq >= 0) {
  1561. dynamic_irq_init(irq);
  1562. }
  1563. return irq;
  1564. }
  1565. void destroy_irq(unsigned int irq)
  1566. {
  1567. unsigned long flags;
  1568. dynamic_irq_cleanup(irq);
  1569. spin_lock_irqsave(&vector_lock, flags);
  1570. __clear_irq_vector(irq);
  1571. spin_unlock_irqrestore(&vector_lock, flags);
  1572. }
  1573. /*
  1574. * MSI mesage composition
  1575. */
  1576. #ifdef CONFIG_PCI_MSI
  1577. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1578. {
  1579. int vector;
  1580. unsigned dest;
  1581. cpumask_t tmp;
  1582. vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
  1583. if (vector >= 0) {
  1584. dest = cpu_mask_to_apicid(tmp);
  1585. msg->address_hi = MSI_ADDR_BASE_HI;
  1586. msg->address_lo =
  1587. MSI_ADDR_BASE_LO |
  1588. ((INT_DEST_MODE == 0) ?
  1589. MSI_ADDR_DEST_MODE_PHYSICAL:
  1590. MSI_ADDR_DEST_MODE_LOGICAL) |
  1591. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1592. MSI_ADDR_REDIRECTION_CPU:
  1593. MSI_ADDR_REDIRECTION_LOWPRI) |
  1594. MSI_ADDR_DEST_ID(dest);
  1595. msg->data =
  1596. MSI_DATA_TRIGGER_EDGE |
  1597. MSI_DATA_LEVEL_ASSERT |
  1598. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1599. MSI_DATA_DELIVERY_FIXED:
  1600. MSI_DATA_DELIVERY_LOWPRI) |
  1601. MSI_DATA_VECTOR(vector);
  1602. }
  1603. return vector;
  1604. }
  1605. #ifdef CONFIG_SMP
  1606. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  1607. {
  1608. struct msi_msg msg;
  1609. unsigned int dest;
  1610. cpumask_t tmp;
  1611. int vector;
  1612. cpus_and(tmp, mask, cpu_online_map);
  1613. if (cpus_empty(tmp))
  1614. tmp = TARGET_CPUS;
  1615. cpus_and(mask, tmp, CPU_MASK_ALL);
  1616. vector = assign_irq_vector(irq, mask, &tmp);
  1617. if (vector < 0)
  1618. return;
  1619. dest = cpu_mask_to_apicid(tmp);
  1620. read_msi_msg(irq, &msg);
  1621. msg.data &= ~MSI_DATA_VECTOR_MASK;
  1622. msg.data |= MSI_DATA_VECTOR(vector);
  1623. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  1624. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  1625. write_msi_msg(irq, &msg);
  1626. set_native_irq_info(irq, mask);
  1627. }
  1628. #endif /* CONFIG_SMP */
  1629. /*
  1630. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  1631. * which implement the MSI or MSI-X Capability Structure.
  1632. */
  1633. static struct irq_chip msi_chip = {
  1634. .name = "PCI-MSI",
  1635. .unmask = unmask_msi_irq,
  1636. .mask = mask_msi_irq,
  1637. .ack = ack_apic_edge,
  1638. #ifdef CONFIG_SMP
  1639. .set_affinity = set_msi_irq_affinity,
  1640. #endif
  1641. .retrigger = ioapic_retrigger_irq,
  1642. };
  1643. int arch_setup_msi_irq(unsigned int irq, struct pci_dev *dev)
  1644. {
  1645. struct msi_msg msg;
  1646. int ret;
  1647. ret = msi_compose_msg(dev, irq, &msg);
  1648. if (ret < 0)
  1649. return ret;
  1650. write_msi_msg(irq, &msg);
  1651. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  1652. return 0;
  1653. }
  1654. void arch_teardown_msi_irq(unsigned int irq)
  1655. {
  1656. return;
  1657. }
  1658. #endif /* CONFIG_PCI_MSI */
  1659. /*
  1660. * Hypertransport interrupt support
  1661. */
  1662. #ifdef CONFIG_HT_IRQ
  1663. #ifdef CONFIG_SMP
  1664. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  1665. {
  1666. struct ht_irq_msg msg;
  1667. fetch_ht_irq_msg(irq, &msg);
  1668. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  1669. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  1670. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  1671. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  1672. write_ht_irq_msg(irq, &msg);
  1673. }
  1674. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  1675. {
  1676. unsigned int dest;
  1677. cpumask_t tmp;
  1678. int vector;
  1679. cpus_and(tmp, mask, cpu_online_map);
  1680. if (cpus_empty(tmp))
  1681. tmp = TARGET_CPUS;
  1682. cpus_and(mask, tmp, CPU_MASK_ALL);
  1683. vector = assign_irq_vector(irq, mask, &tmp);
  1684. if (vector < 0)
  1685. return;
  1686. dest = cpu_mask_to_apicid(tmp);
  1687. target_ht_irq(irq, dest, vector);
  1688. set_native_irq_info(irq, mask);
  1689. }
  1690. #endif
  1691. static struct irq_chip ht_irq_chip = {
  1692. .name = "PCI-HT",
  1693. .mask = mask_ht_irq,
  1694. .unmask = unmask_ht_irq,
  1695. .ack = ack_apic_edge,
  1696. #ifdef CONFIG_SMP
  1697. .set_affinity = set_ht_irq_affinity,
  1698. #endif
  1699. .retrigger = ioapic_retrigger_irq,
  1700. };
  1701. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  1702. {
  1703. int vector;
  1704. cpumask_t tmp;
  1705. vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
  1706. if (vector >= 0) {
  1707. struct ht_irq_msg msg;
  1708. unsigned dest;
  1709. dest = cpu_mask_to_apicid(tmp);
  1710. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  1711. msg.address_lo =
  1712. HT_IRQ_LOW_BASE |
  1713. HT_IRQ_LOW_DEST_ID(dest) |
  1714. HT_IRQ_LOW_VECTOR(vector) |
  1715. ((INT_DEST_MODE == 0) ?
  1716. HT_IRQ_LOW_DM_PHYSICAL :
  1717. HT_IRQ_LOW_DM_LOGICAL) |
  1718. HT_IRQ_LOW_RQEOI_EDGE |
  1719. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1720. HT_IRQ_LOW_MT_FIXED :
  1721. HT_IRQ_LOW_MT_ARBITRATED) |
  1722. HT_IRQ_LOW_IRQ_MASKED;
  1723. write_ht_irq_msg(irq, &msg);
  1724. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  1725. handle_edge_irq, "edge");
  1726. }
  1727. return vector;
  1728. }
  1729. #endif /* CONFIG_HT_IRQ */
  1730. /* --------------------------------------------------------------------------
  1731. ACPI-based IOAPIC Configuration
  1732. -------------------------------------------------------------------------- */
  1733. #ifdef CONFIG_ACPI
  1734. #define IO_APIC_MAX_ID 0xFE
  1735. int __init io_apic_get_redir_entries (int ioapic)
  1736. {
  1737. union IO_APIC_reg_01 reg_01;
  1738. unsigned long flags;
  1739. spin_lock_irqsave(&ioapic_lock, flags);
  1740. reg_01.raw = io_apic_read(ioapic, 1);
  1741. spin_unlock_irqrestore(&ioapic_lock, flags);
  1742. return reg_01.bits.entries;
  1743. }
  1744. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  1745. {
  1746. struct IO_APIC_route_entry entry;
  1747. unsigned long flags;
  1748. int vector;
  1749. cpumask_t mask;
  1750. if (!IO_APIC_IRQ(irq)) {
  1751. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  1752. ioapic);
  1753. return -EINVAL;
  1754. }
  1755. /*
  1756. * IRQs < 16 are already in the irq_2_pin[] map
  1757. */
  1758. if (irq >= 16)
  1759. add_pin_to_irq(irq, ioapic, pin);
  1760. vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
  1761. if (vector < 0)
  1762. return vector;
  1763. /*
  1764. * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
  1765. * Note that we mask (disable) IRQs now -- these get enabled when the
  1766. * corresponding device driver registers for this IRQ.
  1767. */
  1768. memset(&entry,0,sizeof(entry));
  1769. entry.delivery_mode = INT_DELIVERY_MODE;
  1770. entry.dest_mode = INT_DEST_MODE;
  1771. entry.dest.logical.logical_dest = cpu_mask_to_apicid(mask);
  1772. entry.trigger = triggering;
  1773. entry.polarity = polarity;
  1774. entry.mask = 1; /* Disabled (masked) */
  1775. entry.vector = vector & 0xff;
  1776. apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
  1777. "IRQ %d Mode:%i Active:%i)\n", ioapic,
  1778. mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
  1779. triggering, polarity);
  1780. ioapic_register_intr(irq, entry.vector, triggering);
  1781. if (!ioapic && (irq < 16))
  1782. disable_8259A_irq(irq);
  1783. ioapic_write_entry(ioapic, pin, entry);
  1784. spin_lock_irqsave(&ioapic_lock, flags);
  1785. set_native_irq_info(irq, TARGET_CPUS);
  1786. spin_unlock_irqrestore(&ioapic_lock, flags);
  1787. return 0;
  1788. }
  1789. #endif /* CONFIG_ACPI */
  1790. /*
  1791. * This function currently is only a helper for the i386 smp boot process where
  1792. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  1793. * so mask in all cases should simply be TARGET_CPUS
  1794. */
  1795. #ifdef CONFIG_SMP
  1796. void __init setup_ioapic_dest(void)
  1797. {
  1798. int pin, ioapic, irq, irq_entry;
  1799. if (skip_ioapic_setup == 1)
  1800. return;
  1801. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  1802. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  1803. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  1804. if (irq_entry == -1)
  1805. continue;
  1806. irq = pin_2_irq(irq_entry, ioapic, pin);
  1807. /* setup_IO_APIC_irqs could fail to get vector for some device
  1808. * when you have too many devices, because at that time only boot
  1809. * cpu is online.
  1810. */
  1811. if(!irq_vector[irq])
  1812. setup_IO_APIC_irq(ioapic, pin, irq_entry, irq);
  1813. else
  1814. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  1815. }
  1816. }
  1817. }
  1818. #endif