Contiki 3.x
shared-isr.c
1 /*
2  * Copyright (C) 2016, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <assert.h>
32 #include "idt.h"
33 #include "interrupt.h"
34 #include "pic.h"
35 #include "shared-isr.h"
36 
37 /* Defined in linker script */
38 extern shared_isr_client_t _sdata_shared_isr, _edata_shared_isr;
39 
40 static void __attribute__((used))
41 shared_handler(void)
42 {
43  shared_isr_client_t *client;
44  for(client = &_sdata_shared_isr; client < &_edata_shared_isr; client++) {
45  if(client->handler()) {
46  pic_eoi(client->irq);
47  return;
48  }
49  }
50 }
51 
52 /**
53  * \brief Initialize shared ISR by iterating through all of its clients and
54  * configuring their interrupts to route to the shared ISR.
55  */
56 void
57 shared_isr_init(void)
58 {
59  shared_isr_client_t *client = &_sdata_shared_isr;
60  shared_isr_client_t *consistency_check_client;
61  bool prev_conf;
62 
63  void shared_isr_stub(void);
64  __asm__ __volatile__ (
65  ISR_STUB("shared_isr_stub", 0, "shared_handler", 0)
66  :
67  );
68 
69  while(client < &_edata_shared_isr) {
70  consistency_check_client = &_sdata_shared_isr;
71 
72  prev_conf = false;
73 
74  while(consistency_check_client < client) {
75  if((client->irq == consistency_check_client->irq) ||
76  (client->pin == consistency_check_client->pin) ||
77  (client->pirq == consistency_check_client->pirq)) {
78 
79  prev_conf = true;
80 
81  /* This interrupt was previously configured. */
82  break;
83  }
84 
85  consistency_check_client++;
86  }
87 
88  if(prev_conf) {
89  /* The requested configurations for each IRQ must be consistent. */
90  assert((client->irq == consistency_check_client->irq) &&
91  (client->agent == consistency_check_client->agent) &&
92  (client->pin == consistency_check_client->pin) &&
93  (client->pirq == consistency_check_client->pirq));
94  } else {
95  idt_set_intr_gate_desc(PIC_INT(client->irq), (uint32_t)shared_isr_stub,
96  GDT_SEL_CODE_INT, PRIV_LVL_INT);
97 
98  pci_irq_agent_set_pirq(client->agent, client->pin, client->pirq);
99 
100  pci_pirq_set_irq(client->pirq, client->irq, 1);
101 
102  pic_unmask_irq(client->irq);
103  }
104 
105  client++;
106  }
107 }
#define __attribute__(nothing)
Define attribute to nothing since it isn't handled by IAR.
Definition: iar.h:194