summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/shared/src/irq-generic.c
blob: c62a75602e606b9f0df37712f0c0883782477e87 (plain) (blame)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/**
 * @file
 *
 * @ingroup bsp_interrupt
 *
 * @brief Generic BSP interrupt support implementation.
 */

/*
 * Based on concepts of Pavel Pisa, Till Straumann and Eric Valette.
 *
 * Copyright (c) 2008-2014 embedded brains GmbH.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <bsp/irq-generic.h>
#include <bsp/fatal.h>

#include <stdlib.h>

#include <rtems/score/apimutex.h>
#include <rtems/score/sysstate.h>

#ifdef BSP_INTERRUPT_USE_INDEX_TABLE
  bsp_interrupt_handler_index_type bsp_interrupt_handler_index_table
    [BSP_INTERRUPT_VECTOR_NUMBER];
#endif

bsp_interrupt_handler_entry bsp_interrupt_handler_table
  [BSP_INTERRUPT_HANDLER_TABLE_SIZE];

/* The last entry indicates if everything is initialized */
static uint8_t bsp_interrupt_handler_unique_table
  [(BSP_INTERRUPT_HANDLER_TABLE_SIZE + 7 + 1) / 8];

static void bsp_interrupt_handler_empty(void *arg)
{
  rtems_vector_number vector = (rtems_vector_number) arg;

  bsp_interrupt_handler_default(vector);
}

#ifdef RTEMS_SMP
  static void bsp_interrupt_handler_do_nothing(void *arg)
  {
    (void) arg;
  }
#endif

static inline bool bsp_interrupt_is_handler_unique(rtems_vector_number index)
{
  rtems_vector_number i = index / 8;
  rtems_vector_number s = index % 8;
  return (bsp_interrupt_handler_unique_table [i] >> s) & 0x1;
}

static inline void bsp_interrupt_set_handler_unique(
  rtems_vector_number index,
  bool unique
)
{
  rtems_vector_number i = index / 8;
  rtems_vector_number s = index % 8;
  if (unique) {
    bsp_interrupt_handler_unique_table [i] |= (uint8_t) (0x1U << s);
  } else {
    bsp_interrupt_handler_unique_table [i] &= (uint8_t) ~(0x1U << s);
  }
}

static inline bool bsp_interrupt_is_initialized(void)
{
  return bsp_interrupt_is_handler_unique(BSP_INTERRUPT_HANDLER_TABLE_SIZE);
}

static inline void bsp_interrupt_set_initialized(void)
{
  bsp_interrupt_set_handler_unique(BSP_INTERRUPT_HANDLER_TABLE_SIZE, true);
}

static inline bool bsp_interrupt_is_empty_handler_entry(
  const bsp_interrupt_handler_entry *e
)
{
  return e->handler == bsp_interrupt_handler_empty;
}

static inline void bsp_interrupt_clear_handler_entry(
  bsp_interrupt_handler_entry *e,
  rtems_vector_number vector
)
{
  e->handler = bsp_interrupt_handler_empty;
  bsp_interrupt_fence(ATOMIC_ORDER_RELEASE);
  e->arg = (void *) vector;
  e->info = NULL;
  e->next = NULL;
}

static inline bool bsp_interrupt_allocate_handler_index(
  rtems_vector_number vector,
  rtems_vector_number *index
)
{
  #ifdef BSP_INTERRUPT_USE_INDEX_TABLE
    rtems_vector_number i = 0;

    /* The first entry will remain empty */
    for (i = 1; i < BSP_INTERRUPT_HANDLER_TABLE_SIZE; ++i) {
      const bsp_interrupt_handler_entry *e = &bsp_interrupt_handler_table [i];
      if (bsp_interrupt_is_empty_handler_entry(e)) {
        *index = i;
        return true;
      }
    }

    return false;
  #else
    *index = vector;
    return true;
  #endif
}

static bsp_interrupt_handler_entry *bsp_interrupt_allocate_handler_entry(void)
{
  #ifdef BSP_INTERRUPT_NO_HEAP_USAGE
    rtems_vector_number index = 0;
    if (bsp_interrupt_allocate_handler_index(0, &index)) {
      return &bsp_interrupt_handler_table [index];
    } else {
      return NULL;
    }
  #else
    return malloc(sizeof(bsp_interrupt_handler_entry));
  #endif
}

