io_apic.c 69 KB

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