File: irq.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (356 lines) | stat: -rw-r--r-- 7,638 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (c) 1996-1999 The University of Utah and the Flux Group.
 * 
 * This file is part of the OSKit Linux Glue Libraries, which are free
 * software, also known as "open source;" you can redistribute them and/or
 * modify them under the terms of the GNU General Public License (GPL),
 * version 2, as published by the Free Software Foundation (FSF).
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Emulate Linux IRQ management.
 */

#ifndef OSKIT
#define OSKIT
#endif

#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/kernel_stat.h>

#include "irq.h"
#include "osenv.h"

static void linux_intr(void *data);
static void probe_intr(void *data);

static unsigned probe_mask;

#if 0
#define DEBUG_MSG() osenv_log(OSENV_LOG_DEBUG, "%s:%d:\n", __FILE__, __LINE__);
#else
#define DEBUG_MSG()
#endif

#define NHWI 16

/* flag = 1 if sharing allowed; set to 0 if not */
static char shared[NHWI];

/* linked list of functions for an IRQ */
struct int_handler {
	void	(*func)(int, void*, struct pt_regs *);
	int	flags;
        unsigned long mask;
        const char *name;
        void *dev_id;
	struct int_handler *next;
};

/* array of pointers to lists of interrupt handlers */
static struct int_handler *handlers[NHWI];


/*
 * Flag indicating a hard interrupt is being handled.
 */
unsigned int local_irq_count[1];		/* SMP */


void
linux_intr_init()
{
	int i;

	for (i = 0; i < NHWI; i++) {
		handlers[i] = NULL;
		shared[i] = 1;
	}
}



/*
 * Generic Linux interrupt handler.
 * I believe that the Linux equivalent to this routine expects interrupts
 * to be disabled (EFL_IF) coming in and going out.
 */
static void
linux_intr(void *data)
{
	int irq = (int)data;
	struct pt_regs regs;
	struct int_handler *hand;
	struct task_struct *cur = current;

	kstat.irqs[0][irq]++;
	local_irq_count[0]++;

	if ((handlers[irq]->flags & SA_INTERRUPT) == 0)
		linux_sti();

	hand = handlers[irq];

	while (hand) {
		(*hand->func)(irq, hand->dev_id, &regs);
		hand = hand->next;
	}

	local_irq_count[0]--;

	/* Run any Linux software interrupt handlers before returning */
	if ((local_irq_count[0] == 0) && (bh_mask & bh_active)) {
		linux_sti();
		do_bottom_half();
	}

	linux_cli();

	current = cur;
}


/*
 * Probe interrupts set the bit indicating they were received
 */
static void
probe_intr(void *data)
{
	int irq = (int)data;
#ifdef IRQ_DEBUG
	osenv_log(OSENV_LOG_DEBUG, "probe IRQ%d\n", irq);
#endif
	probe_mask |= (1 << irq);
}

/*
 * Attach a handler to an IRQ.
 */
int
request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *),
	    unsigned long flags, const char *device, void *dev_id)
{
	void (*intr_handler)(void *);
	struct task_struct *cur = current;
	int rc;
	int fdev_flags;
	struct int_handler *temp;

	/* Make sure it is valid to assign this handler */
#ifdef IRQ_DEBUG
	osenv_log(OSENV_LOG_DEBUG, "IRQ %d requested by %s at %p\n",
		irq, (device ? device : ""), handler);
#endif

	if (!handler) {
		return (-EINVAL);
	}

	if (!shared[irq]) {
		return (-EBUSY);
	}

	/* If owned by sharing, and we won't share, we're done too */
	if (handlers[irq] && ((flags & SA_SHIRQ) == 0)) {
		return (-EBUSY);
	}

	/* okay, now set up the handler */

	temp = osenv_mem_alloc(sizeof(struct int_handler), 0, 1);
	if (!temp)
		return (-ENOMEM);

	temp->next = handlers[irq];
	handlers[irq] = temp;

	handlers[irq]->func = handler;
	handlers[irq]->flags = flags;
	handlers[irq]->dev_id = dev_id;

	if (handlers[irq]->next) { /* already have the IRQ from FDEV. */
		osenv_log(OSENV_LOG_DEBUG, 
			"adding another linux handler to irq %d, tag = %x\n",
			irq, (unsigned)handler);
	} else {

		intr_handler = (flags & SA_PROBE) ? probe_intr : linux_intr;
        	fdev_flags = (flags & SA_SHIRQ) ? OSENV_IRQ_SHAREABLE : 0;

		rc=osenv_irq_alloc(irq, intr_handler, (void *)irq, fdev_flags);
		current = cur;	/* restore current after possibly blocking */
		if (rc) {
			temp = handlers[irq];
			handlers[irq] = handlers[irq]->next;
			osenv_mem_free(temp, 0, sizeof(struct int_handler));

			return (-EBUSY);
		}
	}

	/* update the shared flag */
	if ((flags & SA_SHIRQ) == 0)
		shared[irq] = 0;

	DEBUG_MSG();
	return (0);
}

