summaryrefslogtreecommitdiff
path: root/libdde_linux26/lib/src/arch/l4/irq.c
blob: 20bd57f00889e49f407a770b73fd8570a7719e39 (plain)
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
/*
 * \brief   Hardware-interrupt support
 * \author  Christian Helmuth <ch12@os.inf.tu-dresden.de>
 * \date    2007-02-12
 *
 *
 *
 * XXX Consider support for IRQ_HANDLED and friends (linux/irqreturn.h)
 */

/* Linux */
#include <linux/interrupt.h>
#include <linux/string.h>     /* memset() */

/* local */
#include "dde26.h"
#include "local.h"

/* dummy */
irq_cpustat_t irq_stat[CONFIG_NR_CPUS];

/**
 * IRQ handling data
 */
static struct dde_irq
{
	unsigned              irq;     /* IRQ number */
	unsigned              count;   /* usage count */
	int                   shared;  /* shared IRQ */
	struct ddekit_thread *thread;  /* DDEKit interrupt thread */
	struct irqaction     *action;  /* Linux IRQ action */

	struct dde_irq       *next;    /* next DDE IRQ */
} *used_irqs;


static void irq_thread_init(void *p) {
	l4dde26_process_add_worker(); }


extern ddekit_sem_t *dde_softirq_sem;
static void irq_handler(void *arg)
{
	struct dde_irq *irq = arg;
	struct irqaction *action;

#if 0
	DEBUG_MSG("irq 0x%x", irq->irq);
#endif
	/* interrupt occurred - call all handlers */
	for (action = irq->action; action; action = action->next) {
		irqreturn_t r = action->handler(action->irq, action->dev_id);
#if 0
		DEBUG_MSG("return: %s", r == IRQ_HANDLED ? "IRQ_HANDLED" : r == IRQ_NONE ? "IRQ_NONE" : "??");
#endif
	}

	/* upon return we check for pending soft irqs */
	if (local_softirq_pending())
		ddekit_sem_up(dde_softirq_sem);
}


/*****************************
 ** IRQ handler bookkeeping **
 *****************************/

/**
 * Claim IRQ
 *
 * \return usage counter or negative error code
 *
 * FIXME list locking
 * FIXME are there more races?
 */
static int claim_irq(struct irqaction *action)
{
	int shared = action->flags & IRQF_SHARED ? 1 : 0;
	struct dde_irq *irq;

	/* check if IRQ already used */
	for (irq = used_irqs; irq; irq = irq->next)
		if (irq->irq == action->irq) break;

	/* we have to setup IRQ handling */
	if (!irq) {
		/* allocate and initalize new descriptor */
		irq = ddekit_simple_malloc(sizeof(*irq));
		if (!irq) return -ENOMEM;
		memset(irq, 0, sizeof(*irq));

		irq->irq    = action->irq;
		irq->shared = shared;
		irq->next   = used_irqs;
		used_irqs   = irq;

		/* attach to interrupt */
		irq->thread = ddekit_interrupt_attach(irq->irq,
		                                      irq->shared,
		                                      irq_thread_init,
		                                      irq_handler,
		                                      (void *)irq);
		if (!irq->thread) {
			used_irqs = irq->next;
			ddekit_simple_free(irq);
			return -EBUSY;
		}
	}

	/* does desciptor allow our new handler? */
	if ((!irq->shared || !shared) && irq->action) return -EBUSY;

	/* add handler */
	irq->count++;
	action->next = irq->action;
	irq->action = action;

	return irq->count;
}


/**
 * Free previously claimed IRQ
 *
 * \return usage counter or negative error code
 */
static struct irqaction *release_irq(unsigned irq_num, void *dev_id)
{
	struct dde_irq *prev_irq, *irq;

	/* check if IRQ already used */
	for (prev_irq = 0, irq = used_irqs; irq;
	     prev_irq = irq, irq = irq->next)
		if (irq->irq == irq_num) break;

	if (!irq) return 0;

	struct irqaction *prev_action, *action;

	for (prev_action = 0, action = irq->action; action;
	     prev_action = action, action = action->next)
		if (action->dev_id == dev_id) break;

	if (!action) return 0;

	/* dequeue action from irq */
	if (prev_action)
		prev_action->next = action->next;
	else
		irq->action = action->next;

	/* dequeue irq from used_irqs list and free structure,
	   if no more actions available */
	if (!irq->action) {
		if (prev_irq)
			prev_irq->next = irq->next;
		else
			used_irqs = irq->next;

		/* detach from interrupt */
		ddekit_interrupt_detach(irq->irq);

		ddekit_simple_free(irq);
	}

  return action;
}


/***************
 ** Linux API **
 ***************/

/**
 * Request interrupt
 *
 * \param  irq       interrupt number
 * \param  handler   interrupt handler -> top half
 * \param  flags     interrupt handling flags (SA_SHIRQ, ...)
 * \param  dev_name  device name
 * \param  dev_id    cookie passed back to handler
 *
 * \return 0 on success; error code otherwise
 *
 * \todo FIXME consider locking!
 */
int request_irq(unsigned int irq, irq_handler_t handler,
                unsigned long flags, const char *dev_name, void *dev_id)
{
  if (!handler) return -EINVAL;

  /* facilitate Linux irqaction for this handler */
  struct irqaction *irq_action = ddekit_simple_malloc(sizeof(*irq_action));
  if (!irq_action) return -ENOMEM;
  memset(irq_action, 0, sizeof(*irq_action));

  irq_action->handler = handler;
  irq_action->flags   = flags;
  irq_action->name    = dev_name;
  irq_action->dev_id  = dev_id;
  irq_action->irq     = irq;

  /* attach to IRQ */
  int err = claim_irq(irq_action);
  if (err < 0) return err;

  return 0;
}

/** Release Interrupt
 * \ingroup mod_irq
 *
 * \param  irq     interrupt number
 * \param  dev_id  cookie passed back to handler
 *
 */
void free_irq(unsigned int irq, void *dev_id)
{
	struct irqaction *irq_action = release_irq(irq, dev_id);

	if (irq_action)
		ddekit_simple_free(irq_action);
}

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

void disable_irq_nosync(unsigned int irq)
{
	/*
	 * Note:
	 * In contrast to the _nosync semantics, DDEKit's
	 * disable definitely waits until a currently executed
	 * IRQ handler terminates.
	 */
	ddekit_interrupt_disable(irq);
}

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