open_pic.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*
  2. * Copyright (C) 1997 Geert Uytterhoeven
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file COPYING in the main directory of this archive
  6. * for more details.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/sysdev.h>
  14. #include <linux/errno.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/signal.h>
  17. #include <asm/io.h>
  18. #include <asm/irq.h>
  19. #include <asm/sections.h>
  20. #include <asm/open_pic.h>
  21. #include <asm/i8259.h>
  22. #include <asm/machdep.h>
  23. #include "open_pic_defs.h"
  24. #if defined(CONFIG_PRPMC800) || defined(CONFIG_85xx)
  25. #define OPENPIC_BIG_ENDIAN
  26. #endif
  27. void __iomem *OpenPIC_Addr;
  28. static volatile struct OpenPIC __iomem *OpenPIC = NULL;
  29. /*
  30. * We define OpenPIC_InitSenses table thusly:
  31. * bit 0x1: sense, 0 for edge and 1 for level.
  32. * bit 0x2: polarity, 0 for negative, 1 for positive.
  33. */
  34. u_int OpenPIC_NumInitSenses __initdata = 0;
  35. u_char *OpenPIC_InitSenses __initdata = NULL;
  36. extern int use_of_interrupt_tree;
  37. static u_int NumProcessors;
  38. static u_int NumSources;
  39. static int open_pic_irq_offset;
  40. static volatile OpenPIC_Source __iomem *ISR[NR_IRQS];
  41. static int openpic_cascade_irq = -1;
  42. static int (*openpic_cascade_fn)(void);
  43. /* Global Operations */
  44. static void openpic_disable_8259_pass_through(void);
  45. static void openpic_set_spurious(u_int vector);
  46. #ifdef CONFIG_SMP
  47. /* Interprocessor Interrupts */
  48. static void openpic_initipi(u_int ipi, u_int pri, u_int vector);
  49. static irqreturn_t openpic_ipi_action(int cpl, void *dev_id);
  50. #endif
  51. /* Timer Interrupts */
  52. static void openpic_inittimer(u_int timer, u_int pri, u_int vector);
  53. static void openpic_maptimer(u_int timer, cpumask_t cpumask);
  54. /* Interrupt Sources */
  55. static void openpic_enable_irq(u_int irq);
  56. static void openpic_disable_irq(u_int irq);
  57. static void openpic_initirq(u_int irq, u_int pri, u_int vector, int polarity,
  58. int is_level);
  59. static void openpic_mapirq(u_int irq, cpumask_t cpumask, cpumask_t keepmask);
  60. /*
  61. * These functions are not used but the code is kept here
  62. * for completeness and future reference.
  63. */
  64. #ifdef notused
  65. static void openpic_enable_8259_pass_through(void);
  66. static u_int openpic_get_spurious(void);
  67. static void openpic_set_sense(u_int irq, int sense);
  68. #endif /* notused */
  69. /*
  70. * Description of the openpic for the higher-level irq code
  71. */
  72. static void openpic_end_irq(unsigned int irq_nr);
  73. static void openpic_ack_irq(unsigned int irq_nr);
  74. static void openpic_set_affinity(unsigned int irq_nr, cpumask_t cpumask);
  75. struct hw_interrupt_type open_pic = {
  76. .typename = " OpenPIC ",
  77. .enable = openpic_enable_irq,
  78. .disable = openpic_disable_irq,
  79. .ack = openpic_ack_irq,
  80. .end = openpic_end_irq,
  81. .set_affinity = openpic_set_affinity,
  82. };
  83. #ifdef CONFIG_SMP
  84. static void openpic_end_ipi(unsigned int irq_nr);
  85. static void openpic_ack_ipi(unsigned int irq_nr);
  86. static void openpic_enable_ipi(unsigned int irq_nr);
  87. static void openpic_disable_ipi(unsigned int irq_nr);
  88. struct hw_interrupt_type open_pic_ipi = {
  89. .typename = " OpenPIC ",
  90. .enable = openpic_enable_ipi,
  91. .disable = openpic_disable_ipi,
  92. .ack = openpic_ack_ipi,
  93. .end = openpic_end_ipi,
  94. };
  95. #endif /* CONFIG_SMP */
  96. /*
  97. * Accesses to the current processor's openpic registers
  98. */
  99. #ifdef CONFIG_SMP
  100. #define THIS_CPU Processor[cpu]
  101. #define DECL_THIS_CPU int cpu = smp_hw_index[smp_processor_id()]
  102. #define CHECK_THIS_CPU check_arg_cpu(cpu)
  103. #else
  104. #define THIS_CPU Processor[0]
  105. #define DECL_THIS_CPU
  106. #define CHECK_THIS_CPU
  107. #endif /* CONFIG_SMP */
  108. #if 1
  109. #define check_arg_ipi(ipi) \
  110. if (ipi < 0 || ipi >= OPENPIC_NUM_IPI) \
  111. printk("open_pic.c:%d: invalid ipi %d\n", __LINE__, ipi);
  112. #define check_arg_timer(timer) \
  113. if (timer < 0 || timer >= OPENPIC_NUM_TIMERS) \
  114. printk("open_pic.c:%d: invalid timer %d\n", __LINE__, timer);
  115. #define check_arg_vec(vec) \
  116. if (vec < 0 || vec >= OPENPIC_NUM_VECTORS) \
  117. printk("open_pic.c:%d: invalid vector %d\n", __LINE__, vec);
  118. #define check_arg_pri(pri) \
  119. if (pri < 0 || pri >= OPENPIC_NUM_PRI) \
  120. printk("open_pic.c:%d: invalid priority %d\n", __LINE__, pri);
  121. /*
  122. * Print out a backtrace if it's out of range, since if it's larger than NR_IRQ's
  123. * data has probably been corrupted and we're going to panic or deadlock later
  124. * anyway --Troy
  125. */
  126. #define check_arg_irq(irq) \
  127. if (irq < open_pic_irq_offset || irq >= NumSources+open_pic_irq_offset \
  128. || ISR[irq - open_pic_irq_offset] == 0) { \
  129. printk("open_pic.c:%d: invalid irq %d\n", __LINE__, irq); \
  130. dump_stack(); }
  131. #define check_arg_cpu(cpu) \
  132. if (cpu < 0 || cpu >= NumProcessors){ \
  133. printk("open_pic.c:%d: invalid cpu %d\n", __LINE__, cpu); \
  134. dump_stack(); }
  135. #else
  136. #define check_arg_ipi(ipi) do {} while (0)
  137. #define check_arg_timer(timer) do {} while (0)
  138. #define check_arg_vec(vec) do {} while (0)
  139. #define check_arg_pri(pri) do {} while (0)
  140. #define check_arg_irq(irq) do {} while (0)
  141. #define check_arg_cpu(cpu) do {} while (0)
  142. #endif
  143. u_int openpic_read(volatile u_int __iomem *addr)
  144. {
  145. u_int val;
  146. #ifdef OPENPIC_BIG_ENDIAN
  147. val = in_be32(addr);
  148. #else
  149. val = in_le32(addr);
  150. #endif
  151. return val;
  152. }
  153. static inline void openpic_write(volatile u_int __iomem *addr, u_int val)
  154. {
  155. #ifdef OPENPIC_BIG_ENDIAN
  156. out_be32(addr, val);
  157. #else
  158. out_le32(addr, val);
  159. #endif
  160. }
  161. static inline u_int openpic_readfield(volatile u_int __iomem *addr, u_int mask)
  162. {
  163. u_int val = openpic_read(addr);
  164. return val & mask;
  165. }
  166. inline void openpic_writefield(volatile u_int __iomem *addr, u_int mask,
  167. u_int field)
  168. {
  169. u_int val = openpic_read(addr);
  170. openpic_write(addr, (val & ~mask) | (field & mask));
  171. }
  172. static inline void openpic_clearfield(volatile u_int __iomem *addr, u_int mask)
  173. {
  174. openpic_writefield(addr, mask, 0);
  175. }
  176. static inline void openpic_setfield(volatile u_int __iomem *addr, u_int mask)
  177. {
  178. openpic_writefield(addr, mask, mask);
  179. }
  180. static void openpic_safe_writefield(volatile u_int __iomem *addr, u_int mask,
  181. u_int field)
  182. {
  183. openpic_setfield(addr, OPENPIC_MASK);
  184. while (openpic_read(addr) & OPENPIC_ACTIVITY);
  185. openpic_writefield(addr, mask | OPENPIC_MASK, field | OPENPIC_MASK);
  186. }
  187. #ifdef CONFIG_SMP
  188. /* yes this is right ... bug, feature, you decide! -- tgall */
  189. u_int openpic_read_IPI(volatile u_int __iomem * addr)
  190. {
  191. u_int val = 0;
  192. #if defined(OPENPIC_BIG_ENDIAN)
  193. val = in_be32(addr);
  194. #else
  195. val = in_le32(addr);
  196. #endif
  197. return val;
  198. }
  199. /* because of the power3 be / le above, this is needed */
  200. inline void openpic_writefield_IPI(volatile u_int __iomem * addr, u_int mask, u_int field)
  201. {
  202. u_int val = openpic_read_IPI(addr);
  203. openpic_write(addr, (val & ~mask) | (field & mask));
  204. }
  205. static inline void openpic_clearfield_IPI(volatile u_int __iomem *addr, u_int mask)
  206. {
  207. openpic_writefield_IPI(addr, mask, 0);
  208. }
  209. static inline void openpic_setfield_IPI(volatile u_int __iomem *addr, u_int mask)
  210. {
  211. openpic_writefield_IPI(addr, mask, mask);
  212. }
  213. static void openpic_safe_writefield_IPI(volatile u_int __iomem *addr, u_int mask, u_int field)
  214. {
  215. openpic_setfield_IPI(addr, OPENPIC_MASK);
  216. /* wait until it's not in use */
  217. /* BenH: Is this code really enough ? I would rather check the result
  218. * and eventually retry ...
  219. */
  220. while(openpic_read_IPI(addr) & OPENPIC_ACTIVITY);
  221. openpic_writefield_IPI(addr, mask | OPENPIC_MASK, field | OPENPIC_MASK);
  222. }
  223. #endif /* CONFIG_SMP */
  224. #ifdef CONFIG_EPIC_SERIAL_MODE
  225. /* On platforms that may use EPIC serial mode, the default is enabled. */
  226. int epic_serial_mode = 1;
  227. static void __init openpic_eicr_set_clk(u_int clkval)
  228. {
  229. openpic_writefield(&OpenPIC->Global.Global_Configuration1,
  230. OPENPIC_EICR_S_CLK_MASK, (clkval << 28));
  231. }
  232. static void __init openpic_enable_sie(void)
  233. {
  234. openpic_setfield(&OpenPIC->Global.Global_Configuration1,
  235. OPENPIC_EICR_SIE);
  236. }
  237. #endif
  238. #if defined(CONFIG_EPIC_SERIAL_MODE)
  239. static void openpic_reset(void)
  240. {
  241. openpic_setfield(&OpenPIC->Global.Global_Configuration0,
  242. OPENPIC_CONFIG_RESET);
  243. while (openpic_readfield(&OpenPIC->Global.Global_Configuration0,
  244. OPENPIC_CONFIG_RESET))
  245. mb();
  246. }
  247. #endif
  248. void __init openpic_set_sources(int first_irq, int num_irqs, void __iomem *first_ISR)
  249. {
  250. volatile OpenPIC_Source __iomem *src = first_ISR;
  251. int i, last_irq;
  252. last_irq = first_irq + num_irqs;
  253. if (last_irq > NumSources)
  254. NumSources = last_irq;
  255. if (src == 0)
  256. src = &((struct OpenPIC __iomem *)OpenPIC_Addr)->Source[first_irq];
  257. for (i = first_irq; i < last_irq; ++i, ++src)
  258. ISR[i] = src;
  259. }
  260. /*
  261. * The `offset' parameter defines where the interrupts handled by the
  262. * OpenPIC start in the space of interrupt numbers that the kernel knows
  263. * about. In other words, the OpenPIC's IRQ0 is numbered `offset' in the
  264. * kernel's interrupt numbering scheme.
  265. * We assume there is only one OpenPIC.
  266. */
  267. void __init openpic_init(int offset)
  268. {
  269. u_int t, i;
  270. u_int timerfreq;
  271. const char *version;
  272. if (!OpenPIC_Addr) {
  273. printk("No OpenPIC found !\n");
  274. return;
  275. }
  276. OpenPIC = (volatile struct OpenPIC __iomem *)OpenPIC_Addr;
  277. #ifdef CONFIG_EPIC_SERIAL_MODE
  278. /* Have to start from ground zero.
  279. */
  280. openpic_reset();
  281. #endif
  282. if (ppc_md.progress) ppc_md.progress("openpic: enter", 0x122);
  283. t = openpic_read(&OpenPIC->Global.Feature_Reporting0);
  284. switch (t & OPENPIC_FEATURE_VERSION_MASK) {
  285. case 1:
  286. version = "1.0";
  287. break;
  288. case 2:
  289. version = "1.2";
  290. break;
  291. case 3:
  292. version = "1.3";
  293. break;
  294. default:
  295. version = "?";
  296. break;
  297. }
  298. NumProcessors = ((t & OPENPIC_FEATURE_LAST_PROCESSOR_MASK) >>
  299. OPENPIC_FEATURE_LAST_PROCESSOR_SHIFT) + 1;
  300. if (NumSources == 0)
  301. openpic_set_sources(0,
  302. ((t & OPENPIC_FEATURE_LAST_SOURCE_MASK) >>
  303. OPENPIC_FEATURE_LAST_SOURCE_SHIFT) + 1,
  304. NULL);
  305. printk("OpenPIC Version %s (%d CPUs and %d IRQ sources) at %p\n",
  306. version, NumProcessors, NumSources, OpenPIC);
  307. timerfreq = openpic_read(&OpenPIC->Global.Timer_Frequency);
  308. if (timerfreq)
  309. printk("OpenPIC timer frequency is %d.%06d MHz\n",
  310. timerfreq / 1000000, timerfreq % 1000000);
  311. open_pic_irq_offset = offset;
  312. /* Initialize timer interrupts */
  313. if ( ppc_md.progress ) ppc_md.progress("openpic: timer",0x3ba);
  314. for (i = 0; i < OPENPIC_NUM_TIMERS; i++) {
  315. /* Disabled, Priority 0 */
  316. openpic_inittimer(i, 0, OPENPIC_VEC_TIMER+i+offset);
  317. /* No processor */
  318. openpic_maptimer(i, CPU_MASK_NONE);
  319. }
  320. #ifdef CONFIG_SMP
  321. /* Initialize IPI interrupts */
  322. if ( ppc_md.progress ) ppc_md.progress("openpic: ipi",0x3bb);
  323. for (i = 0; i < OPENPIC_NUM_IPI; i++) {
  324. /* Disabled, increased priorities 10..13 */
  325. openpic_initipi(i, OPENPIC_PRIORITY_IPI_BASE+i,
  326. OPENPIC_VEC_IPI+i+offset);
  327. /* IPIs are per-CPU */
  328. irq_desc[OPENPIC_VEC_IPI+i+offset].status |= IRQ_PER_CPU;
  329. irq_desc[OPENPIC_VEC_IPI+i+offset].chip = &open_pic_ipi;
  330. }
  331. #endif
  332. /* Initialize external interrupts */
  333. if (ppc_md.progress) ppc_md.progress("openpic: external",0x3bc);
  334. openpic_set_priority(0xf);
  335. /* Init all external sources, including possibly the cascade. */
  336. for (i = 0; i < NumSources; i++) {
  337. int sense;
  338. if (ISR[i] == 0)
  339. continue;
  340. /* the bootloader may have left it enabled (bad !) */
  341. openpic_disable_irq(i+offset);
  342. sense = (i < OpenPIC_NumInitSenses)? OpenPIC_InitSenses[i]: \
  343. (IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE);
  344. if (sense & IRQ_SENSE_MASK)
  345. irq_desc[i+offset].status = IRQ_LEVEL;
  346. /* Enabled, Default priority */
  347. openpic_initirq(i, OPENPIC_PRIORITY_DEFAULT, i+offset,
  348. (sense & IRQ_POLARITY_MASK),
  349. (sense & IRQ_SENSE_MASK));
  350. /* Processor 0 */
  351. openpic_mapirq(i, CPU_MASK_CPU0, CPU_MASK_NONE);
  352. }
  353. /* Init descriptors */
  354. for (i = offset; i < NumSources + offset; i++)
  355. irq_desc[i].chip = &open_pic;
  356. /* Initialize the spurious interrupt */
  357. if (ppc_md.progress) ppc_md.progress("openpic: spurious",0x3bd);
  358. openpic_set_spurious(OPENPIC_VEC_SPURIOUS);
  359. openpic_disable_8259_pass_through();
  360. #ifdef CONFIG_EPIC_SERIAL_MODE
  361. if (epic_serial_mode) {
  362. openpic_eicr_set_clk(7); /* Slowest value until we know better */
  363. openpic_enable_sie();
  364. }
  365. #endif
  366. openpic_set_priority(0);
  367. if (ppc_md.progress) ppc_md.progress("openpic: exit",0x222);
  368. }
  369. #ifdef notused
  370. static void openpic_enable_8259_pass_through(void)
  371. {
  372. openpic_clearfield(&OpenPIC->Global.Global_Configuration0,
  373. OPENPIC_CONFIG_8259_PASSTHROUGH_DISABLE);
  374. }
  375. #endif /* notused */
  376. static void openpic_disable_8259_pass_through(void)
  377. {
  378. openpic_setfield(&OpenPIC->Global.Global_Configuration0,
  379. OPENPIC_CONFIG_8259_PASSTHROUGH_DISABLE);
  380. }
  381. /*
  382. * Find out the current interrupt
  383. */
  384. u_int openpic_irq(void)
  385. {
  386. u_int vec;
  387. DECL_THIS_CPU;
  388. CHECK_THIS_CPU;
  389. vec = openpic_readfield(&OpenPIC->THIS_CPU.Interrupt_Acknowledge,
  390. OPENPIC_VECTOR_MASK);
  391. return vec;
  392. }
  393. void openpic_eoi(void)
  394. {
  395. DECL_THIS_CPU;
  396. CHECK_THIS_CPU;
  397. openpic_write(&OpenPIC->THIS_CPU.EOI, 0);
  398. /* Handle PCI write posting */
  399. (void)openpic_read(&OpenPIC->THIS_CPU.EOI);
  400. }
  401. u_int openpic_get_priority(void)
  402. {
  403. DECL_THIS_CPU;
  404. CHECK_THIS_CPU;
  405. return openpic_readfield(&OpenPIC->THIS_CPU.Current_Task_Priority,
  406. OPENPIC_CURRENT_TASK_PRIORITY_MASK);
  407. }
  408. void openpic_set_priority(u_int pri)
  409. {
  410. DECL_THIS_CPU;
  411. CHECK_THIS_CPU;
  412. check_arg_pri(pri);
  413. openpic_writefield(&OpenPIC->THIS_CPU.Current_Task_Priority,
  414. OPENPIC_CURRENT_TASK_PRIORITY_MASK, pri);
  415. }
  416. /*
  417. * Get/set the spurious vector
  418. */
  419. #ifdef notused
  420. static u_int openpic_get_spurious(void)
  421. {
  422. return openpic_readfield(&OpenPIC->Global.Spurious_Vector,
  423. OPENPIC_VECTOR_MASK);
  424. }
  425. #endif /* notused */
  426. static void openpic_set_spurious(u_int vec)
  427. {
  428. check_arg_vec(vec);
  429. openpic_writefield(&OpenPIC->Global.Spurious_Vector, OPENPIC_VECTOR_MASK,
  430. vec);
  431. }
  432. #ifdef CONFIG_SMP
  433. /*
  434. * Convert a cpu mask from logical to physical cpu numbers.
  435. */
  436. static inline cpumask_t physmask(cpumask_t cpumask)
  437. {
  438. int i;
  439. cpumask_t mask = CPU_MASK_NONE;
  440. cpus_and(cpumask, cpu_online_map, cpumask);
  441. for (i = 0; i < NR_CPUS; i++)
  442. if (cpu_isset(i, cpumask))
  443. cpu_set(smp_hw_index[i], mask);
  444. return mask;
  445. }
  446. #else
  447. #define physmask(cpumask) (cpumask)
  448. #endif
  449. void openpic_reset_processor_phys(u_int mask)
  450. {
  451. openpic_write(&OpenPIC->Global.Processor_Initialization, mask);
  452. }
  453. #if defined(CONFIG_SMP) || defined(CONFIG_PM)
  454. static DEFINE_SPINLOCK(openpic_setup_lock);
  455. #endif
  456. #ifdef CONFIG_SMP
  457. /*
  458. * Initialize an interprocessor interrupt (and disable it)
  459. *
  460. * ipi: OpenPIC interprocessor interrupt number
  461. * pri: interrupt source priority
  462. * vec: the vector it will produce
  463. */
  464. static void __init openpic_initipi(u_int ipi, u_int pri, u_int vec)
  465. {
  466. check_arg_ipi(ipi);
  467. check_arg_pri(pri);
  468. check_arg_vec(vec);
  469. openpic_safe_writefield_IPI(&OpenPIC->Global.IPI_Vector_Priority(ipi),
  470. OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK,
  471. (pri << OPENPIC_PRIORITY_SHIFT) | vec);
  472. }
  473. /*
  474. * Send an IPI to one or more CPUs
  475. *
  476. * Externally called, however, it takes an IPI number (0...OPENPIC_NUM_IPI)
  477. * and not a system-wide interrupt number
  478. */
  479. void openpic_cause_IPI(u_int ipi, cpumask_t cpumask)
  480. {
  481. DECL_THIS_CPU;
  482. CHECK_THIS_CPU;
  483. check_arg_ipi(ipi);
  484. openpic_write(&OpenPIC->THIS_CPU.IPI_Dispatch(ipi),
  485. cpus_addr(physmask(cpumask))[0]);
  486. }
  487. void openpic_request_IPIs(void)
  488. {
  489. int i;
  490. /*
  491. * Make sure this matches what is defined in smp.c for
  492. * smp_message_{pass|recv}() or what shows up in
  493. * /proc/interrupts will be wrong!!! --Troy */
  494. if (OpenPIC == NULL)
  495. return;
  496. /*
  497. * IPIs are marked IRQF_DISABLED as they must run with irqs
  498. * disabled
  499. */
  500. request_irq(OPENPIC_VEC_IPI+open_pic_irq_offset,
  501. openpic_ipi_action, IRQF_DISABLED,
  502. "IPI0 (call function)", NULL);
  503. request_irq(OPENPIC_VEC_IPI+open_pic_irq_offset+1,
  504. openpic_ipi_action, IRQF_DISABLED,
  505. "IPI1 (reschedule)", NULL);
  506. request_irq(OPENPIC_VEC_IPI+open_pic_irq_offset+2,
  507. openpic_ipi_action, IRQF_DISABLED,
  508. "IPI2 (invalidate tlb)", NULL);
  509. request_irq(OPENPIC_VEC_IPI+open_pic_irq_offset+3,
  510. openpic_ipi_action, IRQF_DISABLED,
  511. "IPI3 (xmon break)", NULL);
  512. for ( i = 0; i < OPENPIC_NUM_IPI ; i++ )
  513. openpic_enable_ipi(OPENPIC_VEC_IPI+open_pic_irq_offset+i);
  514. }
  515. /*
  516. * Do per-cpu setup for SMP systems.
  517. *
  518. * Get IPI's working and start taking interrupts.
  519. * -- Cort
  520. */
  521. void __devinit do_openpic_setup_cpu(void)
  522. {
  523. #ifdef CONFIG_IRQ_ALL_CPUS
  524. int i;
  525. cpumask_t msk = CPU_MASK_NONE;
  526. #endif
  527. spin_lock(&openpic_setup_lock);
  528. #ifdef CONFIG_IRQ_ALL_CPUS
  529. cpu_set(smp_hw_index[smp_processor_id()], msk);
  530. /* let the openpic know we want intrs. default affinity
  531. * is 0xffffffff until changed via /proc
  532. * That's how it's done on x86. If we want it differently, then
  533. * we should make sure we also change the default values of
  534. * irq_desc[].affinity in irq.c.
  535. */
  536. for (i = 0; i < NumSources; i++)
  537. openpic_mapirq(i, msk, CPU_MASK_ALL);
  538. #endif /* CONFIG_IRQ_ALL_CPUS */
  539. openpic_set_priority(0);
  540. spin_unlock(&openpic_setup_lock);
  541. }
  542. #endif /* CONFIG_SMP */
  543. /*
  544. * Initialize a timer interrupt (and disable it)
  545. *
  546. * timer: OpenPIC timer number
  547. * pri: interrupt source priority
  548. * vec: the vector it will produce
  549. */
  550. static void __init openpic_inittimer(u_int timer, u_int pri, u_int vec)
  551. {
  552. check_arg_timer(timer);
  553. check_arg_pri(pri);
  554. check_arg_vec(vec);
  555. openpic_safe_writefield(&OpenPIC->Global.Timer[timer].Vector_Priority,
  556. OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK,
  557. (pri << OPENPIC_PRIORITY_SHIFT) | vec);
  558. }
  559. /*
  560. * Map a timer interrupt to one or more CPUs
  561. */
  562. static void __init openpic_maptimer(u_int timer, cpumask_t cpumask)
  563. {
  564. cpumask_t phys = physmask(cpumask);
  565. check_arg_timer(timer);
  566. openpic_write(&OpenPIC->Global.Timer[timer].Destination,
  567. cpus_addr(phys)[0]);
  568. }
  569. /*
  570. * Change the priority of an interrupt
  571. */
  572. void __init
  573. openpic_set_irq_priority(u_int irq, u_int pri)
  574. {
  575. check_arg_irq(irq);
  576. openpic_safe_writefield(&ISR[irq - open_pic_irq_offset]->Vector_Priority,
  577. OPENPIC_PRIORITY_MASK,
  578. pri << OPENPIC_PRIORITY_SHIFT);
  579. }
  580. /*
  581. * Initalize the interrupt source which will generate an NMI.
  582. * This raises the interrupt's priority from 8 to 9.
  583. *
  584. * irq: The logical IRQ which generates an NMI.
  585. */
  586. void __init
  587. openpic_init_nmi_irq(u_int irq)
  588. {
  589. check_arg_irq(irq);
  590. openpic_set_irq_priority(irq, OPENPIC_PRIORITY_NMI);
  591. }
  592. /*
  593. *
  594. * All functions below take an offset'ed irq argument
  595. *
  596. */
  597. /*
  598. * Hookup a cascade to the OpenPIC.
  599. */
  600. static struct irqaction openpic_cascade_irqaction = {
  601. .handler = no_action,
  602. .flags = IRQF_DISABLED,
  603. .mask = CPU_MASK_NONE,
  604. };
  605. void __init
  606. openpic_hookup_cascade(u_int irq, char *name,
  607. int (*cascade_fn)(void))
  608. {
  609. openpic_cascade_irq = irq;
  610. openpic_cascade_fn = cascade_fn;
  611. if (setup_irq(irq, &openpic_cascade_irqaction))
  612. printk("Unable to get OpenPIC IRQ %d for cascade\n",
  613. irq - open_pic_irq_offset);
  614. }
  615. /*
  616. * Enable/disable an external interrupt source
  617. *
  618. * Externally called, irq is an offseted system-wide interrupt number
  619. */
  620. static void openpic_enable_irq(u_int irq)
  621. {
  622. volatile u_int __iomem *vpp;
  623. check_arg_irq(irq);
  624. vpp = &ISR[irq - open_pic_irq_offset]->Vector_Priority;
  625. openpic_clearfield(vpp, OPENPIC_MASK);
  626. /* make sure mask gets to controller before we return to user */
  627. do {
  628. mb(); /* sync is probably useless here */
  629. } while (openpic_readfield(vpp, OPENPIC_MASK));
  630. }
  631. static void openpic_disable_irq(u_int irq)
  632. {
  633. volatile u_int __iomem *vpp;
  634. u32 vp;
  635. check_arg_irq(irq);
  636. vpp = &ISR[irq - open_pic_irq_offset]->Vector_Priority;
  637. openpic_setfield(vpp, OPENPIC_MASK);
  638. /* make sure mask gets to controller before we return to user */
  639. do {
  640. mb(); /* sync is probably useless here */
  641. vp = openpic_readfield(vpp, OPENPIC_MASK | OPENPIC_ACTIVITY);
  642. } while((vp & OPENPIC_ACTIVITY) && !(vp & OPENPIC_MASK));
  643. }
  644. #ifdef CONFIG_SMP
  645. /*
  646. * Enable/disable an IPI interrupt source
  647. *
  648. * Externally called, irq is an offseted system-wide interrupt number
  649. */
  650. void openpic_enable_ipi(u_int irq)
  651. {
  652. irq -= (OPENPIC_VEC_IPI+open_pic_irq_offset);
  653. check_arg_ipi(irq);
  654. openpic_clearfield_IPI(&OpenPIC->Global.IPI_Vector_Priority(irq), OPENPIC_MASK);
  655. }
  656. void openpic_disable_ipi(u_int irq)
  657. {
  658. irq -= (OPENPIC_VEC_IPI+open_pic_irq_offset);
  659. check_arg_ipi(irq);
  660. openpic_setfield_IPI(&OpenPIC->Global.IPI_Vector_Priority(irq), OPENPIC_MASK);
  661. }
  662. #endif
  663. /*
  664. * Initialize an interrupt source (and disable it!)
  665. *
  666. * irq: OpenPIC interrupt number
  667. * pri: interrupt source priority
  668. * vec: the vector it will produce
  669. * pol: polarity (1 for positive, 0 for negative)
  670. * sense: 1 for level, 0 for edge
  671. */
  672. static void __init
  673. openpic_initirq(u_int irq, u_int pri, u_int vec, int pol, int sense)
  674. {
  675. openpic_safe_writefield(&ISR[irq]->Vector_Priority,
  676. OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK |
  677. OPENPIC_SENSE_MASK | OPENPIC_POLARITY_MASK,
  678. (pri << OPENPIC_PRIORITY_SHIFT) | vec |
  679. (pol ? OPENPIC_POLARITY_POSITIVE :
  680. OPENPIC_POLARITY_NEGATIVE) |
  681. (sense ? OPENPIC_SENSE_LEVEL : OPENPIC_SENSE_EDGE));
  682. }
  683. /*
  684. * Map an interrupt source to one or more CPUs
  685. */
  686. static void openpic_mapirq(u_int irq, cpumask_t physmask, cpumask_t keepmask)
  687. {
  688. if (ISR[irq] == 0)
  689. return;
  690. if (!cpus_empty(keepmask)) {
  691. cpumask_t irqdest = { .bits[0] = openpic_read(&ISR[irq]->Destination) };
  692. cpus_and(irqdest, irqdest, keepmask);
  693. cpus_or(physmask, physmask, irqdest);
  694. }
  695. openpic_write(&ISR[irq]->Destination, cpus_addr(physmask)[0]);
  696. }
  697. #ifdef notused
  698. /*
  699. * Set the sense for an interrupt source (and disable it!)
  700. *
  701. * sense: 1 for level, 0 for edge
  702. */
  703. static void openpic_set_sense(u_int irq, int sense)
  704. {
  705. if (ISR[irq] != 0)
  706. openpic_safe_writefield(&ISR[irq]->Vector_Priority,
  707. OPENPIC_SENSE_LEVEL,
  708. (sense ? OPENPIC_SENSE_LEVEL : 0));
  709. }
  710. #endif /* notused */
  711. /* No spinlocks, should not be necessary with the OpenPIC
  712. * (1 register = 1 interrupt and we have the desc lock).
  713. */
  714. static void openpic_ack_irq(unsigned int irq_nr)
  715. {
  716. #ifdef __SLOW_VERSION__
  717. openpic_disable_irq(irq_nr);
  718. openpic_eoi();
  719. #else
  720. if ((irq_desc[irq_nr].status & IRQ_LEVEL) == 0)
  721. openpic_eoi();
  722. #endif
  723. }
  724. static void openpic_end_irq(unsigned int irq_nr)
  725. {
  726. #ifdef __SLOW_VERSION__
  727. if (!(irq_desc[irq_nr].status & (IRQ_DISABLED|IRQ_INPROGRESS))
  728. && irq_desc[irq_nr].action)
  729. openpic_enable_irq(irq_nr);
  730. #else
  731. if ((irq_desc[irq_nr].status & IRQ_LEVEL) != 0)
  732. openpic_eoi();
  733. #endif
  734. }
  735. static void openpic_set_affinity(unsigned int irq_nr, cpumask_t cpumask)
  736. {
  737. openpic_mapirq(irq_nr - open_pic_irq_offset, physmask(cpumask), CPU_MASK_NONE);
  738. }
  739. #ifdef CONFIG_SMP
  740. static void openpic_ack_ipi(unsigned int irq_nr)
  741. {
  742. openpic_eoi();
  743. }
  744. static void openpic_end_ipi(unsigned int irq_nr)
  745. {
  746. }
  747. static irqreturn_t openpic_ipi_action(int cpl, void *dev_id)
  748. {
  749. smp_message_recv(cpl-OPENPIC_VEC_IPI-open_pic_irq_offset);
  750. return IRQ_HANDLED;
  751. }
  752. #endif /* CONFIG_SMP */
  753. int
  754. openpic_get_irq(void)
  755. {
  756. int irq = openpic_irq();
  757. /*
  758. * Check for the cascade interrupt and call the cascaded
  759. * interrupt controller function (usually i8259_irq) if so.
  760. * This should move to irq.c eventually. -- paulus
  761. */
  762. if (irq == openpic_cascade_irq && openpic_cascade_fn != NULL) {
  763. int cirq = openpic_cascade_fn();
  764. /* Allow for the cascade being shared with other devices */
  765. if (cirq != -1) {
  766. irq = cirq;
  767. openpic_eoi();
  768. }
  769. } else if (irq == OPENPIC_VEC_SPURIOUS)
  770. irq = -1;
  771. return irq;
  772. }
  773. #ifdef CONFIG_SMP
  774. void
  775. smp_openpic_message_pass(int target, int msg)
  776. {
  777. cpumask_t mask = CPU_MASK_ALL;
  778. /* make sure we're sending something that translates to an IPI */
  779. if (msg > 0x3) {
  780. printk("SMP %d: smp_message_pass: unknown msg %d\n",
  781. smp_processor_id(), msg);
  782. return;
  783. }
  784. switch (target) {
  785. case MSG_ALL:
  786. openpic_cause_IPI(msg, mask);
  787. break;
  788. case MSG_ALL_BUT_SELF:
  789. cpu_clear(smp_processor_id(), mask);
  790. openpic_cause_IPI(msg, mask);
  791. break;
  792. default:
  793. openpic_cause_IPI(msg, cpumask_of_cpu(target));
  794. break;
  795. }
  796. }
  797. #endif /* CONFIG_SMP */
  798. #ifdef CONFIG_PM
  799. /*
  800. * We implement the IRQ controller as a sysdev and put it
  801. * to sleep at powerdown stage (the callback is named suspend,
  802. * but it's old semantics, for the Device Model, it's really
  803. * powerdown). The possible problem is that another sysdev that
  804. * happens to be suspend after this one will have interrupts off,
  805. * that may be an issue... For now, this isn't an issue on pmac
  806. * though...
  807. */
  808. static u32 save_ipi_vp[OPENPIC_NUM_IPI];
  809. static u32 save_irq_src_vp[OPENPIC_MAX_SOURCES];
  810. static u32 save_irq_src_dest[OPENPIC_MAX_SOURCES];
  811. static u32 save_cpu_task_pri[OPENPIC_MAX_PROCESSORS];
  812. static int openpic_suspend_count;
  813. static void openpic_cached_enable_irq(u_int irq)
  814. {
  815. check_arg_irq(irq);
  816. save_irq_src_vp[irq - open_pic_irq_offset] &= ~OPENPIC_MASK;
  817. }
  818. static void openpic_cached_disable_irq(u_int irq)
  819. {
  820. check_arg_irq(irq);
  821. save_irq_src_vp[irq - open_pic_irq_offset] |= OPENPIC_MASK;
  822. }
  823. /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
  824. * we need something better to deal with that... Maybe switch to S1 for
  825. * cpufreq changes
  826. */
  827. int openpic_suspend(struct sys_device *sysdev, pm_message_t state)
  828. {
  829. int i;
  830. unsigned long flags;
  831. spin_lock_irqsave(&openpic_setup_lock, flags);
  832. if (openpic_suspend_count++ > 0) {
  833. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  834. return 0;
  835. }
  836. openpic_set_priority(0xf);
  837. open_pic.enable = openpic_cached_enable_irq;
  838. open_pic.disable = openpic_cached_disable_irq;
  839. for (i=0; i<NumProcessors; i++) {
  840. save_cpu_task_pri[i] = openpic_read(&OpenPIC->Processor[i].Current_Task_Priority);
  841. openpic_writefield(&OpenPIC->Processor[i].Current_Task_Priority,
  842. OPENPIC_CURRENT_TASK_PRIORITY_MASK, 0xf);
  843. }
  844. for (i=0; i<OPENPIC_NUM_IPI; i++)
  845. save_ipi_vp[i] = openpic_read(&OpenPIC->Global.IPI_Vector_Priority(i));
  846. for (i=0; i<NumSources; i++) {
  847. if (ISR[i] == 0)
  848. continue;
  849. save_irq_src_vp[i] = openpic_read(&ISR[i]->Vector_Priority) & ~OPENPIC_ACTIVITY;
  850. save_irq_src_dest[i] = openpic_read(&ISR[i]->Destination);
  851. }
  852. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  853. return 0;
  854. }
  855. /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
  856. * we need something better to deal with that... Maybe switch to S1 for
  857. * cpufreq changes
  858. */
  859. int openpic_resume(struct sys_device *sysdev)
  860. {
  861. int i;
  862. unsigned long flags;
  863. u32 vppmask = OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK |
  864. OPENPIC_SENSE_MASK | OPENPIC_POLARITY_MASK |
  865. OPENPIC_MASK;
  866. spin_lock_irqsave(&openpic_setup_lock, flags);
  867. if ((--openpic_suspend_count) > 0) {
  868. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  869. return 0;
  870. }
  871. /* OpenPIC sometimes seem to need some time to be fully back up... */
  872. do {
  873. openpic_set_spurious(OPENPIC_VEC_SPURIOUS);
  874. } while(openpic_readfield(&OpenPIC->Global.Spurious_Vector, OPENPIC_VECTOR_MASK)
  875. != OPENPIC_VEC_SPURIOUS);
  876. openpic_disable_8259_pass_through();
  877. for (i=0; i<OPENPIC_NUM_IPI; i++)
  878. openpic_write(&OpenPIC->Global.IPI_Vector_Priority(i),
  879. save_ipi_vp[i]);
  880. for (i=0; i<NumSources; i++) {
  881. if (ISR[i] == 0)
  882. continue;
  883. openpic_write(&ISR[i]->Destination, save_irq_src_dest[i]);
  884. openpic_write(&ISR[i]->Vector_Priority, save_irq_src_vp[i]);
  885. /* make sure mask gets to controller before we return to user */
  886. do {
  887. openpic_write(&ISR[i]->Vector_Priority, save_irq_src_vp[i]);
  888. } while (openpic_readfield(&ISR[i]->Vector_Priority, vppmask)
  889. != (save_irq_src_vp[i] & vppmask));
  890. }
  891. for (i=0; i<NumProcessors; i++)
  892. openpic_write(&OpenPIC->Processor[i].Current_Task_Priority,
  893. save_cpu_task_pri[i]);
  894. open_pic.enable = openpic_enable_irq;
  895. open_pic.disable = openpic_disable_irq;
  896. openpic_set_priority(0);
  897. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  898. return 0;
  899. }
  900. #endif /* CONFIG_PM */
  901. static struct sysdev_class openpic_sysclass = {
  902. set_kset_name("openpic"),
  903. };
  904. static struct sys_device device_openpic = {
  905. .id = 0,
  906. .cls = &openpic_sysclass,
  907. };
  908. static struct sysdev_driver driver_openpic = {
  909. #ifdef CONFIG_PM
  910. .suspend = &openpic_suspend,
  911. .resume = &openpic_resume,
  912. #endif /* CONFIG_PM */
  913. };
  914. static int __init init_openpic_sysfs(void)
  915. {
  916. int rc;
  917. if (!OpenPIC_Addr)
  918. return -ENODEV;
  919. printk(KERN_DEBUG "Registering openpic with sysfs...\n");
  920. rc = sysdev_class_register(&openpic_sysclass);
  921. if (rc) {
  922. printk(KERN_ERR "Failed registering openpic sys class\n");
  923. return -ENODEV;
  924. }
  925. rc = sysdev_register(&device_openpic);
  926. if (rc) {
  927. printk(KERN_ERR "Failed registering openpic sys device\n");
  928. return -ENODEV;
  929. }
  930. rc = sysdev_driver_register(&openpic_sysclass, &driver_openpic);
  931. if (rc) {
  932. printk(KERN_ERR "Failed registering openpic sys driver\n");
  933. return -ENODEV;
  934. }
  935. return 0;
  936. }
  937. subsys_initcall(init_openpic_sysfs);