io_apic.c 68 KB

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