static void bsp_interrupt_free_handler_entry(bsp_interrupt_handler_entry *e)
{
  #ifdef BSP_INTERRUPT_NO_HEAP_USAGE
    bsp_interrupt_clear_handler_entry(e, 0);
  #else
    free(e);
  #endif
}

void bsp_interrupt_lock(void)
{
  if (_System_state_Is_up(_System_state_Get())) {
    _RTEMS_Lock_allocator();
  }
}

void bsp_interrupt_unlock(void)
{
  if (_System_state_Is_up(_System_state_Get())) {
    _RTEMS_Unlock_allocator();
  }
}

void bsp_interrupt_initialize(void)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  size_t i = 0;

  /* Initialize handler table */
  for (i = 0; i < BSP_INTERRUPT_HANDLER_TABLE_SIZE; ++i) {
    bsp_interrupt_handler_table [i].handler = bsp_interrupt_handler_empty;
    bsp_interrupt_handler_table [i].arg = (void *) i;
  }

  sc = bsp_interrupt_facility_initialize();
  if (sc != RTEMS_SUCCESSFUL) {
    bsp_fatal(BSP_FATAL_INTERRUPT_INITIALIZATION);
  }

  bsp_interrupt_set_initialized();
}

/**
 * @brief Installs an interrupt handler.
 *
 * @ingroup bsp_interrupt
 *
 * @return In addition to the standard status codes this function returns:
 * - If the BSP interrupt support is not initialized RTEMS_INTERNAL_ERROR will
 * be returned.
 * - If not enough memory for a new handler is available RTEMS_NO_MEMORY will
 * be returned
 *
 * @see rtems_interrupt_handler_install()
 */
static rtems_status_code bsp_interrupt_handler_install(
  rtems_vector_number vector,
  const char *info,
  rtems_option options,
  rtems_interrupt_handler handler,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_interrupt_level level;
  rtems_vector_number index = 0;
  bsp_interrupt_handler_entry *head = NULL;
  bool enable_vector = false;
  bool replace = RTEMS_INTERRUPT_IS_REPLACE(options);

  /* Check parameters and system state */
  if (!bsp_interrupt_is_initialized()) {
    return RTEMS_INTERNAL_ERROR;
  } else if (!bsp_interrupt_is_valid_vector(vector)) {
    return RTEMS_INVALID_ID;
  } else if (handler == NULL) {
    return RTEMS_INVALID_ADDRESS;
  } else if (rtems_interrupt_is_in_progress()) {
    return RTEMS_CALLED_FROM_ISR;
  }

  /* Lock */
  bsp_interrupt_lock();

  /* Get handler table index */
  index = bsp_interrupt_handler_index(vector);

  /* Get head entry of the handler list for current vector */
  head = &bsp_interrupt_handler_table [index];

  if (bsp_interrupt_is_empty_handler_entry(head)) {
    if (replace) {
      /* No handler to replace exists */
      bsp_interrupt_unlock();
      return RTEMS_UNSATISFIED;
    }

    /*
     * No real handler installed yet.  So allocate a new index in
     * the handler table and fill the entry with life.
     */
    if (bsp_interrupt_allocate_handler_index(vector, &index)) {
      bsp_interrupt_disable(level);
      bsp_interrupt_handler_table [index].arg = arg;
      bsp_interrupt_fence(ATOMIC_ORDER_RELEASE);
      bsp_interrupt_handler_table [index].handler = handler;
      #ifdef BSP_INTERRUPT_USE_INDEX_TABLE
        bsp_interrupt_handler_index_table [vector] = index;
      #endif
      bsp_interrupt_enable(level);
      bsp_interrupt_handler_table [index].info = info;
    } else {
      /* Handler table is full */
      bsp_interrupt_unlock();
      return RTEMS_NO_MEMORY;
    }

    /* This is the first handler so enable the vector later */
    enable_vector = true;
  } else {
    bsp_interrupt_handler_entry *current = head;
    bsp_interrupt_handler_entry *tail = NULL;
    bsp_interrupt_handler_entry *match = NULL;

    /* Ensure that a unique handler remains unique */
    if (
      !replace
        && (RTEMS_INTERRUPT_IS_UNIQUE(options)
          || bsp_interrupt_is_handler_unique(index))
    ) {
      /*
       * Tried to install a unique handler on a not empty
       * list or there is already a unique handler installed.
       */
      bsp_interrupt_unlock();
      return RTEMS_RESOURCE_IN_USE;
    }

    /*
     * Search for the list tail and check if the handler is already
     * installed.
     */
    do {
      if (
        match == NULL
          && (current->handler == handler || replace)
          && current->arg == arg
      ) {
        match = current;
      }
      tail = current;
      current = current->next;
    } while (current != NULL);

    if (replace) {
      /* Ensure that a handler to replace exists */
      if (match == NULL) {
        bsp_interrupt_unlock();
        return RTEMS_UNSATISFIED;
      }

      /* Use existing entry */
      current = match;
    } else {
      /* Ensure the handler is not already installed */
      if (match != NULL) {
        /* The handler is already installed */
        bsp_interrupt_unlock();
        return RTEMS_TOO_MANY;
      }

      /* Allocate a new entry */
      current = bsp_interrupt_allocate_handler_entry();
      if (current == NULL) {
        /* Not enough memory */
        bsp_interrupt_unlock();
        return RTEMS_NO_MEMORY;
      }
    }

    /* Update existing entry or set new entry */
    current->handler = handler;
    current->info = info;

    if (!replace) {
      /* Set new entry */
      current->arg = arg;
      current->next = NULL;

      /* Link to list tail */
      bsp_interrupt_disable(level);
      bsp_interrupt_fence(ATOMIC_ORDER_RELEASE);
      tail->next = current;
      bsp_interrupt_enable(level);
    }
  }

  /* Make the handler unique if necessary */
  bsp_interrupt_set_handler_unique(index, RTEMS_INTERRUPT_IS_UNIQUE(options));

  /* Enable the vector if necessary */
  if (enable_vector) {
    sc = bsp_interrupt_vector_enable(vector);
    if (sc != RTEMS_SUCCESSFUL) {
      bsp_interrupt_unlock();
      return sc;
    }
  }

  /* Unlock */
  bsp_interrupt_unlock();

  return RTEMS_SUCCESSFUL;
}

