1/*  $Id: sun4d_irq.c,v 1.1.1.1 2007/08/03 18:52:17 Exp $
2 *  arch/sparc/kernel/sun4d_irq.c:
3 *			SS1000/SC2000 interrupt handling.
4 *
5 *  Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 *  Heavily based on arch/sparc/kernel/irq.c.
7 */
8
9#include <linux/errno.h>
10#include <linux/linkage.h>
11#include <linux/kernel_stat.h>
12#include <linux/signal.h>
13#include <linux/sched.h>
14#include <linux/ptrace.h>
15#include <linux/interrupt.h>
16#include <linux/slab.h>
17#include <linux/random.h>
18#include <linux/init.h>
19#include <linux/smp.h>
20#include <linux/spinlock.h>
21#include <linux/seq_file.h>
22
23#include <asm/ptrace.h>
24#include <asm/processor.h>
25#include <asm/system.h>
26#include <asm/psr.h>
27#include <asm/smp.h>
28#include <asm/vaddrs.h>
29#include <asm/timer.h>
30#include <asm/openprom.h>
31#include <asm/oplib.h>
32#include <asm/traps.h>
33#include <asm/irq.h>
34#include <asm/io.h>
35#include <asm/pgalloc.h>
36#include <asm/pgtable.h>
37#include <asm/sbus.h>
38#include <asm/sbi.h>
39#include <asm/cacheflush.h>
40#include <asm/irq_regs.h>
41
42/* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */
43/* #define DISTRIBUTE_IRQS */
44
45struct sun4d_timer_regs *sun4d_timers;
46#define TIMER_IRQ	10
47
48#define MAX_STATIC_ALLOC	4
49extern struct irqaction static_irqaction[MAX_STATIC_ALLOC];
50extern int static_irq_count;
51unsigned char cpu_leds[32];
52#ifdef CONFIG_SMP
53unsigned char sbus_tid[32];
54#endif
55
56static struct irqaction *irq_action[NR_IRQS];
57extern spinlock_t irq_action_lock;
58
59struct sbus_action {
60	struct irqaction *action;
61	/* For SMP this needs to be extended */
62} *sbus_actions;
63
64static int pil_to_sbus[] = {
65	0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
66};
67
68static int sbus_to_pil[] = {
69	0, 2, 3, 5, 7, 9, 11, 13,
70};
71
72static int nsbi;
73#ifdef CONFIG_SMP
74DEFINE_SPINLOCK(sun4d_imsk_lock);
75#endif
76
77int show_sun4d_interrupts(struct seq_file *p, void *v)
78{
79	int i = *(loff_t *) v, j = 0, k = 0, sbusl;
80	struct irqaction * action;
81	unsigned long flags;
82#ifdef CONFIG_SMP
83	int x;
84#endif
85
86	spin_lock_irqsave(&irq_action_lock, flags);
87	if (i < NR_IRQS) {
88		sbusl = pil_to_sbus[i];
89		if (!sbusl) {
90	 		action = *(i + irq_action);
91			if (!action)
92		        	goto out_unlock;
93		} else {
94			for (j = 0; j < nsbi; j++) {
95				for (k = 0; k < 4; k++)
96					if ((action = sbus_actions [(j << 5) + (sbusl << 2) + k].action))
97						goto found_it;
98			}
99			goto out_unlock;
100		}
101found_it:	seq_printf(p, "%3d: ", i);
102#ifndef CONFIG_SMP
103		seq_printf(p, "%10u ", kstat_irqs(i));
104#else
105		for_each_online_cpu(x)
106			seq_printf(p, "%10u ",
107			       kstat_cpu(cpu_logical_map(x)).irqs[i]);
108#endif
109		seq_printf(p, "%c %s",
110			(action->flags & IRQF_DISABLED) ? '+' : ' ',
111			action->name);
112		action = action->next;
113		for (;;) {
114			for (; action; action = action->next) {
115				seq_printf(p, ",%s %s",
116					(action->flags & IRQF_DISABLED) ? " +" : "",
117					action->name);
118			}
119			if (!sbusl) break;
120			k++;
121			if (k < 4)
122				action = sbus_actions [(j << 5) + (sbusl << 2) + k].action;
123			else {
124				j++;
125				if (j == nsbi) break;
126				k = 0;
127				action = sbus_actions [(j << 5) + (sbusl << 2)].action;
128			}
129		}
130		seq_putc(p, '\n');
131	}
132out_unlock:
133	spin_unlock_irqrestore(&irq_action_lock, flags);
134	return 0;
135}
136
137void sun4d_free_irq(unsigned int irq, void *dev_id)
138{
139	struct irqaction *action, **actionp;
140	struct irqaction *tmp = NULL;
141        unsigned long flags;
142
143	spin_lock_irqsave(&irq_action_lock, flags);
144	if (irq < 15)
145		actionp = irq + irq_action;
146	else
147		actionp = &(sbus_actions[irq - (1 << 5)].action);
148	action = *actionp;
149	if (!action) {
150		printk("Trying to free free IRQ%d\n",irq);
151		goto out_unlock;
152	}
153	if (dev_id) {
154		for (; action; action = action->next) {
155			if (action->dev_id == dev_id)
156				break;
157			tmp = action;
158		}
159		if (!action) {
160			printk("Trying to free free shared IRQ%d\n",irq);
161			goto out_unlock;
162		}
163	} else if (action->flags & IRQF_SHARED) {
164		printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
165		goto out_unlock;
166	}
167	if (action->flags & SA_STATIC_ALLOC)
168	{
169		/* This interrupt is marked as specially allocated
170		 * so it is a bad idea to free it.
171		 */
172		printk("Attempt to free statically allocated IRQ%d (%s)\n",
173		       irq, action->name);
174		goto out_unlock;
175	}
176
177	if (action && tmp)
178		tmp->next = action->next;
179	else
180		*actionp = action->next;
181
182	spin_unlock_irqrestore(&irq_action_lock, flags);
183
184	synchronize_irq(irq);
185
186	spin_lock_irqsave(&irq_action_lock, flags);
187
188	kfree(action);
189
190	if (!(*actionp))
191		disable_irq(irq);
192
193out_unlock:
194	spin_unlock_irqrestore(&irq_action_lock, flags);
195}
196
197extern void unexpected_irq(int, void *, struct pt_regs *);
198
199void sun4d_handler_irq(int irq, struct pt_regs * regs)
200{
201	struct pt_regs *old_regs;
202	struct irqaction * action;
203	int cpu = smp_processor_id();
204	/* SBUS IRQ level (1 - 7) */
205	int sbusl = pil_to_sbus[irq];
206
207	cc_get_ipen();
208
209	cc_set_iclr(1 << irq);
210
211	old_regs = set_irq_regs(regs);
212	irq_enter();
213	kstat_cpu(cpu).irqs[irq]++;
214	if (!sbusl) {
215		action = *(irq + irq_action);
216		if (!action)
217			unexpected_irq(irq, NULL, regs);
218		do {
219			action->handler(irq, action->dev_id);
220			action = action->next;
221		} while (action);
222	} else {
223		int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
224		int sbino;
225		struct sbus_action *actionp;
226		unsigned mask, slot;
227		int sbil = (sbusl << 2);
228
229		bw_clear_intr_mask(sbusl, bus_mask);
230
231		/* Loop for each pending SBI */
232		for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
233			if (bus_mask & 1) {
234				mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
235				mask &= (0xf << sbil);
236				actionp = sbus_actions + (sbino << 5) + (sbil);
237				/* Loop for each pending SBI slot */
238				for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
239					if (mask & slot) {
240						mask &= ~slot;
241						action = actionp->action;
242
243						if (!action)
244							unexpected_irq(irq, NULL, regs);
245						do {
246							action->handler(irq, action->dev_id);
247							action = action->next;
248						} while (action);
249						release_sbi(SBI2DEVID(sbino), slot);
250					}
251			}
252	}
253	irq_exit();
254	set_irq_regs(old_regs);
255}
256
257unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq)
258{
259	int sbusl = pil_to_sbus[irq];
260
261	if (sbusl)
262		return ((sdev->bus->board + 1) << 5) + (sbusl << 2) + sdev->slot;
263	else
264		return irq;
265}
266
267unsigned int sun4d_sbint_to_irq(struct sbus_dev *sdev, unsigned int sbint)
268{
269	if (sbint >= sizeof(sbus_to_pil)) {
270		printk(KERN_ERR "%s: bogus SBINT %d\n", sdev->prom_name, sbint);
271		BUG();
272	}
273	return sun4d_build_irq(sdev, sbus_to_pil[sbint]);
274}
275
276int sun4d_request_irq(unsigned int irq,
277		irq_handler_t handler,
278		unsigned long irqflags, const char * devname, void *dev_id)
279{
280	struct irqaction *action, *tmp = NULL, **actionp;
281	unsigned long flags;
282	int ret;
283
284	if(irq > 14 && irq < (1 << 5)) {
285		ret = -EINVAL;
286		goto out;
287	}
288
289	if (!handler) {
290		ret = -EINVAL;
291		goto out;
292	}
293
294	spin_lock_irqsave(&irq_action_lock, flags);
295
296	if (irq >= (1 << 5))
297		actionp = &(sbus_actions[irq - (1 << 5)].action);
298	else
299		actionp = irq + irq_action;
300	action = *actionp;
301
302	if (action) {
303		if ((action->flags & IRQF_SHARED) && (irqflags & IRQF_SHARED)) {
304			for (tmp = action; tmp->next; tmp = tmp->next);
305		} else {
306			ret = -EBUSY;
307			goto out_unlock;
308		}
309		if ((action->flags & IRQF_DISABLED) ^ (irqflags & IRQF_DISABLED)) {
310			printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
311			ret = -EBUSY;
312			goto out_unlock;
313		}
314		action = NULL;		/* Or else! */
315	}
316
317	/* If this is flagged as statically allocated then we use our
318	 * private struct which is never freed.
319	 */
320	if (irqflags & SA_STATIC_ALLOC) {
321		if (static_irq_count < MAX_STATIC_ALLOC)
322			action = &static_irqaction[static_irq_count++];
323		else
324			printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname);
325	}
326
327	if (action == NULL)
328		action = kmalloc(sizeof(struct irqaction),
329						     GFP_ATOMIC);
330
331	if (!action) {
332		ret = -ENOMEM;
333		goto out_unlock;
334	}
335
336	action->handler = handler;
337	action->flags = irqflags;
338	cpus_clear(action->mask);
339	action->name = devname;
340	action->next = NULL;
341	action->dev_id = dev_id;
342
343	if (tmp)
344		tmp->next = action;
345	else
346		*actionp = action;
347
348	enable_irq(irq);
349
350	ret = 0;
351out_unlock:
352	spin_unlock_irqrestore(&irq_action_lock, flags);
353out:
354	return ret;
355}
356
357static void sun4d_disable_irq(unsigned int irq)
358{
359#ifdef CONFIG_SMP
360	int tid = sbus_tid[(irq >> 5) - 1];
361	unsigned long flags;
362#endif
363
364	if (irq < NR_IRQS) return;
365#ifdef CONFIG_SMP
366	spin_lock_irqsave(&sun4d_imsk_lock, flags);
367	cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
368	spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
369#else
370	cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil[(irq >> 2) & 7]));
371#endif
372}
373
374static void sun4d_enable_irq(unsigned int irq)
375{
376#ifdef CONFIG_SMP
377	int tid = sbus_tid[(irq >> 5) - 1];
378	unsigned long flags;
379#endif
380
381	if (irq < NR_IRQS) return;
382#ifdef CONFIG_SMP
383	spin_lock_irqsave(&sun4d_imsk_lock, flags);
384	cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
385	spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
386#else
387	cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
388#endif
389}
390
391#ifdef CONFIG_SMP
392static void sun4d_set_cpu_int(int cpu, int level)
393{
394	sun4d_send_ipi(cpu, level);
395}
396
397static void sun4d_clear_ipi(int cpu, int level)
398{
399}
400
401static void sun4d_set_udt(int cpu)
402{
403}
404
405/* Setup IRQ distribution scheme. */
406void __init sun4d_distribute_irqs(void)
407{
408#ifdef DISTRIBUTE_IRQS
409	struct sbus_bus *sbus;
410	unsigned long sbus_serving_map;
411
412	sbus_serving_map = cpu_present_map;
413	for_each_sbus(sbus) {
414		if ((sbus->board * 2) == boot_cpu_id && (cpu_present_map & (1 << (sbus->board * 2 + 1))))
415			sbus_tid[sbus->board] = (sbus->board * 2 + 1);
416		else if (cpu_present_map & (1 << (sbus->board * 2)))
417			sbus_tid[sbus->board] = (sbus->board * 2);
418		else if (cpu_present_map & (1 << (sbus->board * 2 + 1)))
419			sbus_tid[sbus->board] = (sbus->board * 2 + 1);
420		else
421			sbus_tid[sbus->board] = 0xff;
422		if (sbus_tid[sbus->board] != 0xff)
423			sbus_serving_map &= ~(1 << sbus_tid[sbus->board]);
424	}
425	for_each_sbus(sbus)
426		if (sbus_tid[sbus->board] == 0xff) {
427			int i = 31;
428
429			if (!sbus_serving_map)
430				sbus_serving_map = cpu_present_map;
431			while (!(sbus_serving_map & (1 << i)))
432				i--;
433			sbus_tid[sbus->board] = i;
434			sbus_serving_map &= ~(1 << i);
435		}
436	for_each_sbus(sbus) {
437		printk("sbus%d IRQs directed to CPU%d\n", sbus->board, sbus_tid[sbus->board]);
438		set_sbi_tid(sbus->devid, sbus_tid[sbus->board] << 3);
439	}
440#else
441	struct sbus_bus *sbus;
442	int cpuid = cpu_logical_map(1);
443
444	if (cpuid == -1)
445		cpuid = cpu_logical_map(0);
446	for_each_sbus(sbus) {
447		sbus_tid[sbus->board] = cpuid;
448		set_sbi_tid(sbus->devid, cpuid << 3);
449	}
450	printk("All sbus IRQs directed to CPU%d\n", cpuid);
451#endif
452}
453#endif
454
455static void sun4d_clear_clock_irq(void)
456{
457	volatile unsigned int clear_intr;
458	clear_intr = sun4d_timers->l10_timer_limit;
459}
460
461static void sun4d_clear_profile_irq(int cpu)
462{
463	bw_get_prof_limit(cpu);
464}
465
466static void sun4d_load_profile_irq(int cpu, unsigned int limit)
467{
468	bw_set_prof_limit(cpu, limit);
469}
470
471static void __init sun4d_init_timers(irq_handler_t counter_fn)
472{
473	int irq;
474	int cpu;
475	struct resource r;
476	int mid;
477
478	/* Map the User Timer registers. */
479	memset(&r, 0, sizeof(r));
480#ifdef CONFIG_SMP
481	r.start = CSR_BASE(boot_cpu_id)+BW_TIMER_LIMIT;
482#else
483	r.start = CSR_BASE(0)+BW_TIMER_LIMIT;
484#endif
485	r.flags = 0xf;
486	sun4d_timers = (struct sun4d_timer_regs *) sbus_ioremap(&r, 0,
487	    PAGE_SIZE, "user timer");
488
489	sun4d_timers->l10_timer_limit =  (((1000000/HZ) + 1) << 10);
490	master_l10_counter = &sun4d_timers->l10_cur_count;
491	master_l10_limit = &sun4d_timers->l10_timer_limit;
492
493	irq = request_irq(TIMER_IRQ,
494			  counter_fn,
495			  (IRQF_DISABLED | SA_STATIC_ALLOC),
496			  "timer", NULL);
497	if (irq) {
498		prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ);
499		prom_halt();
500	}
501
502	/* Enable user timer free run for CPU 0 in BW */
503	/* bw_set_ctrl(0, bw_get_ctrl(0) | BW_CTRL_USER_TIMER); */
504
505	cpu = 0;
506	while (!cpu_find_by_instance(cpu, NULL, &mid)) {
507		sun4d_load_profile_irq(mid >> 3, 0);
508		cpu++;
509	}
510
511#ifdef CONFIG_SMP
512	{
513		unsigned long flags;
514		extern unsigned long lvl14_save[4];
515		struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
516		extern unsigned int real_irq_entry[], smp4d_ticker[];
517		extern unsigned int patchme_maybe_smp_msg[];
518
519		/* Adjust so that we jump directly to smp4d_ticker */
520		lvl14_save[2] += smp4d_ticker - real_irq_entry;
521
522		/* For SMP we use the level 14 ticker, however the bootup code
523		 * has copied the firmware's level 14 vector into the boot cpu's
524		 * trap table, we must fix this now or we get squashed.
525		 */
526		local_irq_save(flags);
527		patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
528		trap_table->inst_one = lvl14_save[0];
529		trap_table->inst_two = lvl14_save[1];
530		trap_table->inst_three = lvl14_save[2];
531		trap_table->inst_four = lvl14_save[3];
532		local_flush_cache_all();
533		local_irq_restore(flags);
534	}
535#endif
536}
537
538void __init sun4d_init_sbi_irq(void)
539{
540	struct sbus_bus *sbus;
541	unsigned mask;
542
543	nsbi = 0;
544	for_each_sbus(sbus)
545		nsbi++;
546	sbus_actions = kzalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
547	if (!sbus_actions) {
548		prom_printf("SUN4D: Cannot allocate sbus_actions, halting.\n");
549		prom_halt();
550	}
551	for_each_sbus(sbus) {
552#ifdef CONFIG_SMP
553		extern unsigned char boot_cpu_id;
554
555		set_sbi_tid(sbus->devid, boot_cpu_id << 3);
556		sbus_tid[sbus->board] = boot_cpu_id;
557#endif
558		/* Get rid of pending irqs from PROM */
559		mask = acquire_sbi(sbus->devid, 0xffffffff);
560		if (mask) {
561			printk ("Clearing pending IRQs %08x on SBI %d\n", mask, sbus->board);
562			release_sbi(sbus->devid, mask);
563		}
564	}
565}
566
567void __init sun4d_init_IRQ(void)
568{
569	local_irq_disable();
570
571	BTFIXUPSET_CALL(sbint_to_irq, sun4d_sbint_to_irq, BTFIXUPCALL_NORM);
572	BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
573	BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
574	BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
575	BTFIXUPSET_CALL(clear_profile_irq, sun4d_clear_profile_irq, BTFIXUPCALL_NORM);
576	BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
577	sparc_init_timers = sun4d_init_timers;
578#ifdef CONFIG_SMP
579	BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
580	BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
581	BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
582#endif
583	/* Cannot enable interrupts until OBP ticker is disabled. */
584}
585