open_pic.c 28 KB

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