File: intr.c

package info (click to toggle)
rtai 3.1.0-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 23,560 kB
  • ctags: 19,698
  • sloc: ansic: 88,861; cpp: 31,340; tcl: 14,684; sh: 10,652; xml: 760; yacc: 575; lex: 537; makefile: 394; asm: 310; php: 300; perl: 108
file content (447 lines) | stat: -rw-r--r-- 13,806 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*!\file intr.c
 * \brief Interrupt management.
 * \author Philippe Gerum
 *
 * Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
 *
 * Xenomai is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Xenomai 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 GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Xenomai; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * As a special exception, the RTAI project gives permission
 * for additional uses of the text contained in its release of
 * Xenomai.
 *
 * The exception is that, if you link the Xenomai libraries with other
 * files to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the Xenomai libraries code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public
 * License.
 *
 * This exception applies only to the code released by the
 * RTAI project under the name Xenomai.  If you copy code from other
 * RTAI project releases into a copy of Xenomai, as the General Public
 * License permits, the exception does not apply to the code that you
 * add in this way.  To avoid misleading anyone as to the status of
 * such modified files, you must delete this exception notice from
 * them.
 *
 * If you write modifications of your own for Xenomai, it is your
 * choice whether to permit this exception to apply to your
 * modifications. If you do not wish that, delete this exception
 * notice.
 *
 * \ingroup intr
 */

/*!
 * \ingroup xenomai
 * \defgroup intr Interrupt management.
 *
 * Interrupt management.
 *
 *@{*/

#define XENO_INTR_MODULE

#include "rtai_config.h"
#include <xenomai/pod.h>
#include <xenomai/mutex.h>
#include <xenomai/intr.h>

static void xnintr_irq_handler(unsigned irq, void *cookie);

static void xnintr_svc_thread(void *cookie);

/*! 
 * \fn int xnintr_init (xnintr_t *intr,
                        unsigned irq,
                        int priority,
                        xnisr_t isr,
                        xnist_t ist,
  			   xnflags_t flags);
 * \brief Initialize an interrupt object.
 *
 * The nanokernel defines a threaded interrupt model in order to:
 *
 * - provide a mean for prioritizing interrupt handling by software.
 *
 * - allow the interrupt code to synchronize with other system code
 * using mutexes, therefore reducing the need for hard interrupt
 * masking in critical sections.
 *
 * Xenomai's nanokernel exhibits a split interrupt handling scheme
 * separated into two parts. The first part is known as the Interrupt
 * Service Routine (ISR), the second is the Interrupt Service Tasklet
 * (IST).
 *
 * When an interrupt occurs, the ISR is fired in order to deal with
 * the hardware event as fast as possible, without any interaction
 * with the nanokernel. If the interrupt service code needs to reenter
 * the nanokernel, the ISR may require an associated interrupt service
 * tasklet to be scheduled immediately upon return. The IST has a
 * lightweight thread context that allows it to invoke all nanokernel
 * services safely. A Xenomai interrupt object may be associated with
 * an ISR and/or an IST to process each IRQ event.
 *
 ********************************************************************
 * [WARNING] Interrupt service threads/tasklets are deprecated in
 * newer versions of the Xenomai nucleus, basically due to design and
 * performance issues. Do not use the IST facility if you plan to port
 * to RTAI/fusion.
 ********************************************************************
 *
 * Upon receipt of an IRQ, the ISR/IST invocation policy is as
 * follows:
 *
 * - if an ISR has been defined, it is immediately called on behalf of
 * the interrupt stack context.
 *
 * - if an IST has been defined, then its is scheduled upon return of
 * the ISR if the XN_ISR_SCHED_T bit set in its return code or if no
 * ISR has been defined for this interrupt object. The tasklet will
 * run after all ISRs have completed and all more prioritary IST have
 * returned. In any cases, the IST will run before any regular
 * real-time threads (i.e. all but interrupt service threads).
 *
 * If an ISR has been defined, the following bits are checked from its
 * return value:
 *
 * - XN_ISR_ENABLE asks the nanokernel to re-enable the IRQ line. Over
 * some real-time control layers which mask and acknowledge IRQs, this
 * operation is necessary to revalidate the interrupt channel so that
 * more interrupts can be notified. The presence of such bit in the
 * ISR's return code causes Xenomai to ask the real-time control layer
 * to re-enable the interrupt.
 *
 * - XN_ISR_CHAINED tells the nanokernel to require the real-time
 * control layer to forward the IRQ. For instance, this would cause the
 * Adeos control layer to propagate the interrupt down the interrupt
 * pipeline to other Adeos domains, such as Linux. This is the regular
 * way to share interrupts between Xenomai and the host system.

 * - XN_ISR_SCHED_T tells the nanokernel to schedule the interrupt
 * service tasklet (IST) which will be in charge of completing the
 * interrupt processing on behalf of a lightweight thread context.
 *
 * @param intr The address of a interrupt object descriptor Xenomai
 * will use to store the object-specific data.  This descriptor must
 * always be valid while the object is active therefore it must be
 * allocated in permanent memory.
 *
 * @param irq The hardware interrupt channel associated with the
 * interrupt object. This value is architecture-dependent. An
 * interrupt object must then be attached to the hardware interrupt
 * vector using the xnintr_attach() service for the associated IRQs
 * to be directed to this object.
 *
 * @param priority The priority level of the interrupt. If a valid
 * interrupt service thread is passed in ist, this value will be used
 * to compute the base priority of the service thread. This value must
 * range from [0 .. XNINTR_MAX_PRIORITY] (inclusive). The higher the
 * value, the higher the priority, whatever the current pod priority
 * scheme is.
 *
 * @param isr The address of a valid low-level interrupt service
 * routine if this parameter is non-zero. This handler will be called
 * each time the corresponding IRQ is delivered on behalf of an
 * interrupt context.  When called, the ISR is passed the descriptor
 * address of the interrupt object.
 *
 * @param ist If non-zero, this parameter should contain the address
 * of a valid hi-level interrupt service tasklet. The tasklet will be
 * called on behalf of an interrupt service thread each time the
 * associated ISR sets the XN_ISR_SCHED_T bit in its return code.  The
 * underlying lightweight thread is immediately started with a stack
 * of XNARCH_THREAD_STACKSZ bytes.
 *
 * @param flags A set of creation flags affecting the operation. Since
 * no flags are currently defined, zero should be passed for this
 * parameter.
 *
 * @return XN_OK is returned upon success. Otherwise XNERR_PARAM is
 * returned if the interrupt priority level is out of range.
 *
 * Side-effect: This routine calls the rescheduling procedure as a
 * result of starting the interrupt service thread (if any).
 *
 * Context: This routine must be called on behalf of a thread context.
 */

