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