/**
 * @brief Removes an interrupt handler.
 *
 * @ingroup bsp_interrupt
 *
 * @return In addition to the standard status codes this function returns
 * RTEMS_INTERNAL_ERROR if the BSP interrupt support is not initialized.
 *
 * @see rtems_interrupt_handler_remove().
 */
static rtems_status_code bsp_interrupt_handler_remove(
  rtems_vector_number vector,
  rtems_interrupt_handler handler,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_interrupt_level level;
  rtems_vector_number index = 0;
  bsp_interrupt_handler_entry *head = NULL;
  bsp_interrupt_handler_entry *current = NULL;
  bsp_interrupt_handler_entry *previous = NULL;
  bsp_interrupt_handler_entry *match = NULL;

  /* Check parameters and system state */
  if (!bsp_interrupt_is_initialized()) {
    return RTEMS_INTERNAL_ERROR;
  } else if (!bsp_interrupt_is_valid_vector(vector)) {
    return RTEMS_INVALID_ID;
  } else if (handler == NULL) {
    return RTEMS_INVALID_ADDRESS;
  } else if (rtems_interrupt_is_in_progress()) {
    return RTEMS_CALLED_FROM_ISR;
  }

  /* Lock */
  bsp_interrupt_lock();

  /* Get handler table index */
  index = bsp_interrupt_handler_index(vector);

  /* Get head entry of the handler list for current vector */
  head = &bsp_interrupt_handler_table [index];

  /* Search for a matching entry */
  current = head;
  do {
    if (current->handler == handler && current->arg == arg) {
      match = current;
      break;
    }
    previous = current;
    current = current->next;
  } while (current != NULL);

  /* Remove the matching entry */
  if (match != NULL) {
    if (match->next != NULL) {
      /*
       * The match has a successor.  A successor is always
       * allocated.  So replace the match with its successor
       * and free the successor entry.
       */
      current = match->next;

      bsp_interrupt_disable(level);
      #ifdef RTEMS_SMP
        match->handler = bsp_interrupt_handler_do_nothing;
        bsp_interrupt_fence(ATOMIC_ORDER_RELEASE);
      #endif
      match->arg = current->arg;
      bsp_interrupt_fence(ATOMIC_ORDER_RELEASE);
      match->handler = current->handler;
      match->info = current->info;
      match->next = current->next;
      bsp_interrupt_enable(level);

      bsp_interrupt_free_handler_entry(current);
    } else if (match == head) {
      /*
       * The match is the list head and has no successor.
       * The list head is stored in a static table so clear
       * this entry.  Since now the list is empty disable the
       * vector.
       */

      /* Disable the vector */
      sc = bsp_interrupt_vector_disable(vector);

      /* Clear entry */
      bsp_interrupt_disable(level);
      bsp_interrupt_clear_handler_entry(head, vector);
      #ifdef BSP_INTERRUPT_USE_INDEX_TABLE
        bsp_interrupt_handler_index_table [vector] = 0;
      #endif
      bsp_interrupt_enable(level);

      /* Allow shared handlers */
      bsp_interrupt_set_handler_unique(index, false);

      /* Check status code */
      if (sc != RTEMS_SUCCESSFUL) {
        bsp_interrupt_unlock();
        return sc;
      }
    } else {
      /*
       * The match is the list tail and has a predecessor.
       * So terminate the predecessor and free the match.
       */
      bsp_interrupt_disable(level);
      previous->next = NULL;
      bsp_interrupt_fence(ATOMIC_ORDER_RELEASE);
      bsp_interrupt_enable(level);

      bsp_interrupt_free_handler_entry(match);
    }
  } else {
    /* No matching entry found */
    bsp_interrupt_unlock();
    return RTEMS_UNSATISFIED;
  }

  /* Unlock */
  bsp_interrupt_unlock();

  return RTEMS_SUCCESSFUL;
}