int xnintr_init (xnintr_t *intr,
		 unsigned irq,
		 int priority,
		 xnisr_t isr,
		 xnist_t ist,
		 xnflags_t flags)
{
    char name[16];

    if (priority < 0 || priority > XNINTR_MAX_PRIORITY)
	return XNERR_PARAM;

    intr->irq = irq;
    intr->isr = isr;
    intr->ist = ist;
    intr->pending = 0;
    intr->cookie = NULL;
    intr->status = 0;
    intr->priority = XNPOD_ISVC_PRIO_BASE(priority);

    if (ist)
	{
	sprintf(name,"isvc%u",irq);

	xnpod_init_thread(&intr->svcthread,
			  name,
			  intr->priority,
			  XNISVC,
			  XNARCH_THREAD_STACKSZ,
			  NULL,
			  0);

	xnpod_start_thread(&intr->svcthread,
			   0,
			   0,
			   &xnintr_svc_thread,
			   intr);
	}

    return XN_OK;
}

/*! 
 * \fn int xnintr_destroy (xnintr_t *intr);
 * \brief Destroy an interrupt object.
 *
 * Destroys an interrupt object previously initialized by
 * xnintr_init(). The interrupt object is automatically detached by a
 * call to xnintr_detach(). No more IRQs will be dispatched by this
 * object after this service has returned.
 *
 * @param intr The descriptor address of the interrupt object to
 * destroy.
 *
 * @return XN_OK is returned on success. Otherwise, XNERR_BUSY is
 * returned if an error occurred while detaching the interrupt (see
 * xnintr_detach()).
 *
 * Side-effect: This routine calls the rescheduling procedure as a
 * result of an interrupt service thread deleting its own interrupt
 * object (this should rarely happen though).
 *
 * Context: This routine must be called on behalf of a thread or IST
 * context.
 */

int xnintr_destroy (xnintr_t *intr)

{
    int s = xnintr_detach(intr);

    if (s == XN_OK && intr->ist != NULL)
	xnpod_delete_thread(&intr->svcthread,NULL);

    return s;
}

