root / hw / syborg_interrupt.c @ e6e055c9
History | View | Annotate | Download (6.5 kB)
1 | 4af39611 | Paul Brook | /*
|
---|---|---|---|
2 | 4af39611 | Paul Brook | * Syborg interrupt controller.
|
3 | 4af39611 | Paul Brook | *
|
4 | 4af39611 | Paul Brook | * Copyright (c) 2008 CodeSourcery
|
5 | 4af39611 | Paul Brook | *
|
6 | 4af39611 | Paul Brook | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 4af39611 | Paul Brook | * of this software and associated documentation files (the "Software"), to deal
|
8 | 4af39611 | Paul Brook | * in the Software without restriction, including without limitation the rights
|
9 | 4af39611 | Paul Brook | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 4af39611 | Paul Brook | * copies of the Software, and to permit persons to whom the Software is
|
11 | 4af39611 | Paul Brook | * furnished to do so, subject to the following conditions:
|
12 | 4af39611 | Paul Brook | *
|
13 | 4af39611 | Paul Brook | * The above copyright notice and this permission notice shall be included in
|
14 | 4af39611 | Paul Brook | * all copies or substantial portions of the Software.
|
15 | 4af39611 | Paul Brook | *
|
16 | 4af39611 | Paul Brook | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 4af39611 | Paul Brook | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 4af39611 | Paul Brook | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 4af39611 | Paul Brook | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 4af39611 | Paul Brook | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 4af39611 | Paul Brook | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 4af39611 | Paul Brook | * THE SOFTWARE.
|
23 | 4af39611 | Paul Brook | */
|
24 | 4af39611 | Paul Brook | |
25 | 4af39611 | Paul Brook | #include "sysbus.h" |
26 | 4af39611 | Paul Brook | #include "syborg.h" |
27 | 4af39611 | Paul Brook | |
28 | 4af39611 | Paul Brook | //#define DEBUG_SYBORG_INT
|
29 | 4af39611 | Paul Brook | |
30 | 4af39611 | Paul Brook | #ifdef DEBUG_SYBORG_INT
|
31 | 4af39611 | Paul Brook | #define DPRINTF(fmt, ...) \
|
32 | 4af39611 | Paul Brook | do { printf("syborg_int: " fmt , ## __VA_ARGS__); } while (0) |
33 | 4af39611 | Paul Brook | #define BADF(fmt, ...) \
|
34 | 4af39611 | Paul Brook | do { fprintf(stderr, "syborg_int: error: " fmt , ## __VA_ARGS__); \ |
35 | 4af39611 | Paul Brook | exit(1);} while (0) |
36 | 4af39611 | Paul Brook | #else
|
37 | 4af39611 | Paul Brook | #define DPRINTF(fmt, ...) do {} while(0) |
38 | 4af39611 | Paul Brook | #define BADF(fmt, ...) \
|
39 | 4af39611 | Paul Brook | do { fprintf(stderr, "syborg_int: error: " fmt , ## __VA_ARGS__);} while (0) |
40 | 4af39611 | Paul Brook | #endif
|
41 | 4af39611 | Paul Brook | enum {
|
42 | 4af39611 | Paul Brook | INT_ID = 0,
|
43 | 4af39611 | Paul Brook | INT_STATUS = 1, /* number of pending interrupts */ |
44 | 4af39611 | Paul Brook | INT_CURRENT = 2, /* next interrupt to be serviced */ |
45 | 4af39611 | Paul Brook | INT_DISABLE_ALL = 3,
|
46 | 4af39611 | Paul Brook | INT_DISABLE = 4,
|
47 | 4af39611 | Paul Brook | INT_ENABLE = 5,
|
48 | 4af39611 | Paul Brook | INT_TOTAL = 6
|
49 | 4af39611 | Paul Brook | }; |
50 | 4af39611 | Paul Brook | |
51 | 4af39611 | Paul Brook | typedef struct { |
52 | 4af39611 | Paul Brook | unsigned level:1; |
53 | 4af39611 | Paul Brook | unsigned enabled:1; |
54 | 4af39611 | Paul Brook | } syborg_int_flags; |
55 | 4af39611 | Paul Brook | |
56 | 4af39611 | Paul Brook | typedef struct { |
57 | 4af39611 | Paul Brook | SysBusDevice busdev; |
58 | 4af39611 | Paul Brook | int pending_count;
|
59 | ee6847d1 | Gerd Hoffmann | uint32_t num_irqs; |
60 | 4af39611 | Paul Brook | syborg_int_flags *flags; |
61 | 4af39611 | Paul Brook | qemu_irq parent_irq; |
62 | 4af39611 | Paul Brook | } SyborgIntState; |
63 | 4af39611 | Paul Brook | |
64 | 4af39611 | Paul Brook | static void syborg_int_update(SyborgIntState *s) |
65 | 4af39611 | Paul Brook | { |
66 | 4af39611 | Paul Brook | DPRINTF("pending %d\n", s->pending_count);
|
67 | 4af39611 | Paul Brook | qemu_set_irq(s->parent_irq, s->pending_count > 0);
|
68 | 4af39611 | Paul Brook | } |
69 | 4af39611 | Paul Brook | |
70 | 4af39611 | Paul Brook | static void syborg_int_set_irq(void *opaque, int irq, int level) |
71 | 4af39611 | Paul Brook | { |
72 | 4af39611 | Paul Brook | SyborgIntState *s = (SyborgIntState *)opaque; |
73 | 4af39611 | Paul Brook | |
74 | 4af39611 | Paul Brook | if (s->flags[irq].level == level)
|
75 | 4af39611 | Paul Brook | return;
|
76 | 4af39611 | Paul Brook | |
77 | 4af39611 | Paul Brook | s->flags[irq].level = level; |
78 | 4af39611 | Paul Brook | if (s->flags[irq].enabled) {
|
79 | 4af39611 | Paul Brook | if (level)
|
80 | 4af39611 | Paul Brook | s->pending_count++; |
81 | 4af39611 | Paul Brook | else
|
82 | 4af39611 | Paul Brook | s->pending_count--; |
83 | 4af39611 | Paul Brook | syborg_int_update(s); |
84 | 4af39611 | Paul Brook | } |
85 | 4af39611 | Paul Brook | } |
86 | 4af39611 | Paul Brook | |
87 | c227f099 | Anthony Liguori | static uint32_t syborg_int_read(void *opaque, target_phys_addr_t offset) |
88 | 4af39611 | Paul Brook | { |
89 | 4af39611 | Paul Brook | SyborgIntState *s = (SyborgIntState *)opaque; |
90 | 4af39611 | Paul Brook | int i;
|
91 | 4af39611 | Paul Brook | |
92 | 4af39611 | Paul Brook | offset &= 0xfff;
|
93 | 4af39611 | Paul Brook | switch (offset >> 2) { |
94 | 4af39611 | Paul Brook | case INT_ID:
|
95 | 4af39611 | Paul Brook | return SYBORG_ID_INT;
|
96 | 4af39611 | Paul Brook | case INT_STATUS:
|
97 | 4af39611 | Paul Brook | DPRINTF("read status=%d\n", s->pending_count);
|
98 | 4af39611 | Paul Brook | return s->pending_count;
|
99 | 4af39611 | Paul Brook | |
100 | 4af39611 | Paul Brook | case INT_CURRENT:
|
101 | 4af39611 | Paul Brook | for (i = 0; i < s->num_irqs; i++) { |
102 | 4af39611 | Paul Brook | if (s->flags[i].level & s->flags[i].enabled) {
|
103 | 4af39611 | Paul Brook | DPRINTF("read current=%d\n", i);
|
104 | 4af39611 | Paul Brook | return i;
|
105 | 4af39611 | Paul Brook | } |
106 | 4af39611 | Paul Brook | } |
107 | 4af39611 | Paul Brook | DPRINTF("read current=none\n");
|
108 | 4af39611 | Paul Brook | return 0xffffffffu; |
109 | 4af39611 | Paul Brook | |
110 | 4af39611 | Paul Brook | default:
|
111 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_int_read: Bad offset %x\n",
|
112 | 4af39611 | Paul Brook | (int)offset);
|
113 | 4af39611 | Paul Brook | return 0; |
114 | 4af39611 | Paul Brook | } |
115 | 4af39611 | Paul Brook | } |
116 | 4af39611 | Paul Brook | |
117 | c227f099 | Anthony Liguori | static void syborg_int_write(void *opaque, target_phys_addr_t offset, uint32_t value) |
118 | 4af39611 | Paul Brook | { |
119 | 4af39611 | Paul Brook | SyborgIntState *s = (SyborgIntState *)opaque; |
120 | 4af39611 | Paul Brook | int i;
|
121 | 4af39611 | Paul Brook | offset &= 0xfff;
|
122 | 4af39611 | Paul Brook | |
123 | 4af39611 | Paul Brook | DPRINTF("syborg_int_write offset=%d val=%d\n", (int)offset, (int)value); |
124 | 4af39611 | Paul Brook | switch (offset >> 2) { |
125 | 4af39611 | Paul Brook | case INT_DISABLE_ALL:
|
126 | 4af39611 | Paul Brook | s->pending_count = 0;
|
127 | 4af39611 | Paul Brook | for (i = 0; i < s->num_irqs; i++) |
128 | 4af39611 | Paul Brook | s->flags[i].enabled = 0;
|
129 | 4af39611 | Paul Brook | break;
|
130 | 4af39611 | Paul Brook | |
131 | 4af39611 | Paul Brook | case INT_DISABLE:
|
132 | 4af39611 | Paul Brook | if (value >= s->num_irqs)
|
133 | 4af39611 | Paul Brook | break;
|
134 | 4af39611 | Paul Brook | if (s->flags[value].enabled) {
|
135 | 4af39611 | Paul Brook | if (s->flags[value].enabled)
|
136 | 4af39611 | Paul Brook | s->pending_count--; |
137 | 4af39611 | Paul Brook | s->flags[value].enabled = 0;
|
138 | 4af39611 | Paul Brook | } |
139 | 4af39611 | Paul Brook | break;
|
140 | 4af39611 | Paul Brook | |
141 | 4af39611 | Paul Brook | case INT_ENABLE:
|
142 | 4af39611 | Paul Brook | if (value >= s->num_irqs)
|
143 | 4af39611 | Paul Brook | break;
|
144 | 4af39611 | Paul Brook | if (!(s->flags[value].enabled)) {
|
145 | 4af39611 | Paul Brook | if(s->flags[value].level)
|
146 | 4af39611 | Paul Brook | s->pending_count++; |
147 | 4af39611 | Paul Brook | s->flags[value].enabled = 1;
|
148 | 4af39611 | Paul Brook | } |
149 | 4af39611 | Paul Brook | break;
|
150 | 4af39611 | Paul Brook | |
151 | 4af39611 | Paul Brook | default:
|
152 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_int_write: Bad offset %x\n",
|
153 | 4af39611 | Paul Brook | (int)offset);
|
154 | 4af39611 | Paul Brook | return;
|
155 | 4af39611 | Paul Brook | } |
156 | 4af39611 | Paul Brook | syborg_int_update(s); |
157 | 4af39611 | Paul Brook | } |
158 | 4af39611 | Paul Brook | |
159 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const syborg_int_readfn[] = { |
160 | 4af39611 | Paul Brook | syborg_int_read, |
161 | 4af39611 | Paul Brook | syborg_int_read, |
162 | 4af39611 | Paul Brook | syborg_int_read |
163 | 4af39611 | Paul Brook | }; |
164 | 4af39611 | Paul Brook | |
165 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const syborg_int_writefn[] = { |
166 | 4af39611 | Paul Brook | syborg_int_write, |
167 | 4af39611 | Paul Brook | syborg_int_write, |
168 | 4af39611 | Paul Brook | syborg_int_write |
169 | 4af39611 | Paul Brook | }; |
170 | 4af39611 | Paul Brook | |
171 | 4af39611 | Paul Brook | static void syborg_int_save(QEMUFile *f, void *opaque) |
172 | 4af39611 | Paul Brook | { |
173 | 4af39611 | Paul Brook | SyborgIntState *s = (SyborgIntState *)opaque; |
174 | 4af39611 | Paul Brook | int i;
|
175 | 4af39611 | Paul Brook | |
176 | 4af39611 | Paul Brook | qemu_put_be32(f, s->num_irqs); |
177 | 4af39611 | Paul Brook | qemu_put_be32(f, s->pending_count); |
178 | 4af39611 | Paul Brook | for (i = 0; i < s->num_irqs; i++) { |
179 | 4af39611 | Paul Brook | qemu_put_be32(f, s->flags[i].enabled |
180 | 4af39611 | Paul Brook | | ((unsigned)s->flags[i].level << 1)); |
181 | 4af39611 | Paul Brook | } |
182 | 4af39611 | Paul Brook | } |
183 | 4af39611 | Paul Brook | |
184 | 4af39611 | Paul Brook | static int syborg_int_load(QEMUFile *f, void *opaque, int version_id) |
185 | 4af39611 | Paul Brook | { |
186 | 4af39611 | Paul Brook | SyborgIntState *s = (SyborgIntState *)opaque; |
187 | 4af39611 | Paul Brook | uint32_t val; |
188 | 4af39611 | Paul Brook | int i;
|
189 | 4af39611 | Paul Brook | |
190 | 4af39611 | Paul Brook | if (version_id != 1) |
191 | 4af39611 | Paul Brook | return -EINVAL;
|
192 | 4af39611 | Paul Brook | |
193 | 4af39611 | Paul Brook | val = qemu_get_be32(f); |
194 | 4af39611 | Paul Brook | if (val != s->num_irqs)
|
195 | 4af39611 | Paul Brook | return -EINVAL;
|
196 | 4af39611 | Paul Brook | s->pending_count = qemu_get_be32(f); |
197 | 4af39611 | Paul Brook | for (i = 0; i < s->num_irqs; i++) { |
198 | 4af39611 | Paul Brook | val = qemu_get_be32(f); |
199 | 4af39611 | Paul Brook | s->flags[i].enabled = val & 1;
|
200 | 4af39611 | Paul Brook | s->flags[i].level = (val >> 1) & 1; |
201 | 4af39611 | Paul Brook | } |
202 | 4af39611 | Paul Brook | return 0; |
203 | 4af39611 | Paul Brook | } |
204 | 4af39611 | Paul Brook | |
205 | 81a322d4 | Gerd Hoffmann | static int syborg_int_init(SysBusDevice *dev) |
206 | 4af39611 | Paul Brook | { |
207 | 4af39611 | Paul Brook | SyborgIntState *s = FROM_SYSBUS(SyborgIntState, dev); |
208 | 4af39611 | Paul Brook | int iomemtype;
|
209 | 4af39611 | Paul Brook | |
210 | 4af39611 | Paul Brook | sysbus_init_irq(dev, &s->parent_irq); |
211 | 067a3ddc | Paul Brook | qdev_init_gpio_in(&dev->qdev, syborg_int_set_irq, s->num_irqs); |
212 | 1eed09cb | Avi Kivity | iomemtype = cpu_register_io_memory(syborg_int_readfn, |
213 | 4af39611 | Paul Brook | syborg_int_writefn, s); |
214 | 4af39611 | Paul Brook | sysbus_init_mmio(dev, 0x1000, iomemtype);
|
215 | 4af39611 | Paul Brook | s->flags = qemu_mallocz(s->num_irqs * sizeof(syborg_int_flags));
|
216 | 4af39611 | Paul Brook | |
217 | 0be71e32 | Alex Williamson | register_savevm(&dev->qdev, "syborg_int", -1, 1, syborg_int_save, |
218 | 0be71e32 | Alex Williamson | syborg_int_load, s); |
219 | 81a322d4 | Gerd Hoffmann | return 0; |
220 | 4af39611 | Paul Brook | } |
221 | 4af39611 | Paul Brook | |
222 | ee6847d1 | Gerd Hoffmann | static SysBusDeviceInfo syborg_int_info = {
|
223 | ee6847d1 | Gerd Hoffmann | .init = syborg_int_init, |
224 | ee6847d1 | Gerd Hoffmann | .qdev.name = "syborg,interrupt",
|
225 | ee6847d1 | Gerd Hoffmann | .qdev.size = sizeof(SyborgIntState),
|
226 | ee6847d1 | Gerd Hoffmann | .qdev.props = (Property[]) { |
227 | 3c2aed8b | Gerd Hoffmann | DEFINE_PROP_UINT32("num-interrupts", SyborgIntState, num_irqs, 64), |
228 | 3c2aed8b | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
229 | ee6847d1 | Gerd Hoffmann | } |
230 | ee6847d1 | Gerd Hoffmann | }; |
231 | ee6847d1 | Gerd Hoffmann | |
232 | 4af39611 | Paul Brook | static void syborg_interrupt_register_devices(void) |
233 | 4af39611 | Paul Brook | { |
234 | ee6847d1 | Gerd Hoffmann | sysbus_register_withprop(&syborg_int_info); |
235 | 4af39611 | Paul Brook | } |
236 | 4af39611 | Paul Brook | |
237 | 4af39611 | Paul Brook | device_init(syborg_interrupt_register_devices) |