io_apic.c 64 KB

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