io_apic.c 69 KB

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