/*
 * Deallocate an irq.
 */
void
free_irq(unsigned int irq, void *dev_id)
{
	struct task_struct *cur = current;
	struct int_handler *temp, *last;

	temp = handlers[irq];

	if (!temp)
		return;

	/* one element -- free the irq */
	if (!temp->next) {
		void (*intr_handler)(void *);

		if (temp->dev_id != dev_id)
			return;
		handlers[irq]=NULL;
		intr_handler = (temp->flags & SA_PROBE) ? 
			probe_intr : linux_intr; 
		osenv_irq_free(irq, intr_handler, (void*)irq);
		osenv_mem_free(temp, 0, sizeof(struct int_handler));
		shared[irq] = 1;

		current = cur;
		return;
	}

	last = temp;

	/* find the correct element and remove it */
	while (temp && (temp->dev_id != dev_id)) {
		last = temp;
		temp = temp->next;
	}

	if (!temp)	/* not found */
		return;

	if (temp == last) {	/* first node */
		handlers[irq] = temp->next;
		osenv_mem_free(temp, 0, sizeof(struct int_handler));
	} else {
		last->next = temp->next;
		osenv_mem_free(temp, 0, sizeof(struct int_handler));
	}

	current = cur;
}

void
disable_irq(unsigned int irq)
{
	osenv_irq_disable(irq);
}

#ifndef OSKIT_ARM32
void
disable_irq_nosync(unsigned int irq)
{
	osenv_irq_disable(irq);
}
#endif

void
enable_irq(unsigned int irq)
{
	osenv_irq_enable(irq);
}


static void no_action(int cpl, void *arg, struct pt_regs * regs) { }

/*
 * Get ready to probe for the interrupts.
 * Set IRQs received to 0 (probe_mask).
 * Allocate all available IRQs, and return that set.
 * If receive any IRQs here, we don't want that IRQ
 */
unsigned long 
probe_irq_on (void)
{
	unsigned int i, irqs = 0, irqmask;
	unsigned long delay;

	/* first, snaffle up any unassigned irqs */
	probe_mask = 0;
	for (i = (NHWI - 1); i > 0; i--) {
		if (!request_irq(i, no_action, SA_PROBE, "probe", NULL)) {
			enable_irq(i);
			irqs |= (1 << i);
		}
	}

	/* wait for spurious interrupts to mask themselves out again */
	for (delay = jiffies + 5; delay > jiffies; );	/* min 40ms delay */

	irqmask = probe_mask;	/* if bit is set, it is spurious */
	/* now filter out any obviously spurious interrupts */
	for (i = (NHWI - 1); i > 0; i--) {
		if (irqs & (1 << i) & irqmask) {
#ifdef IRQ_DEBUG
			osenv_log(OSENV_LOG_NOTICE, "Spurious IRQ%d\n", i);
#endif
			irqs ^= (1 << i);
			free_irq(i, NULL);
		}
	}

#ifdef IRQ_DEBUG
	printk("probe_irq_on:  irqs=0x%04x irqmask=0x%04x\n\n", irqs, 
	       probe_mask);
#endif
	return irqs;
}

/*
 * Free all allocated IRQs.
 * If only one of the IRQs grabbed was received, return it.
 * Otherwise, autoprobe failed.
 */
int 
probe_irq_off (unsigned long irqs)
{
	unsigned int i, irqmask;

	irqmask = probe_mask;	/* IRQs received */
	/* free all allocated IRQs */
	for (i = (NHWI - 1); i > 0; i--) {
		if (irqs & (1 << i)) {
			free_irq(i, NULL);
		}
	}
#ifdef IRQ_DEBUG
	printk("probe_irq_off: irqs=0x%04x irqmask=0x%04x\n", (unsigned)irqs, 
	       irqmask);
#endif
	/* what allocated IRQs did I receive? */
	irqs &= irqmask;
	if (!irqs)	/* none found... */
		return 0;
	i = ffz(~irqs);
	if (irqs != (irqs & (1 << i)))	/* multiple IRQs found? */
		i = -i;
	return i;
}