io_apic.c 65 KB

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