io_apic_32.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  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/bootmem.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <linux/compiler.h>
  30. #include <linux/acpi.h>
  31. #include <linux/module.h>
  32. #include <linux/sysdev.h>
  33. #include <linux/pci.h>
  34. #include <linux/msi.h>
  35. #include <linux/htirq.h>
  36. #include <linux/freezer.h>
  37. #include <linux/kthread.h>
  38. #include <linux/jiffies.h> /* time_after() */
  39. #include <asm/io.h>
  40. #include <asm/smp.h>
  41. #include <asm/desc.h>
  42. #include <asm/timer.h>
  43. #include <asm/i8259.h>
  44. #include <asm/nmi.h>
  45. #include <asm/msidef.h>
  46. #include <asm/hypertransport.h>
  47. #include <asm/setup.h>
  48. #include <mach_apic.h>
  49. #include <mach_apicdef.h>
  50. #define __apicdebuginit(type) static type __init
  51. int (*ioapic_renumber_irq)(int ioapic, int irq);
  52. atomic_t irq_mis_count;
  53. /* Where if anywhere is the i8259 connect in external int mode */
  54. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  55. static DEFINE_SPINLOCK(ioapic_lock);
  56. DEFINE_SPINLOCK(vector_lock);
  57. int timer_through_8259 __initdata;
  58. /*
  59. * Is the SiS APIC rmw bug present ?
  60. * -1 = don't know, 0 = no, 1 = yes
  61. */
  62. int sis_apic_bug = -1;
  63. int first_free_entry;
  64. /*
  65. * # of IRQ routing registers
  66. */
  67. int nr_ioapic_registers[MAX_IO_APICS];
  68. /* I/O APIC entries */
  69. struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
  70. int nr_ioapics;
  71. /* MP IRQ source entries */
  72. struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  73. /* # of MP IRQ source entries */
  74. int mp_irq_entries;
  75. #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
  76. int mp_bus_id_to_type[MAX_MP_BUSSES];
  77. #endif
  78. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  79. static int disable_timer_pin_1 __initdata;
  80. struct irq_cfg;
  81. struct irq_cfg {
  82. unsigned int irq;
  83. struct irq_cfg *next;
  84. u8 vector;
  85. };
  86. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  87. static struct irq_cfg irq_cfg_legacy[] __initdata = {
  88. [0] = { .irq = 0, .vector = IRQ0_VECTOR, },
  89. [1] = { .irq = 1, .vector = IRQ1_VECTOR, },
  90. [2] = { .irq = 2, .vector = IRQ2_VECTOR, },
  91. [3] = { .irq = 3, .vector = IRQ3_VECTOR, },
  92. [4] = { .irq = 4, .vector = IRQ4_VECTOR, },
  93. [5] = { .irq = 5, .vector = IRQ5_VECTOR, },
  94. [6] = { .irq = 6, .vector = IRQ6_VECTOR, },
  95. [7] = { .irq = 7, .vector = IRQ7_VECTOR, },
  96. [8] = { .irq = 8, .vector = IRQ8_VECTOR, },
  97. [9] = { .irq = 9, .vector = IRQ9_VECTOR, },
  98. [10] = { .irq = 10, .vector = IRQ10_VECTOR, },
  99. [11] = { .irq = 11, .vector = IRQ11_VECTOR, },
  100. [12] = { .irq = 12, .vector = IRQ12_VECTOR, },
  101. [13] = { .irq = 13, .vector = IRQ13_VECTOR, },
  102. [14] = { .irq = 14, .vector = IRQ14_VECTOR, },
  103. [15] = { .irq = 15, .vector = IRQ15_VECTOR, },
  104. };
  105. static struct irq_cfg irq_cfg_init = { .irq = -1U, };
  106. /* need to be biger than size of irq_cfg_legacy */
  107. static int nr_irq_cfg = 32;
  108. static int __init parse_nr_irq_cfg(char *arg)
  109. {
  110. if (arg) {
  111. nr_irq_cfg = simple_strtoul(arg, NULL, 0);
  112. if (nr_irq_cfg < 32)
  113. nr_irq_cfg = 32;
  114. }
  115. return 0;
  116. }
  117. early_param("nr_irq_cfg", parse_nr_irq_cfg);
  118. static void init_one_irq_cfg(struct irq_cfg *cfg)
  119. {
  120. memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
  121. }
  122. static struct irq_cfg *irq_cfgx;
  123. static struct irq_cfg *irq_cfgx_free;
  124. static void __init init_work(void *data)
  125. {
  126. struct dyn_array *da = data;
  127. struct irq_cfg *cfg;
  128. int legacy_count;
  129. int i;
  130. cfg = *da->name;
  131. memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
  132. legacy_count = sizeof(irq_cfg_legacy)/sizeof(irq_cfg_legacy[0]);
  133. for (i = legacy_count; i < *da->nr; i++)
  134. init_one_irq_cfg(&cfg[i]);
  135. for (i = 1; i < *da->nr; i++)
  136. cfg[i-1].next = &cfg[i];
  137. irq_cfgx_free = &irq_cfgx[legacy_count];
  138. irq_cfgx[legacy_count - 1].next = NULL;
  139. }
  140. #define for_each_irq_cfg(cfg) \
  141. for (cfg = irq_cfgx; cfg; cfg = cfg->next)
  142. DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
  143. static struct irq_cfg *irq_cfg(unsigned int irq)
  144. {
  145. struct irq_cfg *cfg;
  146. cfg = irq_cfgx;
  147. while (cfg) {
  148. if (cfg->irq == irq)
  149. return cfg;
  150. cfg = cfg->next;
  151. }
  152. return NULL;
  153. }
  154. static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
  155. {
  156. struct irq_cfg *cfg, *cfg_pri;
  157. int i;
  158. int count = 0;
  159. cfg_pri = cfg = irq_cfgx;
  160. while (cfg) {
  161. if (cfg->irq == irq)
  162. return cfg;
  163. cfg_pri = cfg;
  164. cfg = cfg->next;
  165. count++;
  166. }
  167. if (!irq_cfgx_free) {
  168. unsigned long phys;
  169. unsigned long total_bytes;
  170. /*
  171. * we run out of pre-allocate ones, allocate more
  172. */
  173. printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
  174. total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
  175. if (after_bootmem)
  176. cfg = kzalloc(total_bytes, GFP_ATOMIC);
  177. else
  178. cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
  179. if (!cfg)
  180. panic("please boot with nr_irq_cfg= %d\n", count * 2);
  181. phys = __pa(cfg);
  182. printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
  183. for (i = 0; i < nr_irq_cfg; i++)
  184. init_one_irq_cfg(&cfg[i]);
  185. for (i = 1; i < nr_irq_cfg; i++)
  186. cfg[i-1].next = &cfg[i];
  187. irq_cfgx_free = cfg;
  188. }
  189. cfg = irq_cfgx_free;
  190. irq_cfgx_free = irq_cfgx_free->next;
  191. cfg->next = NULL;
  192. if (cfg_pri)
  193. cfg_pri->next = cfg;
  194. else
  195. irq_cfgx = cfg;
  196. cfg->irq = irq;
  197. printk(KERN_DEBUG "found new irq_cfg for irq %d\n", cfg->irq);
  198. #ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
  199. {
  200. /* dump the results */
  201. struct irq_cfg *cfg;
  202. unsigned long phys;
  203. unsigned long bytes = sizeof(struct irq_cfg);
  204. printk(KERN_DEBUG "=========================== %d\n", irq);
  205. printk(KERN_DEBUG "irq_cfg dump after get that for %d\n", irq);
  206. for_each_irq_cfg(cfg) {
  207. phys = __pa(cfg);
  208. printk(KERN_DEBUG "irq_cfg %d ==> [%#lx - %#lx]\n", cfg->irq, phys, phys + bytes);
  209. }
  210. printk(KERN_DEBUG "===========================\n");
  211. }
  212. #endif
  213. return cfg;
  214. }
  215. /*
  216. * Rough estimation of how many shared IRQs there are, can
  217. * be changed anytime.
  218. */
  219. int pin_map_size;
  220. /*
  221. * This is performance-critical, we want to do it O(1)
  222. *
  223. * the indexing order of this array favors 1:1 mappings
  224. * between pins and IRQs.
  225. */
  226. static struct irq_pin_list {
  227. int apic, pin, next;
  228. } *irq_2_pin;
  229. DEFINE_DYN_ARRAY(irq_2_pin, sizeof(struct irq_pin_list), pin_map_size, 16, NULL);
  230. struct io_apic {
  231. unsigned int index;
  232. unsigned int unused[3];
  233. unsigned int data;
  234. };
  235. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  236. {
  237. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  238. + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
  239. }
  240. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  241. {
  242. struct io_apic __iomem *io_apic = io_apic_base(apic);
  243. writel(reg, &io_apic->index);
  244. return readl(&io_apic->data);
  245. }
  246. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  247. {
  248. struct io_apic __iomem *io_apic = io_apic_base(apic);
  249. writel(reg, &io_apic->index);
  250. writel(value, &io_apic->data);
  251. }
  252. /*
  253. * Re-write a value: to be used for read-modify-write
  254. * cycles where the read already set up the index register.
  255. *
  256. * Older SiS APIC requires we rewrite the index register
  257. */
  258. static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
  259. {
  260. volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
  261. if (sis_apic_bug)
  262. writel(reg, &io_apic->index);
  263. writel(value, &io_apic->data);
  264. }
  265. union entry_union {
  266. struct { u32 w1, w2; };
  267. struct IO_APIC_route_entry entry;
  268. };
  269. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  270. {
  271. union entry_union eu;
  272. unsigned long flags;
  273. spin_lock_irqsave(&ioapic_lock, flags);
  274. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  275. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  276. spin_unlock_irqrestore(&ioapic_lock, flags);
  277. return eu.entry;
  278. }
  279. /*
  280. * When we write a new IO APIC routing entry, we need to write the high
  281. * word first! If the mask bit in the low word is clear, we will enable
  282. * the interrupt, and we need to make sure the entry is fully populated
  283. * before that happens.
  284. */
  285. static void
  286. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  287. {
  288. union entry_union eu;
  289. eu.entry = e;
  290. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  291. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  292. }
  293. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  294. {
  295. unsigned long flags;
  296. spin_lock_irqsave(&ioapic_lock, flags);
  297. __ioapic_write_entry(apic, pin, e);
  298. spin_unlock_irqrestore(&ioapic_lock, flags);
  299. }
  300. /*
  301. * When we mask an IO APIC routing entry, we need to write the low
  302. * word first, in order to set the mask bit before we change the
  303. * high bits!
  304. */
  305. static void ioapic_mask_entry(int apic, int pin)
  306. {
  307. unsigned long flags;
  308. union entry_union eu = { .entry.mask = 1 };
  309. spin_lock_irqsave(&ioapic_lock, flags);
  310. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  311. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  312. spin_unlock_irqrestore(&ioapic_lock, flags);
  313. }
  314. /*
  315. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  316. * shared ISA-space IRQs, so we have to support them. We are super
  317. * fast in the common case, and fast for shared ISA-space IRQs.
  318. */
  319. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  320. {
  321. struct irq_pin_list *entry = irq_2_pin + irq;
  322. irq_cfg_alloc(irq);
  323. while (entry->next)
  324. entry = irq_2_pin + entry->next;
  325. if (entry->pin != -1) {
  326. entry->next = first_free_entry;
  327. entry = irq_2_pin + entry->next;
  328. if (++first_free_entry >= pin_map_size)
  329. panic("io_apic.c: whoops");
  330. }
  331. entry->apic = apic;
  332. entry->pin = pin;
  333. }
  334. /*
  335. * Reroute an IRQ to a different pin.
  336. */
  337. static void __init replace_pin_at_irq(unsigned int irq,
  338. int oldapic, int oldpin,
  339. int newapic, int newpin)
  340. {
  341. struct irq_pin_list *entry = irq_2_pin + irq;
  342. while (1) {
  343. if (entry->apic == oldapic && entry->pin == oldpin) {
  344. entry->apic = newapic;
  345. entry->pin = newpin;
  346. }
  347. if (!entry->next)
  348. break;
  349. entry = irq_2_pin + entry->next;
  350. }
  351. }
  352. static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
  353. {
  354. struct irq_pin_list *entry = irq_2_pin + irq;
  355. unsigned int pin, reg;
  356. for (;;) {
  357. pin = entry->pin;
  358. if (pin == -1)
  359. break;
  360. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  361. reg &= ~disable;
  362. reg |= enable;
  363. io_apic_modify(entry->apic, 0x10 + pin*2, reg);
  364. if (!entry->next)
  365. break;
  366. entry = irq_2_pin + entry->next;
  367. }
  368. }
  369. /* mask = 1 */
  370. static void __mask_IO_APIC_irq(unsigned int irq)
  371. {
  372. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
  373. }
  374. /* mask = 0 */
  375. static void __unmask_IO_APIC_irq(unsigned int irq)
  376. {
  377. __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
  378. }
  379. /* mask = 1, trigger = 0 */
  380. static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
  381. {
  382. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
  383. IO_APIC_REDIR_LEVEL_TRIGGER);
  384. }
  385. /* mask = 0, trigger = 1 */
  386. static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
  387. {
  388. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
  389. IO_APIC_REDIR_MASKED);
  390. }
  391. static void mask_IO_APIC_irq(unsigned int irq)
  392. {
  393. unsigned long flags;
  394. spin_lock_irqsave(&ioapic_lock, flags);
  395. __mask_IO_APIC_irq(irq);
  396. spin_unlock_irqrestore(&ioapic_lock, flags);
  397. }
  398. static void unmask_IO_APIC_irq(unsigned int irq)
  399. {
  400. unsigned long flags;
  401. spin_lock_irqsave(&ioapic_lock, flags);
  402. __unmask_IO_APIC_irq(irq);
  403. spin_unlock_irqrestore(&ioapic_lock, flags);
  404. }
  405. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  406. {
  407. struct IO_APIC_route_entry entry;
  408. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  409. entry = ioapic_read_entry(apic, pin);
  410. if (entry.delivery_mode == dest_SMI)
  411. return;
  412. /*
  413. * Disable it in the IO-APIC irq-routing table:
  414. */
  415. ioapic_mask_entry(apic, pin);
  416. }
  417. static void clear_IO_APIC(void)
  418. {
  419. int apic, pin;
  420. for (apic = 0; apic < nr_ioapics; apic++)
  421. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  422. clear_IO_APIC_pin(apic, pin);
  423. }
  424. #ifdef CONFIG_SMP
  425. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
  426. {
  427. unsigned long flags;
  428. int pin;
  429. struct irq_pin_list *entry = irq_2_pin + irq;
  430. unsigned int apicid_value;
  431. cpumask_t tmp;
  432. struct irq_desc *desc;
  433. cpus_and(tmp, cpumask, cpu_online_map);
  434. if (cpus_empty(tmp))
  435. tmp = TARGET_CPUS;
  436. cpus_and(cpumask, tmp, CPU_MASK_ALL);
  437. apicid_value = cpu_mask_to_apicid(cpumask);
  438. /* Prepare to do the io_apic_write */
  439. apicid_value = apicid_value << 24;
  440. spin_lock_irqsave(&ioapic_lock, flags);
  441. for (;;) {
  442. pin = entry->pin;
  443. if (pin == -1)
  444. break;
  445. io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
  446. if (!entry->next)
  447. break;
  448. entry = irq_2_pin + entry->next;
  449. }
  450. desc = irq_to_desc(irq);
  451. desc->affinity = cpumask;
  452. spin_unlock_irqrestore(&ioapic_lock, flags);
  453. }
  454. #endif /* CONFIG_SMP */
  455. #ifndef CONFIG_SMP
  456. void send_IPI_self(int vector)
  457. {
  458. unsigned int cfg;
  459. /*
  460. * Wait for idle.
  461. */
  462. apic_wait_icr_idle();
  463. cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
  464. /*
  465. * Send the IPI. The write to APIC_ICR fires this off.
  466. */
  467. apic_write(APIC_ICR, cfg);
  468. }
  469. #endif /* !CONFIG_SMP */
  470. /*
  471. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  472. * specific CPU-side IRQs.
  473. */
  474. #define MAX_PIRQS 8
  475. static int pirq_entries [MAX_PIRQS];
  476. static int pirqs_enabled;
  477. int skip_ioapic_setup;
  478. static int __init ioapic_pirq_setup(char *str)
  479. {
  480. int i, max;
  481. int ints[MAX_PIRQS+1];
  482. get_options(str, ARRAY_SIZE(ints), ints);
  483. for (i = 0; i < MAX_PIRQS; i++)
  484. pirq_entries[i] = -1;
  485. pirqs_enabled = 1;
  486. apic_printk(APIC_VERBOSE, KERN_INFO
  487. "PIRQ redirection, working around broken MP-BIOS.\n");
  488. max = MAX_PIRQS;
  489. if (ints[0] < MAX_PIRQS)
  490. max = ints[0];
  491. for (i = 0; i < max; i++) {
  492. apic_printk(APIC_VERBOSE, KERN_DEBUG
  493. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  494. /*
  495. * PIRQs are mapped upside down, usually.
  496. */
  497. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  498. }
  499. return 1;
  500. }
  501. __setup("pirq=", ioapic_pirq_setup);
  502. /*
  503. * Find the IRQ entry number of a certain pin.
  504. */
  505. static int find_irq_entry(int apic, int pin, int type)
  506. {
  507. int i;
  508. for (i = 0; i < mp_irq_entries; i++)
  509. if (mp_irqs[i].mp_irqtype == type &&
  510. (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
  511. mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
  512. mp_irqs[i].mp_dstirq == pin)
  513. return i;
  514. return -1;
  515. }
  516. /*
  517. * Find the pin to which IRQ[irq] (ISA) is connected
  518. */
  519. static int __init find_isa_irq_pin(int irq, int type)
  520. {
  521. int i;
  522. for (i = 0; i < mp_irq_entries; i++) {
  523. int lbus = mp_irqs[i].mp_srcbus;
  524. if (test_bit(lbus, mp_bus_not_pci) &&
  525. (mp_irqs[i].mp_irqtype == type) &&
  526. (mp_irqs[i].mp_srcbusirq == irq))
  527. return mp_irqs[i].mp_dstirq;
  528. }
  529. return -1;
  530. }
  531. static int __init find_isa_irq_apic(int irq, int type)
  532. {
  533. int i;
  534. for (i = 0; i < mp_irq_entries; i++) {
  535. int lbus = mp_irqs[i].mp_srcbus;
  536. if (test_bit(lbus, mp_bus_not_pci) &&
  537. (mp_irqs[i].mp_irqtype == type) &&
  538. (mp_irqs[i].mp_srcbusirq == irq))
  539. break;
  540. }
  541. if (i < mp_irq_entries) {
  542. int apic;
  543. for (apic = 0; apic < nr_ioapics; apic++) {
  544. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
  545. return apic;
  546. }
  547. }
  548. return -1;
  549. }
  550. /*
  551. * Find a specific PCI IRQ entry.
  552. * Not an __init, possibly needed by modules
  553. */
  554. static int pin_2_irq(int idx, int apic, int pin);
  555. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  556. {
  557. int apic, i, best_guess = -1;
  558. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
  559. "slot:%d, pin:%d.\n", bus, slot, pin);
  560. if (test_bit(bus, mp_bus_not_pci)) {
  561. printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  562. return -1;
  563. }
  564. for (i = 0; i < mp_irq_entries; i++) {
  565. int lbus = mp_irqs[i].mp_srcbus;
  566. for (apic = 0; apic < nr_ioapics; apic++)
  567. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
  568. mp_irqs[i].mp_dstapic == MP_APIC_ALL)
  569. break;
  570. if (!test_bit(lbus, mp_bus_not_pci) &&
  571. !mp_irqs[i].mp_irqtype &&
  572. (bus == lbus) &&
  573. (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
  574. int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
  575. if (!(apic || IO_APIC_IRQ(irq)))
  576. continue;
  577. if (pin == (mp_irqs[i].mp_srcbusirq & 3))
  578. return irq;
  579. /*
  580. * Use the first all-but-pin matching entry as a
  581. * best-guess fuzzy result for broken mptables.
  582. */
  583. if (best_guess < 0)
  584. best_guess = irq;
  585. }
  586. }
  587. return best_guess;
  588. }
  589. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  590. /*
  591. * This function currently is only a helper for the i386 smp boot process where
  592. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  593. * so mask in all cases should simply be TARGET_CPUS
  594. */
  595. #ifdef CONFIG_SMP
  596. void __init setup_ioapic_dest(void)
  597. {
  598. int pin, ioapic, irq, irq_entry;
  599. if (skip_ioapic_setup == 1)
  600. return;
  601. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  602. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  603. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  604. if (irq_entry == -1)
  605. continue;
  606. irq = pin_2_irq(irq_entry, ioapic, pin);
  607. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  608. }
  609. }
  610. }
  611. #endif
  612. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  613. /*
  614. * EISA Edge/Level control register, ELCR
  615. */
  616. static int EISA_ELCR(unsigned int irq)
  617. {
  618. if (irq < 16) {
  619. unsigned int port = 0x4d0 + (irq >> 3);
  620. return (inb(port) >> (irq & 7)) & 1;
  621. }
  622. apic_printk(APIC_VERBOSE, KERN_INFO
  623. "Broken MPtable reports ISA irq %d\n", irq);
  624. return 0;
  625. }
  626. #endif
  627. /* ISA interrupts are always polarity zero edge triggered,
  628. * when listed as conforming in the MP table. */
  629. #define default_ISA_trigger(idx) (0)
  630. #define default_ISA_polarity(idx) (0)
  631. /* EISA interrupts are always polarity zero and can be edge or level
  632. * trigger depending on the ELCR value. If an interrupt is listed as
  633. * EISA conforming in the MP table, that means its trigger type must
  634. * be read in from the ELCR */
  635. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
  636. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  637. /* PCI interrupts are always polarity one level triggered,
  638. * when listed as conforming in the MP table. */
  639. #define default_PCI_trigger(idx) (1)
  640. #define default_PCI_polarity(idx) (1)
  641. /* MCA interrupts are always polarity zero level triggered,
  642. * when listed as conforming in the MP table. */
  643. #define default_MCA_trigger(idx) (1)
  644. #define default_MCA_polarity(idx) default_ISA_polarity(idx)
  645. static int MPBIOS_polarity(int idx)
  646. {
  647. int bus = mp_irqs[idx].mp_srcbus;
  648. int polarity;
  649. /*
  650. * Determine IRQ line polarity (high active or low active):
  651. */
  652. switch (mp_irqs[idx].mp_irqflag & 3) {
  653. case 0: /* conforms, ie. bus-type dependent polarity */
  654. {
  655. polarity = test_bit(bus, mp_bus_not_pci)?
  656. default_ISA_polarity(idx):
  657. default_PCI_polarity(idx);
  658. break;
  659. }
  660. case 1: /* high active */
  661. {
  662. polarity = 0;
  663. break;
  664. }
  665. case 2: /* reserved */
  666. {
  667. printk(KERN_WARNING "broken BIOS!!\n");
  668. polarity = 1;
  669. break;
  670. }
  671. case 3: /* low active */
  672. {
  673. polarity = 1;
  674. break;
  675. }
  676. default: /* invalid */
  677. {
  678. printk(KERN_WARNING "broken BIOS!!\n");
  679. polarity = 1;
  680. break;
  681. }
  682. }
  683. return polarity;
  684. }
  685. static int MPBIOS_trigger(int idx)
  686. {
  687. int bus = mp_irqs[idx].mp_srcbus;
  688. int trigger;
  689. /*
  690. * Determine IRQ trigger mode (edge or level sensitive):
  691. */
  692. switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
  693. case 0: /* conforms, ie. bus-type dependent */
  694. {
  695. trigger = test_bit(bus, mp_bus_not_pci)?
  696. default_ISA_trigger(idx):
  697. default_PCI_trigger(idx);
  698. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  699. switch (mp_bus_id_to_type[bus]) {
  700. case MP_BUS_ISA: /* ISA pin */
  701. {
  702. /* set before the switch */
  703. break;
  704. }
  705. case MP_BUS_EISA: /* EISA pin */
  706. {
  707. trigger = default_EISA_trigger(idx);
  708. break;
  709. }
  710. case MP_BUS_PCI: /* PCI pin */
  711. {
  712. /* set before the switch */
  713. break;
  714. }
  715. case MP_BUS_MCA: /* MCA pin */
  716. {
  717. trigger = default_MCA_trigger(idx);
  718. break;
  719. }
  720. default:
  721. {
  722. printk(KERN_WARNING "broken BIOS!!\n");
  723. trigger = 1;
  724. break;
  725. }
  726. }
  727. #endif
  728. break;
  729. }
  730. case 1: /* edge */
  731. {
  732. trigger = 0;
  733. break;
  734. }
  735. case 2: /* reserved */
  736. {
  737. printk(KERN_WARNING "broken BIOS!!\n");
  738. trigger = 1;
  739. break;
  740. }
  741. case 3: /* level */
  742. {
  743. trigger = 1;
  744. break;
  745. }
  746. default: /* invalid */
  747. {
  748. printk(KERN_WARNING "broken BIOS!!\n");
  749. trigger = 0;
  750. break;
  751. }
  752. }
  753. return trigger;
  754. }
  755. static inline int irq_polarity(int idx)
  756. {
  757. return MPBIOS_polarity(idx);
  758. }
  759. static inline int irq_trigger(int idx)
  760. {
  761. return MPBIOS_trigger(idx);
  762. }
  763. static int pin_2_irq(int idx, int apic, int pin)
  764. {
  765. int irq, i;
  766. int bus = mp_irqs[idx].mp_srcbus;
  767. /*
  768. * Debugging check, we are in big trouble if this message pops up!
  769. */
  770. if (mp_irqs[idx].mp_dstirq != pin)
  771. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  772. if (test_bit(bus, mp_bus_not_pci))
  773. irq = mp_irqs[idx].mp_srcbusirq;
  774. else {
  775. /*
  776. * PCI IRQs are mapped in order
  777. */
  778. i = irq = 0;
  779. while (i < apic)
  780. irq += nr_ioapic_registers[i++];
  781. irq += pin;
  782. /*
  783. * For MPS mode, so far only needed by ES7000 platform
  784. */
  785. if (ioapic_renumber_irq)
  786. irq = ioapic_renumber_irq(apic, irq);
  787. }
  788. /*
  789. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  790. */
  791. if ((pin >= 16) && (pin <= 23)) {
  792. if (pirq_entries[pin-16] != -1) {
  793. if (!pirq_entries[pin-16]) {
  794. apic_printk(APIC_VERBOSE, KERN_DEBUG
  795. "disabling PIRQ%d\n", pin-16);
  796. } else {
  797. irq = pirq_entries[pin-16];
  798. apic_printk(APIC_VERBOSE, KERN_DEBUG
  799. "using PIRQ%d -> IRQ %d\n",
  800. pin-16, irq);
  801. }
  802. }
  803. }
  804. return irq;
  805. }
  806. static inline int IO_APIC_irq_trigger(int irq)
  807. {
  808. int apic, idx, pin;
  809. for (apic = 0; apic < nr_ioapics; apic++) {
  810. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  811. idx = find_irq_entry(apic, pin, mp_INT);
  812. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
  813. return irq_trigger(idx);
  814. }
  815. }
  816. /*
  817. * nonexistent IRQs are edge default
  818. */
  819. return 0;
  820. }
  821. static int __assign_irq_vector(int irq)
  822. {
  823. static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
  824. int vector, offset;
  825. struct irq_cfg *cfg;
  826. BUG_ON((unsigned)irq >= nr_irqs);
  827. cfg = irq_cfg(irq);
  828. if (cfg->vector > 0)
  829. return cfg->vector;
  830. vector = current_vector;
  831. offset = current_offset;
  832. next:
  833. vector += 8;
  834. if (vector >= first_system_vector) {
  835. offset = (offset + 1) % 8;
  836. vector = FIRST_DEVICE_VECTOR + offset;
  837. }
  838. if (vector == current_vector)
  839. return -ENOSPC;
  840. if (test_and_set_bit(vector, used_vectors))
  841. goto next;
  842. current_vector = vector;
  843. current_offset = offset;
  844. cfg->vector = vector;
  845. return vector;
  846. }
  847. static int assign_irq_vector(int irq)
  848. {
  849. unsigned long flags;
  850. int vector;
  851. spin_lock_irqsave(&vector_lock, flags);
  852. vector = __assign_irq_vector(irq);
  853. spin_unlock_irqrestore(&vector_lock, flags);
  854. return vector;
  855. }
  856. static struct irq_chip ioapic_chip;
  857. #define IOAPIC_AUTO -1
  858. #define IOAPIC_EDGE 0
  859. #define IOAPIC_LEVEL 1
  860. static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
  861. {
  862. struct irq_desc *desc;
  863. desc = irq_to_desc(irq);
  864. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  865. trigger == IOAPIC_LEVEL) {
  866. desc->status |= IRQ_LEVEL;
  867. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  868. handle_fasteoi_irq, "fasteoi");
  869. } else {
  870. desc->status &= ~IRQ_LEVEL;
  871. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  872. handle_edge_irq, "edge");
  873. }
  874. set_intr_gate(vector, interrupt[irq]);
  875. }
  876. static void __init setup_IO_APIC_irqs(void)
  877. {
  878. struct IO_APIC_route_entry entry;
  879. int apic, pin, idx, irq, first_notcon = 1, vector;
  880. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  881. for (apic = 0; apic < nr_ioapics; apic++) {
  882. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  883. /*
  884. * add it to the IO-APIC irq-routing table:
  885. */
  886. memset(&entry, 0, sizeof(entry));
  887. entry.delivery_mode = INT_DELIVERY_MODE;
  888. entry.dest_mode = INT_DEST_MODE;
  889. entry.mask = 0; /* enable IRQ */
  890. entry.dest.logical.logical_dest =
  891. cpu_mask_to_apicid(TARGET_CPUS);
  892. idx = find_irq_entry(apic, pin, mp_INT);
  893. if (idx == -1) {
  894. if (first_notcon) {
  895. apic_printk(APIC_VERBOSE, KERN_DEBUG
  896. " IO-APIC (apicid-pin) %d-%d",
  897. mp_ioapics[apic].mp_apicid,
  898. pin);
  899. first_notcon = 0;
  900. } else
  901. apic_printk(APIC_VERBOSE, ", %d-%d",
  902. mp_ioapics[apic].mp_apicid, pin);
  903. continue;
  904. }
  905. if (!first_notcon) {
  906. apic_printk(APIC_VERBOSE, " not connected.\n");
  907. first_notcon = 1;
  908. }
  909. entry.trigger = irq_trigger(idx);
  910. entry.polarity = irq_polarity(idx);
  911. if (irq_trigger(idx)) {
  912. entry.trigger = 1;
  913. entry.mask = 1;
  914. }
  915. irq = pin_2_irq(idx, apic, pin);
  916. /*
  917. * skip adding the timer int on secondary nodes, which causes
  918. * a small but painful rift in the time-space continuum
  919. */
  920. if (multi_timer_check(apic, irq))
  921. continue;
  922. else
  923. add_pin_to_irq(irq, apic, pin);
  924. if (!apic && !IO_APIC_IRQ(irq))
  925. continue;
  926. if (IO_APIC_IRQ(irq)) {
  927. vector = assign_irq_vector(irq);
  928. entry.vector = vector;
  929. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  930. if (!apic && (irq < 16))
  931. disable_8259A_irq(irq);
  932. }
  933. ioapic_write_entry(apic, pin, entry);
  934. }
  935. }
  936. if (!first_notcon)
  937. apic_printk(APIC_VERBOSE, " not connected.\n");
  938. }
  939. /*
  940. * Set up the timer pin, possibly with the 8259A-master behind.
  941. */
  942. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  943. int vector)
  944. {
  945. struct IO_APIC_route_entry entry;
  946. memset(&entry, 0, sizeof(entry));
  947. /*
  948. * We use logical delivery to get the timer IRQ
  949. * to the first CPU.
  950. */
  951. entry.dest_mode = INT_DEST_MODE;
  952. entry.mask = 1; /* mask IRQ now */
  953. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  954. entry.delivery_mode = INT_DELIVERY_MODE;
  955. entry.polarity = 0;
  956. entry.trigger = 0;
  957. entry.vector = vector;
  958. /*
  959. * The timer IRQ doesn't have to know that behind the
  960. * scene we may have a 8259A-master in AEOI mode ...
  961. */
  962. ioapic_register_intr(0, vector, IOAPIC_EDGE);
  963. /*
  964. * Add it to the IO-APIC irq-routing table:
  965. */
  966. ioapic_write_entry(apic, pin, entry);
  967. }
  968. __apicdebuginit(void) print_IO_APIC(void)
  969. {
  970. int apic, i;
  971. union IO_APIC_reg_00 reg_00;
  972. union IO_APIC_reg_01 reg_01;
  973. union IO_APIC_reg_02 reg_02;
  974. union IO_APIC_reg_03 reg_03;
  975. unsigned long flags;
  976. if (apic_verbosity == APIC_QUIET)
  977. return;
  978. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  979. for (i = 0; i < nr_ioapics; i++)
  980. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  981. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  982. /*
  983. * We are a bit conservative about what we expect. We have to
  984. * know about every hardware change ASAP.
  985. */
  986. printk(KERN_INFO "testing the IO APIC.......................\n");
  987. for (apic = 0; apic < nr_ioapics; apic++) {
  988. spin_lock_irqsave(&ioapic_lock, flags);
  989. reg_00.raw = io_apic_read(apic, 0);
  990. reg_01.raw = io_apic_read(apic, 1);
  991. if (reg_01.bits.version >= 0x10)
  992. reg_02.raw = io_apic_read(apic, 2);
  993. if (reg_01.bits.version >= 0x20)
  994. reg_03.raw = io_apic_read(apic, 3);
  995. spin_unlock_irqrestore(&ioapic_lock, flags);
  996. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  997. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  998. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  999. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1000. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1001. printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
  1002. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  1003. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1004. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  1005. /*
  1006. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1007. * but the value of reg_02 is read as the previous read register
  1008. * value, so ignore it if reg_02 == reg_01.
  1009. */
  1010. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1011. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1012. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1013. }
  1014. /*
  1015. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1016. * or reg_03, but the value of reg_0[23] is read as the previous read
  1017. * register value, so ignore it if reg_03 == reg_0[12].
  1018. */
  1019. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1020. reg_03.raw != reg_01.raw) {
  1021. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1022. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1023. }
  1024. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1025. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  1026. " Stat Dest Deli Vect: \n");
  1027. for (i = 0; i <= reg_01.bits.entries; i++) {
  1028. struct IO_APIC_route_entry entry;
  1029. entry = ioapic_read_entry(apic, i);
  1030. printk(KERN_DEBUG " %02x %03X %02X ",
  1031. i,
  1032. entry.dest.logical.logical_dest,
  1033. entry.dest.physical.physical_dest
  1034. );
  1035. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1036. entry.mask,
  1037. entry.trigger,
  1038. entry.irr,
  1039. entry.polarity,
  1040. entry.delivery_status,
  1041. entry.dest_mode,
  1042. entry.delivery_mode,
  1043. entry.vector
  1044. );
  1045. }
  1046. }
  1047. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1048. for (i = 0; i < nr_irqs; i++) {
  1049. struct irq_pin_list *entry = irq_2_pin + i;
  1050. if (entry->pin < 0)
  1051. continue;
  1052. printk(KERN_DEBUG "IRQ%d ", i);
  1053. for (;;) {
  1054. printk("-> %d:%d", entry->apic, entry->pin);
  1055. if (!entry->next)
  1056. break;
  1057. entry = irq_2_pin + entry->next;
  1058. }
  1059. printk("\n");
  1060. }
  1061. printk(KERN_INFO ".................................... done.\n");
  1062. return;
  1063. }
  1064. __apicdebuginit(void) print_APIC_bitfield(int base)
  1065. {
  1066. unsigned int v;
  1067. int i, j;
  1068. if (apic_verbosity == APIC_QUIET)
  1069. return;
  1070. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1071. for (i = 0; i < 8; i++) {
  1072. v = apic_read(base + i*0x10);
  1073. for (j = 0; j < 32; j++) {
  1074. if (v & (1<<j))
  1075. printk("1");
  1076. else
  1077. printk("0");
  1078. }
  1079. printk("\n");
  1080. }
  1081. }
  1082. __apicdebuginit(void) print_local_APIC(void *dummy)
  1083. {
  1084. unsigned int v, ver, maxlvt;
  1085. u64 icr;
  1086. if (apic_verbosity == APIC_QUIET)
  1087. return;
  1088. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1089. smp_processor_id(), hard_smp_processor_id());
  1090. v = apic_read(APIC_ID);
  1091. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
  1092. GET_APIC_ID(v));
  1093. v = apic_read(APIC_LVR);
  1094. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1095. ver = GET_APIC_VERSION(v);
  1096. maxlvt = lapic_get_maxlvt();
  1097. v = apic_read(APIC_TASKPRI);
  1098. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1099. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1100. v = apic_read(APIC_ARBPRI);
  1101. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1102. v & APIC_ARBPRI_MASK);
  1103. v = apic_read(APIC_PROCPRI);
  1104. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1105. }
  1106. v = apic_read(APIC_EOI);
  1107. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  1108. v = apic_read(APIC_RRR);
  1109. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1110. v = apic_read(APIC_LDR);
  1111. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1112. v = apic_read(APIC_DFR);
  1113. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1114. v = apic_read(APIC_SPIV);
  1115. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1116. printk(KERN_DEBUG "... APIC ISR field:\n");
  1117. print_APIC_bitfield(APIC_ISR);
  1118. printk(KERN_DEBUG "... APIC TMR field:\n");
  1119. print_APIC_bitfield(APIC_TMR);
  1120. printk(KERN_DEBUG "... APIC IRR field:\n");
  1121. print_APIC_bitfield(APIC_IRR);
  1122. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1123. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1124. apic_write(APIC_ESR, 0);
  1125. v = apic_read(APIC_ESR);
  1126. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1127. }
  1128. icr = apic_icr_read();
  1129. printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
  1130. printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
  1131. v = apic_read(APIC_LVTT);
  1132. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1133. if (maxlvt > 3) { /* PC is LVT#4. */
  1134. v = apic_read(APIC_LVTPC);
  1135. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1136. }
  1137. v = apic_read(APIC_LVT0);
  1138. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1139. v = apic_read(APIC_LVT1);
  1140. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1141. if (maxlvt > 2) { /* ERR is LVT#3. */
  1142. v = apic_read(APIC_LVTERR);
  1143. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1144. }
  1145. v = apic_read(APIC_TMICT);
  1146. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1147. v = apic_read(APIC_TMCCT);
  1148. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1149. v = apic_read(APIC_TDCR);
  1150. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1151. printk("\n");
  1152. }
  1153. __apicdebuginit(void) print_all_local_APICs(void)
  1154. {
  1155. on_each_cpu(print_local_APIC, NULL, 1);
  1156. }
  1157. __apicdebuginit(void) print_PIC(void)
  1158. {
  1159. unsigned int v;
  1160. unsigned long flags;
  1161. if (apic_verbosity == APIC_QUIET)
  1162. return;
  1163. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1164. spin_lock_irqsave(&i8259A_lock, flags);
  1165. v = inb(0xa1) << 8 | inb(0x21);
  1166. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1167. v = inb(0xa0) << 8 | inb(0x20);
  1168. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1169. outb(0x0b, 0xa0);
  1170. outb(0x0b, 0x20);
  1171. v = inb(0xa0) << 8 | inb(0x20);
  1172. outb(0x0a, 0xa0);
  1173. outb(0x0a, 0x20);
  1174. spin_unlock_irqrestore(&i8259A_lock, flags);
  1175. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1176. v = inb(0x4d1) << 8 | inb(0x4d0);
  1177. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1178. }
  1179. __apicdebuginit(int) print_all_ICs(void)
  1180. {
  1181. print_PIC();
  1182. print_all_local_APICs();
  1183. print_IO_APIC();
  1184. return 0;
  1185. }
  1186. fs_initcall(print_all_ICs);
  1187. static void __init enable_IO_APIC(void)
  1188. {
  1189. union IO_APIC_reg_01 reg_01;
  1190. int i8259_apic, i8259_pin;
  1191. int i, apic;
  1192. unsigned long flags;
  1193. for (i = 0; i < pin_map_size; i++) {
  1194. irq_2_pin[i].pin = -1;
  1195. irq_2_pin[i].next = 0;
  1196. }
  1197. if (!pirqs_enabled)
  1198. for (i = 0; i < MAX_PIRQS; i++)
  1199. pirq_entries[i] = -1;
  1200. /*
  1201. * The number of IO-APIC IRQ registers (== #pins):
  1202. */
  1203. for (apic = 0; apic < nr_ioapics; apic++) {
  1204. spin_lock_irqsave(&ioapic_lock, flags);
  1205. reg_01.raw = io_apic_read(apic, 1);
  1206. spin_unlock_irqrestore(&ioapic_lock, flags);
  1207. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1208. }
  1209. for (apic = 0; apic < nr_ioapics; apic++) {
  1210. int pin;
  1211. /* See if any of the pins is in ExtINT mode */
  1212. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1213. struct IO_APIC_route_entry entry;
  1214. entry = ioapic_read_entry(apic, pin);
  1215. /* If the interrupt line is enabled and in ExtInt mode
  1216. * I have found the pin where the i8259 is connected.
  1217. */
  1218. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1219. ioapic_i8259.apic = apic;
  1220. ioapic_i8259.pin = pin;
  1221. goto found_i8259;
  1222. }
  1223. }
  1224. }
  1225. found_i8259:
  1226. /* Look to see what if the MP table has reported the ExtINT */
  1227. /* If we could not find the appropriate pin by looking at the ioapic
  1228. * the i8259 probably is not connected the ioapic but give the
  1229. * mptable a chance anyway.
  1230. */
  1231. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1232. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1233. /* Trust the MP table if nothing is setup in the hardware */
  1234. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1235. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1236. ioapic_i8259.pin = i8259_pin;
  1237. ioapic_i8259.apic = i8259_apic;
  1238. }
  1239. /* Complain if the MP table and the hardware disagree */
  1240. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1241. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1242. {
  1243. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1244. }
  1245. /*
  1246. * Do not trust the IO-APIC being empty at bootup
  1247. */
  1248. clear_IO_APIC();
  1249. }
  1250. /*
  1251. * Not an __init, needed by the reboot code
  1252. */
  1253. void disable_IO_APIC(void)
  1254. {
  1255. /*
  1256. * Clear the IO-APIC before rebooting:
  1257. */
  1258. clear_IO_APIC();
  1259. /*
  1260. * If the i8259 is routed through an IOAPIC
  1261. * Put that IOAPIC in virtual wire mode
  1262. * so legacy interrupts can be delivered.
  1263. */
  1264. if (ioapic_i8259.pin != -1) {
  1265. struct IO_APIC_route_entry entry;
  1266. memset(&entry, 0, sizeof(entry));
  1267. entry.mask = 0; /* Enabled */
  1268. entry.trigger = 0; /* Edge */
  1269. entry.irr = 0;
  1270. entry.polarity = 0; /* High */
  1271. entry.delivery_status = 0;
  1272. entry.dest_mode = 0; /* Physical */
  1273. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1274. entry.vector = 0;
  1275. entry.dest.physical.physical_dest = read_apic_id();
  1276. /*
  1277. * Add it to the IO-APIC irq-routing table:
  1278. */
  1279. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1280. }
  1281. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1282. }
  1283. /*
  1284. * function to set the IO-APIC physical IDs based on the
  1285. * values stored in the MPC table.
  1286. *
  1287. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1288. */
  1289. static void __init setup_ioapic_ids_from_mpc(void)
  1290. {
  1291. union IO_APIC_reg_00 reg_00;
  1292. physid_mask_t phys_id_present_map;
  1293. int apic;
  1294. int i;
  1295. unsigned char old_id;
  1296. unsigned long flags;
  1297. if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
  1298. return;
  1299. /*
  1300. * Don't check I/O APIC IDs for xAPIC systems. They have
  1301. * no meaning without the serial APIC bus.
  1302. */
  1303. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1304. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1305. return;
  1306. /*
  1307. * This is broken; anything with a real cpu count has to
  1308. * circumvent this idiocy regardless.
  1309. */
  1310. phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
  1311. /*
  1312. * Set the IOAPIC ID to the value stored in the MPC table.
  1313. */
  1314. for (apic = 0; apic < nr_ioapics; apic++) {
  1315. /* Read the register 0 value */
  1316. spin_lock_irqsave(&ioapic_lock, flags);
  1317. reg_00.raw = io_apic_read(apic, 0);
  1318. spin_unlock_irqrestore(&ioapic_lock, flags);
  1319. old_id = mp_ioapics[apic].mp_apicid;
  1320. if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
  1321. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1322. apic, mp_ioapics[apic].mp_apicid);
  1323. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1324. reg_00.bits.ID);
  1325. mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
  1326. }
  1327. /*
  1328. * Sanity check, is the ID really free? Every APIC in a
  1329. * system must have a unique ID or we get lots of nice
  1330. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1331. */
  1332. if (check_apicid_used(phys_id_present_map,
  1333. mp_ioapics[apic].mp_apicid)) {
  1334. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1335. apic, mp_ioapics[apic].mp_apicid);
  1336. for (i = 0; i < get_physical_broadcast(); i++)
  1337. if (!physid_isset(i, phys_id_present_map))
  1338. break;
  1339. if (i >= get_physical_broadcast())
  1340. panic("Max APIC ID exceeded!\n");
  1341. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1342. i);
  1343. physid_set(i, phys_id_present_map);
  1344. mp_ioapics[apic].mp_apicid = i;
  1345. } else {
  1346. physid_mask_t tmp;
  1347. tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
  1348. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1349. "phys_id_present_map\n",
  1350. mp_ioapics[apic].mp_apicid);
  1351. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1352. }
  1353. /*
  1354. * We need to adjust the IRQ routing table
  1355. * if the ID changed.
  1356. */
  1357. if (old_id != mp_ioapics[apic].mp_apicid)
  1358. for (i = 0; i < mp_irq_entries; i++)
  1359. if (mp_irqs[i].mp_dstapic == old_id)
  1360. mp_irqs[i].mp_dstapic
  1361. = mp_ioapics[apic].mp_apicid;
  1362. /*
  1363. * Read the right value from the MPC table and
  1364. * write it into the ID register.
  1365. */
  1366. apic_printk(APIC_VERBOSE, KERN_INFO
  1367. "...changing IO-APIC physical APIC ID to %d ...",
  1368. mp_ioapics[apic].mp_apicid);
  1369. reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
  1370. spin_lock_irqsave(&ioapic_lock, flags);
  1371. io_apic_write(apic, 0, reg_00.raw);
  1372. spin_unlock_irqrestore(&ioapic_lock, flags);
  1373. /*
  1374. * Sanity check
  1375. */
  1376. spin_lock_irqsave(&ioapic_lock, flags);
  1377. reg_00.raw = io_apic_read(apic, 0);
  1378. spin_unlock_irqrestore(&ioapic_lock, flags);
  1379. if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
  1380. printk("could not set ID!\n");
  1381. else
  1382. apic_printk(APIC_VERBOSE, " ok.\n");
  1383. }
  1384. }
  1385. int no_timer_check __initdata;
  1386. static int __init notimercheck(char *s)
  1387. {
  1388. no_timer_check = 1;
  1389. return 1;
  1390. }
  1391. __setup("no_timer_check", notimercheck);
  1392. /*
  1393. * There is a nasty bug in some older SMP boards, their mptable lies
  1394. * about the timer IRQ. We do the following to work around the situation:
  1395. *
  1396. * - timer IRQ defaults to IO-APIC IRQ
  1397. * - if this function detects that timer IRQs are defunct, then we fall
  1398. * back to ISA timer IRQs
  1399. */
  1400. static int __init timer_irq_works(void)
  1401. {
  1402. unsigned long t1 = jiffies;
  1403. unsigned long flags;
  1404. if (no_timer_check)
  1405. return 1;
  1406. local_save_flags(flags);
  1407. local_irq_enable();
  1408. /* Let ten ticks pass... */
  1409. mdelay((10 * 1000) / HZ);
  1410. local_irq_restore(flags);
  1411. /*
  1412. * Expect a few ticks at least, to be sure some possible
  1413. * glue logic does not lock up after one or two first
  1414. * ticks in a non-ExtINT mode. Also the local APIC
  1415. * might have cached one ExtINT interrupt. Finally, at
  1416. * least one tick may be lost due to delays.
  1417. */
  1418. if (time_after(jiffies, t1 + 4))
  1419. return 1;
  1420. return 0;
  1421. }
  1422. /*
  1423. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1424. * number of pending IRQ events unhandled. These cases are very rare,
  1425. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1426. * better to do it this way as thus we do not have to be aware of
  1427. * 'pending' interrupts in the IRQ path, except at this point.
  1428. */
  1429. /*
  1430. * Edge triggered needs to resend any interrupt
  1431. * that was delayed but this is now handled in the device
  1432. * independent code.
  1433. */
  1434. /*
  1435. * Startup quirk:
  1436. *
  1437. * Starting up a edge-triggered IO-APIC interrupt is
  1438. * nasty - we need to make sure that we get the edge.
  1439. * If it is already asserted for some reason, we need
  1440. * return 1 to indicate that is was pending.
  1441. *
  1442. * This is not complete - we should be able to fake
  1443. * an edge even if it isn't on the 8259A...
  1444. *
  1445. * (We do this for level-triggered IRQs too - it cannot hurt.)
  1446. */
  1447. static unsigned int startup_ioapic_irq(unsigned int irq)
  1448. {
  1449. int was_pending = 0;
  1450. unsigned long flags;
  1451. spin_lock_irqsave(&ioapic_lock, flags);
  1452. if (irq < 16) {
  1453. disable_8259A_irq(irq);
  1454. if (i8259A_irq_pending(irq))
  1455. was_pending = 1;
  1456. }
  1457. __unmask_IO_APIC_irq(irq);
  1458. spin_unlock_irqrestore(&ioapic_lock, flags);
  1459. return was_pending;
  1460. }
  1461. static void ack_ioapic_irq(unsigned int irq)
  1462. {
  1463. move_native_irq(irq);
  1464. ack_APIC_irq();
  1465. }
  1466. static void ack_ioapic_quirk_irq(unsigned int irq)
  1467. {
  1468. unsigned long v;
  1469. int i;
  1470. move_native_irq(irq);
  1471. /*
  1472. * It appears there is an erratum which affects at least version 0x11
  1473. * of I/O APIC (that's the 82093AA and cores integrated into various
  1474. * chipsets). Under certain conditions a level-triggered interrupt is
  1475. * erroneously delivered as edge-triggered one but the respective IRR
  1476. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  1477. * message but it will never arrive and further interrupts are blocked
  1478. * from the source. The exact reason is so far unknown, but the
  1479. * phenomenon was observed when two consecutive interrupt requests
  1480. * from a given source get delivered to the same CPU and the source is
  1481. * temporarily disabled in between.
  1482. *
  1483. * A workaround is to simulate an EOI message manually. We achieve it
  1484. * by setting the trigger mode to edge and then to level when the edge
  1485. * trigger mode gets detected in the TMR of a local APIC for a
  1486. * level-triggered interrupt. We mask the source for the time of the
  1487. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  1488. * The idea is from Manfred Spraul. --macro
  1489. */
  1490. i = irq_cfg(irq)->vector;
  1491. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  1492. ack_APIC_irq();
  1493. if (!(v & (1 << (i & 0x1f)))) {
  1494. atomic_inc(&irq_mis_count);
  1495. spin_lock(&ioapic_lock);
  1496. __mask_and_edge_IO_APIC_irq(irq);
  1497. __unmask_and_level_IO_APIC_irq(irq);
  1498. spin_unlock(&ioapic_lock);
  1499. }
  1500. }
  1501. static int ioapic_retrigger_irq(unsigned int irq)
  1502. {
  1503. send_IPI_self(irq_cfg(irq)->vector);
  1504. return 1;
  1505. }
  1506. static struct irq_chip ioapic_chip __read_mostly = {
  1507. .name = "IO-APIC",
  1508. .startup = startup_ioapic_irq,
  1509. .mask = mask_IO_APIC_irq,
  1510. .unmask = unmask_IO_APIC_irq,
  1511. .ack = ack_ioapic_irq,
  1512. .eoi = ack_ioapic_quirk_irq,
  1513. #ifdef CONFIG_SMP
  1514. .set_affinity = set_ioapic_affinity_irq,
  1515. #endif
  1516. .retrigger = ioapic_retrigger_irq,
  1517. };
  1518. static inline void init_IO_APIC_traps(void)
  1519. {
  1520. int irq;
  1521. struct irq_desc *desc;
  1522. struct irq_cfg *cfg;
  1523. /*
  1524. * NOTE! The local APIC isn't very good at handling
  1525. * multiple interrupts at the same interrupt level.
  1526. * As the interrupt level is determined by taking the
  1527. * vector number and shifting that right by 4, we
  1528. * want to spread these out a bit so that they don't
  1529. * all fall in the same interrupt level.
  1530. *
  1531. * Also, we've got to be careful not to trash gate
  1532. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1533. */
  1534. for_each_irq_cfg(cfg) {
  1535. irq = cfg->irq;
  1536. if (IO_APIC_IRQ(irq) && !cfg->vector) {
  1537. /*
  1538. * Hmm.. We don't have an entry for this,
  1539. * so default to an old-fashioned 8259
  1540. * interrupt if we can..
  1541. */
  1542. if (irq < 16)
  1543. make_8259A_irq(irq);
  1544. else {
  1545. desc = irq_to_desc(irq);
  1546. /* Strange. Oh, well.. */
  1547. desc->chip = &no_irq_chip;
  1548. }
  1549. }
  1550. }
  1551. }
  1552. /*
  1553. * The local APIC irq-chip implementation:
  1554. */
  1555. static void ack_lapic_irq(unsigned int irq)
  1556. {
  1557. ack_APIC_irq();
  1558. }
  1559. static void mask_lapic_irq(unsigned int irq)
  1560. {
  1561. unsigned long v;
  1562. v = apic_read(APIC_LVT0);
  1563. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1564. }
  1565. static void unmask_lapic_irq(unsigned int irq)
  1566. {
  1567. unsigned long v;
  1568. v = apic_read(APIC_LVT0);
  1569. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1570. }
  1571. static struct irq_chip lapic_chip __read_mostly = {
  1572. .name = "local-APIC",
  1573. .mask = mask_lapic_irq,
  1574. .unmask = unmask_lapic_irq,
  1575. .ack = ack_lapic_irq,
  1576. };
  1577. static void lapic_register_intr(int irq, int vector)
  1578. {
  1579. struct irq_desc *desc;
  1580. desc = irq_to_desc(irq);
  1581. desc->status &= ~IRQ_LEVEL;
  1582. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  1583. "edge");
  1584. set_intr_gate(vector, interrupt[irq]);
  1585. }
  1586. static void __init setup_nmi(void)
  1587. {
  1588. /*
  1589. * Dirty trick to enable the NMI watchdog ...
  1590. * We put the 8259A master into AEOI mode and
  1591. * unmask on all local APICs LVT0 as NMI.
  1592. *
  1593. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1594. * is from Maciej W. Rozycki - so we do not have to EOI from
  1595. * the NMI handler or the timer interrupt.
  1596. */
  1597. apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
  1598. enable_NMI_through_LVT0();
  1599. apic_printk(APIC_VERBOSE, " done.\n");
  1600. }
  1601. /*
  1602. * This looks a bit hackish but it's about the only one way of sending
  1603. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1604. * not support the ExtINT mode, unfortunately. We need to send these
  1605. * cycles as some i82489DX-based boards have glue logic that keeps the
  1606. * 8259A interrupt line asserted until INTA. --macro
  1607. */
  1608. static inline void __init unlock_ExtINT_logic(void)
  1609. {
  1610. int apic, pin, i;
  1611. struct IO_APIC_route_entry entry0, entry1;
  1612. unsigned char save_control, save_freq_select;
  1613. pin = find_isa_irq_pin(8, mp_INT);
  1614. if (pin == -1) {
  1615. WARN_ON_ONCE(1);
  1616. return;
  1617. }
  1618. apic = find_isa_irq_apic(8, mp_INT);
  1619. if (apic == -1) {
  1620. WARN_ON_ONCE(1);
  1621. return;
  1622. }
  1623. entry0 = ioapic_read_entry(apic, pin);
  1624. clear_IO_APIC_pin(apic, pin);
  1625. memset(&entry1, 0, sizeof(entry1));
  1626. entry1.dest_mode = 0; /* physical delivery */
  1627. entry1.mask = 0; /* unmask IRQ now */
  1628. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1629. entry1.delivery_mode = dest_ExtINT;
  1630. entry1.polarity = entry0.polarity;
  1631. entry1.trigger = 0;
  1632. entry1.vector = 0;
  1633. ioapic_write_entry(apic, pin, entry1);
  1634. save_control = CMOS_READ(RTC_CONTROL);
  1635. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1636. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1637. RTC_FREQ_SELECT);
  1638. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1639. i = 100;
  1640. while (i-- > 0) {
  1641. mdelay(10);
  1642. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1643. i -= 10;
  1644. }
  1645. CMOS_WRITE(save_control, RTC_CONTROL);
  1646. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1647. clear_IO_APIC_pin(apic, pin);
  1648. ioapic_write_entry(apic, pin, entry0);
  1649. }
  1650. /*
  1651. * This code may look a bit paranoid, but it's supposed to cooperate with
  1652. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1653. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1654. * fanatically on his truly buggy board.
  1655. */
  1656. static inline void __init check_timer(void)
  1657. {
  1658. int apic1, pin1, apic2, pin2;
  1659. int no_pin1 = 0;
  1660. int vector;
  1661. unsigned int ver;
  1662. unsigned long flags;
  1663. local_irq_save(flags);
  1664. ver = apic_read(APIC_LVR);
  1665. ver = GET_APIC_VERSION(ver);
  1666. /*
  1667. * get/set the timer IRQ vector:
  1668. */
  1669. disable_8259A_irq(0);
  1670. vector = assign_irq_vector(0);
  1671. set_intr_gate(vector, interrupt[0]);
  1672. /*
  1673. * As IRQ0 is to be enabled in the 8259A, the virtual
  1674. * wire has to be disabled in the local APIC. Also
  1675. * timer interrupts need to be acknowledged manually in
  1676. * the 8259A for the i82489DX when using the NMI
  1677. * watchdog as that APIC treats NMIs as level-triggered.
  1678. * The AEOI mode will finish them in the 8259A
  1679. * automatically.
  1680. */
  1681. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1682. init_8259A(1);
  1683. timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
  1684. pin1 = find_isa_irq_pin(0, mp_INT);
  1685. apic1 = find_isa_irq_apic(0, mp_INT);
  1686. pin2 = ioapic_i8259.pin;
  1687. apic2 = ioapic_i8259.apic;
  1688. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  1689. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1690. vector, apic1, pin1, apic2, pin2);
  1691. /*
  1692. * Some BIOS writers are clueless and report the ExtINTA
  1693. * I/O APIC input from the cascaded 8259A as the timer
  1694. * interrupt input. So just in case, if only one pin
  1695. * was found above, try it both directly and through the
  1696. * 8259A.
  1697. */
  1698. if (pin1 == -1) {
  1699. pin1 = pin2;
  1700. apic1 = apic2;
  1701. no_pin1 = 1;
  1702. } else if (pin2 == -1) {
  1703. pin2 = pin1;
  1704. apic2 = apic1;
  1705. }
  1706. if (pin1 != -1) {
  1707. /*
  1708. * Ok, does IRQ0 through the IOAPIC work?
  1709. */
  1710. if (no_pin1) {
  1711. add_pin_to_irq(0, apic1, pin1);
  1712. setup_timer_IRQ0_pin(apic1, pin1, vector);
  1713. }
  1714. unmask_IO_APIC_irq(0);
  1715. if (timer_irq_works()) {
  1716. if (nmi_watchdog == NMI_IO_APIC) {
  1717. setup_nmi();
  1718. enable_8259A_irq(0);
  1719. }
  1720. if (disable_timer_pin_1 > 0)
  1721. clear_IO_APIC_pin(0, pin1);
  1722. goto out;
  1723. }
  1724. clear_IO_APIC_pin(apic1, pin1);
  1725. if (!no_pin1)
  1726. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  1727. "8254 timer not connected to IO-APIC\n");
  1728. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  1729. "(IRQ0) through the 8259A ...\n");
  1730. apic_printk(APIC_QUIET, KERN_INFO
  1731. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  1732. /*
  1733. * legacy devices should be connected to IO APIC #0
  1734. */
  1735. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  1736. setup_timer_IRQ0_pin(apic2, pin2, vector);
  1737. unmask_IO_APIC_irq(0);
  1738. enable_8259A_irq(0);
  1739. if (timer_irq_works()) {
  1740. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  1741. timer_through_8259 = 1;
  1742. if (nmi_watchdog == NMI_IO_APIC) {
  1743. disable_8259A_irq(0);
  1744. setup_nmi();
  1745. enable_8259A_irq(0);
  1746. }
  1747. goto out;
  1748. }
  1749. /*
  1750. * Cleanup, just in case ...
  1751. */
  1752. disable_8259A_irq(0);
  1753. clear_IO_APIC_pin(apic2, pin2);
  1754. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  1755. }
  1756. if (nmi_watchdog == NMI_IO_APIC) {
  1757. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  1758. "through the IO-APIC - disabling NMI Watchdog!\n");
  1759. nmi_watchdog = NMI_NONE;
  1760. }
  1761. timer_ack = 0;
  1762. apic_printk(APIC_QUIET, KERN_INFO
  1763. "...trying to set up timer as Virtual Wire IRQ...\n");
  1764. lapic_register_intr(0, vector);
  1765. apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1766. enable_8259A_irq(0);
  1767. if (timer_irq_works()) {
  1768. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1769. goto out;
  1770. }
  1771. disable_8259A_irq(0);
  1772. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1773. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  1774. apic_printk(APIC_QUIET, KERN_INFO
  1775. "...trying to set up timer as ExtINT IRQ...\n");
  1776. init_8259A(0);
  1777. make_8259A_irq(0);
  1778. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1779. unlock_ExtINT_logic();
  1780. if (timer_irq_works()) {
  1781. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1782. goto out;
  1783. }
  1784. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  1785. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1786. "report. Then try booting with the 'noapic' option.\n");
  1787. out:
  1788. local_irq_restore(flags);
  1789. }
  1790. /*
  1791. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  1792. * to devices. However there may be an I/O APIC pin available for
  1793. * this interrupt regardless. The pin may be left unconnected, but
  1794. * typically it will be reused as an ExtINT cascade interrupt for
  1795. * the master 8259A. In the MPS case such a pin will normally be
  1796. * reported as an ExtINT interrupt in the MP table. With ACPI
  1797. * there is no provision for ExtINT interrupts, and in the absence
  1798. * of an override it would be treated as an ordinary ISA I/O APIC
  1799. * interrupt, that is edge-triggered and unmasked by default. We
  1800. * used to do this, but it caused problems on some systems because
  1801. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  1802. * the same ExtINT cascade interrupt to drive the local APIC of the
  1803. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  1804. * the I/O APIC in all cases now. No actual device should request
  1805. * it anyway. --macro
  1806. */
  1807. #define PIC_IRQS (1 << PIC_CASCADE_IR)
  1808. void __init setup_IO_APIC(void)
  1809. {
  1810. int i;
  1811. /* Reserve all the system vectors. */
  1812. for (i = first_system_vector; i < NR_VECTORS; i++)
  1813. set_bit(i, used_vectors);
  1814. enable_IO_APIC();
  1815. io_apic_irqs = ~PIC_IRQS;
  1816. printk("ENABLING IO-APIC IRQs\n");
  1817. /*
  1818. * Set up IO-APIC IRQ routing.
  1819. */
  1820. if (!acpi_ioapic)
  1821. setup_ioapic_ids_from_mpc();
  1822. sync_Arb_IDs();
  1823. setup_IO_APIC_irqs();
  1824. init_IO_APIC_traps();
  1825. check_timer();
  1826. }
  1827. /*
  1828. * Called after all the initialization is done. If we didnt find any
  1829. * APIC bugs then we can allow the modify fast path
  1830. */
  1831. static int __init io_apic_bug_finalize(void)
  1832. {
  1833. if (sis_apic_bug == -1)
  1834. sis_apic_bug = 0;
  1835. return 0;
  1836. }
  1837. late_initcall(io_apic_bug_finalize);
  1838. struct sysfs_ioapic_data {
  1839. struct sys_device dev;
  1840. struct IO_APIC_route_entry entry[0];
  1841. };
  1842. static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
  1843. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1844. {
  1845. struct IO_APIC_route_entry *entry;
  1846. struct sysfs_ioapic_data *data;
  1847. int i;
  1848. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1849. entry = data->entry;
  1850. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1851. entry[i] = ioapic_read_entry(dev->id, i);
  1852. return 0;
  1853. }
  1854. static int ioapic_resume(struct sys_device *dev)
  1855. {
  1856. struct IO_APIC_route_entry *entry;
  1857. struct sysfs_ioapic_data *data;
  1858. unsigned long flags;
  1859. union IO_APIC_reg_00 reg_00;
  1860. int i;
  1861. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1862. entry = data->entry;
  1863. spin_lock_irqsave(&ioapic_lock, flags);
  1864. reg_00.raw = io_apic_read(dev->id, 0);
  1865. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  1866. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  1867. io_apic_write(dev->id, 0, reg_00.raw);
  1868. }
  1869. spin_unlock_irqrestore(&ioapic_lock, flags);
  1870. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1871. ioapic_write_entry(dev->id, i, entry[i]);
  1872. return 0;
  1873. }
  1874. static struct sysdev_class ioapic_sysdev_class = {
  1875. .name = "ioapic",
  1876. .suspend = ioapic_suspend,
  1877. .resume = ioapic_resume,
  1878. };
  1879. static int __init ioapic_init_sysfs(void)
  1880. {
  1881. struct sys_device *dev;
  1882. int i, size, error = 0;
  1883. error = sysdev_class_register(&ioapic_sysdev_class);
  1884. if (error)
  1885. return error;
  1886. for (i = 0; i < nr_ioapics; i++) {
  1887. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1888. * sizeof(struct IO_APIC_route_entry);
  1889. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  1890. if (!mp_ioapic_data[i]) {
  1891. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1892. continue;
  1893. }
  1894. dev = &mp_ioapic_data[i]->dev;
  1895. dev->id = i;
  1896. dev->cls = &ioapic_sysdev_class;
  1897. error = sysdev_register(dev);
  1898. if (error) {
  1899. kfree(mp_ioapic_data[i]);
  1900. mp_ioapic_data[i] = NULL;
  1901. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1902. continue;
  1903. }
  1904. }
  1905. return 0;
  1906. }
  1907. device_initcall(ioapic_init_sysfs);
  1908. /*
  1909. * Dynamic irq allocate and deallocation
  1910. */
  1911. int create_irq(void)
  1912. {
  1913. /* Allocate an unused irq */
  1914. int irq, new, vector = 0;
  1915. unsigned long flags;
  1916. struct irq_cfg *cfg_new;
  1917. irq = -ENOSPC;
  1918. spin_lock_irqsave(&vector_lock, flags);
  1919. for (new = (nr_irqs - 1); new >= 0; new--) {
  1920. if (platform_legacy_irq(new))
  1921. continue;
  1922. cfg_new = irq_cfg(new);
  1923. if (cfg_new && cfg_new->vector != 0)
  1924. continue;
  1925. if (!cfg_new)
  1926. cfg_new = irq_cfg_alloc(new);
  1927. vector = __assign_irq_vector(new);
  1928. if (likely(vector > 0))
  1929. irq = new;
  1930. break;
  1931. }
  1932. spin_unlock_irqrestore(&vector_lock, flags);
  1933. if (irq >= 0) {
  1934. set_intr_gate(vector, interrupt[irq]);
  1935. dynamic_irq_init(irq);
  1936. }
  1937. return irq;
  1938. }
  1939. void destroy_irq(unsigned int irq)
  1940. {
  1941. unsigned long flags;
  1942. dynamic_irq_cleanup(irq);
  1943. spin_lock_irqsave(&vector_lock, flags);
  1944. clear_bit(irq_cfg(irq)->vector, used_vectors);
  1945. irq_cfg(irq)->vector = 0;
  1946. spin_unlock_irqrestore(&vector_lock, flags);
  1947. }
  1948. /*
  1949. * MSI message composition
  1950. */
  1951. #ifdef CONFIG_PCI_MSI
  1952. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1953. {
  1954. int vector;
  1955. unsigned dest;
  1956. vector = assign_irq_vector(irq);
  1957. if (vector >= 0) {
  1958. dest = cpu_mask_to_apicid(TARGET_CPUS);
  1959. msg->address_hi = MSI_ADDR_BASE_HI;
  1960. msg->address_lo =
  1961. MSI_ADDR_BASE_LO |
  1962. ((INT_DEST_MODE == 0) ?
  1963. MSI_ADDR_DEST_MODE_PHYSICAL:
  1964. MSI_ADDR_DEST_MODE_LOGICAL) |
  1965. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1966. MSI_ADDR_REDIRECTION_CPU:
  1967. MSI_ADDR_REDIRECTION_LOWPRI) |
  1968. MSI_ADDR_DEST_ID(dest);
  1969. msg->data =
  1970. MSI_DATA_TRIGGER_EDGE |
  1971. MSI_DATA_LEVEL_ASSERT |
  1972. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1973. MSI_DATA_DELIVERY_FIXED:
  1974. MSI_DATA_DELIVERY_LOWPRI) |
  1975. MSI_DATA_VECTOR(vector);
  1976. }
  1977. return vector;
  1978. }
  1979. #ifdef CONFIG_SMP
  1980. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  1981. {
  1982. struct msi_msg msg;
  1983. unsigned int dest;
  1984. cpumask_t tmp;
  1985. int vector;
  1986. struct irq_desc *desc;
  1987. cpus_and(tmp, mask, cpu_online_map);
  1988. if (cpus_empty(tmp))
  1989. tmp = TARGET_CPUS;
  1990. vector = assign_irq_vector(irq);
  1991. if (vector < 0)
  1992. return;
  1993. dest = cpu_mask_to_apicid(mask);
  1994. read_msi_msg(irq, &msg);
  1995. msg.data &= ~MSI_DATA_VECTOR_MASK;
  1996. msg.data |= MSI_DATA_VECTOR(vector);
  1997. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  1998. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  1999. write_msi_msg(irq, &msg);
  2000. desc = irq_to_desc(irq);
  2001. desc->affinity = mask;
  2002. }
  2003. #endif /* CONFIG_SMP */
  2004. /*
  2005. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2006. * which implement the MSI or MSI-X Capability Structure.
  2007. */
  2008. static struct irq_chip msi_chip = {
  2009. .name = "PCI-MSI",
  2010. .unmask = unmask_msi_irq,
  2011. .mask = mask_msi_irq,
  2012. .ack = ack_ioapic_irq,
  2013. #ifdef CONFIG_SMP
  2014. .set_affinity = set_msi_irq_affinity,
  2015. #endif
  2016. .retrigger = ioapic_retrigger_irq,
  2017. };
  2018. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  2019. {
  2020. struct msi_msg msg;
  2021. int irq, ret;
  2022. irq = create_irq();
  2023. if (irq < 0)
  2024. return irq;
  2025. ret = msi_compose_msg(dev, irq, &msg);
  2026. if (ret < 0) {
  2027. destroy_irq(irq);
  2028. return ret;
  2029. }
  2030. set_irq_msi(irq, desc);
  2031. write_msi_msg(irq, &msg);
  2032. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
  2033. "edge");
  2034. return 0;
  2035. }
  2036. void arch_teardown_msi_irq(unsigned int irq)
  2037. {
  2038. destroy_irq(irq);
  2039. }
  2040. #endif /* CONFIG_PCI_MSI */
  2041. /*
  2042. * Hypertransport interrupt support
  2043. */
  2044. #ifdef CONFIG_HT_IRQ
  2045. #ifdef CONFIG_SMP
  2046. static void target_ht_irq(unsigned int irq, unsigned int dest)
  2047. {
  2048. struct ht_irq_msg msg;
  2049. fetch_ht_irq_msg(irq, &msg);
  2050. msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
  2051. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  2052. msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
  2053. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  2054. write_ht_irq_msg(irq, &msg);
  2055. }
  2056. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  2057. {
  2058. unsigned int dest;
  2059. cpumask_t tmp;
  2060. struct irq_desc *desc;
  2061. cpus_and(tmp, mask, cpu_online_map);
  2062. if (cpus_empty(tmp))
  2063. tmp = TARGET_CPUS;
  2064. cpus_and(mask, tmp, CPU_MASK_ALL);
  2065. dest = cpu_mask_to_apicid(mask);
  2066. target_ht_irq(irq, dest);
  2067. desc = irq_to_desc(irq);
  2068. desc->affinity = mask;
  2069. }
  2070. #endif
  2071. static struct irq_chip ht_irq_chip = {
  2072. .name = "PCI-HT",
  2073. .mask = mask_ht_irq,
  2074. .unmask = unmask_ht_irq,
  2075. .ack = ack_ioapic_irq,
  2076. #ifdef CONFIG_SMP
  2077. .set_affinity = set_ht_irq_affinity,
  2078. #endif
  2079. .retrigger = ioapic_retrigger_irq,
  2080. };
  2081. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  2082. {
  2083. int vector;
  2084. vector = assign_irq_vector(irq);
  2085. if (vector >= 0) {
  2086. struct ht_irq_msg msg;
  2087. unsigned dest;
  2088. cpumask_t tmp;
  2089. cpus_clear(tmp);
  2090. cpu_set(vector >> 8, tmp);
  2091. dest = cpu_mask_to_apicid(tmp);
  2092. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  2093. msg.address_lo =
  2094. HT_IRQ_LOW_BASE |
  2095. HT_IRQ_LOW_DEST_ID(dest) |
  2096. HT_IRQ_LOW_VECTOR(vector) |
  2097. ((INT_DEST_MODE == 0) ?
  2098. HT_IRQ_LOW_DM_PHYSICAL :
  2099. HT_IRQ_LOW_DM_LOGICAL) |
  2100. HT_IRQ_LOW_RQEOI_EDGE |
  2101. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2102. HT_IRQ_LOW_MT_FIXED :
  2103. HT_IRQ_LOW_MT_ARBITRATED) |
  2104. HT_IRQ_LOW_IRQ_MASKED;
  2105. write_ht_irq_msg(irq, &msg);
  2106. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  2107. handle_edge_irq, "edge");
  2108. }
  2109. return vector;
  2110. }
  2111. #endif /* CONFIG_HT_IRQ */
  2112. /* --------------------------------------------------------------------------
  2113. ACPI-based IOAPIC Configuration
  2114. -------------------------------------------------------------------------- */
  2115. #ifdef CONFIG_ACPI
  2116. int __init io_apic_get_unique_id(int ioapic, int apic_id)
  2117. {
  2118. union IO_APIC_reg_00 reg_00;
  2119. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  2120. physid_mask_t tmp;
  2121. unsigned long flags;
  2122. int i = 0;
  2123. /*
  2124. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  2125. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  2126. * supports up to 16 on one shared APIC bus.
  2127. *
  2128. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  2129. * advantage of new APIC bus architecture.
  2130. */
  2131. if (physids_empty(apic_id_map))
  2132. apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
  2133. spin_lock_irqsave(&ioapic_lock, flags);
  2134. reg_00.raw = io_apic_read(ioapic, 0);
  2135. spin_unlock_irqrestore(&ioapic_lock, flags);
  2136. if (apic_id >= get_physical_broadcast()) {
  2137. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  2138. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  2139. apic_id = reg_00.bits.ID;
  2140. }
  2141. /*
  2142. * Every APIC in a system must have a unique ID or we get lots of nice
  2143. * 'stuck on smp_invalidate_needed IPI wait' messages.
  2144. */
  2145. if (check_apicid_used(apic_id_map, apic_id)) {
  2146. for (i = 0; i < get_physical_broadcast(); i++) {
  2147. if (!check_apicid_used(apic_id_map, i))
  2148. break;
  2149. }
  2150. if (i == get_physical_broadcast())
  2151. panic("Max apic_id exceeded!\n");
  2152. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  2153. "trying %d\n", ioapic, apic_id, i);
  2154. apic_id = i;
  2155. }
  2156. tmp = apicid_to_cpu_present(apic_id);
  2157. physids_or(apic_id_map, apic_id_map, tmp);
  2158. if (reg_00.bits.ID != apic_id) {
  2159. reg_00.bits.ID = apic_id;
  2160. spin_lock_irqsave(&ioapic_lock, flags);
  2161. io_apic_write(ioapic, 0, reg_00.raw);
  2162. reg_00.raw = io_apic_read(ioapic, 0);
  2163. spin_unlock_irqrestore(&ioapic_lock, flags);
  2164. /* Sanity check */
  2165. if (reg_00.bits.ID != apic_id) {
  2166. printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
  2167. return -1;
  2168. }
  2169. }
  2170. apic_printk(APIC_VERBOSE, KERN_INFO
  2171. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  2172. return apic_id;
  2173. }
  2174. int __init io_apic_get_version(int ioapic)
  2175. {
  2176. union IO_APIC_reg_01 reg_01;
  2177. unsigned long flags;
  2178. spin_lock_irqsave(&ioapic_lock, flags);
  2179. reg_01.raw = io_apic_read(ioapic, 1);
  2180. spin_unlock_irqrestore(&ioapic_lock, flags);
  2181. return reg_01.bits.version;
  2182. }
  2183. int __init io_apic_get_redir_entries(int ioapic)
  2184. {
  2185. union IO_APIC_reg_01 reg_01;
  2186. unsigned long flags;
  2187. spin_lock_irqsave(&ioapic_lock, flags);
  2188. reg_01.raw = io_apic_read(ioapic, 1);
  2189. spin_unlock_irqrestore(&ioapic_lock, flags);
  2190. return reg_01.bits.entries;
  2191. }
  2192. int io_apic_set_pci_routing(int ioapic, int pin, int irq, int edge_level, int active_high_low)
  2193. {
  2194. struct IO_APIC_route_entry entry;
  2195. if (!IO_APIC_IRQ(irq)) {
  2196. printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  2197. ioapic);
  2198. return -EINVAL;
  2199. }
  2200. /*
  2201. * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
  2202. * Note that we mask (disable) IRQs now -- these get enabled when the
  2203. * corresponding device driver registers for this IRQ.
  2204. */
  2205. memset(&entry, 0, sizeof(entry));
  2206. entry.delivery_mode = INT_DELIVERY_MODE;
  2207. entry.dest_mode = INT_DEST_MODE;
  2208. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  2209. entry.trigger = edge_level;
  2210. entry.polarity = active_high_low;
  2211. entry.mask = 1;
  2212. /*
  2213. * IRQs < 16 are already in the irq_2_pin[] map
  2214. */
  2215. if (irq >= 16)
  2216. add_pin_to_irq(irq, ioapic, pin);
  2217. entry.vector = assign_irq_vector(irq);
  2218. apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
  2219. "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
  2220. mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
  2221. edge_level, active_high_low);
  2222. ioapic_register_intr(irq, entry.vector, edge_level);
  2223. if (!ioapic && (irq < 16))
  2224. disable_8259A_irq(irq);
  2225. ioapic_write_entry(ioapic, pin, entry);
  2226. return 0;
  2227. }
  2228. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  2229. {
  2230. int i;
  2231. if (skip_ioapic_setup)
  2232. return -1;
  2233. for (i = 0; i < mp_irq_entries; i++)
  2234. if (mp_irqs[i].mp_irqtype == mp_INT &&
  2235. mp_irqs[i].mp_srcbusirq == bus_irq)
  2236. break;
  2237. if (i >= mp_irq_entries)
  2238. return -1;
  2239. *trigger = irq_trigger(i);
  2240. *polarity = irq_polarity(i);
  2241. return 0;
  2242. }
  2243. #endif /* CONFIG_ACPI */
  2244. static int __init parse_disable_timer_pin_1(char *arg)
  2245. {
  2246. disable_timer_pin_1 = 1;
  2247. return 0;
  2248. }
  2249. early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
  2250. static int __init parse_enable_timer_pin_1(char *arg)
  2251. {
  2252. disable_timer_pin_1 = -1;
  2253. return 0;
  2254. }
  2255. early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
  2256. static int __init parse_noapic(char *arg)
  2257. {
  2258. /* disable IO-APIC */
  2259. disable_ioapic_setup();
  2260. return 0;
  2261. }
  2262. early_param("noapic", parse_noapic);
  2263. void __init ioapic_init_mappings(void)
  2264. {
  2265. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  2266. int i;
  2267. for (i = 0; i < nr_ioapics; i++) {
  2268. if (smp_found_config) {
  2269. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  2270. if (!ioapic_phys) {
  2271. printk(KERN_ERR
  2272. "WARNING: bogus zero IO-APIC "
  2273. "address found in MPTABLE, "
  2274. "disabling IO/APIC support!\n");
  2275. smp_found_config = 0;
  2276. skip_ioapic_setup = 1;
  2277. goto fake_ioapic_page;
  2278. }
  2279. } else {
  2280. fake_ioapic_page:
  2281. ioapic_phys = (unsigned long)
  2282. alloc_bootmem_pages(PAGE_SIZE);
  2283. ioapic_phys = __pa(ioapic_phys);
  2284. }
  2285. set_fixmap_nocache(idx, ioapic_phys);
  2286. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  2287. __fix_to_virt(idx), ioapic_phys);
  2288. idx++;
  2289. }
  2290. }