io_apic.c 69 KB

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