io_apic.c 69 KB

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