open_pic.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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)
  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. 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. * Initalize the interrupt source which will generate an NMI.
  570. * This raises the interrupt's priority from 8 to 9.
  571. *
  572. * irq: The logical IRQ which generates an NMI.
  573. */
  574. void __init
  575. openpic_init_nmi_irq(u_int irq)
  576. {
  577. check_arg_irq(irq);
  578. openpic_safe_writefield(&ISR[irq - open_pic_irq_offset]->Vector_Priority,
  579. OPENPIC_PRIORITY_MASK,
  580. 9 << OPENPIC_PRIORITY_SHIFT);
  581. }
  582. /*
  583. *
  584. * All functions below take an offset'ed irq argument
  585. *
  586. */
  587. /*
  588. * Hookup a cascade to the OpenPIC.
  589. */
  590. static struct irqaction openpic_cascade_irqaction = {
  591. .handler = no_action,
  592. .flags = SA_INTERRUPT,
  593. .mask = CPU_MASK_NONE,
  594. };
  595. void __init
  596. openpic_hookup_cascade(u_int irq, char *name,
  597. int (*cascade_fn)(struct pt_regs *))
  598. {
  599. openpic_cascade_irq = irq;
  600. openpic_cascade_fn = cascade_fn;
  601. if (setup_irq(irq, &openpic_cascade_irqaction))
  602. printk("Unable to get OpenPIC IRQ %d for cascade\n",
  603. irq - open_pic_irq_offset);
  604. }
  605. /*
  606. * Enable/disable an external interrupt source
  607. *
  608. * Externally called, irq is an offseted system-wide interrupt number
  609. */
  610. static void openpic_enable_irq(u_int irq)
  611. {
  612. volatile u_int __iomem *vpp;
  613. check_arg_irq(irq);
  614. vpp = &ISR[irq - open_pic_irq_offset]->Vector_Priority;
  615. openpic_clearfield(vpp, OPENPIC_MASK);
  616. /* make sure mask gets to controller before we return to user */
  617. do {
  618. mb(); /* sync is probably useless here */
  619. } while (openpic_readfield(vpp, OPENPIC_MASK));
  620. }
  621. static void openpic_disable_irq(u_int irq)
  622. {
  623. volatile u_int __iomem *vpp;
  624. u32 vp;
  625. check_arg_irq(irq);
  626. vpp = &ISR[irq - open_pic_irq_offset]->Vector_Priority;
  627. openpic_setfield(vpp, OPENPIC_MASK);
  628. /* make sure mask gets to controller before we return to user */
  629. do {
  630. mb(); /* sync is probably useless here */
  631. vp = openpic_readfield(vpp, OPENPIC_MASK | OPENPIC_ACTIVITY);
  632. } while((vp & OPENPIC_ACTIVITY) && !(vp & OPENPIC_MASK));
  633. }
  634. #ifdef CONFIG_SMP
  635. /*
  636. * Enable/disable an IPI interrupt source
  637. *
  638. * Externally called, irq is an offseted system-wide interrupt number
  639. */
  640. void openpic_enable_ipi(u_int irq)
  641. {
  642. irq -= (OPENPIC_VEC_IPI+open_pic_irq_offset);
  643. check_arg_ipi(irq);
  644. openpic_clearfield_IPI(&OpenPIC->Global.IPI_Vector_Priority(irq), OPENPIC_MASK);
  645. }
  646. void openpic_disable_ipi(u_int irq)
  647. {
  648. irq -= (OPENPIC_VEC_IPI+open_pic_irq_offset);
  649. check_arg_ipi(irq);
  650. openpic_setfield_IPI(&OpenPIC->Global.IPI_Vector_Priority(irq), OPENPIC_MASK);
  651. }
  652. #endif
  653. /*
  654. * Initialize an interrupt source (and disable it!)
  655. *
  656. * irq: OpenPIC interrupt number
  657. * pri: interrupt source priority
  658. * vec: the vector it will produce
  659. * pol: polarity (1 for positive, 0 for negative)
  660. * sense: 1 for level, 0 for edge
  661. */
  662. static void __init
  663. openpic_initirq(u_int irq, u_int pri, u_int vec, int pol, int sense)
  664. {
  665. openpic_safe_writefield(&ISR[irq]->Vector_Priority,
  666. OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK |
  667. OPENPIC_SENSE_MASK | OPENPIC_POLARITY_MASK,
  668. (pri << OPENPIC_PRIORITY_SHIFT) | vec |
  669. (pol ? OPENPIC_POLARITY_POSITIVE :
  670. OPENPIC_POLARITY_NEGATIVE) |
  671. (sense ? OPENPIC_SENSE_LEVEL : OPENPIC_SENSE_EDGE));
  672. }
  673. /*
  674. * Map an interrupt source to one or more CPUs
  675. */
  676. static void openpic_mapirq(u_int irq, cpumask_t physmask, cpumask_t keepmask)
  677. {
  678. if (ISR[irq] == 0)
  679. return;
  680. if (!cpus_empty(keepmask)) {
  681. cpumask_t irqdest = { .bits[0] = openpic_read(&ISR[irq]->Destination) };
  682. cpus_and(irqdest, irqdest, keepmask);
  683. cpus_or(physmask, physmask, irqdest);
  684. }
  685. openpic_write(&ISR[irq]->Destination, cpus_addr(physmask)[0]);
  686. }
  687. #ifdef notused
  688. /*
  689. * Set the sense for an interrupt source (and disable it!)
  690. *
  691. * sense: 1 for level, 0 for edge
  692. */
  693. static void openpic_set_sense(u_int irq, int sense)
  694. {
  695. if (ISR[irq] != 0)
  696. openpic_safe_writefield(&ISR[irq]->Vector_Priority,
  697. OPENPIC_SENSE_LEVEL,
  698. (sense ? OPENPIC_SENSE_LEVEL : 0));
  699. }
  700. #endif /* notused */
  701. /* No spinlocks, should not be necessary with the OpenPIC
  702. * (1 register = 1 interrupt and we have the desc lock).
  703. */
  704. static void openpic_ack_irq(unsigned int irq_nr)
  705. {
  706. #ifdef __SLOW_VERSION__
  707. openpic_disable_irq(irq_nr);
  708. openpic_eoi();
  709. #else
  710. if ((irq_desc[irq_nr].status & IRQ_LEVEL) == 0)
  711. openpic_eoi();
  712. #endif
  713. }
  714. static void openpic_end_irq(unsigned int irq_nr)
  715. {
  716. #ifdef __SLOW_VERSION__
  717. if (!(irq_desc[irq_nr].status & (IRQ_DISABLED|IRQ_INPROGRESS))
  718. && irq_desc[irq_nr].action)
  719. openpic_enable_irq(irq_nr);
  720. #else
  721. if ((irq_desc[irq_nr].status & IRQ_LEVEL) != 0)
  722. openpic_eoi();
  723. #endif
  724. }
  725. static void openpic_set_affinity(unsigned int irq_nr, cpumask_t cpumask)
  726. {
  727. openpic_mapirq(irq_nr - open_pic_irq_offset, physmask(cpumask), CPU_MASK_NONE);
  728. }
  729. #ifdef CONFIG_SMP
  730. static void openpic_ack_ipi(unsigned int irq_nr)
  731. {
  732. openpic_eoi();
  733. }
  734. static void openpic_end_ipi(unsigned int irq_nr)
  735. {
  736. }
  737. static irqreturn_t openpic_ipi_action(int cpl, void *dev_id, struct pt_regs *regs)
  738. {
  739. smp_message_recv(cpl-OPENPIC_VEC_IPI-open_pic_irq_offset, regs);
  740. return IRQ_HANDLED;
  741. }
  742. #endif /* CONFIG_SMP */
  743. int
  744. openpic_get_irq(struct pt_regs *regs)
  745. {
  746. int irq = openpic_irq();
  747. /*
  748. * Check for the cascade interrupt and call the cascaded
  749. * interrupt controller function (usually i8259_irq) if so.
  750. * This should move to irq.c eventually. -- paulus
  751. */
  752. if (irq == openpic_cascade_irq && openpic_cascade_fn != NULL) {
  753. int cirq = openpic_cascade_fn(regs);
  754. /* Allow for the cascade being shared with other devices */
  755. if (cirq != -1) {
  756. irq = cirq;
  757. openpic_eoi();
  758. }
  759. } else if (irq == OPENPIC_VEC_SPURIOUS)
  760. irq = -1;
  761. return irq;
  762. }
  763. #ifdef CONFIG_SMP
  764. void
  765. smp_openpic_message_pass(int target, int msg, unsigned long data, int wait)
  766. {
  767. cpumask_t mask = CPU_MASK_ALL;
  768. /* make sure we're sending something that translates to an IPI */
  769. if (msg > 0x3) {
  770. printk("SMP %d: smp_message_pass: unknown msg %d\n",
  771. smp_processor_id(), msg);
  772. return;
  773. }
  774. switch (target) {
  775. case MSG_ALL:
  776. openpic_cause_IPI(msg, mask);
  777. break;
  778. case MSG_ALL_BUT_SELF:
  779. cpu_clear(smp_processor_id(), mask);
  780. openpic_cause_IPI(msg, mask);
  781. break;
  782. default:
  783. openpic_cause_IPI(msg, cpumask_of_cpu(target));
  784. break;
  785. }
  786. }
  787. #endif /* CONFIG_SMP */
  788. #ifdef CONFIG_PM
  789. /*
  790. * We implement the IRQ controller as a sysdev and put it
  791. * to sleep at powerdown stage (the callback is named suspend,
  792. * but it's old semantics, for the Device Model, it's really
  793. * powerdown). The possible problem is that another sysdev that
  794. * happens to be suspend after this one will have interrupts off,
  795. * that may be an issue... For now, this isn't an issue on pmac
  796. * though...
  797. */
  798. static u32 save_ipi_vp[OPENPIC_NUM_IPI];
  799. static u32 save_irq_src_vp[OPENPIC_MAX_SOURCES];
  800. static u32 save_irq_src_dest[OPENPIC_MAX_SOURCES];
  801. static u32 save_cpu_task_pri[OPENPIC_MAX_PROCESSORS];
  802. static int openpic_suspend_count;
  803. static void openpic_cached_enable_irq(u_int irq)
  804. {
  805. check_arg_irq(irq);
  806. save_irq_src_vp[irq - open_pic_irq_offset] &= ~OPENPIC_MASK;
  807. }
  808. static void openpic_cached_disable_irq(u_int irq)
  809. {
  810. check_arg_irq(irq);
  811. save_irq_src_vp[irq - open_pic_irq_offset] |= OPENPIC_MASK;
  812. }
  813. /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
  814. * we need something better to deal with that... Maybe switch to S1 for
  815. * cpufreq changes
  816. */
  817. int openpic_suspend(struct sys_device *sysdev, u32 state)
  818. {
  819. int i;
  820. unsigned long flags;
  821. spin_lock_irqsave(&openpic_setup_lock, flags);
  822. if (openpic_suspend_count++ > 0) {
  823. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  824. return 0;
  825. }
  826. openpic_set_priority(0xf);
  827. open_pic.enable = openpic_cached_enable_irq;
  828. open_pic.disable = openpic_cached_disable_irq;
  829. for (i=0; i<NumProcessors; i++) {
  830. save_cpu_task_pri[i] = openpic_read(&OpenPIC->Processor[i].Current_Task_Priority);
  831. openpic_writefield(&OpenPIC->Processor[i].Current_Task_Priority,
  832. OPENPIC_CURRENT_TASK_PRIORITY_MASK, 0xf);
  833. }
  834. for (i=0; i<OPENPIC_NUM_IPI; i++)
  835. save_ipi_vp[i] = openpic_read(&OpenPIC->Global.IPI_Vector_Priority(i));
  836. for (i=0; i<NumSources; i++) {
  837. if (ISR[i] == 0)
  838. continue;
  839. save_irq_src_vp[i] = openpic_read(&ISR[i]->Vector_Priority) & ~OPENPIC_ACTIVITY;
  840. save_irq_src_dest[i] = openpic_read(&ISR[i]->Destination);
  841. }
  842. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  843. return 0;
  844. }
  845. /* WARNING: Can be called directly by the cpufreq code with NULL parameter,
  846. * we need something better to deal with that... Maybe switch to S1 for
  847. * cpufreq changes
  848. */
  849. int openpic_resume(struct sys_device *sysdev)
  850. {
  851. int i;
  852. unsigned long flags;
  853. u32 vppmask = OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK |
  854. OPENPIC_SENSE_MASK | OPENPIC_POLARITY_MASK |
  855. OPENPIC_MASK;
  856. spin_lock_irqsave(&openpic_setup_lock, flags);
  857. if ((--openpic_suspend_count) > 0) {
  858. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  859. return 0;
  860. }
  861. /* OpenPIC sometimes seem to need some time to be fully back up... */
  862. do {
  863. openpic_set_spurious(OPENPIC_VEC_SPURIOUS);
  864. } while(openpic_readfield(&OpenPIC->Global.Spurious_Vector, OPENPIC_VECTOR_MASK)
  865. != OPENPIC_VEC_SPURIOUS);
  866. openpic_disable_8259_pass_through();
  867. for (i=0; i<OPENPIC_NUM_IPI; i++)
  868. openpic_write(&OpenPIC->Global.IPI_Vector_Priority(i),
  869. save_ipi_vp[i]);
  870. for (i=0; i<NumSources; i++) {
  871. if (ISR[i] == 0)
  872. continue;
  873. openpic_write(&ISR[i]->Destination, save_irq_src_dest[i]);
  874. openpic_write(&ISR[i]->Vector_Priority, save_irq_src_vp[i]);
  875. /* make sure mask gets to controller before we return to user */
  876. do {
  877. openpic_write(&ISR[i]->Vector_Priority, save_irq_src_vp[i]);
  878. } while (openpic_readfield(&ISR[i]->Vector_Priority, vppmask)
  879. != (save_irq_src_vp[i] & vppmask));
  880. }
  881. for (i=0; i<NumProcessors; i++)
  882. openpic_write(&OpenPIC->Processor[i].Current_Task_Priority,
  883. save_cpu_task_pri[i]);
  884. open_pic.enable = openpic_enable_irq;
  885. open_pic.disable = openpic_disable_irq;
  886. openpic_set_priority(0);
  887. spin_unlock_irqrestore(&openpic_setup_lock, flags);
  888. return 0;
  889. }
  890. #endif /* CONFIG_PM */
  891. static struct sysdev_class openpic_sysclass = {
  892. set_kset_name("openpic"),
  893. };
  894. static struct sys_device device_openpic = {
  895. .id = 0,
  896. .cls = &openpic_sysclass,
  897. };
  898. static struct sysdev_driver driver_openpic = {
  899. #ifdef CONFIG_PM
  900. .suspend = &openpic_suspend,
  901. .resume = &openpic_resume,
  902. #endif /* CONFIG_PM */
  903. };
  904. static int __init init_openpic_sysfs(void)
  905. {
  906. int rc;
  907. if (!OpenPIC_Addr)
  908. return -ENODEV;
  909. printk(KERN_DEBUG "Registering openpic with sysfs...\n");
  910. rc = sysdev_class_register(&openpic_sysclass);
  911. if (rc) {
  912. printk(KERN_ERR "Failed registering openpic sys class\n");
  913. return -ENODEV;
  914. }
  915. rc = sysdev_register(&device_openpic);
  916. if (rc) {
  917. printk(KERN_ERR "Failed registering openpic sys device\n");
  918. return -ENODEV;
  919. }
  920. rc = sysdev_driver_register(&openpic_sysclass, &driver_openpic);
  921. if (rc) {
  922. printk(KERN_ERR "Failed registering openpic sys driver\n");
  923. return -ENODEV;
  924. }
  925. return 0;
  926. }
  927. subsys_initcall(init_openpic_sysfs);