open_pic.c 28 KB

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