blob: 641ee4727ce3d8e468366f844d706e2ce2175f8f [file] [log] [blame]
Michael Walle4ef66fa2011-02-17 23:45:07 +01001/*
2 * LatticeMico32 CPU interrupt controller logic.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <assert.h>
21
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010022#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010023#include "hw/i386/pc.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010024#include "monitor/monitor.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/sysbus.h"
Michael Walle4ef66fa2011-02-17 23:45:07 +010026#include "trace.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010027#include "hw/lm32/lm32_pic.h"
Michael Walle4ef66fa2011-02-17 23:45:07 +010028
Andreas Färber1f8a9ea2013-07-26 19:53:49 +020029#define TYPE_LM32_PIC "lm32-pic"
30#define LM32_PIC(obj) OBJECT_CHECK(LM32PicState, (obj), TYPE_LM32_PIC)
31
Michael Walle4ef66fa2011-02-17 23:45:07 +010032struct LM32PicState {
Andreas Färber1f8a9ea2013-07-26 19:53:49 +020033 SysBusDevice parent_obj;
34
Michael Walle4ef66fa2011-02-17 23:45:07 +010035 qemu_irq parent_irq;
36 uint32_t im; /* interrupt mask */
37 uint32_t ip; /* interrupt pending */
38 uint32_t irq_state;
39
40 /* statistics */
41 uint32_t stats_irq_count[32];
42};
43typedef struct LM32PicState LM32PicState;
44
45static LM32PicState *pic;
Markus Armbruster1ce6be22015-02-06 14:18:24 +010046void lm32_hmp_info_pic(Monitor *mon, const QDict *qdict)
Michael Walle4ef66fa2011-02-17 23:45:07 +010047{
48 if (pic == NULL) {
49 return;
50 }
51
52 monitor_printf(mon, "lm32-pic: im=%08x ip=%08x irq_state=%08x\n",
53 pic->im, pic->ip, pic->irq_state);
54}
55
Markus Armbruster1ce6be22015-02-06 14:18:24 +010056void lm32_hmp_info_irq(Monitor *mon, const QDict *qdict)
Michael Walle4ef66fa2011-02-17 23:45:07 +010057{
58 int i;
59 uint32_t count;
60
61 if (pic == NULL) {
62 return;
63 }
64
65 monitor_printf(mon, "IRQ statistics:\n");
66 for (i = 0; i < 32; i++) {
67 count = pic->stats_irq_count[i];
68 if (count > 0) {
69 monitor_printf(mon, "%2d: %u\n", i, count);
70 }
71 }
72}
73
74static void update_irq(LM32PicState *s)
75{
76 s->ip |= s->irq_state;
77
78 if (s->ip & s->im) {
79 trace_lm32_pic_raise_irq();
80 qemu_irq_raise(s->parent_irq);
81 } else {
82 trace_lm32_pic_lower_irq();
83 qemu_irq_lower(s->parent_irq);
84 }
85}
86
87static void irq_handler(void *opaque, int irq, int level)
88{
89 LM32PicState *s = opaque;
90
91 assert(irq < 32);
92 trace_lm32_pic_interrupt(irq, level);
93
94 if (level) {
95 s->irq_state |= (1 << irq);
96 s->stats_irq_count[irq]++;
97 } else {
98 s->irq_state &= ~(1 << irq);
99 }
100
101 update_irq(s);
102}
103
104void lm32_pic_set_im(DeviceState *d, uint32_t im)
105{
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200106 LM32PicState *s = LM32_PIC(d);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100107
108 trace_lm32_pic_set_im(im);
109 s->im = im;
110
111 update_irq(s);
112}
113
114void lm32_pic_set_ip(DeviceState *d, uint32_t ip)
115{
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200116 LM32PicState *s = LM32_PIC(d);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100117
118 trace_lm32_pic_set_ip(ip);
119
120 /* ack interrupt */
121 s->ip &= ~ip;
122
123 update_irq(s);
124}
125
126uint32_t lm32_pic_get_im(DeviceState *d)
127{
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200128 LM32PicState *s = LM32_PIC(d);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100129
130 trace_lm32_pic_get_im(s->im);
131 return s->im;
132}
133
134uint32_t lm32_pic_get_ip(DeviceState *d)
135{
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200136 LM32PicState *s = LM32_PIC(d);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100137
138 trace_lm32_pic_get_ip(s->ip);
139 return s->ip;
140}
141
142static void pic_reset(DeviceState *d)
143{
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200144 LM32PicState *s = LM32_PIC(d);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100145 int i;
146
147 s->im = 0;
148 s->ip = 0;
149 s->irq_state = 0;
150 for (i = 0; i < 32; i++) {
151 s->stats_irq_count[i] = 0;
152 }
153}
154
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200155static int lm32_pic_init(SysBusDevice *sbd)
Michael Walle4ef66fa2011-02-17 23:45:07 +0100156{
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200157 DeviceState *dev = DEVICE(sbd);
158 LM32PicState *s = LM32_PIC(dev);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100159
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200160 qdev_init_gpio_in(dev, irq_handler, 32);
161 sysbus_init_irq(sbd, &s->parent_irq);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100162
163 pic = s;
164
165 return 0;
166}
167
168static const VMStateDescription vmstate_lm32_pic = {
169 .name = "lm32-pic",
170 .version_id = 1,
171 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200172 .fields = (VMStateField[]) {
Michael Walle4ef66fa2011-02-17 23:45:07 +0100173 VMSTATE_UINT32(im, LM32PicState),
174 VMSTATE_UINT32(ip, LM32PicState),
175 VMSTATE_UINT32(irq_state, LM32PicState),
176 VMSTATE_UINT32_ARRAY(stats_irq_count, LM32PicState, 32),
177 VMSTATE_END_OF_LIST()
178 }
179};
180
Anthony Liguori999e12b2012-01-24 13:12:29 -0600181static void lm32_pic_class_init(ObjectClass *klass, void *data)
182{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600183 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600184 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
185
186 k->init = lm32_pic_init;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600187 dc->reset = pic_reset;
188 dc->vmsd = &vmstate_lm32_pic;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600189}
190
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100191static const TypeInfo lm32_pic_info = {
Andreas Färber1f8a9ea2013-07-26 19:53:49 +0200192 .name = TYPE_LM32_PIC,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600193 .parent = TYPE_SYS_BUS_DEVICE,
194 .instance_size = sizeof(LM32PicState),
195 .class_init = lm32_pic_class_init,
Michael Walle4ef66fa2011-02-17 23:45:07 +0100196};
197
Andreas Färber83f7d432012-02-09 15:20:55 +0100198static void lm32_pic_register_types(void)
Michael Walle4ef66fa2011-02-17 23:45:07 +0100199{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600200 type_register_static(&lm32_pic_info);
Michael Walle4ef66fa2011-02-17 23:45:07 +0100201}
202
Andreas Färber83f7d432012-02-09 15:20:55 +0100203type_init(lm32_pic_register_types)