io_apic.c 65 KB

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