io_apic.c 65 KB

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