/**
 * @brief Iterates over all installed interrupt handler of a vector.
 *
 * @ingroup bsp_interrupt
 *
 * @return In addition to the standard status codes this function returns
 * RTEMS_INTERNAL_ERROR if the BSP interrupt support is not initialized.
 *
 * @see rtems_interrupt_handler_iterate().
 */
static rtems_status_code bsp_interrupt_handler_iterate(
  rtems_vector_number vector,
  rtems_interrupt_per_handler_routine routine,
  void *arg
)
{
  bsp_interrupt_handler_entry *current = NULL;
  rtems_option options = 0;
  rtems_vector_number index = 0;

  /* Check parameters and system state */
  if (!bsp_interrupt_is_initialized()) {
    return RTEMS_INTERNAL_ERROR;
  } else if (!bsp_interrupt_is_valid_vector(vector)) {
    return RTEMS_INVALID_ID;
  } else if (rtems_interrupt_is_in_progress()) {
    return RTEMS_CALLED_FROM_ISR;
  }

  /* Lock */
  bsp_interrupt_lock();

  /* Interate */
  index = bsp_interrupt_handler_index(vector);
  current = &bsp_interrupt_handler_table [index];
  if (!bsp_interrupt_is_empty_handler_entry(current)) {
    do {
      options = bsp_interrupt_is_handler_unique(index) ?
        RTEMS_INTERRUPT_UNIQUE : RTEMS_INTERRUPT_SHARED;
      routine(arg, current->info, options, current->handler, current->arg);
      current = current->next;
    } while (current != NULL);
  }

  /* Unlock */
  bsp_interrupt_unlock();

  return RTEMS_SUCCESSFUL;
}

rtems_status_code rtems_interrupt_handler_install(
  rtems_vector_number vector,
  const char *info,
  rtems_option options,
  rtems_interrupt_handler handler,
  void *arg
)
{
  return bsp_interrupt_handler_install(vector, info, options, handler, arg);
}

rtems_status_code rtems_interrupt_handler_remove(
  rtems_vector_number vector,
  rtems_interrupt_handler handler,
  void *arg
)
{
  return bsp_interrupt_handler_remove(vector, handler, arg);
}

rtems_status_code rtems_interrupt_handler_iterate(
  rtems_vector_number vector,
  rtems_interrupt_per_handler_routine routine,
  void *arg
)
{
  return bsp_interrupt_handler_iterate(vector, routine, arg);
}

bool bsp_interrupt_handler_is_empty(rtems_vector_number vector)
{
  rtems_vector_number index = 0;
  bsp_interrupt_handler_entry *head = NULL;
  bool empty;

  /* For use in interrupts so no lock. */

  /* Get handler table index */
  index = bsp_interrupt_handler_index(vector);

  /* Get head entry of the handler list for the vector */
  head = &bsp_interrupt_handler_table [index];

  empty = bsp_interrupt_is_empty_handler_entry(head);

  return empty;
}