io_apic.c 64 KB

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