sparc32: Move cache and TLB flushes over to method ops.
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / sparc / kernel / sun4d_irq.c
1 /*
2  * SS1000/SC2000 interrupt handling.
3  *
4  *  Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5  *  Heavily based on arch/sparc/kernel/irq.c.
6  */
7
8 #include <linux/kernel_stat.h>
9 #include <linux/seq_file.h>
10
11 #include <asm/timer.h>
12 #include <asm/traps.h>
13 #include <asm/irq.h>
14 #include <asm/io.h>
15 #include <asm/sbi.h>
16 #include <asm/cacheflush.h>
17 #include <asm/setup.h>
18 #include <asm/oplib.h>
19
20 #include "kernel.h"
21 #include "irq.h"
22
23 /* Sun4d interrupts fall roughly into two categories.  SBUS and
24  * cpu local.  CPU local interrupts cover the timer interrupts
25  * and whatnot, and we encode those as normal PILs between
26  * 0 and 15.
27  * SBUS interrupts are encodes as a combination of board, level and slot.
28  */
29
30 struct sun4d_handler_data {
31         unsigned int cpuid;    /* target cpu */
32         unsigned int real_irq; /* interrupt level */
33 };
34
35
36 static unsigned int sun4d_encode_irq(int board, int lvl, int slot)
37 {
38         return (board + 1) << 5 | (lvl << 2) | slot;
39 }
40
41 struct sun4d_timer_regs {
42         u32     l10_timer_limit;
43         u32     l10_cur_countx;
44         u32     l10_limit_noclear;
45         u32     ctrl;
46         u32     l10_cur_count;
47 };
48
49 static struct sun4d_timer_regs __iomem *sun4d_timers;
50
51 #define SUN4D_TIMER_IRQ        10
52
53 /* Specify which cpu handle interrupts from which board.
54  * Index is board - value is cpu.
55  */
56 static unsigned char board_to_cpu[32];
57
58 static int pil_to_sbus[] = {
59         0,
60         0,
61         1,
62         2,
63         0,
64         3,
65         0,
66         4,
67         0,
68         5,
69         0,
70         6,
71         0,
72         7,
73         0,
74         0,
75 };
76
77 /* Exported for sun4d_smp.c */
78 DEFINE_SPINLOCK(sun4d_imsk_lock);
79
80 /* SBUS interrupts are encoded integers including the board number
81  * (plus one), the SBUS level, and the SBUS slot number.  Sun4D
82  * IRQ dispatch is done by:
83  *
84  * 1) Reading the BW local interrupt table in order to get the bus
85  *    interrupt mask.
86  *
87  *    This table is indexed by SBUS interrupt level which can be
88  *    derived from the PIL we got interrupted on.
89  *
90  * 2) For each bus showing interrupt pending from #1, read the
91  *    SBI interrupt state register.  This will indicate which slots
92  *    have interrupts pending for that SBUS interrupt level.
93  *
94  * 3) Call the genreric IRQ support.
95  */
96 static void sun4d_sbus_handler_irq(int sbusl)
97 {
98         unsigned int bus_mask;
99         unsigned int sbino, slot;
100         unsigned int sbil;
101
102         bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
103         bw_clear_intr_mask(sbusl, bus_mask);
104
105         sbil = (sbusl << 2);
106         /* Loop for each pending SBI */
107         for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1) {
108                 unsigned int idx, mask;
109
110                 if (!(bus_mask & 1))
111                         continue;
112                 /* XXX This seems to ACK the irq twice.  acquire_sbi()
113                  * XXX uses swap, therefore this writes 0xf << sbil,
114                  * XXX then later release_sbi() will write the individual
115                  * XXX bits which were set again.
116                  */
117                 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
118                 mask &= (0xf << sbil);
119
120                 /* Loop for each pending SBI slot */
121                 slot = (1 << sbil);
122                 for (idx = 0; mask != 0; idx++, slot <<= 1) {
123                         unsigned int pil;
124                         struct irq_bucket *p;
125
126                         if (!(mask & slot))
127                                 continue;
128
129                         mask &= ~slot;
130                         pil = sun4d_encode_irq(sbino, sbusl, idx);
131
132                         p = irq_map[pil];
133                         while (p) {
134                                 struct irq_bucket *next;
135
136                                 next = p->next;
137                                 generic_handle_irq(p->irq);
138                                 p = next;
139                         }
140                         release_sbi(SBI2DEVID(sbino), slot);
141                 }
142         }
143 }
144
145 void sun4d_handler_irq(int pil, struct pt_regs *regs)
146 {
147         struct pt_regs *old_regs;
148         /* SBUS IRQ level (1 - 7) */
149         int sbusl = pil_to_sbus[pil];
150
151         /* FIXME: Is this necessary?? */
152         cc_get_ipen();
153
154         cc_set_iclr(1 << pil);
155
156 #ifdef CONFIG_SMP
157         /*
158          * Check IPI data structures after IRQ has been cleared. Hard and Soft
159          * IRQ can happen at the same time, so both cases are always handled.
160          */
161         if (pil == SUN4D_IPI_IRQ)
162                 sun4d_ipi_interrupt();
163 #endif
164
165         old_regs = set_irq_regs(regs);
166         irq_enter();
167         if (sbusl == 0) {
168                 /* cpu interrupt */
169                 struct irq_bucket *p;
170
171                 p = irq_map[pil];
172                 while (p) {
173                         struct irq_bucket *next;
174
175                         next = p->next;
176                         generic_handle_irq(p->irq);
177                         p = next;
178                 }
179         } else {
180                 /* SBUS interrupt */
181                 sun4d_sbus_handler_irq(sbusl);
182         }
183         irq_exit();
184         set_irq_regs(old_regs);
185 }
186
187
188 static void sun4d_mask_irq(struct irq_data *data)
189 {
190         struct sun4d_handler_data *handler_data = data->handler_data;
191         unsigned int real_irq;
192 #ifdef CONFIG_SMP
193         int cpuid = handler_data->cpuid;
194         unsigned long flags;
195 #endif
196         real_irq = handler_data->real_irq;
197 #ifdef CONFIG_SMP
198         spin_lock_irqsave(&sun4d_imsk_lock, flags);
199         cc_set_imsk_other(cpuid, cc_get_imsk_other(cpuid) | (1 << real_irq));
200         spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
201 #else
202         cc_set_imsk(cc_get_imsk() | (1 << real_irq));
203 #endif
204 }
205
206 static void sun4d_unmask_irq(struct irq_data *data)
207 {
208         struct sun4d_handler_data *handler_data = data->handler_data;
209         unsigned int real_irq;
210 #ifdef CONFIG_SMP
211         int cpuid = handler_data->cpuid;
212         unsigned long flags;
213 #endif
214         real_irq = handler_data->real_irq;
215
216 #ifdef CONFIG_SMP
217         spin_lock_irqsave(&sun4d_imsk_lock, flags);
218         cc_set_imsk_other(cpuid, cc_get_imsk_other(cpuid) & ~(1 << real_irq));
219         spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
220 #else
221         cc_set_imsk(cc_get_imsk() & ~(1 << real_irq));
222 #endif
223 }
224
225 static unsigned int sun4d_startup_irq(struct irq_data *data)
226 {
227         irq_link(data->irq);
228         sun4d_unmask_irq(data);
229         return 0;
230 }
231
232 static void sun4d_shutdown_irq(struct irq_data *data)
233 {
234         sun4d_mask_irq(data);
235         irq_unlink(data->irq);
236 }
237
238 struct irq_chip sun4d_irq = {
239         .name           = "sun4d",
240         .irq_startup    = sun4d_startup_irq,
241         .irq_shutdown   = sun4d_shutdown_irq,
242         .irq_unmask     = sun4d_unmask_irq,
243         .irq_mask       = sun4d_mask_irq,
244 };
245
246 #ifdef CONFIG_SMP
247 static void sun4d_set_cpu_int(int cpu, int level)
248 {
249         sun4d_send_ipi(cpu, level);
250 }
251
252 /* Setup IRQ distribution scheme. */
253 void __init sun4d_distribute_irqs(void)
254 {
255         struct device_node *dp;
256
257         int cpuid = cpu_logical_map(1);
258
259         if (cpuid == -1)
260                 cpuid = cpu_logical_map(0);
261         for_each_node_by_name(dp, "sbi") {
262                 int devid = of_getintprop_default(dp, "device-id", 0);
263                 int board = of_getintprop_default(dp, "board#", 0);
264                 board_to_cpu[board] = cpuid;
265                 set_sbi_tid(devid, cpuid << 3);
266         }
267         printk(KERN_ERR "All sbus IRQs directed to CPU%d\n", cpuid);
268 }
269 #endif
270
271 static void sun4d_clear_clock_irq(void)
272 {
273         sbus_readl(&sun4d_timers->l10_timer_limit);
274 }
275
276 static void sun4d_load_profile_irq(int cpu, unsigned int limit)
277 {
278         unsigned int value = limit ? timer_value(limit) : 0;
279         bw_set_prof_limit(cpu, value);
280 }
281
282 static void __init sun4d_load_profile_irqs(void)
283 {
284         int cpu = 0, mid;
285
286         while (!cpu_find_by_instance(cpu, NULL, &mid)) {
287                 sun4d_load_profile_irq(mid >> 3, 0);
288                 cpu++;
289         }
290 }
291
292 unsigned int _sun4d_build_device_irq(unsigned int real_irq,
293                                      unsigned int pil,
294                                      unsigned int board)
295 {
296         struct sun4d_handler_data *handler_data;
297         unsigned int irq;
298
299         irq = irq_alloc(real_irq, pil);
300         if (irq == 0) {
301                 prom_printf("IRQ: allocate for %d %d %d failed\n",
302                         real_irq, pil, board);
303                 goto err_out;
304         }
305
306         handler_data = irq_get_handler_data(irq);
307         if (unlikely(handler_data))
308                 goto err_out;
309
310         handler_data = kzalloc(sizeof(struct sun4d_handler_data), GFP_ATOMIC);
311         if (unlikely(!handler_data)) {
312                 prom_printf("IRQ: kzalloc(sun4d_handler_data) failed.\n");
313                 prom_halt();
314         }
315         handler_data->cpuid    = board_to_cpu[board];
316         handler_data->real_irq = real_irq;
317         irq_set_chip_and_handler_name(irq, &sun4d_irq,
318                                       handle_level_irq, "level");
319         irq_set_handler_data(irq, handler_data);
320
321 err_out:
322         return irq;
323 }
324
325
326
327 unsigned int sun4d_build_device_irq(struct platform_device *op,
328                                     unsigned int real_irq)
329 {
330         struct device_node *dp = op->dev.of_node;
331         struct device_node *board_parent, *bus = dp->parent;
332         char *bus_connection;
333         const struct linux_prom_registers *regs;
334         unsigned int pil;
335         unsigned int irq;
336         int board, slot;
337         int sbusl;
338
339         irq = real_irq;
340         while (bus) {
341                 if (!strcmp(bus->name, "sbi")) {
342                         bus_connection = "io-unit";
343                         break;
344                 }
345
346                 if (!strcmp(bus->name, "bootbus")) {
347                         bus_connection = "cpu-unit";
348                         break;
349                 }
350
351                 bus = bus->parent;
352         }
353         if (!bus)
354                 goto err_out;
355
356         regs = of_get_property(dp, "reg", NULL);
357         if (!regs)
358                 goto err_out;
359
360         slot = regs->which_io;
361
362         /*
363          * If Bus nodes parent is not io-unit/cpu-unit or the io-unit/cpu-unit
364          * lacks a "board#" property, something is very wrong.
365          */
366         if (!bus->parent || strcmp(bus->parent->name, bus_connection)) {
367                 printk(KERN_ERR "%s: Error, parent is not %s.\n",
368                         bus->full_name, bus_connection);
369                 goto err_out;
370         }
371         board_parent = bus->parent;
372         board = of_getintprop_default(board_parent, "board#", -1);
373         if (board == -1) {
374                 printk(KERN_ERR "%s: Error, lacks board# property.\n",
375                         board_parent->full_name);
376                 goto err_out;
377         }
378
379         sbusl = pil_to_sbus[real_irq];
380         if (sbusl)
381                 pil = sun4d_encode_irq(board, sbusl, slot);
382         else
383                 pil = real_irq;
384
385         irq = _sun4d_build_device_irq(real_irq, pil, board);
386 err_out:
387         return irq;
388 }
389
390 unsigned int sun4d_build_timer_irq(unsigned int board, unsigned int real_irq)
391 {
392         return _sun4d_build_device_irq(real_irq, real_irq, board);
393 }
394
395
396 static void __init sun4d_fixup_trap_table(void)
397 {
398 #ifdef CONFIG_SMP
399         unsigned long flags;
400         struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
401
402         /* Adjust so that we jump directly to smp4d_ticker */
403         lvl14_save[2] += smp4d_ticker - real_irq_entry;
404
405         /* For SMP we use the level 14 ticker, however the bootup code
406          * has copied the firmware's level 14 vector into the boot cpu's
407          * trap table, we must fix this now or we get squashed.
408          */
409         local_irq_save(flags);
410         patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
411         trap_table->inst_one = lvl14_save[0];
412         trap_table->inst_two = lvl14_save[1];
413         trap_table->inst_three = lvl14_save[2];
414         trap_table->inst_four = lvl14_save[3];
415         local_ops->cache_all();
416         local_irq_restore(flags);
417 #endif
418 }
419
420 static void __init sun4d_init_timers(void)
421 {
422         struct device_node *dp;
423         struct resource res;
424         unsigned int irq;
425         const u32 *reg;
426         int err;
427         int board;
428
429         dp = of_find_node_by_name(NULL, "cpu-unit");
430         if (!dp) {
431                 prom_printf("sun4d_init_timers: Unable to find cpu-unit\n");
432                 prom_halt();
433         }
434
435         /* Which cpu-unit we use is arbitrary, we can view the bootbus timer
436          * registers via any cpu's mapping.  The first 'reg' property is the
437          * bootbus.
438          */
439         reg = of_get_property(dp, "reg", NULL);
440         if (!reg) {
441                 prom_printf("sun4d_init_timers: No reg property\n");
442                 prom_halt();
443         }
444
445         board = of_getintprop_default(dp, "board#", -1);
446         if (board == -1) {
447                 prom_printf("sun4d_init_timers: No board# property on cpu-unit\n");
448                 prom_halt();
449         }
450
451         of_node_put(dp);
452
453         res.start = reg[1];
454         res.end = reg[2] - 1;
455         res.flags = reg[0] & 0xff;
456         sun4d_timers = of_ioremap(&res, BW_TIMER_LIMIT,
457                                   sizeof(struct sun4d_timer_regs), "user timer");
458         if (!sun4d_timers) {
459                 prom_printf("sun4d_init_timers: Can't map timer regs\n");
460                 prom_halt();
461         }
462
463 #ifdef CONFIG_SMP
464         sparc_config.cs_period = SBUS_CLOCK_RATE * 2;  /* 2 seconds */
465 #else
466         sparc_config.cs_period = SBUS_CLOCK_RATE / HZ; /* 1/HZ sec  */
467         sparc_config.features |= FEAT_L10_CLOCKEVENT;
468 #endif
469         sparc_config.features |= FEAT_L10_CLOCKSOURCE;
470         sbus_writel(timer_value(sparc_config.cs_period),
471                     &sun4d_timers->l10_timer_limit);
472
473         master_l10_counter = &sun4d_timers->l10_cur_count;
474
475         irq = sun4d_build_timer_irq(board, SUN4D_TIMER_IRQ);
476         err = request_irq(irq, timer_interrupt, IRQF_TIMER, "timer", NULL);
477         if (err) {
478                 prom_printf("sun4d_init_timers: request_irq() failed with %d\n",
479                              err);
480                 prom_halt();
481         }
482         sun4d_load_profile_irqs();
483         sun4d_fixup_trap_table();
484 }
485
486 void __init sun4d_init_sbi_irq(void)
487 {
488         struct device_node *dp;
489         int target_cpu;
490
491         target_cpu = boot_cpu_id;
492         for_each_node_by_name(dp, "sbi") {
493                 int devid = of_getintprop_default(dp, "device-id", 0);
494                 int board = of_getintprop_default(dp, "board#", 0);
495                 unsigned int mask;
496
497                 set_sbi_tid(devid, target_cpu << 3);
498                 board_to_cpu[board] = target_cpu;
499
500                 /* Get rid of pending irqs from PROM */
501                 mask = acquire_sbi(devid, 0xffffffff);
502                 if (mask) {
503                         printk(KERN_ERR "Clearing pending IRQs %08x on SBI %d\n",
504                                mask, board);
505                         release_sbi(devid, mask);
506                 }
507         }
508 }
509
510 void __init sun4d_init_IRQ(void)
511 {
512         local_irq_disable();
513
514         BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
515         BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
516
517         sparc_config.init_timers      = sun4d_init_timers;
518         sparc_config.build_device_irq = sun4d_build_device_irq;
519         sparc_config.clock_rate       = SBUS_CLOCK_RATE;
520
521 #ifdef CONFIG_SMP
522         BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
523 #endif
524         /* Cannot enable interrupts until OBP ticker is disabled. */
525 }