io_apic_32.c 69 KB

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