io_apic.c 65 KB

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