io_apic_32.c 68 KB

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