open_pic.c 28 KB

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