/*! 
 * \fn int xnintr_attach (xnintr_t *intr, void *cookie);
 * \brief Attach an interrupt object.
 *
 * Attach an interrupt object previously initialized by
 * xnintr_init(). After this operation is completed, all IRQs received
 * from the corresponding interrupt channel are directed to the
 * object's ISR/IST handlers.
 *
 * @param intr The descriptor address of the interrupt object to
 * attach.
 *
 * @param cookie A user-defined opaque value which is stored into the
 * interrupt object descriptor for further retrieval by the ISR/ISR
 * handlers.
 *
 * @return XN_OK is returned on success. Otherwise, XNERR_NOSYS is
 * returned if a low-level error occurred while attaching the
 * interrupt. XNERR_BUSY is specifically returned if the interrupt
 * object was already attached.
 *
 * Side-effect: This routine does not call the rescheduling procedure.
 *
 * Context: This routine must be called on behalf of a thread or IST
 * context.
 */

int xnintr_attach (xnintr_t *intr,
		   void *cookie)
{
    spl_t s;
    int err;

    splhigh(s);

    intr->cookie = cookie;

    switch (xnarch_hook_irq(intr->irq,&xnintr_irq_handler,intr))
	{
	case -EINVAL:

	    err = XNERR_NOSYS;
	    break;
	    
	case -EBUSY:

	    err = XNERR_BUSY;
	    break;

	default:

	    err = XN_OK;
	    break;
	}

    setbits(intr->status,XNINTR_ATTACHED);

    splexit(s);

    return err;
}

/*! 
 * \fn int xnintr_detach (xnintr_t *intr);
 * \brief Detach an interrupt object.
 *
 * Detach an interrupt object previously attached by
 * xnintr_attach(). After this operation is completed, no more IRQs
 * are directed to the object's ISR/IST handlers, but the interrupt
 * object itself remains valid. A detached interrupt object can be
 * attached again by a subsequent call to xnintr_attach().
 *
 * @param intr The descriptor address of the interrupt object to
 * detach.
 *
 * @return XN_OK is returned on success. Otherwise, XNERR_BUSY is
 * returned if a low-level error occurred while detaching the
 * interrupt. Detaching a non-attached interrupt object leads to a
 * null-effect and returns XN_OK.
 *
 * Side-effect: This routine does not call the rescheduling procedure.
 *
 * Context: This routine must be called on behalf of a thread or IST
 * context.
 */

int xnintr_detach (xnintr_t *intr)

{
    int err = XN_OK;
    spl_t s;

    splhigh(s);

    if (testbits(intr->status,XNINTR_ATTACHED))
	{
	if (xnarch_release_irq(intr->irq) == -EINVAL)
	    err = XNERR_BUSY;
	else
	    clrbits(intr->status,XNINTR_ATTACHED);
	}

    splexit(s);

    return err;
}

static void xnintr_svc_thread (void *cookie)

{
    xnsched_t *sched = xnpod_current_sched();
    xnintr_t *intr = (xnintr_t *)cookie;
    int hits;
    spl_t s;

    splhigh(s);

    for (;;)
	{
	xnpod_renice_isvc(&intr->svcthread,XNPOD_ISVC_PRIO_IDLE);
	sched->inesting++;

	while (intr->pending > 0)
	    {
	    hits = intr->pending;
	    intr->pending = 0;
	    splexit(s);
	    intr->ist(intr,hits);
	    splhigh(s);
	    }

	sched->inesting--;
	}
}

static void xnintr_irq_handler (unsigned irq, void *cookie)

{
    xnsched_t *sched = xnpod_current_sched();
    xnintr_t *intr = (xnintr_t *)cookie;
    int s = XN_ISR_SCHED_T;

    xnarch_memory_barrier();

    intr->pending++;

    /* If a raw interrupt handler has been given, fire it. */

    if (intr->isr != NULL)
	{
	sched->inesting++;
	s = intr->isr(intr);
	sched->inesting--;

	if (s & XN_ISR_ENABLE)
	    xnarch_isr_enable_irq(irq);

	if (s & XN_ISR_CHAINED)
	    xnarch_isr_chain_irq(irq);
	}

    /* If an interrupt service task has been given AND if the raw
       interrupt handler asked for the interrupt service task to be
       scheduled (or if no raw interrupt handler exists), resume the
       interrupt service thread immediately. */

    if (intr->ist != NULL && (s & XN_ISR_SCHED_T) != 0)
	{
	if (xnpod_priocompare(intr->svcthread.cprio,intr->priority) < 0)
	    xnpod_renice_isvc(&intr->svcthread,intr->priority);
	}
    else
	{
	intr->pending = 0;

	if (testbits(nkpod->status,XNSCHED))
	    xnpod_schedule_runnable(xnpod_current_thread(),XNPOD_SCHEDLIFO);
	}
}

/*@{*/

EXPORT_SYMBOL(xnintr_attach);
EXPORT_SYMBOL(xnintr_destroy);
EXPORT_SYMBOL(xnintr_detach);
EXPORT_SYMBOL(xnintr_init);