blob: ed032fed548d7f194134e32a9592002a1cd25733 [file] [log] [blame]
balrogc1713132007-04-30 01:26:42 +00001/*
2 * Intel XScale PXA Programmable Interrupt Controller.
3 *
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Copyright (c) 2006 Thorsten Zitterell
6 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10008 * This code is licensed under the GPL.
balrogc1713132007-04-30 01:26:42 +00009 */
10
Peter Maydell12b16722015-12-07 16:23:45 +000011#include "qemu/osdep.h"
Markus Armbruster3e80f692020-06-10 07:31:58 +020012#include "qapi/error.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020013#include "qemu/module.h"
Peter Maydelle53652e2020-07-03 16:59:45 +010014#include "qemu/log.h"
Paolo Bonzini4771d752016-01-19 21:51:44 +010015#include "cpu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010016#include "hw/arm/pxa.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010017#include "hw/sysbus.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020018#include "migration/vmstate.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040019#include "qom/object.h"
balrogc1713132007-04-30 01:26:42 +000020
21#define ICIP 0x00 /* Interrupt Controller IRQ Pending register */
22#define ICMR 0x04 /* Interrupt Controller Mask register */
23#define ICLR 0x08 /* Interrupt Controller Level register */
24#define ICFP 0x0c /* Interrupt Controller FIQ Pending register */
25#define ICPR 0x10 /* Interrupt Controller Pending register */
26#define ICCR 0x14 /* Interrupt Controller Control register */
27#define ICHP 0x18 /* Interrupt Controller Highest Priority register */
28#define IPR0 0x1c /* Interrupt Controller Priority register 0 */
29#define IPR31 0x98 /* Interrupt Controller Priority register 31 */
30#define ICIP2 0x9c /* Interrupt Controller IRQ Pending register 2 */
31#define ICMR2 0xa0 /* Interrupt Controller Mask register 2 */
32#define ICLR2 0xa4 /* Interrupt Controller Level register 2 */
33#define ICFP2 0xa8 /* Interrupt Controller FIQ Pending register 2 */
34#define ICPR2 0xac /* Interrupt Controller Pending register 2 */
35#define IPR32 0xb0 /* Interrupt Controller Priority register 32 */
36#define IPR39 0xcc /* Interrupt Controller Priority register 39 */
37
38#define PXA2XX_PIC_SRCS 40
39
Andreas Färber6050ed52013-07-24 02:08:09 +020040#define TYPE_PXA2XX_PIC "pxa2xx_pic"
Eduardo Habkost80633962020-09-16 14:25:19 -040041OBJECT_DECLARE_SIMPLE_TYPE(PXA2xxPICState, PXA2XX_PIC)
Andreas Färber6050ed52013-07-24 02:08:09 +020042
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040043struct PXA2xxPICState {
Andreas Färber6050ed52013-07-24 02:08:09 +020044 /*< private >*/
45 SysBusDevice parent_obj;
46 /*< public >*/
47
Benoît Canet90e8e5a2011-10-30 14:50:17 +010048 MemoryRegion iomem;
Andreas Färbere9d872c2012-05-04 00:05:29 +020049 ARMCPU *cpu;
balrogc1713132007-04-30 01:26:42 +000050 uint32_t int_enabled[2];
51 uint32_t int_pending[2];
52 uint32_t is_fiq[2];
53 uint32_t int_idle;
54 uint32_t priority[PXA2XX_PIC_SRCS];
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040055};
balrogc1713132007-04-30 01:26:42 +000056
57static void pxa2xx_pic_update(void *opaque)
58{
59 uint32_t mask[2];
Paul Brookbc24a222009-05-10 01:44:56 +010060 PXA2xxPICState *s = (PXA2xxPICState *) opaque;
Andreas Färber259186a2013-01-17 18:51:17 +010061 CPUState *cpu = CPU(s->cpu);
balrogc1713132007-04-30 01:26:42 +000062
Andreas Färber259186a2013-01-17 18:51:17 +010063 if (cpu->halted) {
balrogc1713132007-04-30 01:26:42 +000064 mask[0] = s->int_pending[0] & (s->int_enabled[0] | s->int_idle);
65 mask[1] = s->int_pending[1] & (s->int_enabled[1] | s->int_idle);
Andreas Färbere9d872c2012-05-04 00:05:29 +020066 if (mask[0] || mask[1]) {
Andreas Färberc3affe52013-01-18 15:03:43 +010067 cpu_interrupt(cpu, CPU_INTERRUPT_EXITTB);
Andreas Färbere9d872c2012-05-04 00:05:29 +020068 }
balrogc1713132007-04-30 01:26:42 +000069 }
70
71 mask[0] = s->int_pending[0] & s->int_enabled[0];
72 mask[1] = s->int_pending[1] & s->int_enabled[1];
73
Andreas Färbere9d872c2012-05-04 00:05:29 +020074 if ((mask[0] & s->is_fiq[0]) || (mask[1] & s->is_fiq[1])) {
Andreas Färberc3affe52013-01-18 15:03:43 +010075 cpu_interrupt(cpu, CPU_INTERRUPT_FIQ);
Andreas Färbere9d872c2012-05-04 00:05:29 +020076 } else {
Andreas Färberd8ed8872013-01-17 22:30:20 +010077 cpu_reset_interrupt(cpu, CPU_INTERRUPT_FIQ);
Andreas Färbere9d872c2012-05-04 00:05:29 +020078 }
balrogc1713132007-04-30 01:26:42 +000079
Andreas Färbere9d872c2012-05-04 00:05:29 +020080 if ((mask[0] & ~s->is_fiq[0]) || (mask[1] & ~s->is_fiq[1])) {
Andreas Färberc3affe52013-01-18 15:03:43 +010081 cpu_interrupt(cpu, CPU_INTERRUPT_HARD);
Andreas Färbere9d872c2012-05-04 00:05:29 +020082 } else {
Andreas Färberd8ed8872013-01-17 22:30:20 +010083 cpu_reset_interrupt(cpu, CPU_INTERRUPT_HARD);
Andreas Färbere9d872c2012-05-04 00:05:29 +020084 }
balrogc1713132007-04-30 01:26:42 +000085}
86
87/* Note: Here level means state of the signal on a pin, not
88 * IRQ/FIQ distinction as in PXA Developer Manual. */
89static void pxa2xx_pic_set_irq(void *opaque, int irq, int level)
90{
Paul Brookbc24a222009-05-10 01:44:56 +010091 PXA2xxPICState *s = (PXA2xxPICState *) opaque;
balrogc1713132007-04-30 01:26:42 +000092 int int_set = (irq >= 32);
93 irq &= 31;
94
95 if (level)
96 s->int_pending[int_set] |= 1 << irq;
97 else
98 s->int_pending[int_set] &= ~(1 << irq);
99
100 pxa2xx_pic_update(opaque);
101}
102
Paul Brookbc24a222009-05-10 01:44:56 +0100103static inline uint32_t pxa2xx_pic_highest(PXA2xxPICState *s) {
balrogc1713132007-04-30 01:26:42 +0000104 int i, int_set, irq;
105 uint32_t bit, mask[2];
106 uint32_t ichp = 0x003f003f; /* Both IDs invalid */
107
108 mask[0] = s->int_pending[0] & s->int_enabled[0];
109 mask[1] = s->int_pending[1] & s->int_enabled[1];
110
111 for (i = PXA2XX_PIC_SRCS - 1; i >= 0; i --) {
112 irq = s->priority[i] & 0x3f;
Peter Maydell43a32ed2014-03-10 14:56:29 +0000113 if ((s->priority[i] & (1U << 31)) && irq < PXA2XX_PIC_SRCS) {
balrogc1713132007-04-30 01:26:42 +0000114 /* Source peripheral ID is valid. */
115 bit = 1 << (irq & 31);
116 int_set = (irq >= 32);
117
118 if (mask[int_set] & bit & s->is_fiq[int_set]) {
119 /* FIQ asserted */
120 ichp &= 0xffff0000;
121 ichp |= (1 << 15) | irq;
122 }
123
124 if (mask[int_set] & bit & ~s->is_fiq[int_set]) {
125 /* IRQ asserted */
126 ichp &= 0x0000ffff;
Peter Maydell43a32ed2014-03-10 14:56:29 +0000127 ichp |= (1U << 31) | (irq << 16);
balrogc1713132007-04-30 01:26:42 +0000128 }
129 }
130 }
131
132 return ichp;
133}
134
Avi Kivitya8170e52012-10-23 12:30:10 +0200135static uint64_t pxa2xx_pic_mem_read(void *opaque, hwaddr offset,
Benoît Canet90e8e5a2011-10-30 14:50:17 +0100136 unsigned size)
balrogc1713132007-04-30 01:26:42 +0000137{
Paul Brookbc24a222009-05-10 01:44:56 +0100138 PXA2xxPICState *s = (PXA2xxPICState *) opaque;
balrogc1713132007-04-30 01:26:42 +0000139
140 switch (offset) {
141 case ICIP: /* IRQ Pending register */
142 return s->int_pending[0] & ~s->is_fiq[0] & s->int_enabled[0];
143 case ICIP2: /* IRQ Pending register 2 */
144 return s->int_pending[1] & ~s->is_fiq[1] & s->int_enabled[1];
145 case ICMR: /* Mask register */
146 return s->int_enabled[0];
147 case ICMR2: /* Mask register 2 */
148 return s->int_enabled[1];
149 case ICLR: /* Level register */
150 return s->is_fiq[0];
151 case ICLR2: /* Level register 2 */
152 return s->is_fiq[1];
153 case ICCR: /* Idle mask */
154 return (s->int_idle == 0);
155 case ICFP: /* FIQ Pending register */
156 return s->int_pending[0] & s->is_fiq[0] & s->int_enabled[0];
157 case ICFP2: /* FIQ Pending register 2 */
158 return s->int_pending[1] & s->is_fiq[1] & s->int_enabled[1];
159 case ICPR: /* Pending register */
160 return s->int_pending[0];
161 case ICPR2: /* Pending register 2 */
162 return s->int_pending[1];
163 case IPR0 ... IPR31:
164 return s->priority[0 + ((offset - IPR0 ) >> 2)];
165 case IPR32 ... IPR39:
166 return s->priority[32 + ((offset - IPR32) >> 2)];
167 case ICHP: /* Highest Priority register */
168 return pxa2xx_pic_highest(s);
169 default:
Peter Maydelle53652e2020-07-03 16:59:45 +0100170 qemu_log_mask(LOG_GUEST_ERROR,
171 "pxa2xx_pic_mem_read: bad register offset 0x%" HWADDR_PRIx
172 "\n", offset);
balrogc1713132007-04-30 01:26:42 +0000173 return 0;
174 }
175}
176
Avi Kivitya8170e52012-10-23 12:30:10 +0200177static void pxa2xx_pic_mem_write(void *opaque, hwaddr offset,
Benoît Canet90e8e5a2011-10-30 14:50:17 +0100178 uint64_t value, unsigned size)
balrogc1713132007-04-30 01:26:42 +0000179{
Paul Brookbc24a222009-05-10 01:44:56 +0100180 PXA2xxPICState *s = (PXA2xxPICState *) opaque;
balrogc1713132007-04-30 01:26:42 +0000181
182 switch (offset) {
183 case ICMR: /* Mask register */
184 s->int_enabled[0] = value;
185 break;
186 case ICMR2: /* Mask register 2 */
187 s->int_enabled[1] = value;
188 break;
189 case ICLR: /* Level register */
190 s->is_fiq[0] = value;
191 break;
192 case ICLR2: /* Level register 2 */
193 s->is_fiq[1] = value;
194 break;
195 case ICCR: /* Idle mask */
196 s->int_idle = (value & 1) ? 0 : ~0;
197 break;
198 case IPR0 ... IPR31:
199 s->priority[0 + ((offset - IPR0 ) >> 2)] = value & 0x8000003f;
200 break;
201 case IPR32 ... IPR39:
202 s->priority[32 + ((offset - IPR32) >> 2)] = value & 0x8000003f;
203 break;
204 default:
Peter Maydelle53652e2020-07-03 16:59:45 +0100205 qemu_log_mask(LOG_GUEST_ERROR,
206 "pxa2xx_pic_mem_write: bad register offset 0x%"
207 HWADDR_PRIx "\n", offset);
balrogc1713132007-04-30 01:26:42 +0000208 return;
209 }
210 pxa2xx_pic_update(opaque);
211}
212
213/* Interrupt Controller Coprocessor Space Register Mapping */
214static const int pxa2xx_cp_reg_map[0x10] = {
215 [0x0 ... 0xf] = -1,
216 [0x0] = ICIP,
217 [0x1] = ICMR,
218 [0x2] = ICLR,
219 [0x3] = ICFP,
220 [0x4] = ICPR,
221 [0x5] = ICHP,
222 [0x6] = ICIP2,
223 [0x7] = ICMR2,
224 [0x8] = ICLR2,
225 [0x9] = ICFP2,
226 [0xa] = ICPR2,
227};
228
Peter Maydellc4241c72014-02-20 10:35:54 +0000229static uint64_t pxa2xx_pic_cp_read(CPUARMState *env, const ARMCPRegInfo *ri)
balrogc1713132007-04-30 01:26:42 +0000230{
Peter Maydell9ee703b2012-06-20 11:57:08 +0000231 int offset = pxa2xx_cp_reg_map[ri->crn];
Peter Maydellc4241c72014-02-20 10:35:54 +0000232 return pxa2xx_pic_mem_read(ri->opaque, offset, 4);
balrogc1713132007-04-30 01:26:42 +0000233}
234
Peter Maydellc4241c72014-02-20 10:35:54 +0000235static void pxa2xx_pic_cp_write(CPUARMState *env, const ARMCPRegInfo *ri,
236 uint64_t value)
balrogc1713132007-04-30 01:26:42 +0000237{
Peter Maydell9ee703b2012-06-20 11:57:08 +0000238 int offset = pxa2xx_cp_reg_map[ri->crn];
239 pxa2xx_pic_mem_write(ri->opaque, offset, value, 4);
balrogc1713132007-04-30 01:26:42 +0000240}
241
Peter Maydell9ee703b2012-06-20 11:57:08 +0000242#define REGINFO_FOR_PIC_CP(NAME, CRN) \
243 { .name = NAME, .cp = 6, .crn = CRN, .crm = 0, .opc1 = 0, .opc2 = 0, \
Peter Maydell14c30322015-06-15 18:06:09 +0100244 .access = PL1_RW, .type = ARM_CP_IO, \
Peter Maydell9ee703b2012-06-20 11:57:08 +0000245 .readfn = pxa2xx_pic_cp_read, .writefn = pxa2xx_pic_cp_write }
246
247static const ARMCPRegInfo pxa_pic_cp_reginfo[] = {
248 REGINFO_FOR_PIC_CP("ICIP", 0),
249 REGINFO_FOR_PIC_CP("ICMR", 1),
250 REGINFO_FOR_PIC_CP("ICLR", 2),
251 REGINFO_FOR_PIC_CP("ICFP", 3),
252 REGINFO_FOR_PIC_CP("ICPR", 4),
253 REGINFO_FOR_PIC_CP("ICHP", 5),
254 REGINFO_FOR_PIC_CP("ICIP2", 6),
255 REGINFO_FOR_PIC_CP("ICMR2", 7),
256 REGINFO_FOR_PIC_CP("ICLR2", 8),
257 REGINFO_FOR_PIC_CP("ICFP2", 9),
258 REGINFO_FOR_PIC_CP("ICPR2", 0xa),
259 REGINFO_SENTINEL
260};
261
Benoît Canet90e8e5a2011-10-30 14:50:17 +0100262static const MemoryRegionOps pxa2xx_pic_ops = {
263 .read = pxa2xx_pic_mem_read,
264 .write = pxa2xx_pic_mem_write,
265 .endianness = DEVICE_NATIVE_ENDIAN,
balrogc1713132007-04-30 01:26:42 +0000266};
267
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100268static int pxa2xx_pic_post_load(void *opaque, int version_id)
balrogaa941b92007-05-24 18:50:09 +0000269{
balrogaa941b92007-05-24 18:50:09 +0000270 pxa2xx_pic_update(opaque);
271 return 0;
272}
273
Avi Kivitya8170e52012-10-23 12:30:10 +0200274DeviceState *pxa2xx_pic_init(hwaddr base, ARMCPU *cpu)
balrogc1713132007-04-30 01:26:42 +0000275{
Markus Armbruster3e80f692020-06-10 07:31:58 +0200276 DeviceState *dev = qdev_new(TYPE_PXA2XX_PIC);
Andreas Färber6050ed52013-07-24 02:08:09 +0200277 PXA2xxPICState *s = PXA2XX_PIC(dev);
balrogc1713132007-04-30 01:26:42 +0000278
Andreas Färbere9d872c2012-05-04 00:05:29 +0200279 s->cpu = cpu;
balrogc1713132007-04-30 01:26:42 +0000280
281 s->int_pending[0] = 0;
282 s->int_pending[1] = 0;
283 s->int_enabled[0] = 0;
284 s->int_enabled[1] = 0;
285 s->is_fiq[0] = 0;
286 s->is_fiq[1] = 0;
287
Markus Armbruster3c6ef472020-06-10 07:32:34 +0200288 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100289
290 qdev_init_gpio_in(dev, pxa2xx_pic_set_irq, PXA2XX_PIC_SRCS);
balrogc1713132007-04-30 01:26:42 +0000291
292 /* Enable IC memory-mapped registers access. */
Paolo Bonzini64bde0f2013-06-06 21:25:08 -0400293 memory_region_init_io(&s->iomem, OBJECT(s), &pxa2xx_pic_ops, s,
Benoît Canet90e8e5a2011-10-30 14:50:17 +0100294 "pxa2xx-pic", 0x00100000);
Andreas Färber1356b982013-01-20 02:47:33 +0100295 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
296 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
balrogc1713132007-04-30 01:26:42 +0000297
298 /* Enable IC coprocessor access. */
Andreas Färber6050ed52013-07-24 02:08:09 +0200299 define_arm_cp_regs_with_opaque(cpu, pxa_pic_cp_reginfo, s);
balrogc1713132007-04-30 01:26:42 +0000300
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100301 return dev;
balrogc1713132007-04-30 01:26:42 +0000302}
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100303
Philippe Mathieu-Daudécfa52e02021-03-13 18:11:48 +0100304static const VMStateDescription vmstate_pxa2xx_pic_regs = {
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100305 .name = "pxa2xx_pic",
306 .version_id = 0,
307 .minimum_version_id = 0,
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100308 .post_load = pxa2xx_pic_post_load,
309 .fields = (VMStateField[]) {
310 VMSTATE_UINT32_ARRAY(int_enabled, PXA2xxPICState, 2),
311 VMSTATE_UINT32_ARRAY(int_pending, PXA2xxPICState, 2),
312 VMSTATE_UINT32_ARRAY(is_fiq, PXA2xxPICState, 2),
313 VMSTATE_UINT32(int_idle, PXA2xxPICState),
314 VMSTATE_UINT32_ARRAY(priority, PXA2xxPICState, PXA2XX_PIC_SRCS),
315 VMSTATE_END_OF_LIST(),
316 },
317};
318
Anthony Liguori999e12b2012-01-24 13:12:29 -0600319static void pxa2xx_pic_class_init(ObjectClass *klass, void *data)
320{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600321 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600322
Anthony Liguori39bffca2011-12-07 21:34:16 -0600323 dc->desc = "PXA2xx PIC";
324 dc->vmsd = &vmstate_pxa2xx_pic_regs;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600325}
326
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100327static const TypeInfo pxa2xx_pic_info = {
Andreas Färber6050ed52013-07-24 02:08:09 +0200328 .name = TYPE_PXA2XX_PIC,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600329 .parent = TYPE_SYS_BUS_DEVICE,
330 .instance_size = sizeof(PXA2xxPICState),
331 .class_init = pxa2xx_pic_class_init,
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100332};
333
Andreas Färber83f7d432012-02-09 15:20:55 +0100334static void pxa2xx_pic_register_types(void)
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100335{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600336 type_register_static(&pxa2xx_pic_info);
Dmitry Eremin-Solenikove1f8c722011-02-25 12:13:38 +0100337}
Andreas Färber83f7d432012-02-09 15:20:55 +0100338
339type_init(pxa2xx_pic_register_types)