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 <mach_apic.h>
  40. #include "io_ports.h"
  41. int (*ioapic_renumber_irq)(int ioapic, int irq);
  42. atomic_t irq_mis_count;
  43. static DEFINE_SPINLOCK(ioapic_lock);
  44. /*
  45. * Is the SiS APIC rmw bug present ?
  46. * -1 = don't know, 0 = no, 1 = yes
  47. */
  48. int sis_apic_bug = -1;
  49. /*
  50. * # of IRQ routing registers
  51. */
  52. int nr_ioapic_registers[MAX_IO_APICS];
  53. /*
  54. * Rough estimation of how many shared IRQs there are, can
  55. * be changed anytime.
  56. */
  57. #define MAX_PLUS_SHARED_IRQS NR_IRQS
  58. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  59. /*
  60. * This is performance-critical, we want to do it O(1)
  61. *
  62. * the indexing order of this array favors 1:1 mappings
  63. * between pins and IRQs.
  64. */
  65. static struct irq_pin_list {
  66. int apic, pin, next;
  67. } irq_2_pin[PIN_MAP_SIZE];
  68. int vector_irq[NR_VECTORS] = { [0 ... NR_VECTORS - 1] = -1};
  69. #ifdef CONFIG_PCI_MSI
  70. #define vector_to_irq(vector) \
  71. (platform_legacy_irq(vector) ? vector : vector_irq[vector])
  72. #else
  73. #define vector_to_irq(vector) (vector)
  74. #endif
  75. /*
  76. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  77. * shared ISA-space IRQs, so we have to support them. We are super
  78. * fast in the common case, and fast for shared ISA-space IRQs.
  79. */
  80. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  81. {
  82. static int first_free_entry = NR_IRQS;
  83. struct irq_pin_list *entry = irq_2_pin + irq;
  84. while (entry->next)
  85. entry = irq_2_pin + entry->next;
  86. if (entry->pin != -1) {
  87. entry->next = first_free_entry;
  88. entry = irq_2_pin + entry->next;
  89. if (++first_free_entry >= PIN_MAP_SIZE)
  90. panic("io_apic.c: whoops");
  91. }
  92. entry->apic = apic;
  93. entry->pin = pin;
  94. }
  95. /*
  96. * Reroute an IRQ to a different pin.
  97. */
  98. static void __init replace_pin_at_irq(unsigned int irq,
  99. int oldapic, int oldpin,
  100. int newapic, int newpin)
  101. {
  102. struct irq_pin_list *entry = irq_2_pin + irq;
  103. while (1) {
  104. if (entry->apic == oldapic && entry->pin == oldpin) {
  105. entry->apic = newapic;
  106. entry->pin = newpin;
  107. }
  108. if (!entry->next)
  109. break;
  110. entry = irq_2_pin + entry->next;
  111. }
  112. }
  113. static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
  114. {
  115. struct irq_pin_list *entry = irq_2_pin + irq;
  116. unsigned int pin, reg;
  117. for (;;) {
  118. pin = entry->pin;
  119. if (pin == -1)
  120. break;
  121. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  122. reg &= ~disable;
  123. reg |= enable;
  124. io_apic_modify(entry->apic, 0x10 + pin*2, reg);
  125. if (!entry->next)
  126. break;
  127. entry = irq_2_pin + entry->next;
  128. }
  129. }
  130. /* mask = 1 */
  131. static void __mask_IO_APIC_irq (unsigned int irq)
  132. {
  133. __modify_IO_APIC_irq(irq, 0x00010000, 0);
  134. }
  135. /* mask = 0 */
  136. static void __unmask_IO_APIC_irq (unsigned int irq)
  137. {
  138. __modify_IO_APIC_irq(irq, 0, 0x00010000);
  139. }
  140. /* mask = 1, trigger = 0 */
  141. static void __mask_and_edge_IO_APIC_irq (unsigned int irq)
  142. {
  143. __modify_IO_APIC_irq(irq, 0x00010000, 0x00008000);
  144. }
  145. /* mask = 0, trigger = 1 */
  146. static void __unmask_and_level_IO_APIC_irq (unsigned int irq)
  147. {
  148. __modify_IO_APIC_irq(irq, 0x00008000, 0x00010000);
  149. }
  150. static void mask_IO_APIC_irq (unsigned int irq)
  151. {
  152. unsigned long flags;
  153. spin_lock_irqsave(&ioapic_lock, flags);
  154. __mask_IO_APIC_irq(irq);
  155. spin_unlock_irqrestore(&ioapic_lock, flags);
  156. }
  157. static void unmask_IO_APIC_irq (unsigned int irq)
  158. {
  159. unsigned long flags;
  160. spin_lock_irqsave(&ioapic_lock, flags);
  161. __unmask_IO_APIC_irq(irq);
  162. spin_unlock_irqrestore(&ioapic_lock, flags);
  163. }
  164. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  165. {
  166. struct IO_APIC_route_entry entry;
  167. unsigned long flags;
  168. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  169. spin_lock_irqsave(&ioapic_lock, flags);
  170. *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  171. *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  172. spin_unlock_irqrestore(&ioapic_lock, flags);
  173. if (entry.delivery_mode == dest_SMI)
  174. return;
  175. /*
  176. * Disable it in the IO-APIC irq-routing table:
  177. */
  178. memset(&entry, 0, sizeof(entry));
  179. entry.mask = 1;
  180. spin_lock_irqsave(&ioapic_lock, flags);
  181. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
  182. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
  183. spin_unlock_irqrestore(&ioapic_lock, flags);
  184. }
  185. static void clear_IO_APIC (void)
  186. {
  187. int apic, pin;
  188. for (apic = 0; apic < nr_ioapics; apic++)
  189. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  190. clear_IO_APIC_pin(apic, pin);
  191. }
  192. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
  193. {
  194. unsigned long flags;
  195. int pin;
  196. struct irq_pin_list *entry = irq_2_pin + irq;
  197. unsigned int apicid_value;
  198. apicid_value = cpu_mask_to_apicid(cpumask);
  199. /* Prepare to do the io_apic_write */
  200. apicid_value = apicid_value << 24;
  201. spin_lock_irqsave(&ioapic_lock, flags);
  202. for (;;) {
  203. pin = entry->pin;
  204. if (pin == -1)
  205. break;
  206. io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
  207. if (!entry->next)
  208. break;
  209. entry = irq_2_pin + entry->next;
  210. }
  211. spin_unlock_irqrestore(&ioapic_lock, flags);
  212. }
  213. #if defined(CONFIG_IRQBALANCE)
  214. # include <asm/processor.h> /* kernel_thread() */
  215. # include <linux/kernel_stat.h> /* kstat */
  216. # include <linux/slab.h> /* kmalloc() */
  217. # include <linux/timer.h> /* time_after() */
  218. # ifdef CONFIG_BALANCED_IRQ_DEBUG
  219. # define TDprintk(x...) do { printk("<%ld:%s:%d>: ", jiffies, __FILE__, __LINE__); printk(x); } while (0)
  220. # define Dprintk(x...) do { TDprintk(x); } while (0)
  221. # else
  222. # define TDprintk(x...)
  223. # define Dprintk(x...)
  224. # endif
  225. cpumask_t __cacheline_aligned pending_irq_balance_cpumask[NR_IRQS];
  226. #define IRQBALANCE_CHECK_ARCH -999
  227. static int irqbalance_disabled = IRQBALANCE_CHECK_ARCH;
  228. static int physical_balance = 0;
  229. static struct irq_cpu_info {
  230. unsigned long * last_irq;
  231. unsigned long * irq_delta;
  232. unsigned long irq;
  233. } irq_cpu_data[NR_CPUS];
  234. #define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
  235. #define LAST_CPU_IRQ(cpu,irq) (irq_cpu_data[cpu].last_irq[irq])
  236. #define IRQ_DELTA(cpu,irq) (irq_cpu_data[cpu].irq_delta[irq])
  237. #define IDLE_ENOUGH(cpu,now) \
  238. (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
  239. #define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
  240. #define CPU_TO_PACKAGEINDEX(i) (first_cpu(cpu_sibling_map[i]))
  241. #define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
  242. #define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
  243. #define BALANCED_IRQ_MORE_DELTA (HZ/10)
  244. #define BALANCED_IRQ_LESS_DELTA (HZ)
  245. static long balanced_irq_interval = MAX_BALANCED_IRQ_INTERVAL;
  246. static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
  247. unsigned long now, int direction)
  248. {
  249. int search_idle = 1;
  250. int cpu = curr_cpu;
  251. goto inside;
  252. do {
  253. if (unlikely(cpu == curr_cpu))
  254. search_idle = 0;
  255. inside:
  256. if (direction == 1) {
  257. cpu++;
  258. if (cpu >= NR_CPUS)
  259. cpu = 0;
  260. } else {
  261. cpu--;
  262. if (cpu == -1)
  263. cpu = NR_CPUS-1;
  264. }
  265. } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) ||
  266. (search_idle && !IDLE_ENOUGH(cpu,now)));
  267. return cpu;
  268. }
  269. static inline void balance_irq(int cpu, int irq)
  270. {
  271. unsigned long now = jiffies;
  272. cpumask_t allowed_mask;
  273. unsigned int new_cpu;
  274. if (irqbalance_disabled)
  275. return;
  276. cpus_and(allowed_mask, cpu_online_map, irq_affinity[irq]);
  277. new_cpu = move(cpu, allowed_mask, now, 1);
  278. if (cpu != new_cpu) {
  279. irq_desc_t *desc = irq_desc + irq;
  280. unsigned long flags;
  281. spin_lock_irqsave(&desc->lock, flags);
  282. pending_irq_balance_cpumask[irq] = cpumask_of_cpu(new_cpu);
  283. spin_unlock_irqrestore(&desc->lock, flags);
  284. }
  285. }
  286. static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
  287. {
  288. int i, j;
  289. Dprintk("Rotating IRQs among CPUs.\n");
  290. for (i = 0; i < NR_CPUS; i++) {
  291. for (j = 0; cpu_online(i) && (j < NR_IRQS); j++) {
  292. if (!irq_desc[j].action)
  293. continue;
  294. /* Is it a significant load ? */
  295. if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) <
  296. useful_load_threshold)
  297. continue;
  298. balance_irq(i, j);
  299. }
  300. }
  301. balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
  302. balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
  303. return;
  304. }
  305. static void do_irq_balance(void)
  306. {
  307. int i, j;
  308. unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
  309. unsigned long move_this_load = 0;
  310. int max_loaded = 0, min_loaded = 0;
  311. int load;
  312. unsigned long useful_load_threshold = balanced_irq_interval + 10;
  313. int selected_irq;
  314. int tmp_loaded, first_attempt = 1;
  315. unsigned long tmp_cpu_irq;
  316. unsigned long imbalance = 0;
  317. cpumask_t allowed_mask, target_cpu_mask, tmp;
  318. for (i = 0; i < NR_CPUS; i++) {
  319. int package_index;
  320. CPU_IRQ(i) = 0;
  321. if (!cpu_online(i))
  322. continue;
  323. package_index = CPU_TO_PACKAGEINDEX(i);
  324. for (j = 0; j < NR_IRQS; j++) {
  325. unsigned long value_now, delta;
  326. /* Is this an active IRQ? */
  327. if (!irq_desc[j].action)
  328. continue;
  329. if ( package_index == i )
  330. IRQ_DELTA(package_index,j) = 0;
  331. /* Determine the total count per processor per IRQ */
  332. value_now = (unsigned long) kstat_cpu(i).irqs[j];
  333. /* Determine the activity per processor per IRQ */
  334. delta = value_now - LAST_CPU_IRQ(i,j);
  335. /* Update last_cpu_irq[][] for the next time */
  336. LAST_CPU_IRQ(i,j) = value_now;
  337. /* Ignore IRQs whose rate is less than the clock */
  338. if (delta < useful_load_threshold)
  339. continue;
  340. /* update the load for the processor or package total */
  341. IRQ_DELTA(package_index,j) += delta;
  342. /* Keep track of the higher numbered sibling as well */
  343. if (i != package_index)
  344. CPU_IRQ(i) += delta;
  345. /*
  346. * We have sibling A and sibling B in the package
  347. *
  348. * cpu_irq[A] = load for cpu A + load for cpu B
  349. * cpu_irq[B] = load for cpu B
  350. */
  351. CPU_IRQ(package_index) += delta;
  352. }
  353. }
  354. /* Find the least loaded processor package */
  355. for (i = 0; i < NR_CPUS; i++) {
  356. if (!cpu_online(i))
  357. continue;
  358. if (i != CPU_TO_PACKAGEINDEX(i))
  359. continue;
  360. if (min_cpu_irq > CPU_IRQ(i)) {
  361. min_cpu_irq = CPU_IRQ(i);
  362. min_loaded = i;
  363. }
  364. }
  365. max_cpu_irq = ULONG_MAX;
  366. tryanothercpu:
  367. /* Look for heaviest loaded processor.
  368. * We may come back to get the next heaviest loaded processor.
  369. * Skip processors with trivial loads.
  370. */
  371. tmp_cpu_irq = 0;
  372. tmp_loaded = -1;
  373. for (i = 0; i < NR_CPUS; i++) {
  374. if (!cpu_online(i))
  375. continue;
  376. if (i != CPU_TO_PACKAGEINDEX(i))
  377. continue;
  378. if (max_cpu_irq <= CPU_IRQ(i))
  379. continue;
  380. if (tmp_cpu_irq < CPU_IRQ(i)) {
  381. tmp_cpu_irq = CPU_IRQ(i);
  382. tmp_loaded = i;
  383. }
  384. }
  385. if (tmp_loaded == -1) {
  386. /* In the case of small number of heavy interrupt sources,
  387. * loading some of the cpus too much. We use Ingo's original
  388. * approach to rotate them around.
  389. */
  390. if (!first_attempt && imbalance >= useful_load_threshold) {
  391. rotate_irqs_among_cpus(useful_load_threshold);
  392. return;
  393. }
  394. goto not_worth_the_effort;
  395. }
  396. first_attempt = 0; /* heaviest search */
  397. max_cpu_irq = tmp_cpu_irq; /* load */
  398. max_loaded = tmp_loaded; /* processor */
  399. imbalance = (max_cpu_irq - min_cpu_irq) / 2;
  400. Dprintk("max_loaded cpu = %d\n", max_loaded);
  401. Dprintk("min_loaded cpu = %d\n", min_loaded);
  402. Dprintk("max_cpu_irq load = %ld\n", max_cpu_irq);
  403. Dprintk("min_cpu_irq load = %ld\n", min_cpu_irq);
  404. Dprintk("load imbalance = %lu\n", imbalance);
  405. /* if imbalance is less than approx 10% of max load, then
  406. * observe diminishing returns action. - quit
  407. */
  408. if (imbalance < (max_cpu_irq >> 3)) {
  409. Dprintk("Imbalance too trivial\n");
  410. goto not_worth_the_effort;
  411. }
  412. tryanotherirq:
  413. /* if we select an IRQ to move that can't go where we want, then
  414. * see if there is another one to try.
  415. */
  416. move_this_load = 0;
  417. selected_irq = -1;
  418. for (j = 0; j < NR_IRQS; j++) {
  419. /* Is this an active IRQ? */
  420. if (!irq_desc[j].action)
  421. continue;
  422. if (imbalance <= IRQ_DELTA(max_loaded,j))
  423. continue;
  424. /* Try to find the IRQ that is closest to the imbalance
  425. * without going over.
  426. */
  427. if (move_this_load < IRQ_DELTA(max_loaded,j)) {
  428. move_this_load = IRQ_DELTA(max_loaded,j);
  429. selected_irq = j;
  430. }
  431. }
  432. if (selected_irq == -1) {
  433. goto tryanothercpu;
  434. }
  435. imbalance = move_this_load;
  436. /* For physical_balance case, we accumlated both load
  437. * values in the one of the siblings cpu_irq[],
  438. * to use the same code for physical and logical processors
  439. * as much as possible.
  440. *
  441. * NOTE: the cpu_irq[] array holds the sum of the load for
  442. * sibling A and sibling B in the slot for the lowest numbered
  443. * sibling (A), _AND_ the load for sibling B in the slot for
  444. * the higher numbered sibling.
  445. *
  446. * We seek the least loaded sibling by making the comparison
  447. * (A+B)/2 vs B
  448. */
  449. load = CPU_IRQ(min_loaded) >> 1;
  450. for_each_cpu_mask(j, cpu_sibling_map[min_loaded]) {
  451. if (load > CPU_IRQ(j)) {
  452. /* This won't change cpu_sibling_map[min_loaded] */
  453. load = CPU_IRQ(j);
  454. min_loaded = j;
  455. }
  456. }
  457. cpus_and(allowed_mask, cpu_online_map, irq_affinity[selected_irq]);
  458. target_cpu_mask = cpumask_of_cpu(min_loaded);
  459. cpus_and(tmp, target_cpu_mask, allowed_mask);
  460. if (!cpus_empty(tmp)) {
  461. irq_desc_t *desc = irq_desc + selected_irq;
  462. unsigned long flags;
  463. Dprintk("irq = %d moved to cpu = %d\n",
  464. selected_irq, min_loaded);
  465. /* mark for change destination */
  466. spin_lock_irqsave(&desc->lock, flags);
  467. pending_irq_balance_cpumask[selected_irq] =
  468. cpumask_of_cpu(min_loaded);
  469. spin_unlock_irqrestore(&desc->lock, flags);
  470. /* Since we made a change, come back sooner to
  471. * check for more variation.
  472. */
  473. balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
  474. balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
  475. return;
  476. }
  477. goto tryanotherirq;
  478. not_worth_the_effort:
  479. /*
  480. * if we did not find an IRQ to move, then adjust the time interval
  481. * upward
  482. */
  483. balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
  484. balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
  485. Dprintk("IRQ worth rotating not found\n");
  486. return;
  487. }
  488. static int balanced_irq(void *unused)
  489. {
  490. int i;
  491. unsigned long prev_balance_time = jiffies;
  492. long time_remaining = balanced_irq_interval;
  493. daemonize("kirqd");
  494. /* push everything to CPU 0 to give us a starting point. */
  495. for (i = 0 ; i < NR_IRQS ; i++) {
  496. pending_irq_balance_cpumask[i] = cpumask_of_cpu(0);
  497. }
  498. for ( ; ; ) {
  499. set_current_state(TASK_INTERRUPTIBLE);
  500. time_remaining = schedule_timeout(time_remaining);
  501. try_to_freeze();
  502. if (time_after(jiffies,
  503. prev_balance_time+balanced_irq_interval)) {
  504. preempt_disable();
  505. do_irq_balance();
  506. prev_balance_time = jiffies;
  507. time_remaining = balanced_irq_interval;
  508. preempt_enable();
  509. }
  510. }
  511. return 0;
  512. }
  513. static int __init balanced_irq_init(void)
  514. {
  515. int i;
  516. struct cpuinfo_x86 *c;
  517. cpumask_t tmp;
  518. cpus_shift_right(tmp, cpu_online_map, 2);
  519. c = &boot_cpu_data;
  520. /* When not overwritten by the command line ask subarchitecture. */
  521. if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
  522. irqbalance_disabled = NO_BALANCE_IRQ;
  523. if (irqbalance_disabled)
  524. return 0;
  525. /* disable irqbalance completely if there is only one processor online */
  526. if (num_online_cpus() < 2) {
  527. irqbalance_disabled = 1;
  528. return 0;
  529. }
  530. /*
  531. * Enable physical balance only if more than 1 physical processor
  532. * is present
  533. */
  534. if (smp_num_siblings > 1 && !cpus_empty(tmp))
  535. physical_balance = 1;
  536. for (i = 0; i < NR_CPUS; i++) {
  537. if (!cpu_online(i))
  538. continue;
  539. irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
  540. irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
  541. if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
  542. printk(KERN_ERR "balanced_irq_init: out of memory");
  543. goto failed;
  544. }
  545. memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS);
  546. memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS);
  547. }
  548. printk(KERN_INFO "Starting balanced_irq\n");
  549. if (kernel_thread(balanced_irq, NULL, CLONE_KERNEL) >= 0)
  550. return 0;
  551. else
  552. printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
  553. failed:
  554. for (i = 0; i < NR_CPUS; i++) {
  555. kfree(irq_cpu_data[i].irq_delta);
  556. kfree(irq_cpu_data[i].last_irq);
  557. }
  558. return 0;
  559. }
  560. int __init irqbalance_disable(char *str)
  561. {
  562. irqbalance_disabled = 1;
  563. return 0;
  564. }
  565. __setup("noirqbalance", irqbalance_disable);
  566. static inline void move_irq(int irq)
  567. {
  568. /* note - we hold the desc->lock */
  569. if (unlikely(!cpus_empty(pending_irq_balance_cpumask[irq]))) {
  570. set_ioapic_affinity_irq(irq, pending_irq_balance_cpumask[irq]);
  571. cpus_clear(pending_irq_balance_cpumask[irq]);
  572. }
  573. }
  574. late_initcall(balanced_irq_init);
  575. #else /* !CONFIG_IRQBALANCE */
  576. static inline void move_irq(int irq) { }
  577. #endif /* CONFIG_IRQBALANCE */
  578. #ifndef CONFIG_SMP
  579. void fastcall send_IPI_self(int vector)
  580. {
  581. unsigned int cfg;
  582. /*
  583. * Wait for idle.
  584. */
  585. apic_wait_icr_idle();
  586. cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
  587. /*
  588. * Send the IPI. The write to APIC_ICR fires this off.
  589. */
  590. apic_write_around(APIC_ICR, cfg);
  591. }
  592. #endif /* !CONFIG_SMP */
  593. /*
  594. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  595. * specific CPU-side IRQs.
  596. */
  597. #define MAX_PIRQS 8
  598. static int pirq_entries [MAX_PIRQS];
  599. static int pirqs_enabled;
  600. int skip_ioapic_setup;
  601. static int __init ioapic_setup(char *str)
  602. {
  603. skip_ioapic_setup = 1;
  604. return 1;
  605. }
  606. __setup("noapic", ioapic_setup);
  607. static int __init ioapic_pirq_setup(char *str)
  608. {
  609. int i, max;
  610. int ints[MAX_PIRQS+1];
  611. get_options(str, ARRAY_SIZE(ints), ints);
  612. for (i = 0; i < MAX_PIRQS; i++)
  613. pirq_entries[i] = -1;
  614. pirqs_enabled = 1;
  615. apic_printk(APIC_VERBOSE, KERN_INFO
  616. "PIRQ redirection, working around broken MP-BIOS.\n");
  617. max = MAX_PIRQS;
  618. if (ints[0] < MAX_PIRQS)
  619. max = ints[0];
  620. for (i = 0; i < max; i++) {
  621. apic_printk(APIC_VERBOSE, KERN_DEBUG
  622. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  623. /*
  624. * PIRQs are mapped upside down, usually.
  625. */
  626. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  627. }
  628. return 1;
  629. }
  630. __setup("pirq=", ioapic_pirq_setup);
  631. /*
  632. * Find the IRQ entry number of a certain pin.
  633. */
  634. static int find_irq_entry(int apic, int pin, int type)
  635. {
  636. int i;
  637. for (i = 0; i < mp_irq_entries; i++)
  638. if (mp_irqs[i].mpc_irqtype == type &&
  639. (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
  640. mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
  641. mp_irqs[i].mpc_dstirq == pin)
  642. return i;
  643. return -1;
  644. }
  645. /*
  646. * Find the pin to which IRQ[irq] (ISA) is connected
  647. */
  648. static int find_isa_irq_pin(int irq, int type)
  649. {
  650. int i;
  651. for (i = 0; i < mp_irq_entries; i++) {
  652. int lbus = mp_irqs[i].mpc_srcbus;
  653. if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
  654. mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
  655. mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
  656. mp_bus_id_to_type[lbus] == MP_BUS_NEC98
  657. ) &&
  658. (mp_irqs[i].mpc_irqtype == type) &&
  659. (mp_irqs[i].mpc_srcbusirq == irq))
  660. return mp_irqs[i].mpc_dstirq;
  661. }
  662. return -1;
  663. }
  664. /*
  665. * Find a specific PCI IRQ entry.
  666. * Not an __init, possibly needed by modules
  667. */
  668. static int pin_2_irq(int idx, int apic, int pin);
  669. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  670. {
  671. int apic, i, best_guess = -1;
  672. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
  673. "slot:%d, pin:%d.\n", bus, slot, pin);
  674. if (mp_bus_id_to_pci_bus[bus] == -1) {
  675. printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  676. return -1;
  677. }
  678. for (i = 0; i < mp_irq_entries; i++) {
  679. int lbus = mp_irqs[i].mpc_srcbus;
  680. for (apic = 0; apic < nr_ioapics; apic++)
  681. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
  682. mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
  683. break;
  684. if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
  685. !mp_irqs[i].mpc_irqtype &&
  686. (bus == lbus) &&
  687. (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
  688. int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
  689. if (!(apic || IO_APIC_IRQ(irq)))
  690. continue;
  691. if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
  692. return irq;
  693. /*
  694. * Use the first all-but-pin matching entry as a
  695. * best-guess fuzzy result for broken mptables.
  696. */
  697. if (best_guess < 0)
  698. best_guess = irq;
  699. }
  700. }
  701. return best_guess;
  702. }
  703. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  704. /*
  705. * This function currently is only a helper for the i386 smp boot process where
  706. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  707. * so mask in all cases should simply be TARGET_CPUS
  708. */
  709. void __init setup_ioapic_dest(void)
  710. {
  711. int pin, ioapic, irq, irq_entry;
  712. if (skip_ioapic_setup == 1)
  713. return;
  714. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  715. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  716. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  717. if (irq_entry == -1)
  718. continue;
  719. irq = pin_2_irq(irq_entry, ioapic, pin);
  720. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  721. }
  722. }
  723. }
  724. /*
  725. * EISA Edge/Level control register, ELCR
  726. */
  727. static int EISA_ELCR(unsigned int irq)
  728. {
  729. if (irq < 16) {
  730. unsigned int port = 0x4d0 + (irq >> 3);
  731. return (inb(port) >> (irq & 7)) & 1;
  732. }
  733. apic_printk(APIC_VERBOSE, KERN_INFO
  734. "Broken MPtable reports ISA irq %d\n", irq);
  735. return 0;
  736. }
  737. /* EISA interrupts are always polarity zero and can be edge or level
  738. * trigger depending on the ELCR value. If an interrupt is listed as
  739. * EISA conforming in the MP table, that means its trigger type must
  740. * be read in from the ELCR */
  741. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
  742. #define default_EISA_polarity(idx) (0)
  743. /* ISA interrupts are always polarity zero edge triggered,
  744. * when listed as conforming in the MP table. */
  745. #define default_ISA_trigger(idx) (0)
  746. #define default_ISA_polarity(idx) (0)
  747. /* PCI interrupts are always polarity one level triggered,
  748. * when listed as conforming in the MP table. */
  749. #define default_PCI_trigger(idx) (1)
  750. #define default_PCI_polarity(idx) (1)
  751. /* MCA interrupts are always polarity zero level triggered,
  752. * when listed as conforming in the MP table. */
  753. #define default_MCA_trigger(idx) (1)
  754. #define default_MCA_polarity(idx) (0)
  755. /* NEC98 interrupts are always polarity zero edge triggered,
  756. * when listed as conforming in the MP table. */
  757. #define default_NEC98_trigger(idx) (0)
  758. #define default_NEC98_polarity(idx) (0)
  759. static int __init MPBIOS_polarity(int idx)
  760. {
  761. int bus = mp_irqs[idx].mpc_srcbus;
  762. int polarity;
  763. /*
  764. * Determine IRQ line polarity (high active or low active):
  765. */
  766. switch (mp_irqs[idx].mpc_irqflag & 3)
  767. {
  768. case 0: /* conforms, ie. bus-type dependent polarity */
  769. {
  770. switch (mp_bus_id_to_type[bus])
  771. {
  772. case MP_BUS_ISA: /* ISA pin */
  773. {
  774. polarity = default_ISA_polarity(idx);
  775. break;
  776. }
  777. case MP_BUS_EISA: /* EISA pin */
  778. {
  779. polarity = default_EISA_polarity(idx);
  780. break;
  781. }
  782. case MP_BUS_PCI: /* PCI pin */
  783. {
  784. polarity = default_PCI_polarity(idx);
  785. break;
  786. }
  787. case MP_BUS_MCA: /* MCA pin */
  788. {
  789. polarity = default_MCA_polarity(idx);
  790. break;
  791. }
  792. case MP_BUS_NEC98: /* NEC 98 pin */
  793. {
  794. polarity = default_NEC98_polarity(idx);
  795. break;
  796. }
  797. default:
  798. {
  799. printk(KERN_WARNING "broken BIOS!!\n");
  800. polarity = 1;
  801. break;
  802. }
  803. }
  804. break;
  805. }
  806. case 1: /* high active */
  807. {
  808. polarity = 0;
  809. break;
  810. }
  811. case 2: /* reserved */
  812. {
  813. printk(KERN_WARNING "broken BIOS!!\n");
  814. polarity = 1;
  815. break;
  816. }
  817. case 3: /* low active */
  818. {
  819. polarity = 1;
  820. break;
  821. }
  822. default: /* invalid */
  823. {
  824. printk(KERN_WARNING "broken BIOS!!\n");
  825. polarity = 1;
  826. break;
  827. }
  828. }
  829. return polarity;
  830. }
  831. static int MPBIOS_trigger(int idx)
  832. {
  833. int bus = mp_irqs[idx].mpc_srcbus;
  834. int trigger;
  835. /*
  836. * Determine IRQ trigger mode (edge or level sensitive):
  837. */
  838. switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
  839. {
  840. case 0: /* conforms, ie. bus-type dependent */
  841. {
  842. switch (mp_bus_id_to_type[bus])
  843. {
  844. case MP_BUS_ISA: /* ISA pin */
  845. {
  846. trigger = default_ISA_trigger(idx);
  847. break;
  848. }
  849. case MP_BUS_EISA: /* EISA pin */
  850. {
  851. trigger = default_EISA_trigger(idx);
  852. break;
  853. }
  854. case MP_BUS_PCI: /* PCI pin */
  855. {
  856. trigger = default_PCI_trigger(idx);
  857. break;
  858. }
  859. case MP_BUS_MCA: /* MCA pin */
  860. {
  861. trigger = default_MCA_trigger(idx);
  862. break;
  863. }
  864. case MP_BUS_NEC98: /* NEC 98 pin */
  865. {
  866. trigger = default_NEC98_trigger(idx);
  867. break;
  868. }
  869. default:
  870. {
  871. printk(KERN_WARNING "broken BIOS!!\n");
  872. trigger = 1;
  873. break;
  874. }
  875. }
  876. break;
  877. }
  878. case 1: /* edge */
  879. {
  880. trigger = 0;
  881. break;
  882. }
  883. case 2: /* reserved */
  884. {
  885. printk(KERN_WARNING "broken BIOS!!\n");
  886. trigger = 1;
  887. break;
  888. }
  889. case 3: /* level */
  890. {
  891. trigger = 1;
  892. break;
  893. }
  894. default: /* invalid */
  895. {
  896. printk(KERN_WARNING "broken BIOS!!\n");
  897. trigger = 0;
  898. break;
  899. }
  900. }
  901. return trigger;
  902. }
  903. static inline int irq_polarity(int idx)
  904. {
  905. return MPBIOS_polarity(idx);
  906. }
  907. static inline int irq_trigger(int idx)
  908. {
  909. return MPBIOS_trigger(idx);
  910. }
  911. static int pin_2_irq(int idx, int apic, int pin)
  912. {
  913. int irq, i;
  914. int bus = mp_irqs[idx].mpc_srcbus;
  915. /*
  916. * Debugging check, we are in big trouble if this message pops up!
  917. */
  918. if (mp_irqs[idx].mpc_dstirq != pin)
  919. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  920. switch (mp_bus_id_to_type[bus])
  921. {
  922. case MP_BUS_ISA: /* ISA pin */
  923. case MP_BUS_EISA:
  924. case MP_BUS_MCA:
  925. case MP_BUS_NEC98:
  926. {
  927. irq = mp_irqs[idx].mpc_srcbusirq;
  928. break;
  929. }
  930. case MP_BUS_PCI: /* PCI pin */
  931. {
  932. /*
  933. * PCI IRQs are mapped in order
  934. */
  935. i = irq = 0;
  936. while (i < apic)
  937. irq += nr_ioapic_registers[i++];
  938. irq += pin;
  939. /*
  940. * For MPS mode, so far only needed by ES7000 platform
  941. */
  942. if (ioapic_renumber_irq)
  943. irq = ioapic_renumber_irq(apic, irq);
  944. break;
  945. }
  946. default:
  947. {
  948. printk(KERN_ERR "unknown bus type %d.\n",bus);
  949. irq = 0;
  950. break;
  951. }
  952. }
  953. /*
  954. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  955. */
  956. if ((pin >= 16) && (pin <= 23)) {
  957. if (pirq_entries[pin-16] != -1) {
  958. if (!pirq_entries[pin-16]) {
  959. apic_printk(APIC_VERBOSE, KERN_DEBUG
  960. "disabling PIRQ%d\n", pin-16);
  961. } else {
  962. irq = pirq_entries[pin-16];
  963. apic_printk(APIC_VERBOSE, KERN_DEBUG
  964. "using PIRQ%d -> IRQ %d\n",
  965. pin-16, irq);
  966. }
  967. }
  968. }
  969. return irq;
  970. }
  971. static inline int IO_APIC_irq_trigger(int irq)
  972. {
  973. int apic, idx, pin;
  974. for (apic = 0; apic < nr_ioapics; apic++) {
  975. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  976. idx = find_irq_entry(apic,pin,mp_INT);
  977. if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
  978. return irq_trigger(idx);
  979. }
  980. }
  981. /*
  982. * nonexistent IRQs are edge default
  983. */
  984. return 0;
  985. }
  986. /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
  987. u8 irq_vector[NR_IRQ_VECTORS] = { FIRST_DEVICE_VECTOR , 0 };
  988. int assign_irq_vector(int irq)
  989. {
  990. static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
  991. BUG_ON(irq >= NR_IRQ_VECTORS);
  992. if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0)
  993. return IO_APIC_VECTOR(irq);
  994. next:
  995. current_vector += 8;
  996. if (current_vector == SYSCALL_VECTOR)
  997. goto next;
  998. if (current_vector >= FIRST_SYSTEM_VECTOR) {
  999. offset++;
  1000. if (!(offset%8))
  1001. return -ENOSPC;
  1002. current_vector = FIRST_DEVICE_VECTOR + offset;
  1003. }
  1004. vector_irq[current_vector] = irq;
  1005. if (irq != AUTO_ASSIGN)
  1006. IO_APIC_VECTOR(irq) = current_vector;
  1007. return current_vector;
  1008. }
  1009. static struct hw_interrupt_type ioapic_level_type;
  1010. static struct hw_interrupt_type ioapic_edge_type;
  1011. #define IOAPIC_AUTO -1
  1012. #define IOAPIC_EDGE 0
  1013. #define IOAPIC_LEVEL 1
  1014. static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
  1015. {
  1016. if (use_pci_vector() && !platform_legacy_irq(irq)) {
  1017. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1018. trigger == IOAPIC_LEVEL)
  1019. irq_desc[vector].handler = &ioapic_level_type;
  1020. else
  1021. irq_desc[vector].handler = &ioapic_edge_type;
  1022. set_intr_gate(vector, interrupt[vector]);
  1023. } else {
  1024. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1025. trigger == IOAPIC_LEVEL)
  1026. irq_desc[irq].handler = &ioapic_level_type;
  1027. else
  1028. irq_desc[irq].handler = &ioapic_edge_type;
  1029. set_intr_gate(vector, interrupt[irq]);
  1030. }
  1031. }
  1032. static void __init setup_IO_APIC_irqs(void)
  1033. {
  1034. struct IO_APIC_route_entry entry;
  1035. int apic, pin, idx, irq, first_notcon = 1, vector;
  1036. unsigned long flags;
  1037. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1038. for (apic = 0; apic < nr_ioapics; apic++) {
  1039. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1040. /*
  1041. * add it to the IO-APIC irq-routing table:
  1042. */
  1043. memset(&entry,0,sizeof(entry));
  1044. entry.delivery_mode = INT_DELIVERY_MODE;
  1045. entry.dest_mode = INT_DEST_MODE;
  1046. entry.mask = 0; /* enable IRQ */
  1047. entry.dest.logical.logical_dest =
  1048. cpu_mask_to_apicid(TARGET_CPUS);
  1049. idx = find_irq_entry(apic,pin,mp_INT);
  1050. if (idx == -1) {
  1051. if (first_notcon) {
  1052. apic_printk(APIC_VERBOSE, KERN_DEBUG
  1053. " IO-APIC (apicid-pin) %d-%d",
  1054. mp_ioapics[apic].mpc_apicid,
  1055. pin);
  1056. first_notcon = 0;
  1057. } else
  1058. apic_printk(APIC_VERBOSE, ", %d-%d",
  1059. mp_ioapics[apic].mpc_apicid, pin);
  1060. continue;
  1061. }
  1062. entry.trigger = irq_trigger(idx);
  1063. entry.polarity = irq_polarity(idx);
  1064. if (irq_trigger(idx)) {
  1065. entry.trigger = 1;
  1066. entry.mask = 1;
  1067. }
  1068. irq = pin_2_irq(idx, apic, pin);
  1069. /*
  1070. * skip adding the timer int on secondary nodes, which causes
  1071. * a small but painful rift in the time-space continuum
  1072. */
  1073. if (multi_timer_check(apic, irq))
  1074. continue;
  1075. else
  1076. add_pin_to_irq(irq, apic, pin);
  1077. if (!apic && !IO_APIC_IRQ(irq))
  1078. continue;
  1079. if (IO_APIC_IRQ(irq)) {
  1080. vector = assign_irq_vector(irq);
  1081. entry.vector = vector;
  1082. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  1083. if (!apic && (irq < 16))
  1084. disable_8259A_irq(irq);
  1085. }
  1086. spin_lock_irqsave(&ioapic_lock, flags);
  1087. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  1088. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  1089. spin_unlock_irqrestore(&ioapic_lock, flags);
  1090. }
  1091. }
  1092. if (!first_notcon)
  1093. apic_printk(APIC_VERBOSE, " not connected.\n");
  1094. }
  1095. /*
  1096. * Set up the 8259A-master output pin:
  1097. */
  1098. static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
  1099. {
  1100. struct IO_APIC_route_entry entry;
  1101. unsigned long flags;
  1102. memset(&entry,0,sizeof(entry));
  1103. disable_8259A_irq(0);
  1104. /* mask LVT0 */
  1105. apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1106. /*
  1107. * We use logical delivery to get the timer IRQ
  1108. * to the first CPU.
  1109. */
  1110. entry.dest_mode = INT_DEST_MODE;
  1111. entry.mask = 0; /* unmask IRQ now */
  1112. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  1113. entry.delivery_mode = INT_DELIVERY_MODE;
  1114. entry.polarity = 0;
  1115. entry.trigger = 0;
  1116. entry.vector = vector;
  1117. /*
  1118. * The timer IRQ doesn't have to know that behind the
  1119. * scene we have a 8259A-master in AEOI mode ...
  1120. */
  1121. irq_desc[0].handler = &ioapic_edge_type;
  1122. /*
  1123. * Add it to the IO-APIC irq-routing table:
  1124. */
  1125. spin_lock_irqsave(&ioapic_lock, flags);
  1126. io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
  1127. io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
  1128. spin_unlock_irqrestore(&ioapic_lock, flags);
  1129. enable_8259A_irq(0);
  1130. }
  1131. static inline void UNEXPECTED_IO_APIC(void)
  1132. {
  1133. }
  1134. void __init print_IO_APIC(void)
  1135. {
  1136. int apic, i;
  1137. union IO_APIC_reg_00 reg_00;
  1138. union IO_APIC_reg_01 reg_01;
  1139. union IO_APIC_reg_02 reg_02;
  1140. union IO_APIC_reg_03 reg_03;
  1141. unsigned long flags;
  1142. if (apic_verbosity == APIC_QUIET)
  1143. return;
  1144. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1145. for (i = 0; i < nr_ioapics; i++)
  1146. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1147. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  1148. /*
  1149. * We are a bit conservative about what we expect. We have to
  1150. * know about every hardware change ASAP.
  1151. */
  1152. printk(KERN_INFO "testing the IO APIC.......................\n");
  1153. for (apic = 0; apic < nr_ioapics; apic++) {
  1154. spin_lock_irqsave(&ioapic_lock, flags);
  1155. reg_00.raw = io_apic_read(apic, 0);
  1156. reg_01.raw = io_apic_read(apic, 1);
  1157. if (reg_01.bits.version >= 0x10)
  1158. reg_02.raw = io_apic_read(apic, 2);
  1159. if (reg_01.bits.version >= 0x20)
  1160. reg_03.raw = io_apic_read(apic, 3);
  1161. spin_unlock_irqrestore(&ioapic_lock, flags);
  1162. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  1163. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1164. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1165. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1166. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1167. if (reg_00.bits.ID >= get_physical_broadcast())
  1168. UNEXPECTED_IO_APIC();
  1169. if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
  1170. UNEXPECTED_IO_APIC();
  1171. printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
  1172. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  1173. if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
  1174. (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
  1175. (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
  1176. (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
  1177. (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
  1178. (reg_01.bits.entries != 0x2E) &&
  1179. (reg_01.bits.entries != 0x3F)
  1180. )
  1181. UNEXPECTED_IO_APIC();
  1182. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1183. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  1184. if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
  1185. (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
  1186. (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
  1187. (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
  1188. (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
  1189. )
  1190. UNEXPECTED_IO_APIC();
  1191. if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
  1192. UNEXPECTED_IO_APIC();
  1193. /*
  1194. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1195. * but the value of reg_02 is read as the previous read register
  1196. * value, so ignore it if reg_02 == reg_01.
  1197. */
  1198. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1199. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1200. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1201. if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
  1202. UNEXPECTED_IO_APIC();
  1203. }
  1204. /*
  1205. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1206. * or reg_03, but the value of reg_0[23] is read as the previous read
  1207. * register value, so ignore it if reg_03 == reg_0[12].
  1208. */
  1209. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1210. reg_03.raw != reg_01.raw) {
  1211. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1212. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1213. if (reg_03.bits.__reserved_1)
  1214. UNEXPECTED_IO_APIC();
  1215. }
  1216. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1217. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  1218. " Stat Dest Deli Vect: \n");
  1219. for (i = 0; i <= reg_01.bits.entries; i++) {
  1220. struct IO_APIC_route_entry entry;
  1221. spin_lock_irqsave(&ioapic_lock, flags);
  1222. *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
  1223. *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
  1224. spin_unlock_irqrestore(&ioapic_lock, flags);
  1225. printk(KERN_DEBUG " %02x %03X %02X ",
  1226. i,
  1227. entry.dest.logical.logical_dest,
  1228. entry.dest.physical.physical_dest
  1229. );
  1230. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1231. entry.mask,
  1232. entry.trigger,
  1233. entry.irr,
  1234. entry.polarity,
  1235. entry.delivery_status,
  1236. entry.dest_mode,
  1237. entry.delivery_mode,
  1238. entry.vector
  1239. );
  1240. }
  1241. }
  1242. if (use_pci_vector())
  1243. printk(KERN_INFO "Using vector-based indexing\n");
  1244. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1245. for (i = 0; i < NR_IRQS; i++) {
  1246. struct irq_pin_list *entry = irq_2_pin + i;
  1247. if (entry->pin < 0)
  1248. continue;
  1249. if (use_pci_vector() && !platform_legacy_irq(i))
  1250. printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
  1251. else
  1252. printk(KERN_DEBUG "IRQ%d ", i);
  1253. for (;;) {
  1254. printk("-> %d:%d", entry->apic, entry->pin);
  1255. if (!entry->next)
  1256. break;
  1257. entry = irq_2_pin + entry->next;
  1258. }
  1259. printk("\n");
  1260. }
  1261. printk(KERN_INFO ".................................... done.\n");
  1262. return;
  1263. }
  1264. #if 0
  1265. static void print_APIC_bitfield (int base)
  1266. {
  1267. unsigned int v;
  1268. int i, j;
  1269. if (apic_verbosity == APIC_QUIET)
  1270. return;
  1271. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1272. for (i = 0; i < 8; i++) {
  1273. v = apic_read(base + i*0x10);
  1274. for (j = 0; j < 32; j++) {
  1275. if (v & (1<<j))
  1276. printk("1");
  1277. else
  1278. printk("0");
  1279. }
  1280. printk("\n");
  1281. }
  1282. }
  1283. void /*__init*/ print_local_APIC(void * dummy)
  1284. {
  1285. unsigned int v, ver, maxlvt;
  1286. if (apic_verbosity == APIC_QUIET)
  1287. return;
  1288. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1289. smp_processor_id(), hard_smp_processor_id());
  1290. v = apic_read(APIC_ID);
  1291. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
  1292. v = apic_read(APIC_LVR);
  1293. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1294. ver = GET_APIC_VERSION(v);
  1295. maxlvt = get_maxlvt();
  1296. v = apic_read(APIC_TASKPRI);
  1297. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1298. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1299. v = apic_read(APIC_ARBPRI);
  1300. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1301. v & APIC_ARBPRI_MASK);
  1302. v = apic_read(APIC_PROCPRI);
  1303. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1304. }
  1305. v = apic_read(APIC_EOI);
  1306. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  1307. v = apic_read(APIC_RRR);
  1308. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1309. v = apic_read(APIC_LDR);
  1310. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1311. v = apic_read(APIC_DFR);
  1312. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1313. v = apic_read(APIC_SPIV);
  1314. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1315. printk(KERN_DEBUG "... APIC ISR field:\n");
  1316. print_APIC_bitfield(APIC_ISR);
  1317. printk(KERN_DEBUG "... APIC TMR field:\n");
  1318. print_APIC_bitfield(APIC_TMR);
  1319. printk(KERN_DEBUG "... APIC IRR field:\n");
  1320. print_APIC_bitfield(APIC_IRR);
  1321. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1322. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1323. apic_write(APIC_ESR, 0);
  1324. v = apic_read(APIC_ESR);
  1325. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1326. }
  1327. v = apic_read(APIC_ICR);
  1328. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  1329. v = apic_read(APIC_ICR2);
  1330. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  1331. v = apic_read(APIC_LVTT);
  1332. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1333. if (maxlvt > 3) { /* PC is LVT#4. */
  1334. v = apic_read(APIC_LVTPC);
  1335. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1336. }
  1337. v = apic_read(APIC_LVT0);
  1338. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1339. v = apic_read(APIC_LVT1);
  1340. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1341. if (maxlvt > 2) { /* ERR is LVT#3. */
  1342. v = apic_read(APIC_LVTERR);
  1343. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1344. }
  1345. v = apic_read(APIC_TMICT);
  1346. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1347. v = apic_read(APIC_TMCCT);
  1348. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1349. v = apic_read(APIC_TDCR);
  1350. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1351. printk("\n");
  1352. }
  1353. void print_all_local_APICs (void)
  1354. {
  1355. on_each_cpu(print_local_APIC, NULL, 1, 1);
  1356. }
  1357. void /*__init*/ print_PIC(void)
  1358. {
  1359. extern spinlock_t i8259A_lock;
  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*/