io_apic.c 68 KB

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