blob: bfcf155356ff52e0409a13624bc974f5c1d72ee6 [file] [log] [blame]
bellarddbda8082004-06-15 21:38:40 +00001/*
2 * OpenPIC emulation
ths5fafdf22007-09-16 21:08:06 +00003 *
bellarddbda8082004-06-15 21:38:40 +00004 * Copyright (c) 2004 Jocelyn Mayer
Alexander Graf704c7e52011-07-21 01:33:29 +02005 * 2011 Alexander Graf
ths5fafdf22007-09-16 21:08:06 +00006 *
bellarddbda8082004-06-15 21:38:40 +00007 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25/*
26 *
27 * Based on OpenPic implementations:
blueswir167b55782009-02-06 21:30:02 +000028 * - Intel GW80314 I/O companion chip developer's manual
bellarddbda8082004-06-15 21:38:40 +000029 * - Motorola MPC8245 & MPC8540 user manuals.
30 * - Motorola MCP750 (aka Raven) programmer manual.
31 * - Motorola Harrier programmer manuel
32 *
33 * Serial interrupts, as implemented in Raven chipset are not supported yet.
ths5fafdf22007-09-16 21:08:06 +000034 *
bellarddbda8082004-06-15 21:38:40 +000035 */
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010036#include "hw/hw.h"
37#include "hw/ppc/mac.h"
38#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010039#include "hw/ppc/openpic.h"
Andreas Färber2b927572013-06-16 17:04:21 +020040#include "hw/ppc/ppc_e500.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010041#include "hw/sysbus.h"
42#include "hw/pci/msi.h"
Scott Woode69a17f2012-12-21 16:15:48 +000043#include "qemu/bitops.h"
Michael Roth73d963c2014-04-28 16:08:17 +030044#include "qapi/qmp/qerror.h"
bellarddbda8082004-06-15 21:38:40 +000045
bellard611493d2004-06-21 16:50:43 +000046//#define DEBUG_OPENPIC
bellarddbda8082004-06-15 21:38:40 +000047
48#ifdef DEBUG_OPENPIC
Scott Wood4c4f0e42012-12-21 16:15:38 +000049static const int debug_openpic = 1;
bellarddbda8082004-06-15 21:38:40 +000050#else
Scott Wood4c4f0e42012-12-21 16:15:38 +000051static const int debug_openpic = 0;
bellarddbda8082004-06-15 21:38:40 +000052#endif
bellarddbda8082004-06-15 21:38:40 +000053
Scott Wood4c4f0e42012-12-21 16:15:38 +000054#define DPRINTF(fmt, ...) do { \
55 if (debug_openpic) { \
56 printf(fmt , ## __VA_ARGS__); \
57 } \
58 } while (0)
59
Scott Woode0dfe5b2013-01-21 15:53:53 +000060#define MAX_CPU 32
Alexander Graf732aa6e2012-12-08 14:18:00 +010061#define MAX_MSI 8
bellarddbda8082004-06-15 21:38:40 +000062#define VID 0x03 /* MPIC version ID */
bellarddbda8082004-06-15 21:38:40 +000063
Alexander Grafd0b72632012-12-08 05:17:14 +010064/* OpenPIC capability flags */
Scott Woodbe7c2362012-12-21 16:15:42 +000065#define OPENPIC_FLAG_IDR_CRIT (1 << 0)
Scott Woode0dfe5b2013-01-21 15:53:53 +000066#define OPENPIC_FLAG_ILR (2 << 0)
bellarddbda8082004-06-15 21:38:40 +000067
Alexander Grafd0b72632012-12-08 05:17:14 +010068/* OpenPIC address map */
Alexander Graf780d16b2012-12-07 17:15:15 +010069#define OPENPIC_GLB_REG_START 0x0
70#define OPENPIC_GLB_REG_SIZE 0x10F0
71#define OPENPIC_TMR_REG_START 0x10F0
72#define OPENPIC_TMR_REG_SIZE 0x220
Alexander Graf732aa6e2012-12-08 14:18:00 +010073#define OPENPIC_MSI_REG_START 0x1600
74#define OPENPIC_MSI_REG_SIZE 0x200
Scott Woode0dfe5b2013-01-21 15:53:53 +000075#define OPENPIC_SUMMARY_REG_START 0x3800
76#define OPENPIC_SUMMARY_REG_SIZE 0x800
Alexander Graf780d16b2012-12-07 17:15:15 +010077#define OPENPIC_SRC_REG_START 0x10000
Scott Wood8935a442013-04-15 13:19:32 +000078#define OPENPIC_SRC_REG_SIZE (OPENPIC_MAX_SRC * 0x20)
Alexander Graf780d16b2012-12-07 17:15:15 +010079#define OPENPIC_CPU_REG_START 0x20000
80#define OPENPIC_CPU_REG_SIZE 0x100 + ((MAX_CPU - 1) * 0x1000)
81
Alexander Grafd0b72632012-12-08 05:17:14 +010082/* Raven */
83#define RAVEN_MAX_CPU 2
84#define RAVEN_MAX_EXT 48
85#define RAVEN_MAX_IRQ 64
Scott Wood8935a442013-04-15 13:19:32 +000086#define RAVEN_MAX_TMR OPENPIC_MAX_TMR
87#define RAVEN_MAX_IPI OPENPIC_MAX_IPI
Alexander Grafd0b72632012-12-08 05:17:14 +010088
89/* Interrupt definitions */
90#define RAVEN_FE_IRQ (RAVEN_MAX_EXT) /* Internal functional IRQ */
91#define RAVEN_ERR_IRQ (RAVEN_MAX_EXT + 1) /* Error IRQ */
92#define RAVEN_TMR_IRQ (RAVEN_MAX_EXT + 2) /* First timer IRQ */
93#define RAVEN_IPI_IRQ (RAVEN_TMR_IRQ + RAVEN_MAX_TMR) /* First IPI IRQ */
94/* First doorbell IRQ */
95#define RAVEN_DBL_IRQ (RAVEN_IPI_IRQ + (RAVEN_MAX_CPU * RAVEN_MAX_IPI))
96
Scott Woode0dfe5b2013-01-21 15:53:53 +000097typedef struct FslMpicInfo {
98 int max_ext;
99} FslMpicInfo;
aurel32b7169912009-03-02 16:42:04 +0000100
Scott Woode0dfe5b2013-01-21 15:53:53 +0000101static FslMpicInfo fsl_mpic_20 = {
102 .max_ext = 12,
103};
aurel32b7169912009-03-02 16:42:04 +0000104
Scott Woode0dfe5b2013-01-21 15:53:53 +0000105static FslMpicInfo fsl_mpic_42 = {
106 .max_ext = 12,
107};
Bharat Bhushan3e772232012-08-14 04:30:55 +0000108
Scott Woodbe7c2362012-12-21 16:15:42 +0000109#define FRR_NIRQ_SHIFT 16
110#define FRR_NCPU_SHIFT 8
111#define FRR_VID_SHIFT 0
Alexander Graf825463b2012-12-08 00:58:54 +0100112
113#define VID_REVISION_1_2 2
Alexander Grafd0b72632012-12-08 05:17:14 +0100114#define VID_REVISION_1_3 3
Alexander Graf825463b2012-12-08 00:58:54 +0100115
Scott Woodbe7c2362012-12-21 16:15:42 +0000116#define VIR_GENERIC 0x00000000 /* Generic Vendor ID */
Alexander Graf825463b2012-12-08 00:58:54 +0100117
Scott Woodbe7c2362012-12-21 16:15:42 +0000118#define GCR_RESET 0x80000000
Alexander Graf68c2dd72013-01-04 11:21:04 +0100119#define GCR_MODE_PASS 0x00000000
120#define GCR_MODE_MIXED 0x20000000
121#define GCR_MODE_PROXY 0x60000000
Scott Wood71c6cac2012-12-13 16:11:59 +0000122
Scott Woodbe7c2362012-12-21 16:15:42 +0000123#define TBCR_CI 0x80000000 /* count inhibit */
124#define TCCR_TOG 0x80000000 /* toggles when decrement to zero */
aurel32b7169912009-03-02 16:42:04 +0000125
Alexander Graf1945dbc2012-12-08 01:49:52 +0100126#define IDR_EP_SHIFT 31
Peter Maydelldef60292014-03-17 16:00:36 +0000127#define IDR_EP_MASK (1U << IDR_EP_SHIFT)
Alexander Graf1945dbc2012-12-08 01:49:52 +0100128#define IDR_CI0_SHIFT 30
129#define IDR_CI1_SHIFT 29
130#define IDR_P1_SHIFT 1
131#define IDR_P0_SHIFT 0
bellarddbda8082004-06-15 21:38:40 +0000132
Scott Woode0dfe5b2013-01-21 15:53:53 +0000133#define ILR_INTTGT_MASK 0x000000ff
134#define ILR_INTTGT_INT 0x00
135#define ILR_INTTGT_CINT 0x01 /* critical */
136#define ILR_INTTGT_MCP 0x02 /* machine check */
137
138/* The currently supported INTTGT values happen to be the same as QEMU's
139 * openpic output codes, but don't depend on this. The output codes
140 * could change (unlikely, but...) or support could be added for
141 * more INTTGT values.
142 */
143static const int inttgt_output[][2] = {
144 { ILR_INTTGT_INT, OPENPIC_OUTPUT_INT },
145 { ILR_INTTGT_CINT, OPENPIC_OUTPUT_CINT },
146 { ILR_INTTGT_MCP, OPENPIC_OUTPUT_MCK },
147};
148
149static int inttgt_to_output(int inttgt)
150{
151 int i;
152
153 for (i = 0; i < ARRAY_SIZE(inttgt_output); i++) {
154 if (inttgt_output[i][0] == inttgt) {
155 return inttgt_output[i][1];
156 }
157 }
158
159 fprintf(stderr, "%s: unsupported inttgt %d\n", __func__, inttgt);
160 return OPENPIC_OUTPUT_INT;
161}
162
163static int output_to_inttgt(int output)
164{
165 int i;
166
167 for (i = 0; i < ARRAY_SIZE(inttgt_output); i++) {
168 if (inttgt_output[i][1] == output) {
169 return inttgt_output[i][0];
170 }
171 }
172
173 abort();
174}
175
Alexander Graf732aa6e2012-12-08 14:18:00 +0100176#define MSIIR_OFFSET 0x140
177#define MSIIR_SRS_SHIFT 29
178#define MSIIR_SRS_MASK (0x7 << MSIIR_SRS_SHIFT)
179#define MSIIR_IBS_SHIFT 24
180#define MSIIR_IBS_MASK (0x1f << MSIIR_IBS_SHIFT)
181
Alexander Graf704c7e52011-07-21 01:33:29 +0200182static int get_current_cpu(void)
183{
Andreas Färber4917cf42013-05-27 05:17:50 +0200184 if (!current_cpu) {
Scott Woodc3203fa2012-12-13 16:12:02 +0000185 return -1;
186 }
187
Andreas Färber4917cf42013-05-27 05:17:50 +0200188 return current_cpu->cpu_index;
Alexander Graf704c7e52011-07-21 01:33:29 +0200189}
190
Avi Kivitya8170e52012-10-23 12:30:10 +0200191static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
Alexander Graf704c7e52011-07-21 01:33:29 +0200192 int idx);
Avi Kivitya8170e52012-10-23 12:30:10 +0200193static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
Alexander Graf704c7e52011-07-21 01:33:29 +0200194 uint32_t val, int idx);
Paul Janzen8ebe65f2014-05-21 21:46:52 -0700195static void openpic_reset(DeviceState *d);
Alexander Graf704c7e52011-07-21 01:33:29 +0200196
Scott Wood6c5e84c2013-01-03 13:25:37 +0000197typedef enum IRQType {
198 IRQ_TYPE_NORMAL = 0,
199 IRQ_TYPE_FSLINT, /* FSL internal interrupt -- level only */
200 IRQ_TYPE_FSLSPECIAL, /* FSL timer/IPI interrupt, edge, no polarity */
201} IRQType;
202
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +0000203/* Round up to the nearest 64 IRQs so that the queue length
204 * won't change when moving between 32 and 64 bit hosts.
205 */
206#define IRQQUEUE_SIZE_BITS ((OPENPIC_MAX_IRQ + 63) & ~63)
207
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100208typedef struct IRQQueue {
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +0000209 unsigned long *queue;
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +0000210 int32_t queue_size; /* Only used for VMSTATE_BITMAP */
bellarddbda8082004-06-15 21:38:40 +0000211 int next;
212 int priority;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100213} IRQQueue;
bellarddbda8082004-06-15 21:38:40 +0000214
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100215typedef struct IRQSource {
Scott Woodbe7c2362012-12-21 16:15:42 +0000216 uint32_t ivpr; /* IRQ vector/priority register */
217 uint32_t idr; /* IRQ destination register */
Scott Wood5e22c272012-12-21 16:15:43 +0000218 uint32_t destmask; /* bitmap of CPU destinations */
bellarddbda8082004-06-15 21:38:40 +0000219 int last_cpu;
Scott Wood5e22c272012-12-21 16:15:43 +0000220 int output; /* IRQ level, e.g. OPENPIC_OUTPUT_INT */
bellard611493d2004-06-21 16:50:43 +0000221 int pending; /* TRUE if IRQ is pending */
Scott Wood6c5e84c2013-01-03 13:25:37 +0000222 IRQType type;
223 bool level:1; /* level-triggered */
Scott Wood72c1da22012-12-21 16:15:45 +0000224 bool nomask:1; /* critical interrupts ignore mask on some FSL MPICs */
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100225} IRQSource;
bellarddbda8082004-06-15 21:38:40 +0000226
Scott Woodbe7c2362012-12-21 16:15:42 +0000227#define IVPR_MASK_SHIFT 31
Peter Maydelldef60292014-03-17 16:00:36 +0000228#define IVPR_MASK_MASK (1U << IVPR_MASK_SHIFT)
Scott Woodbe7c2362012-12-21 16:15:42 +0000229#define IVPR_ACTIVITY_SHIFT 30
Peter Maydelldef60292014-03-17 16:00:36 +0000230#define IVPR_ACTIVITY_MASK (1U << IVPR_ACTIVITY_SHIFT)
Scott Woodbe7c2362012-12-21 16:15:42 +0000231#define IVPR_MODE_SHIFT 29
Peter Maydelldef60292014-03-17 16:00:36 +0000232#define IVPR_MODE_MASK (1U << IVPR_MODE_SHIFT)
Scott Woodbe7c2362012-12-21 16:15:42 +0000233#define IVPR_POLARITY_SHIFT 23
Peter Maydelldef60292014-03-17 16:00:36 +0000234#define IVPR_POLARITY_MASK (1U << IVPR_POLARITY_SHIFT)
Scott Woodbe7c2362012-12-21 16:15:42 +0000235#define IVPR_SENSE_SHIFT 22
Peter Maydelldef60292014-03-17 16:00:36 +0000236#define IVPR_SENSE_MASK (1U << IVPR_SENSE_SHIFT)
Alexander Graf1945dbc2012-12-08 01:49:52 +0100237
Peter Maydelldef60292014-03-17 16:00:36 +0000238#define IVPR_PRIORITY_MASK (0xFU << 16)
Scott Woodbe7c2362012-12-21 16:15:42 +0000239#define IVPR_PRIORITY(_ivprr_) ((int)(((_ivprr_) & IVPR_PRIORITY_MASK) >> 16))
240#define IVPR_VECTOR(opp, _ivprr_) ((_ivprr_) & (opp)->vector_mask)
bellarddbda8082004-06-15 21:38:40 +0000241
Scott Woodbe7c2362012-12-21 16:15:42 +0000242/* IDR[EP/CI] are only for FSL MPIC prior to v4.0 */
243#define IDR_EP 0x80000000 /* external pin */
244#define IDR_CI 0x40000000 /* critical interrupt */
Scott Wood71c6cac2012-12-13 16:11:59 +0000245
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +0000246typedef struct OpenPICTimer {
247 uint32_t tccr; /* Global timer current count register */
248 uint32_t tbcr; /* Global timer base count register */
249} OpenPICTimer;
250
251typedef struct OpenPICMSI {
252 uint32_t msir; /* Shared Message Signaled Interrupt Register */
253} OpenPICMSI;
254
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100255typedef struct IRQDest {
Scott Woodeb438422012-12-21 16:15:44 +0000256 int32_t ctpr; /* CPU current task priority */
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100257 IRQQueue raised;
258 IRQQueue servicing;
j_mayere9df0142007-04-09 22:45:36 +0000259 qemu_irq *irqs;
Scott Wood9f1d4b12013-01-03 13:25:40 +0000260
261 /* Count of IRQ sources asserting on non-INT outputs */
262 uint32_t outputs_active[OPENPIC_OUTPUT_NB];
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100263} IRQDest;
bellarddbda8082004-06-15 21:38:40 +0000264
Andreas Färbere1766342013-06-18 03:58:07 +0200265#define OPENPIC(obj) OBJECT_CHECK(OpenPICState, (obj), TYPE_OPENPIC)
266
Alexander Graf6d544ee2012-12-08 01:59:20 +0100267typedef struct OpenPICState {
Andreas Färbere1766342013-06-18 03:58:07 +0200268 /*< private >*/
269 SysBusDevice parent_obj;
270 /*< public >*/
271
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300272 MemoryRegion mem;
Fabien Chouteau71cf9e62011-08-30 17:46:26 +0200273
Alexander Graf5861a332012-12-07 23:51:09 +0100274 /* Behavior control */
Scott Woode0dfe5b2013-01-21 15:53:53 +0000275 FslMpicInfo *fsl;
Alexander Grafd0b72632012-12-08 05:17:14 +0100276 uint32_t model;
Alexander Graf5861a332012-12-07 23:51:09 +0100277 uint32_t flags;
Alexander Graf825463b2012-12-08 00:58:54 +0100278 uint32_t nb_irqs;
279 uint32_t vid;
Scott Woodbe7c2362012-12-21 16:15:42 +0000280 uint32_t vir; /* Vendor identification register */
Scott Wood0fe04622012-12-13 16:12:01 +0000281 uint32_t vector_mask;
Scott Woodbe7c2362012-12-21 16:15:42 +0000282 uint32_t tfrr_reset;
283 uint32_t ivpr_reset;
284 uint32_t idr_reset;
Alexander Grafdbbbfd62012-12-08 13:51:50 +0100285 uint32_t brr1;
Alexander Graf68c2dd72013-01-04 11:21:04 +0100286 uint32_t mpic_mode_mask;
Alexander Graf5861a332012-12-07 23:51:09 +0100287
Fabien Chouteau71cf9e62011-08-30 17:46:26 +0200288 /* Sub-regions */
Scott Woode0dfe5b2013-01-21 15:53:53 +0000289 MemoryRegion sub_io_mem[6];
Fabien Chouteau71cf9e62011-08-30 17:46:26 +0200290
bellarddbda8082004-06-15 21:38:40 +0000291 /* Global registers */
Scott Woodbe7c2362012-12-21 16:15:42 +0000292 uint32_t frr; /* Feature reporting register */
293 uint32_t gcr; /* Global configuration register */
294 uint32_t pir; /* Processor initialization register */
bellarddbda8082004-06-15 21:38:40 +0000295 uint32_t spve; /* Spurious vector register */
Scott Woodbe7c2362012-12-21 16:15:42 +0000296 uint32_t tfrr; /* Timer frequency reporting register */
bellarddbda8082004-06-15 21:38:40 +0000297 /* Source registers */
Scott Wood8935a442013-04-15 13:19:32 +0000298 IRQSource src[OPENPIC_MAX_IRQ];
bellarddbda8082004-06-15 21:38:40 +0000299 /* Local registers per output pin */
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100300 IRQDest dst[MAX_CPU];
Alexander Grafd0b72632012-12-08 05:17:14 +0100301 uint32_t nb_cpus;
bellarddbda8082004-06-15 21:38:40 +0000302 /* Timer registers */
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +0000303 OpenPICTimer timers[OPENPIC_MAX_TMR];
Alexander Graf732aa6e2012-12-08 14:18:00 +0100304 /* Shared MSI registers */
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +0000305 OpenPICMSI msi[MAX_MSI];
Alexander Grafd0b72632012-12-08 05:17:14 +0100306 uint32_t max_irq;
307 uint32_t irq_ipi0;
308 uint32_t irq_tim0;
Alexander Graf732aa6e2012-12-08 14:18:00 +0100309 uint32_t irq_msi;
Alexander Graf6d544ee2012-12-08 01:59:20 +0100310} OpenPICState;
bellarddbda8082004-06-15 21:38:40 +0000311
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100312static inline void IRQ_setbit(IRQQueue *q, int n_IRQ)
bellarddbda8082004-06-15 21:38:40 +0000313{
Scott Woode69a17f2012-12-21 16:15:48 +0000314 set_bit(n_IRQ, q->queue);
bellarddbda8082004-06-15 21:38:40 +0000315}
316
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100317static inline void IRQ_resetbit(IRQQueue *q, int n_IRQ)
bellarddbda8082004-06-15 21:38:40 +0000318{
Scott Woode69a17f2012-12-21 16:15:48 +0000319 clear_bit(n_IRQ, q->queue);
bellarddbda8082004-06-15 21:38:40 +0000320}
321
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100322static void IRQ_check(OpenPICState *opp, IRQQueue *q)
bellarddbda8082004-06-15 21:38:40 +0000323{
Scott Wood4417c732013-01-03 13:25:38 +0000324 int irq = -1;
325 int next = -1;
326 int priority = -1;
bellarddbda8082004-06-15 21:38:40 +0000327
Scott Wood4417c732013-01-03 13:25:38 +0000328 for (;;) {
329 irq = find_next_bit(q->queue, opp->max_irq, irq + 1);
330 if (irq == opp->max_irq) {
331 break;
332 }
Alexander Graf76aec1f2012-12-13 12:48:14 +0100333
Scott Wood4417c732013-01-03 13:25:38 +0000334 DPRINTF("IRQ_check: irq %d set ivpr_pr=%d pr=%d\n",
335 irq, IVPR_PRIORITY(opp->src[irq].ivpr), priority);
Alexander Graf76aec1f2012-12-13 12:48:14 +0100336
Scott Wood4417c732013-01-03 13:25:38 +0000337 if (IVPR_PRIORITY(opp->src[irq].ivpr) > priority) {
338 next = irq;
339 priority = IVPR_PRIORITY(opp->src[irq].ivpr);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100340 }
bellarddbda8082004-06-15 21:38:40 +0000341 }
Alexander Graf76aec1f2012-12-13 12:48:14 +0100342
bellarddbda8082004-06-15 21:38:40 +0000343 q->next = next;
344 q->priority = priority;
345}
346
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100347static int IRQ_get_next(OpenPICState *opp, IRQQueue *q)
bellarddbda8082004-06-15 21:38:40 +0000348{
Scott Wood3c943782012-12-21 16:15:46 +0000349 /* XXX: optimize */
350 IRQ_check(opp, q);
bellarddbda8082004-06-15 21:38:40 +0000351
352 return q->next;
353}
354
Scott Wood9f1d4b12013-01-03 13:25:40 +0000355static void IRQ_local_pipe(OpenPICState *opp, int n_CPU, int n_IRQ,
356 bool active, bool was_active)
bellarddbda8082004-06-15 21:38:40 +0000357{
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100358 IRQDest *dst;
359 IRQSource *src;
bellarddbda8082004-06-15 21:38:40 +0000360 int priority;
361
362 dst = &opp->dst[n_CPU];
363 src = &opp->src[n_IRQ];
Scott Wood5e22c272012-12-21 16:15:43 +0000364
Scott Wood9f1d4b12013-01-03 13:25:40 +0000365 DPRINTF("%s: IRQ %d active %d was %d\n",
366 __func__, n_IRQ, active, was_active);
367
Scott Wood5e22c272012-12-21 16:15:43 +0000368 if (src->output != OPENPIC_OUTPUT_INT) {
Scott Wood9f1d4b12013-01-03 13:25:40 +0000369 DPRINTF("%s: output %d irq %d active %d was %d count %d\n",
370 __func__, src->output, n_IRQ, active, was_active,
371 dst->outputs_active[src->output]);
372
Scott Wood5e22c272012-12-21 16:15:43 +0000373 /* On Freescale MPIC, critical interrupts ignore priority,
374 * IACK, EOI, etc. Before MPIC v4.1 they also ignore
375 * masking.
376 */
Scott Wood9f1d4b12013-01-03 13:25:40 +0000377 if (active) {
378 if (!was_active && dst->outputs_active[src->output]++ == 0) {
379 DPRINTF("%s: Raise OpenPIC output %d cpu %d irq %d\n",
380 __func__, src->output, n_CPU, n_IRQ);
381 qemu_irq_raise(dst->irqs[src->output]);
382 }
383 } else {
384 if (was_active && --dst->outputs_active[src->output] == 0) {
385 DPRINTF("%s: Lower OpenPIC output %d cpu %d irq %d\n",
386 __func__, src->output, n_CPU, n_IRQ);
387 qemu_irq_lower(dst->irqs[src->output]);
388 }
389 }
390
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100391 return;
bellarddbda8082004-06-15 21:38:40 +0000392 }
Scott Wood5e22c272012-12-21 16:15:43 +0000393
Scott Woodbe7c2362012-12-21 16:15:42 +0000394 priority = IVPR_PRIORITY(src->ivpr);
Scott Wood9f1d4b12013-01-03 13:25:40 +0000395
396 /* Even if the interrupt doesn't have enough priority,
397 * it is still raised, in case ctpr is lowered later.
398 */
399 if (active) {
400 IRQ_setbit(&dst->raised, n_IRQ);
401 } else {
402 IRQ_resetbit(&dst->raised, n_IRQ);
bellarddbda8082004-06-15 21:38:40 +0000403 }
Scott Wood9f1d4b12013-01-03 13:25:40 +0000404
Scott Wood3c943782012-12-21 16:15:46 +0000405 IRQ_check(opp, &dst->raised);
Scott Wood9f1d4b12013-01-03 13:25:40 +0000406
407 if (active && priority <= dst->ctpr) {
408 DPRINTF("%s: IRQ %d priority %d too low for ctpr %d on CPU %d\n",
409 __func__, n_IRQ, priority, dst->ctpr, n_CPU);
410 active = 0;
bellarddbda8082004-06-15 21:38:40 +0000411 }
Scott Wood9f1d4b12013-01-03 13:25:40 +0000412
413 if (active) {
414 if (IRQ_get_next(opp, &dst->servicing) >= 0 &&
415 priority <= dst->servicing.priority) {
416 DPRINTF("%s: IRQ %d is hidden by servicing IRQ %d on CPU %d\n",
417 __func__, n_IRQ, dst->servicing.next, n_CPU);
418 } else {
419 DPRINTF("%s: Raise OpenPIC INT output cpu %d irq %d/%d\n",
420 __func__, n_CPU, n_IRQ, dst->raised.next);
421 qemu_irq_raise(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_INT]);
422 }
423 } else {
424 IRQ_get_next(opp, &dst->servicing);
425 if (dst->raised.priority > dst->ctpr &&
426 dst->raised.priority > dst->servicing.priority) {
427 DPRINTF("%s: IRQ %d inactive, IRQ %d prio %d above %d/%d, CPU %d\n",
428 __func__, n_IRQ, dst->raised.next, dst->raised.priority,
429 dst->ctpr, dst->servicing.priority, n_CPU);
430 /* IRQ line stays asserted */
431 } else {
432 DPRINTF("%s: IRQ %d inactive, current prio %d/%d, CPU %d\n",
433 __func__, n_IRQ, dst->ctpr, dst->servicing.priority, n_CPU);
434 qemu_irq_lower(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_INT]);
435 }
j_mayere9df0142007-04-09 22:45:36 +0000436 }
bellarddbda8082004-06-15 21:38:40 +0000437}
438
bellard611493d2004-06-21 16:50:43 +0000439/* update pic state because registers for n_IRQ have changed value */
Alexander Graf6d544ee2012-12-08 01:59:20 +0100440static void openpic_update_irq(OpenPICState *opp, int n_IRQ)
bellarddbda8082004-06-15 21:38:40 +0000441{
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100442 IRQSource *src;
Scott Wood9f1d4b12013-01-03 13:25:40 +0000443 bool active, was_active;
bellarddbda8082004-06-15 21:38:40 +0000444 int i;
445
446 src = &opp->src[n_IRQ];
Scott Wood9f1d4b12013-01-03 13:25:40 +0000447 active = src->pending;
bellard611493d2004-06-21 16:50:43 +0000448
Scott Wood72c1da22012-12-21 16:15:45 +0000449 if ((src->ivpr & IVPR_MASK_MASK) && !src->nomask) {
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100450 /* Interrupt source is disabled */
j_mayere9df0142007-04-09 22:45:36 +0000451 DPRINTF("%s: IRQ %d is disabled\n", __func__, n_IRQ);
Scott Wood9f1d4b12013-01-03 13:25:40 +0000452 active = false;
453 }
454
455 was_active = !!(src->ivpr & IVPR_ACTIVITY_MASK);
456
457 /*
458 * We don't have a similar check for already-active because
459 * ctpr may have changed and we need to withdraw the interrupt.
460 */
461 if (!active && !was_active) {
462 DPRINTF("%s: IRQ %d is already inactive\n", __func__, n_IRQ);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100463 return;
bellarddbda8082004-06-15 21:38:40 +0000464 }
Scott Wood9f1d4b12013-01-03 13:25:40 +0000465
466 if (active) {
467 src->ivpr |= IVPR_ACTIVITY_MASK;
468 } else {
469 src->ivpr &= ~IVPR_ACTIVITY_MASK;
bellarddbda8082004-06-15 21:38:40 +0000470 }
Scott Wood9f1d4b12013-01-03 13:25:40 +0000471
Scott Woodf40c3602013-01-21 15:53:51 +0000472 if (src->destmask == 0) {
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100473 /* No target */
j_mayere9df0142007-04-09 22:45:36 +0000474 DPRINTF("%s: IRQ %d has no target\n", __func__, n_IRQ);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100475 return;
bellarddbda8082004-06-15 21:38:40 +0000476 }
bellard611493d2004-06-21 16:50:43 +0000477
Scott Woodf40c3602013-01-21 15:53:51 +0000478 if (src->destmask == (1 << src->last_cpu)) {
j_mayere9df0142007-04-09 22:45:36 +0000479 /* Only one CPU is allowed to receive this IRQ */
Scott Wood9f1d4b12013-01-03 13:25:40 +0000480 IRQ_local_pipe(opp, src->last_cpu, n_IRQ, active, was_active);
Scott Woodbe7c2362012-12-21 16:15:42 +0000481 } else if (!(src->ivpr & IVPR_MODE_MASK)) {
bellard611493d2004-06-21 16:50:43 +0000482 /* Directed delivery mode */
483 for (i = 0; i < opp->nb_cpus; i++) {
Scott Wood5e22c272012-12-21 16:15:43 +0000484 if (src->destmask & (1 << i)) {
Scott Wood9f1d4b12013-01-03 13:25:40 +0000485 IRQ_local_pipe(opp, i, n_IRQ, active, was_active);
Alexander Graf1945dbc2012-12-08 01:49:52 +0100486 }
bellard611493d2004-06-21 16:50:43 +0000487 }
bellarddbda8082004-06-15 21:38:40 +0000488 } else {
bellard611493d2004-06-21 16:50:43 +0000489 /* Distributed delivery mode */
j_mayere9df0142007-04-09 22:45:36 +0000490 for (i = src->last_cpu + 1; i != src->last_cpu; i++) {
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100491 if (i == opp->nb_cpus) {
bellard611493d2004-06-21 16:50:43 +0000492 i = 0;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100493 }
Scott Wood5e22c272012-12-21 16:15:43 +0000494 if (src->destmask & (1 << i)) {
Scott Wood9f1d4b12013-01-03 13:25:40 +0000495 IRQ_local_pipe(opp, i, n_IRQ, active, was_active);
bellard611493d2004-06-21 16:50:43 +0000496 src->last_cpu = i;
497 break;
498 }
499 }
bellarddbda8082004-06-15 21:38:40 +0000500 }
501}
502
pbrookd537cf62007-04-07 18:14:41 +0000503static void openpic_set_irq(void *opaque, int n_IRQ, int level)
bellard611493d2004-06-21 16:50:43 +0000504{
Alexander Graf6d544ee2012-12-08 01:59:20 +0100505 OpenPICState *opp = opaque;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100506 IRQSource *src;
bellard611493d2004-06-21 16:50:43 +0000507
Scott Wood8935a442013-04-15 13:19:32 +0000508 if (n_IRQ >= OPENPIC_MAX_IRQ) {
Scott Wood65b9d0d2012-12-21 16:15:50 +0000509 fprintf(stderr, "%s: IRQ %d out of range\n", __func__, n_IRQ);
510 abort();
511 }
bellard611493d2004-06-21 16:50:43 +0000512
513 src = &opp->src[n_IRQ];
Scott Woodbe7c2362012-12-21 16:15:42 +0000514 DPRINTF("openpic: set irq %d = %d ivpr=0x%08x\n",
515 n_IRQ, level, src->ivpr);
Scott Wood6c5e84c2013-01-03 13:25:37 +0000516 if (src->level) {
bellard611493d2004-06-21 16:50:43 +0000517 /* level-sensitive irq */
518 src->pending = level;
Scott Wood9f1d4b12013-01-03 13:25:40 +0000519 openpic_update_irq(opp, n_IRQ);
bellard611493d2004-06-21 16:50:43 +0000520 } else {
521 /* edge-sensitive irq */
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100522 if (level) {
bellard611493d2004-06-21 16:50:43 +0000523 src->pending = 1;
Scott Wood9f1d4b12013-01-03 13:25:40 +0000524 openpic_update_irq(opp, n_IRQ);
525 }
526
527 if (src->output != OPENPIC_OUTPUT_INT) {
528 /* Edge-triggered interrupts shouldn't be used
529 * with non-INT delivery, but just in case,
530 * try to make it do something sane rather than
531 * cause an interrupt storm. This is close to
532 * what you'd probably see happen in real hardware.
533 */
534 src->pending = 0;
535 openpic_update_irq(opp, n_IRQ);
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100536 }
bellard611493d2004-06-21 16:50:43 +0000537 }
bellard611493d2004-06-21 16:50:43 +0000538}
539
Scott Woodbe7c2362012-12-21 16:15:42 +0000540static inline uint32_t read_IRQreg_idr(OpenPICState *opp, int n_IRQ)
bellarddbda8082004-06-15 21:38:40 +0000541{
Scott Woodbe7c2362012-12-21 16:15:42 +0000542 return opp->src[n_IRQ].idr;
bellarddbda8082004-06-15 21:38:40 +0000543}
544
Scott Woode0dfe5b2013-01-21 15:53:53 +0000545static inline uint32_t read_IRQreg_ilr(OpenPICState *opp, int n_IRQ)
546{
547 if (opp->flags & OPENPIC_FLAG_ILR) {
548 return output_to_inttgt(opp->src[n_IRQ].output);
549 }
550
551 return 0xffffffff;
552}
553
Scott Woodbe7c2362012-12-21 16:15:42 +0000554static inline uint32_t read_IRQreg_ivpr(OpenPICState *opp, int n_IRQ)
Alexander Graf8d3a8c12011-09-07 13:41:54 +0200555{
Scott Woodbe7c2362012-12-21 16:15:42 +0000556 return opp->src[n_IRQ].ivpr;
bellarddbda8082004-06-15 21:38:40 +0000557}
558
Scott Woodbe7c2362012-12-21 16:15:42 +0000559static inline void write_IRQreg_idr(OpenPICState *opp, int n_IRQ, uint32_t val)
bellarddbda8082004-06-15 21:38:40 +0000560{
Scott Wood5e22c272012-12-21 16:15:43 +0000561 IRQSource *src = &opp->src[n_IRQ];
562 uint32_t normal_mask = (1UL << opp->nb_cpus) - 1;
563 uint32_t crit_mask = 0;
564 uint32_t mask = normal_mask;
565 int crit_shift = IDR_EP_SHIFT - opp->nb_cpus;
566 int i;
bellarddbda8082004-06-15 21:38:40 +0000567
Scott Wood5e22c272012-12-21 16:15:43 +0000568 if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
569 crit_mask = mask << crit_shift;
570 mask |= crit_mask | IDR_EP;
571 }
572
573 src->idr = val & mask;
574 DPRINTF("Set IDR %d to 0x%08x\n", n_IRQ, src->idr);
575
576 if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
577 if (src->idr & crit_mask) {
578 if (src->idr & normal_mask) {
579 DPRINTF("%s: IRQ configured for multiple output types, using "
580 "critical\n", __func__);
581 }
582
583 src->output = OPENPIC_OUTPUT_CINT;
Scott Wood72c1da22012-12-21 16:15:45 +0000584 src->nomask = true;
Scott Wood5e22c272012-12-21 16:15:43 +0000585 src->destmask = 0;
586
587 for (i = 0; i < opp->nb_cpus; i++) {
588 int n_ci = IDR_CI0_SHIFT - i;
589
590 if (src->idr & (1UL << n_ci)) {
591 src->destmask |= 1UL << i;
592 }
593 }
594 } else {
595 src->output = OPENPIC_OUTPUT_INT;
Scott Wood72c1da22012-12-21 16:15:45 +0000596 src->nomask = false;
Scott Wood5e22c272012-12-21 16:15:43 +0000597 src->destmask = src->idr & normal_mask;
598 }
599 } else {
600 src->destmask = src->idr;
601 }
Alexander Graf11de8b72011-09-07 13:47:22 +0200602}
603
Scott Woode0dfe5b2013-01-21 15:53:53 +0000604static inline void write_IRQreg_ilr(OpenPICState *opp, int n_IRQ, uint32_t val)
605{
606 if (opp->flags & OPENPIC_FLAG_ILR) {
607 IRQSource *src = &opp->src[n_IRQ];
608
609 src->output = inttgt_to_output(val & ILR_INTTGT_MASK);
610 DPRINTF("Set ILR %d to 0x%08x, output %d\n", n_IRQ, src->idr,
611 src->output);
612
613 /* TODO: on MPIC v4.0 only, set nomask for non-INT */
614 }
615}
616
Scott Woodbe7c2362012-12-21 16:15:42 +0000617static inline void write_IRQreg_ivpr(OpenPICState *opp, int n_IRQ, uint32_t val)
Alexander Graf11de8b72011-09-07 13:47:22 +0200618{
Scott Wood6c5e84c2013-01-03 13:25:37 +0000619 uint32_t mask;
620
621 /* NOTE when implementing newer FSL MPIC models: starting with v4.0,
622 * the polarity bit is read-only on internal interrupts.
623 */
624 mask = IVPR_MASK_MASK | IVPR_PRIORITY_MASK | IVPR_SENSE_MASK |
625 IVPR_POLARITY_MASK | opp->vector_mask;
626
Alexander Graf11de8b72011-09-07 13:47:22 +0200627 /* ACTIVITY bit is read-only */
Scott Wood6c5e84c2013-01-03 13:25:37 +0000628 opp->src[n_IRQ].ivpr =
629 (opp->src[n_IRQ].ivpr & IVPR_ACTIVITY_MASK) | (val & mask);
630
631 /* For FSL internal interrupts, The sense bit is reserved and zero,
632 * and the interrupt is always level-triggered. Timers and IPIs
633 * have no sense or polarity bits, and are edge-triggered.
634 */
635 switch (opp->src[n_IRQ].type) {
636 case IRQ_TYPE_NORMAL:
637 opp->src[n_IRQ].level = !!(opp->src[n_IRQ].ivpr & IVPR_SENSE_MASK);
638 break;
639
640 case IRQ_TYPE_FSLINT:
641 opp->src[n_IRQ].ivpr &= ~IVPR_SENSE_MASK;
642 break;
643
644 case IRQ_TYPE_FSLSPECIAL:
645 opp->src[n_IRQ].ivpr &= ~(IVPR_POLARITY_MASK | IVPR_SENSE_MASK);
646 break;
647 }
648
Alexander Graf11de8b72011-09-07 13:47:22 +0200649 openpic_update_irq(opp, n_IRQ);
Scott Woodbe7c2362012-12-21 16:15:42 +0000650 DPRINTF("Set IVPR %d to 0x%08x -> 0x%08x\n", n_IRQ, val,
651 opp->src[n_IRQ].ivpr);
bellarddbda8082004-06-15 21:38:40 +0000652}
653
Alexander Graf7f115732013-01-07 20:13:52 +0100654static void openpic_gcr_write(OpenPICState *opp, uint64_t val)
655{
Alexander Grafe49798b2013-01-17 11:32:21 +0100656 bool mpic_proxy = false;
Alexander Graf1ac3d712013-01-07 20:15:28 +0100657
Alexander Graf7f115732013-01-07 20:13:52 +0100658 if (val & GCR_RESET) {
Andreas Färbere1766342013-06-18 03:58:07 +0200659 openpic_reset(DEVICE(opp));
Alexander Graf1ac3d712013-01-07 20:15:28 +0100660 return;
661 }
Alexander Graf7f115732013-01-07 20:13:52 +0100662
Alexander Graf1ac3d712013-01-07 20:15:28 +0100663 opp->gcr &= ~opp->mpic_mode_mask;
664 opp->gcr |= val & opp->mpic_mode_mask;
Alexander Graf7f115732013-01-07 20:13:52 +0100665
Alexander Graf1ac3d712013-01-07 20:15:28 +0100666 /* Set external proxy mode */
667 if ((val & opp->mpic_mode_mask) == GCR_MODE_PROXY) {
Alexander Grafe49798b2013-01-17 11:32:21 +0100668 mpic_proxy = true;
Alexander Graf1ac3d712013-01-07 20:15:28 +0100669 }
Alexander Grafe49798b2013-01-17 11:32:21 +0100670
671 ppce500_set_mpic_proxy(mpic_proxy);
Alexander Graf7f115732013-01-07 20:13:52 +0100672}
673
Alexander Grafb9b2aaa2012-12-07 16:31:55 +0100674static void openpic_gbl_write(void *opaque, hwaddr addr, uint64_t val,
675 unsigned len)
bellarddbda8082004-06-15 21:38:40 +0000676{
Alexander Graf6d544ee2012-12-08 01:59:20 +0100677 OpenPICState *opp = opaque;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100678 IRQDest *dst;
j_mayere9df0142007-04-09 22:45:36 +0000679 int idx;
bellarddbda8082004-06-15 21:38:40 +0000680
Scott Wood4c4f0e42012-12-21 16:15:38 +0000681 DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
682 __func__, addr, val);
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100683 if (addr & 0xF) {
bellarddbda8082004-06-15 21:38:40 +0000684 return;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100685 }
bellarddbda8082004-06-15 21:38:40 +0000686 switch (addr) {
Bharat Bhushan3e772232012-08-14 04:30:55 +0000687 case 0x00: /* Block Revision Register1 (BRR1) is Readonly */
688 break;
Alexander Graf704c7e52011-07-21 01:33:29 +0200689 case 0x40:
690 case 0x50:
691 case 0x60:
692 case 0x70:
693 case 0x80:
694 case 0x90:
695 case 0xA0:
696 case 0xB0:
697 openpic_cpu_write_internal(opp, addr, val, get_current_cpu());
bellarddbda8082004-06-15 21:38:40 +0000698 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000699 case 0x1000: /* FRR */
Alexander Graf704c7e52011-07-21 01:33:29 +0200700 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000701 case 0x1020: /* GCR */
Alexander Graf7f115732013-01-07 20:13:52 +0100702 openpic_gcr_write(opp, val);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100703 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000704 case 0x1080: /* VIR */
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100705 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000706 case 0x1090: /* PIR */
j_mayere9df0142007-04-09 22:45:36 +0000707 for (idx = 0; idx < opp->nb_cpus; idx++) {
Scott Woodbe7c2362012-12-21 16:15:42 +0000708 if ((val & (1 << idx)) && !(opp->pir & (1 << idx))) {
j_mayere9df0142007-04-09 22:45:36 +0000709 DPRINTF("Raise OpenPIC RESET output for CPU %d\n", idx);
710 dst = &opp->dst[idx];
711 qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_RESET]);
Scott Woodbe7c2362012-12-21 16:15:42 +0000712 } else if (!(val & (1 << idx)) && (opp->pir & (1 << idx))) {
j_mayere9df0142007-04-09 22:45:36 +0000713 DPRINTF("Lower OpenPIC RESET output for CPU %d\n", idx);
714 dst = &opp->dst[idx];
715 qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_RESET]);
716 }
bellarddbda8082004-06-15 21:38:40 +0000717 }
Scott Woodbe7c2362012-12-21 16:15:42 +0000718 opp->pir = val;
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100719 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000720 case 0x10A0: /* IPI_IVPR */
Alexander Graf704c7e52011-07-21 01:33:29 +0200721 case 0x10B0:
722 case 0x10C0:
723 case 0x10D0:
bellarddbda8082004-06-15 21:38:40 +0000724 {
725 int idx;
Alexander Graf704c7e52011-07-21 01:33:29 +0200726 idx = (addr - 0x10A0) >> 4;
Scott Woodbe7c2362012-12-21 16:15:42 +0000727 write_IRQreg_ivpr(opp, opp->irq_ipi0 + idx, val);
bellarddbda8082004-06-15 21:38:40 +0000728 }
729 break;
Alexander Graf704c7e52011-07-21 01:33:29 +0200730 case 0x10E0: /* SPVE */
Scott Wood0fe04622012-12-13 16:12:01 +0000731 opp->spve = val & opp->vector_mask;
bellarddbda8082004-06-15 21:38:40 +0000732 break;
bellarddbda8082004-06-15 21:38:40 +0000733 default:
734 break;
735 }
736}
737
Alexander Grafb9b2aaa2012-12-07 16:31:55 +0100738static uint64_t openpic_gbl_read(void *opaque, hwaddr addr, unsigned len)
bellarddbda8082004-06-15 21:38:40 +0000739{
Alexander Graf6d544ee2012-12-08 01:59:20 +0100740 OpenPICState *opp = opaque;
bellarddbda8082004-06-15 21:38:40 +0000741 uint32_t retval;
742
Scott Wood4c4f0e42012-12-21 16:15:38 +0000743 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
bellarddbda8082004-06-15 21:38:40 +0000744 retval = 0xFFFFFFFF;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100745 if (addr & 0xF) {
bellarddbda8082004-06-15 21:38:40 +0000746 return retval;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100747 }
bellarddbda8082004-06-15 21:38:40 +0000748 switch (addr) {
Scott Woodbe7c2362012-12-21 16:15:42 +0000749 case 0x1000: /* FRR */
750 retval = opp->frr;
bellarddbda8082004-06-15 21:38:40 +0000751 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000752 case 0x1020: /* GCR */
753 retval = opp->gcr;
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100754 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000755 case 0x1080: /* VIR */
756 retval = opp->vir;
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100757 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000758 case 0x1090: /* PIR */
bellarddbda8082004-06-15 21:38:40 +0000759 retval = 0x00000000;
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100760 break;
Bharat Bhushan3e772232012-08-14 04:30:55 +0000761 case 0x00: /* Block Revision Register1 (BRR1) */
Scott Wood0d404682012-12-13 16:12:03 +0000762 retval = opp->brr1;
763 break;
Alexander Graf704c7e52011-07-21 01:33:29 +0200764 case 0x40:
765 case 0x50:
766 case 0x60:
767 case 0x70:
768 case 0x80:
769 case 0x90:
770 case 0xA0:
bellarddbda8082004-06-15 21:38:40 +0000771 case 0xB0:
Alexander Graf704c7e52011-07-21 01:33:29 +0200772 retval = openpic_cpu_read_internal(opp, addr, get_current_cpu());
773 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000774 case 0x10A0: /* IPI_IVPR */
Alexander Graf704c7e52011-07-21 01:33:29 +0200775 case 0x10B0:
776 case 0x10C0:
777 case 0x10D0:
bellarddbda8082004-06-15 21:38:40 +0000778 {
779 int idx;
Alexander Graf704c7e52011-07-21 01:33:29 +0200780 idx = (addr - 0x10A0) >> 4;
Scott Woodbe7c2362012-12-21 16:15:42 +0000781 retval = read_IRQreg_ivpr(opp, opp->irq_ipi0 + idx);
bellarddbda8082004-06-15 21:38:40 +0000782 }
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100783 break;
Alexander Graf704c7e52011-07-21 01:33:29 +0200784 case 0x10E0: /* SPVE */
bellarddbda8082004-06-15 21:38:40 +0000785 retval = opp->spve;
786 break;
bellarddbda8082004-06-15 21:38:40 +0000787 default:
788 break;
789 }
Scott Wood4c4f0e42012-12-21 16:15:38 +0000790 DPRINTF("%s: => 0x%08x\n", __func__, retval);
bellarddbda8082004-06-15 21:38:40 +0000791
792 return retval;
793}
794
Alexander Graf6d544ee2012-12-08 01:59:20 +0100795static void openpic_tmr_write(void *opaque, hwaddr addr, uint64_t val,
Alexander Grafb9b2aaa2012-12-07 16:31:55 +0100796 unsigned len)
bellarddbda8082004-06-15 21:38:40 +0000797{
Alexander Graf6d544ee2012-12-08 01:59:20 +0100798 OpenPICState *opp = opaque;
bellarddbda8082004-06-15 21:38:40 +0000799 int idx;
800
Scott Wood03274d42013-01-21 15:53:52 +0000801 addr += 0x10f0;
802
Scott Wood4c4f0e42012-12-21 16:15:38 +0000803 DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
804 __func__, addr, val);
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100805 if (addr & 0xF) {
bellarddbda8082004-06-15 21:38:40 +0000806 return;
Alexander Grafaf7e9e72012-12-20 17:30:58 +0100807 }
Alexander Grafc38c0b82012-12-08 00:43:42 +0100808
Scott Wood03274d42013-01-21 15:53:52 +0000809 if (addr == 0x10f0) {
Scott Woodbe7c2362012-12-21 16:15:42 +0000810 /* TFRR */
811 opp->tfrr = val;
Alexander Grafc38c0b82012-12-08 00:43:42 +0100812 return;
813 }
Scott Wood03274d42013-01-21 15:53:52 +0000814
815 idx = (addr >> 6) & 0x3;
816 addr = addr & 0x30;
817
Alexander Grafc38c0b82012-12-08 00:43:42 +0100818 switch (addr & 0x30) {
Scott Woodbe7c2362012-12-21 16:15:42 +0000819 case 0x00: /* TCCR */
bellarddbda8082004-06-15 21:38:40 +0000820 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000821 case 0x10: /* TBCR */
822 if ((opp->timers[idx].tccr & TCCR_TOG) != 0 &&
823 (val & TBCR_CI) == 0 &&
824 (opp->timers[idx].tbcr & TBCR_CI) != 0) {
825 opp->timers[idx].tccr &= ~TCCR_TOG;
Scott Wood71c6cac2012-12-13 16:11:59 +0000826 }
Scott Woodbe7c2362012-12-21 16:15:42 +0000827 opp->timers[idx].tbcr = val;
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100828 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000829 case 0x20: /* TVPR */
830 write_IRQreg_ivpr(opp, opp->irq_tim0 + idx, val);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100831 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000832 case 0x30: /* TDR */
833 write_IRQreg_idr(opp, opp->irq_tim0 + idx, val);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100834 break;
bellarddbda8082004-06-15 21:38:40 +0000835 }
836}
837
Alexander Graf6d544ee2012-12-08 01:59:20 +0100838static uint64_t openpic_tmr_read(void *opaque, hwaddr addr, unsigned len)
bellarddbda8082004-06-15 21:38:40 +0000839{
Alexander Graf6d544ee2012-12-08 01:59:20 +0100840 OpenPICState *opp = opaque;
Alexander Grafc38c0b82012-12-08 00:43:42 +0100841 uint32_t retval = -1;
bellarddbda8082004-06-15 21:38:40 +0000842 int idx;
843
Scott Wood4c4f0e42012-12-21 16:15:38 +0000844 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
Alexander Grafc38c0b82012-12-08 00:43:42 +0100845 if (addr & 0xF) {
846 goto out;
847 }
848 idx = (addr >> 6) & 0x3;
849 if (addr == 0x0) {
Scott Woodbe7c2362012-12-21 16:15:42 +0000850 /* TFRR */
851 retval = opp->tfrr;
Alexander Grafc38c0b82012-12-08 00:43:42 +0100852 goto out;
853 }
854 switch (addr & 0x30) {
Scott Woodbe7c2362012-12-21 16:15:42 +0000855 case 0x00: /* TCCR */
856 retval = opp->timers[idx].tccr;
bellarddbda8082004-06-15 21:38:40 +0000857 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000858 case 0x10: /* TBCR */
859 retval = opp->timers[idx].tbcr;
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100860 break;
Scott Woodbe7c2362012-12-21 16:15:42 +0000861 case 0x20: /* TIPV */
862 retval = read_IRQreg_ivpr(opp, opp->irq_tim0 + idx);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100863 break;
Alexander Grafc38c0b82012-12-08 00:43:42 +0100864 case 0x30: /* TIDE (TIDR) */
Scott Woodbe7c2362012-12-21 16:15:42 +0000865 retval = read_IRQreg_idr(opp, opp->irq_tim0 + idx);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +0100866 break;
bellarddbda8082004-06-15 21:38:40 +0000867 }
Alexander Grafc38c0b82012-12-08 00:43:42 +0100868
869out:
Scott Wood4c4f0e42012-12-21 16:15:38 +0000870 DPRINTF("%s: => 0x%08x\n", __func__, retval);
bellarddbda8082004-06-15 21:38:40 +0000871
872 return retval;
873}
874
Alexander Grafb9b2aaa2012-12-07 16:31:55 +0100875static void openpic_src_write(void *opaque, hwaddr addr, uint64_t val,
876 unsigned len)
bellarddbda8082004-06-15 21:38:40 +0000877{
Alexander Graf6d544ee2012-12-08 01:59:20 +0100878 OpenPICState *opp = opaque;
bellarddbda8082004-06-15 21:38:40 +0000879 int idx;
880
Scott Wood4c4f0e42012-12-21 16:15:38 +0000881 DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
882 __func__, addr, val);
Scott Woode0dfe5b2013-01-21 15:53:53 +0000883
884 addr = addr & 0xffff;
bellarddbda8082004-06-15 21:38:40 +0000885 idx = addr >> 5;
Scott Woode0dfe5b2013-01-21 15:53:53 +0000886
887 switch (addr & 0x1f) {
888 case 0x00:
Scott Woodbe7c2362012-12-21 16:15:42 +0000889 write_IRQreg_ivpr(opp, idx, val);
Scott Woode0dfe5b2013-01-21 15:53:53 +0000890 break;
891 case 0x10:
892 write_IRQreg_idr(opp, idx, val);
893 break;
894 case 0x18:
895 write_IRQreg_ilr(opp, idx, val);
896 break;
bellarddbda8082004-06-15 21:38:40 +0000897 }
898}
899
Alexander Grafb9b2aaa2012-12-07 16:31:55 +0100900static uint64_t openpic_src_read(void *opaque, uint64_t addr, unsigned len)
bellarddbda8082004-06-15 21:38:40 +0000901{
Alexander Graf6d544ee2012-12-08 01:59:20 +0100902 OpenPICState *opp = opaque;
bellarddbda8082004-06-15 21:38:40 +0000903 uint32_t retval;
904 int idx;
905
Scott Wood4c4f0e42012-12-21 16:15:38 +0000906 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
bellarddbda8082004-06-15 21:38:40 +0000907 retval = 0xFFFFFFFF;
bellarddbda8082004-06-15 21:38:40 +0000908
Scott Woode0dfe5b2013-01-21 15:53:53 +0000909 addr = addr & 0xffff;
910 idx = addr >> 5;
911
912 switch (addr & 0x1f) {
913 case 0x00:
914 retval = read_IRQreg_ivpr(opp, idx);
915 break;
916 case 0x10:
917 retval = read_IRQreg_idr(opp, idx);
918 break;
919 case 0x18:
920 retval = read_IRQreg_ilr(opp, idx);
921 break;
922 }
923
924 DPRINTF("%s: => 0x%08x\n", __func__, retval);
bellarddbda8082004-06-15 21:38:40 +0000925 return retval;
926}
927
Alexander Graf732aa6e2012-12-08 14:18:00 +0100928static void openpic_msi_write(void *opaque, hwaddr addr, uint64_t val,
929 unsigned size)
930{
931 OpenPICState *opp = opaque;
932 int idx = opp->irq_msi;
933 int srs, ibs;
934
Scott Wood4c4f0e42012-12-21 16:15:38 +0000935 DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64 "\n",
936 __func__, addr, val);
Alexander Graf732aa6e2012-12-08 14:18:00 +0100937 if (addr & 0xF) {
938 return;
939 }
940
941 switch (addr) {
942 case MSIIR_OFFSET:
943 srs = val >> MSIIR_SRS_SHIFT;
944 idx += srs;
945 ibs = (val & MSIIR_IBS_MASK) >> MSIIR_IBS_SHIFT;
946 opp->msi[srs].msir |= 1 << ibs;
947 openpic_set_irq(opp, idx, 1);
948 break;
949 default:
950 /* most registers are read-only, thus ignored */
951 break;
952 }
953}
954
955static uint64_t openpic_msi_read(void *opaque, hwaddr addr, unsigned size)
956{
957 OpenPICState *opp = opaque;
958 uint64_t r = 0;
959 int i, srs;
960
Scott Wood4c4f0e42012-12-21 16:15:38 +0000961 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
Alexander Graf732aa6e2012-12-08 14:18:00 +0100962 if (addr & 0xF) {
963 return -1;
964 }
965
966 srs = addr >> 4;
967
968 switch (addr) {
969 case 0x00:
970 case 0x10:
971 case 0x20:
972 case 0x30:
973 case 0x40:
974 case 0x50:
975 case 0x60:
976 case 0x70: /* MSIRs */
977 r = opp->msi[srs].msir;
978 /* Clear on read */
979 opp->msi[srs].msir = 0;
Scott Woode99fd8a2012-12-21 16:15:39 +0000980 openpic_set_irq(opp, opp->irq_msi + srs, 0);
Alexander Graf732aa6e2012-12-08 14:18:00 +0100981 break;
982 case 0x120: /* MSISR */
983 for (i = 0; i < MAX_MSI; i++) {
984 r |= (opp->msi[i].msir ? 1 : 0) << i;
985 }
986 break;
987 }
988
989 return r;
990}
991
Scott Woode0dfe5b2013-01-21 15:53:53 +0000992static uint64_t openpic_summary_read(void *opaque, hwaddr addr, unsigned size)
993{
994 uint64_t r = 0;
995
996 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
997
998 /* TODO: EISR/EIMR */
999
1000 return r;
1001}
1002
1003static void openpic_summary_write(void *opaque, hwaddr addr, uint64_t val,
1004 unsigned size)
1005{
1006 DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64 "\n",
1007 __func__, addr, val);
1008
1009 /* TODO: EISR/EIMR */
1010}
1011
Avi Kivitya8170e52012-10-23 12:30:10 +02001012static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
Alexander Graf704c7e52011-07-21 01:33:29 +02001013 uint32_t val, int idx)
bellarddbda8082004-06-15 21:38:40 +00001014{
Alexander Graf6d544ee2012-12-08 01:59:20 +01001015 OpenPICState *opp = opaque;
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001016 IRQSource *src;
1017 IRQDest *dst;
Alexander Graf704c7e52011-07-21 01:33:29 +02001018 int s_IRQ, n_IRQ;
bellarddbda8082004-06-15 21:38:40 +00001019
Scott Wood4c4f0e42012-12-21 16:15:38 +00001020 DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx " <= 0x%08x\n", __func__, idx,
Alexander Graf704c7e52011-07-21 01:33:29 +02001021 addr, val);
Scott Woodc3203fa2012-12-13 16:12:02 +00001022
Fabien Chouteau04d2acb2015-02-25 10:50:28 +01001023 if (idx < 0 || idx >= opp->nb_cpus) {
bellarddbda8082004-06-15 21:38:40 +00001024 return;
Scott Woodc3203fa2012-12-13 16:12:02 +00001025 }
1026
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001027 if (addr & 0xF) {
bellarddbda8082004-06-15 21:38:40 +00001028 return;
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001029 }
bellarddbda8082004-06-15 21:38:40 +00001030 dst = &opp->dst[idx];
1031 addr &= 0xFF0;
1032 switch (addr) {
Alexander Graf704c7e52011-07-21 01:33:29 +02001033 case 0x40: /* IPIDR */
bellarddbda8082004-06-15 21:38:40 +00001034 case 0x50:
1035 case 0x60:
1036 case 0x70:
1037 idx = (addr - 0x40) >> 4;
Alexander Grafa6751552011-07-21 01:36:44 +02001038 /* we use IDE as mask which CPUs to deliver the IPI to still. */
Scott Woodf40c3602013-01-21 15:53:51 +00001039 opp->src[opp->irq_ipi0 + idx].destmask |= val;
aurel32b7169912009-03-02 16:42:04 +00001040 openpic_set_irq(opp, opp->irq_ipi0 + idx, 1);
1041 openpic_set_irq(opp, opp->irq_ipi0 + idx, 0);
bellarddbda8082004-06-15 21:38:40 +00001042 break;
Scott Woodbe7c2362012-12-21 16:15:42 +00001043 case 0x80: /* CTPR */
1044 dst->ctpr = val & 0x0000000F;
Scott Wood9f1d4b12013-01-03 13:25:40 +00001045
1046 DPRINTF("%s: set CPU %d ctpr to %d, raised %d servicing %d\n",
1047 __func__, idx, dst->ctpr, dst->raised.priority,
1048 dst->servicing.priority);
1049
1050 if (dst->raised.priority <= dst->ctpr) {
1051 DPRINTF("%s: Lower OpenPIC INT output cpu %d due to ctpr\n",
1052 __func__, idx);
1053 qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
1054 } else if (dst->raised.priority > dst->servicing.priority) {
1055 DPRINTF("%s: Raise OpenPIC INT output cpu %d irq %d\n",
1056 __func__, idx, dst->raised.next);
1057 qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_INT]);
1058 }
1059
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001060 break;
bellarddbda8082004-06-15 21:38:40 +00001061 case 0x90: /* WHOAMI */
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001062 /* Read-only register */
1063 break;
Scott Woodbe7c2362012-12-21 16:15:42 +00001064 case 0xA0: /* IACK */
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001065 /* Read-only register */
1066 break;
Scott Woodbe7c2362012-12-21 16:15:42 +00001067 case 0xB0: /* EOI */
1068 DPRINTF("EOI\n");
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001069 s_IRQ = IRQ_get_next(opp, &dst->servicing);
Scott Wood65b9d0d2012-12-21 16:15:50 +00001070
1071 if (s_IRQ < 0) {
1072 DPRINTF("%s: EOI with no interrupt in service\n", __func__);
1073 break;
1074 }
1075
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001076 IRQ_resetbit(&dst->servicing, s_IRQ);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001077 /* Set up next servicing IRQ */
1078 s_IRQ = IRQ_get_next(opp, &dst->servicing);
j_mayere9df0142007-04-09 22:45:36 +00001079 /* Check queued interrupts. */
1080 n_IRQ = IRQ_get_next(opp, &dst->raised);
1081 src = &opp->src[n_IRQ];
1082 if (n_IRQ != -1 &&
1083 (s_IRQ == -1 ||
Scott Woodbe7c2362012-12-21 16:15:42 +00001084 IVPR_PRIORITY(src->ivpr) > dst->servicing.priority)) {
j_mayere9df0142007-04-09 22:45:36 +00001085 DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n",
1086 idx, n_IRQ);
Scott Wood5e22c272012-12-21 16:15:43 +00001087 qemu_irq_raise(opp->dst[idx].irqs[OPENPIC_OUTPUT_INT]);
j_mayere9df0142007-04-09 22:45:36 +00001088 }
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001089 break;
bellarddbda8082004-06-15 21:38:40 +00001090 default:
1091 break;
1092 }
1093}
1094
Alexander Grafb9b2aaa2012-12-07 16:31:55 +01001095static void openpic_cpu_write(void *opaque, hwaddr addr, uint64_t val,
1096 unsigned len)
Alexander Graf704c7e52011-07-21 01:33:29 +02001097{
1098 openpic_cpu_write_internal(opaque, addr, val, (addr & 0x1f000) >> 12);
1099}
1100
Scott Wooda898a8f2013-01-03 13:25:39 +00001101
1102static uint32_t openpic_iack(OpenPICState *opp, IRQDest *dst, int cpu)
1103{
1104 IRQSource *src;
1105 int retval, irq;
1106
1107 DPRINTF("Lower OpenPIC INT output\n");
1108 qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
1109
1110 irq = IRQ_get_next(opp, &dst->raised);
1111 DPRINTF("IACK: irq=%d\n", irq);
1112
1113 if (irq == -1) {
1114 /* No more interrupt pending */
1115 return opp->spve;
1116 }
1117
1118 src = &opp->src[irq];
1119 if (!(src->ivpr & IVPR_ACTIVITY_MASK) ||
1120 !(IVPR_PRIORITY(src->ivpr) > dst->ctpr)) {
Scott Wood9f1d4b12013-01-03 13:25:40 +00001121 fprintf(stderr, "%s: bad raised IRQ %d ctpr %d ivpr 0x%08x\n",
1122 __func__, irq, dst->ctpr, src->ivpr);
1123 openpic_update_irq(opp, irq);
Scott Wooda898a8f2013-01-03 13:25:39 +00001124 retval = opp->spve;
1125 } else {
1126 /* IRQ enter servicing state */
1127 IRQ_setbit(&dst->servicing, irq);
1128 retval = IVPR_VECTOR(opp, src->ivpr);
1129 }
Scott Wood9f1d4b12013-01-03 13:25:40 +00001130
Scott Wooda898a8f2013-01-03 13:25:39 +00001131 if (!src->level) {
1132 /* edge-sensitive IRQ */
1133 src->ivpr &= ~IVPR_ACTIVITY_MASK;
1134 src->pending = 0;
Scott Wood9f1d4b12013-01-03 13:25:40 +00001135 IRQ_resetbit(&dst->raised, irq);
Scott Wooda898a8f2013-01-03 13:25:39 +00001136 }
1137
Scott Wood8935a442013-04-15 13:19:32 +00001138 if ((irq >= opp->irq_ipi0) && (irq < (opp->irq_ipi0 + OPENPIC_MAX_IPI))) {
Scott Woodf40c3602013-01-21 15:53:51 +00001139 src->destmask &= ~(1 << cpu);
1140 if (src->destmask && !src->level) {
Scott Wooda898a8f2013-01-03 13:25:39 +00001141 /* trigger on CPUs that didn't know about it yet */
1142 openpic_set_irq(opp, irq, 1);
1143 openpic_set_irq(opp, irq, 0);
1144 /* if all CPUs knew about it, set active bit again */
1145 src->ivpr |= IVPR_ACTIVITY_MASK;
1146 }
1147 }
1148
1149 return retval;
1150}
1151
Avi Kivitya8170e52012-10-23 12:30:10 +02001152static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
Alexander Graf704c7e52011-07-21 01:33:29 +02001153 int idx)
bellarddbda8082004-06-15 21:38:40 +00001154{
Alexander Graf6d544ee2012-12-08 01:59:20 +01001155 OpenPICState *opp = opaque;
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001156 IRQDest *dst;
bellarddbda8082004-06-15 21:38:40 +00001157 uint32_t retval;
ths3b46e622007-09-17 08:09:54 +00001158
Scott Wood4c4f0e42012-12-21 16:15:38 +00001159 DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx "\n", __func__, idx, addr);
bellarddbda8082004-06-15 21:38:40 +00001160 retval = 0xFFFFFFFF;
Scott Woodc3203fa2012-12-13 16:12:02 +00001161
Fabien Chouteau04d2acb2015-02-25 10:50:28 +01001162 if (idx < 0 || idx >= opp->nb_cpus) {
bellarddbda8082004-06-15 21:38:40 +00001163 return retval;
Scott Woodc3203fa2012-12-13 16:12:02 +00001164 }
1165
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001166 if (addr & 0xF) {
bellarddbda8082004-06-15 21:38:40 +00001167 return retval;
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001168 }
bellarddbda8082004-06-15 21:38:40 +00001169 dst = &opp->dst[idx];
1170 addr &= 0xFF0;
1171 switch (addr) {
Scott Woodbe7c2362012-12-21 16:15:42 +00001172 case 0x80: /* CTPR */
1173 retval = dst->ctpr;
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001174 break;
bellarddbda8082004-06-15 21:38:40 +00001175 case 0x90: /* WHOAMI */
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001176 retval = idx;
1177 break;
Scott Woodbe7c2362012-12-21 16:15:42 +00001178 case 0xA0: /* IACK */
Scott Wooda898a8f2013-01-03 13:25:39 +00001179 retval = openpic_iack(opp, dst, idx);
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001180 break;
Scott Woodbe7c2362012-12-21 16:15:42 +00001181 case 0xB0: /* EOI */
Aurelien Jarno060fbfe2009-12-19 15:59:29 +01001182 retval = 0;
1183 break;
bellarddbda8082004-06-15 21:38:40 +00001184 default:
1185 break;
1186 }
Scott Wood4c4f0e42012-12-21 16:15:38 +00001187 DPRINTF("%s: => 0x%08x\n", __func__, retval);
bellarddbda8082004-06-15 21:38:40 +00001188
1189 return retval;
1190}
1191
Alexander Grafb9b2aaa2012-12-07 16:31:55 +01001192static uint64_t openpic_cpu_read(void *opaque, hwaddr addr, unsigned len)
Alexander Graf704c7e52011-07-21 01:33:29 +02001193{
1194 return openpic_cpu_read_internal(opaque, addr, (addr & 0x1f000) >> 12);
1195}
1196
Alexander Graf35732cb2012-12-08 01:04:48 +01001197static const MemoryRegionOps openpic_glb_ops_le = {
Alexander Graf780d16b2012-12-07 17:15:15 +01001198 .write = openpic_gbl_write,
1199 .read = openpic_gbl_read,
1200 .endianness = DEVICE_LITTLE_ENDIAN,
1201 .impl = {
1202 .min_access_size = 4,
1203 .max_access_size = 4,
1204 },
1205};
bellarddbda8082004-06-15 21:38:40 +00001206
Alexander Graf35732cb2012-12-08 01:04:48 +01001207static const MemoryRegionOps openpic_glb_ops_be = {
1208 .write = openpic_gbl_write,
1209 .read = openpic_gbl_read,
1210 .endianness = DEVICE_BIG_ENDIAN,
1211 .impl = {
1212 .min_access_size = 4,
1213 .max_access_size = 4,
1214 },
1215};
1216
1217static const MemoryRegionOps openpic_tmr_ops_le = {
Alexander Graf6d544ee2012-12-08 01:59:20 +01001218 .write = openpic_tmr_write,
1219 .read = openpic_tmr_read,
Alexander Graf780d16b2012-12-07 17:15:15 +01001220 .endianness = DEVICE_LITTLE_ENDIAN,
1221 .impl = {
1222 .min_access_size = 4,
1223 .max_access_size = 4,
1224 },
1225};
bellarddbda8082004-06-15 21:38:40 +00001226
Alexander Graf35732cb2012-12-08 01:04:48 +01001227static const MemoryRegionOps openpic_tmr_ops_be = {
Alexander Graf6d544ee2012-12-08 01:59:20 +01001228 .write = openpic_tmr_write,
1229 .read = openpic_tmr_read,
Alexander Graf35732cb2012-12-08 01:04:48 +01001230 .endianness = DEVICE_BIG_ENDIAN,
1231 .impl = {
1232 .min_access_size = 4,
1233 .max_access_size = 4,
1234 },
1235};
1236
1237static const MemoryRegionOps openpic_cpu_ops_le = {
Alexander Graf780d16b2012-12-07 17:15:15 +01001238 .write = openpic_cpu_write,
1239 .read = openpic_cpu_read,
1240 .endianness = DEVICE_LITTLE_ENDIAN,
1241 .impl = {
1242 .min_access_size = 4,
1243 .max_access_size = 4,
1244 },
1245};
bellarddbda8082004-06-15 21:38:40 +00001246
Alexander Graf35732cb2012-12-08 01:04:48 +01001247static const MemoryRegionOps openpic_cpu_ops_be = {
1248 .write = openpic_cpu_write,
1249 .read = openpic_cpu_read,
1250 .endianness = DEVICE_BIG_ENDIAN,
1251 .impl = {
1252 .min_access_size = 4,
1253 .max_access_size = 4,
1254 },
1255};
1256
1257static const MemoryRegionOps openpic_src_ops_le = {
Alexander Graf780d16b2012-12-07 17:15:15 +01001258 .write = openpic_src_write,
1259 .read = openpic_src_read,
Avi Kivity23c5e4c2011-08-08 16:09:17 +03001260 .endianness = DEVICE_LITTLE_ENDIAN,
Alexander Grafb9b2aaa2012-12-07 16:31:55 +01001261 .impl = {
1262 .min_access_size = 4,
1263 .max_access_size = 4,
1264 },
Avi Kivity23c5e4c2011-08-08 16:09:17 +03001265};
1266
Alexander Graf35732cb2012-12-08 01:04:48 +01001267static const MemoryRegionOps openpic_src_ops_be = {
1268 .write = openpic_src_write,
1269 .read = openpic_src_read,
1270 .endianness = DEVICE_BIG_ENDIAN,
1271 .impl = {
1272 .min_access_size = 4,
1273 .max_access_size = 4,
1274 },
1275};
1276
Scott Woode0dfe5b2013-01-21 15:53:53 +00001277static const MemoryRegionOps openpic_msi_ops_be = {
Alexander Graf732aa6e2012-12-08 14:18:00 +01001278 .read = openpic_msi_read,
1279 .write = openpic_msi_write,
Scott Woode0dfe5b2013-01-21 15:53:53 +00001280 .endianness = DEVICE_BIG_ENDIAN,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001281 .impl = {
1282 .min_access_size = 4,
1283 .max_access_size = 4,
1284 },
1285};
1286
Scott Woode0dfe5b2013-01-21 15:53:53 +00001287static const MemoryRegionOps openpic_summary_ops_be = {
1288 .read = openpic_summary_read,
1289 .write = openpic_summary_write,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001290 .endianness = DEVICE_BIG_ENDIAN,
1291 .impl = {
1292 .min_access_size = 4,
1293 .max_access_size = 4,
1294 },
1295};
1296
Paul Janzen8ebe65f2014-05-21 21:46:52 -07001297static void openpic_reset(DeviceState *d)
1298{
1299 OpenPICState *opp = OPENPIC(d);
1300 int i;
1301
1302 opp->gcr = GCR_RESET;
1303 /* Initialise controller registers */
1304 opp->frr = ((opp->nb_irqs - 1) << FRR_NIRQ_SHIFT) |
1305 ((opp->nb_cpus - 1) << FRR_NCPU_SHIFT) |
1306 (opp->vid << FRR_VID_SHIFT);
1307
1308 opp->pir = 0;
1309 opp->spve = -1 & opp->vector_mask;
1310 opp->tfrr = opp->tfrr_reset;
1311 /* Initialise IRQ sources */
1312 for (i = 0; i < opp->max_irq; i++) {
1313 opp->src[i].ivpr = opp->ivpr_reset;
Paul Janzen8ebe65f2014-05-21 21:46:52 -07001314 switch (opp->src[i].type) {
1315 case IRQ_TYPE_NORMAL:
1316 opp->src[i].level = !!(opp->ivpr_reset & IVPR_SENSE_MASK);
1317 break;
1318
1319 case IRQ_TYPE_FSLINT:
1320 opp->src[i].ivpr |= IVPR_POLARITY_MASK;
1321 break;
1322
1323 case IRQ_TYPE_FSLSPECIAL:
1324 break;
1325 }
Paul Janzenffd5e9f2014-05-21 23:09:45 -07001326
1327 write_IRQreg_idr(opp, i, opp->idr_reset);
Paul Janzen8ebe65f2014-05-21 21:46:52 -07001328 }
1329 /* Initialise IRQ destinations */
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +00001330 for (i = 0; i < opp->nb_cpus; i++) {
Paul Janzen8ebe65f2014-05-21 21:46:52 -07001331 opp->dst[i].ctpr = 15;
Paul Janzen8ebe65f2014-05-21 21:46:52 -07001332 opp->dst[i].raised.next = -1;
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +00001333 opp->dst[i].raised.priority = 0;
1334 bitmap_clear(opp->dst[i].raised.queue, 0, IRQQUEUE_SIZE_BITS);
Paul Janzen8ebe65f2014-05-21 21:46:52 -07001335 opp->dst[i].servicing.next = -1;
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +00001336 opp->dst[i].servicing.priority = 0;
1337 bitmap_clear(opp->dst[i].servicing.queue, 0, IRQQUEUE_SIZE_BITS);
Paul Janzen8ebe65f2014-05-21 21:46:52 -07001338 }
1339 /* Initialise timers */
1340 for (i = 0; i < OPENPIC_MAX_TMR; i++) {
1341 opp->timers[i].tccr = 0;
1342 opp->timers[i].tbcr = TBCR_CI;
1343 }
1344 /* Go out of RESET state */
1345 opp->gcr = 0;
1346}
1347
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001348typedef struct MemReg {
Alexander Grafd0b72632012-12-08 05:17:14 +01001349 const char *name;
1350 MemoryRegionOps const *ops;
1351 hwaddr start_addr;
1352 ram_addr_t size;
Alexander Grafaf7e9e72012-12-20 17:30:58 +01001353} MemReg;
Alexander Grafd0b72632012-12-08 05:17:14 +01001354
Scott Woode0dfe5b2013-01-21 15:53:53 +00001355static void fsl_common_init(OpenPICState *opp)
1356{
1357 int i;
Scott Wood8935a442013-04-15 13:19:32 +00001358 int virq = OPENPIC_MAX_SRC;
Scott Woode0dfe5b2013-01-21 15:53:53 +00001359
1360 opp->vid = VID_REVISION_1_2;
1361 opp->vir = VIR_GENERIC;
1362 opp->vector_mask = 0xFFFF;
1363 opp->tfrr_reset = 0;
1364 opp->ivpr_reset = IVPR_MASK_MASK;
1365 opp->idr_reset = 1 << 0;
Scott Wood8935a442013-04-15 13:19:32 +00001366 opp->max_irq = OPENPIC_MAX_IRQ;
Scott Woode0dfe5b2013-01-21 15:53:53 +00001367
1368 opp->irq_ipi0 = virq;
Scott Wood8935a442013-04-15 13:19:32 +00001369 virq += OPENPIC_MAX_IPI;
Scott Woode0dfe5b2013-01-21 15:53:53 +00001370 opp->irq_tim0 = virq;
Scott Wood8935a442013-04-15 13:19:32 +00001371 virq += OPENPIC_MAX_TMR;
Scott Woode0dfe5b2013-01-21 15:53:53 +00001372
Scott Wood8935a442013-04-15 13:19:32 +00001373 assert(virq <= OPENPIC_MAX_IRQ);
Scott Woode0dfe5b2013-01-21 15:53:53 +00001374
1375 opp->irq_msi = 224;
1376
1377 msi_supported = true;
1378 for (i = 0; i < opp->fsl->max_ext; i++) {
1379 opp->src[i].level = false;
1380 }
1381
1382 /* Internal interrupts, including message and MSI */
Scott Wood8935a442013-04-15 13:19:32 +00001383 for (i = 16; i < OPENPIC_MAX_SRC; i++) {
Scott Woode0dfe5b2013-01-21 15:53:53 +00001384 opp->src[i].type = IRQ_TYPE_FSLINT;
1385 opp->src[i].level = true;
1386 }
1387
1388 /* timers and IPIs */
Scott Wood8935a442013-04-15 13:19:32 +00001389 for (i = OPENPIC_MAX_SRC; i < virq; i++) {
Scott Woode0dfe5b2013-01-21 15:53:53 +00001390 opp->src[i].type = IRQ_TYPE_FSLSPECIAL;
1391 opp->src[i].level = false;
1392 }
1393}
1394
1395static void map_list(OpenPICState *opp, const MemReg *list, int *count)
1396{
1397 while (list->name) {
1398 assert(*count < ARRAY_SIZE(opp->sub_io_mem));
1399
Paolo Bonzini1437c942013-06-06 21:25:08 -04001400 memory_region_init_io(&opp->sub_io_mem[*count], OBJECT(opp), list->ops,
1401 opp, list->name, list->size);
Scott Woode0dfe5b2013-01-21 15:53:53 +00001402
1403 memory_region_add_subregion(&opp->mem, list->start_addr,
1404 &opp->sub_io_mem[*count]);
1405
1406 (*count)++;
1407 list++;
1408 }
1409}
1410
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +00001411static const VMStateDescription vmstate_openpic_irq_queue = {
1412 .name = "openpic_irq_queue",
1413 .version_id = 0,
1414 .minimum_version_id = 0,
1415 .fields = (VMStateField[]) {
1416 VMSTATE_BITMAP(queue, IRQQueue, 0, queue_size),
1417 VMSTATE_INT32(next, IRQQueue),
1418 VMSTATE_INT32(priority, IRQQueue),
1419 VMSTATE_END_OF_LIST()
1420 }
1421};
1422
1423static const VMStateDescription vmstate_openpic_irqdest = {
1424 .name = "openpic_irqdest",
1425 .version_id = 0,
1426 .minimum_version_id = 0,
1427 .fields = (VMStateField[]) {
1428 VMSTATE_INT32(ctpr, IRQDest),
1429 VMSTATE_STRUCT(raised, IRQDest, 0, vmstate_openpic_irq_queue,
1430 IRQQueue),
1431 VMSTATE_STRUCT(servicing, IRQDest, 0, vmstate_openpic_irq_queue,
1432 IRQQueue),
1433 VMSTATE_UINT32_ARRAY(outputs_active, IRQDest, OPENPIC_OUTPUT_NB),
1434 VMSTATE_END_OF_LIST()
1435 }
1436};
1437
1438static const VMStateDescription vmstate_openpic_irqsource = {
1439 .name = "openpic_irqsource",
1440 .version_id = 0,
1441 .minimum_version_id = 0,
1442 .fields = (VMStateField[]) {
1443 VMSTATE_UINT32(ivpr, IRQSource),
1444 VMSTATE_UINT32(idr, IRQSource),
1445 VMSTATE_UINT32(destmask, IRQSource),
1446 VMSTATE_INT32(last_cpu, IRQSource),
1447 VMSTATE_INT32(pending, IRQSource),
1448 VMSTATE_END_OF_LIST()
1449 }
1450};
1451
1452static const VMStateDescription vmstate_openpic_timer = {
1453 .name = "openpic_timer",
1454 .version_id = 0,
1455 .minimum_version_id = 0,
1456 .fields = (VMStateField[]) {
1457 VMSTATE_UINT32(tccr, OpenPICTimer),
1458 VMSTATE_UINT32(tbcr, OpenPICTimer),
1459 VMSTATE_END_OF_LIST()
1460 }
1461};
1462
1463static const VMStateDescription vmstate_openpic_msi = {
1464 .name = "openpic_msi",
1465 .version_id = 0,
1466 .minimum_version_id = 0,
1467 .fields = (VMStateField[]) {
1468 VMSTATE_UINT32(msir, OpenPICMSI),
1469 VMSTATE_END_OF_LIST()
1470 }
1471};
1472
1473static int openpic_post_load(void *opaque, int version_id)
1474{
1475 OpenPICState *opp = (OpenPICState *)opaque;
1476 int i;
1477
1478 /* Update internal ivpr and idr variables */
1479 for (i = 0; i < opp->max_irq; i++) {
1480 write_IRQreg_idr(opp, i, opp->src[i].idr);
1481 write_IRQreg_ivpr(opp, i, opp->src[i].ivpr);
1482 }
1483
1484 return 0;
1485}
1486
1487static const VMStateDescription vmstate_openpic = {
1488 .name = "openpic",
1489 .version_id = 3,
1490 .minimum_version_id = 3,
1491 .post_load = openpic_post_load,
1492 .fields = (VMStateField[]) {
1493 VMSTATE_UINT32(gcr, OpenPICState),
1494 VMSTATE_UINT32(vir, OpenPICState),
1495 VMSTATE_UINT32(pir, OpenPICState),
1496 VMSTATE_UINT32(spve, OpenPICState),
1497 VMSTATE_UINT32(tfrr, OpenPICState),
1498 VMSTATE_UINT32(max_irq, OpenPICState),
1499 VMSTATE_STRUCT_VARRAY_UINT32(src, OpenPICState, max_irq, 0,
1500 vmstate_openpic_irqsource, IRQSource),
1501 VMSTATE_UINT32_EQUAL(nb_cpus, OpenPICState),
1502 VMSTATE_STRUCT_VARRAY_UINT32(dst, OpenPICState, nb_cpus, 0,
1503 vmstate_openpic_irqdest, IRQDest),
1504 VMSTATE_STRUCT_ARRAY(timers, OpenPICState, OPENPIC_MAX_TMR, 0,
1505 vmstate_openpic_timer, OpenPICTimer),
1506 VMSTATE_STRUCT_ARRAY(msi, OpenPICState, MAX_MSI, 0,
1507 vmstate_openpic_msi, OpenPICMSI),
1508 VMSTATE_UINT32(irq_ipi0, OpenPICState),
1509 VMSTATE_UINT32(irq_tim0, OpenPICState),
1510 VMSTATE_UINT32(irq_msi, OpenPICState),
1511 VMSTATE_END_OF_LIST()
1512 }
1513};
1514
Andreas Färbercbe72012013-06-18 03:58:08 +02001515static void openpic_init(Object *obj)
bellarddbda8082004-06-15 21:38:40 +00001516{
Andreas Färbercbe72012013-06-18 03:58:08 +02001517 OpenPICState *opp = OPENPIC(obj);
1518
Paolo Bonzini1437c942013-06-06 21:25:08 -04001519 memory_region_init(&opp->mem, obj, "openpic", 0x40000);
Andreas Färbercbe72012013-06-18 03:58:08 +02001520}
1521
1522static void openpic_realize(DeviceState *dev, Error **errp)
1523{
1524 SysBusDevice *d = SYS_BUS_DEVICE(dev);
Andreas Färbere1766342013-06-18 03:58:07 +02001525 OpenPICState *opp = OPENPIC(dev);
Alexander Grafd0b72632012-12-08 05:17:14 +01001526 int i, j;
Scott Woode0dfe5b2013-01-21 15:53:53 +00001527 int list_count = 0;
1528 static const MemReg list_le[] = {
1529 {"glb", &openpic_glb_ops_le,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001530 OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001531 {"tmr", &openpic_tmr_ops_le,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001532 OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001533 {"src", &openpic_src_ops_le,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001534 OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001535 {"cpu", &openpic_cpu_ops_le,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001536 OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001537 {NULL}
Alexander Graf780d16b2012-12-07 17:15:15 +01001538 };
Scott Woode0dfe5b2013-01-21 15:53:53 +00001539 static const MemReg list_be[] = {
1540 {"glb", &openpic_glb_ops_be,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001541 OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001542 {"tmr", &openpic_tmr_ops_be,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001543 OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001544 {"src", &openpic_src_ops_be,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001545 OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001546 {"cpu", &openpic_cpu_ops_be,
Alexander Graf732aa6e2012-12-08 14:18:00 +01001547 OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
Scott Woode0dfe5b2013-01-21 15:53:53 +00001548 {NULL}
Alexander Grafd0b72632012-12-08 05:17:14 +01001549 };
Scott Woode0dfe5b2013-01-21 15:53:53 +00001550 static const MemReg list_fsl[] = {
1551 {"msi", &openpic_msi_ops_be,
1552 OPENPIC_MSI_REG_START, OPENPIC_MSI_REG_SIZE},
1553 {"summary", &openpic_summary_ops_be,
1554 OPENPIC_SUMMARY_REG_START, OPENPIC_SUMMARY_REG_SIZE},
1555 {NULL}
1556 };
1557
Michael Roth73d963c2014-04-28 16:08:17 +03001558 if (opp->nb_cpus > MAX_CPU) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001559 error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
1560 TYPE_OPENPIC, "nb_cpus", (uint64_t)opp->nb_cpus,
1561 (uint64_t)0, (uint64_t)MAX_CPU);
Michael Roth73d963c2014-04-28 16:08:17 +03001562 return;
1563 }
1564
Alexander Grafd0b72632012-12-08 05:17:14 +01001565 switch (opp->model) {
1566 case OPENPIC_MODEL_FSL_MPIC_20:
1567 default:
Scott Woode0dfe5b2013-01-21 15:53:53 +00001568 opp->fsl = &fsl_mpic_20;
1569 opp->brr1 = 0x00400200;
Scott Woodbe7c2362012-12-21 16:15:42 +00001570 opp->flags |= OPENPIC_FLAG_IDR_CRIT;
Alexander Grafd0b72632012-12-08 05:17:14 +01001571 opp->nb_irqs = 80;
Scott Woode0dfe5b2013-01-21 15:53:53 +00001572 opp->mpic_mode_mask = GCR_MODE_MIXED;
1573
1574 fsl_common_init(opp);
1575 map_list(opp, list_be, &list_count);
1576 map_list(opp, list_fsl, &list_count);
1577
1578 break;
1579
1580 case OPENPIC_MODEL_FSL_MPIC_42:
1581 opp->fsl = &fsl_mpic_42;
1582 opp->brr1 = 0x00400402;
1583 opp->flags |= OPENPIC_FLAG_ILR;
1584 opp->nb_irqs = 196;
Alexander Graf68c2dd72013-01-04 11:21:04 +01001585 opp->mpic_mode_mask = GCR_MODE_PROXY;
1586
Scott Woode0dfe5b2013-01-21 15:53:53 +00001587 fsl_common_init(opp);
1588 map_list(opp, list_be, &list_count);
1589 map_list(opp, list_fsl, &list_count);
Scott Wood6c5e84c2013-01-03 13:25:37 +00001590
Alexander Grafd0b72632012-12-08 05:17:14 +01001591 break;
Scott Wood6c5e84c2013-01-03 13:25:37 +00001592
Alexander Grafd0b72632012-12-08 05:17:14 +01001593 case OPENPIC_MODEL_RAVEN:
1594 opp->nb_irqs = RAVEN_MAX_EXT;
1595 opp->vid = VID_REVISION_1_3;
Scott Woodbe7c2362012-12-21 16:15:42 +00001596 opp->vir = VIR_GENERIC;
Scott Wood0fe04622012-12-13 16:12:01 +00001597 opp->vector_mask = 0xFF;
Scott Woodbe7c2362012-12-21 16:15:42 +00001598 opp->tfrr_reset = 4160000;
1599 opp->ivpr_reset = IVPR_MASK_MASK | IVPR_MODE_MASK;
1600 opp->idr_reset = 0;
Alexander Grafd0b72632012-12-08 05:17:14 +01001601 opp->max_irq = RAVEN_MAX_IRQ;
1602 opp->irq_ipi0 = RAVEN_IPI_IRQ;
1603 opp->irq_tim0 = RAVEN_TMR_IRQ;
Alexander Grafdbbbfd62012-12-08 13:51:50 +01001604 opp->brr1 = -1;
Alexander Graf86e56a82013-01-07 20:17:24 +01001605 opp->mpic_mode_mask = GCR_MODE_MIXED;
Alexander Grafd0b72632012-12-08 05:17:14 +01001606
Alexander Grafd0b72632012-12-08 05:17:14 +01001607 if (opp->nb_cpus != 1) {
Andreas Färbercbe72012013-06-18 03:58:08 +02001608 error_setg(errp, "Only UP supported today");
1609 return;
Alexander Grafd0b72632012-12-08 05:17:14 +01001610 }
Scott Woode0dfe5b2013-01-21 15:53:53 +00001611
1612 map_list(opp, list_le, &list_count);
Alexander Grafd0b72632012-12-08 05:17:14 +01001613 break;
1614 }
Alexander Graf780d16b2012-12-07 17:15:15 +01001615
Alexander Grafd0b72632012-12-08 05:17:14 +01001616 for (i = 0; i < opp->nb_cpus; i++) {
Peter Crosthwaiteaa2ac1d2014-08-15 01:15:10 -07001617 opp->dst[i].irqs = g_new0(qemu_irq, OPENPIC_OUTPUT_NB);
Alexander Grafd0b72632012-12-08 05:17:14 +01001618 for (j = 0; j < OPENPIC_OUTPUT_NB; j++) {
Andreas Färbercbe72012013-06-18 03:58:08 +02001619 sysbus_init_irq(d, &opp->dst[i].irqs[j]);
Alexander Grafd0b72632012-12-08 05:17:14 +01001620 }
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +00001621
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +00001622 opp->dst[i].raised.queue_size = IRQQUEUE_SIZE_BITS;
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +00001623 opp->dst[i].raised.queue = bitmap_new(IRQQUEUE_SIZE_BITS);
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +00001624 opp->dst[i].servicing.queue_size = IRQQUEUE_SIZE_BITS;
Mark Cave-Ayland2ada66f2015-02-09 22:40:51 +00001625 opp->dst[i].servicing.queue = bitmap_new(IRQQUEUE_SIZE_BITS);
aurel32b7169912009-03-02 16:42:04 +00001626 }
1627
Andreas Färbercbe72012013-06-18 03:58:08 +02001628 sysbus_init_mmio(d, &opp->mem);
1629 qdev_init_gpio_in(dev, openpic_set_irq, opp->max_irq);
bellarddbda8082004-06-15 21:38:40 +00001630}
Alexander Grafd0b72632012-12-08 05:17:14 +01001631
1632static Property openpic_properties[] = {
1633 DEFINE_PROP_UINT32("model", OpenPICState, model, OPENPIC_MODEL_FSL_MPIC_20),
1634 DEFINE_PROP_UINT32("nb_cpus", OpenPICState, nb_cpus, 1),
1635 DEFINE_PROP_END_OF_LIST(),
1636};
1637
Andreas Färbercbe72012013-06-18 03:58:08 +02001638static void openpic_class_init(ObjectClass *oc, void *data)
Alexander Grafd0b72632012-12-08 05:17:14 +01001639{
Andreas Färbercbe72012013-06-18 03:58:08 +02001640 DeviceClass *dc = DEVICE_CLASS(oc);
Alexander Grafd0b72632012-12-08 05:17:14 +01001641
Andreas Färbercbe72012013-06-18 03:58:08 +02001642 dc->realize = openpic_realize;
Alexander Grafd0b72632012-12-08 05:17:14 +01001643 dc->props = openpic_properties;
1644 dc->reset = openpic_reset;
Mark Cave-Aylande5f6e732015-02-09 22:40:52 +00001645 dc->vmsd = &vmstate_openpic;
Laurent Vivier29f8dd62015-09-26 18:22:12 +02001646 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Alexander Grafd0b72632012-12-08 05:17:14 +01001647}
1648
Andreas Färber8c43a6f2013-01-10 16:19:07 +01001649static const TypeInfo openpic_info = {
Andreas Färbere1766342013-06-18 03:58:07 +02001650 .name = TYPE_OPENPIC,
Alexander Grafd0b72632012-12-08 05:17:14 +01001651 .parent = TYPE_SYS_BUS_DEVICE,
1652 .instance_size = sizeof(OpenPICState),
Andreas Färbercbe72012013-06-18 03:58:08 +02001653 .instance_init = openpic_init,
Alexander Grafd0b72632012-12-08 05:17:14 +01001654 .class_init = openpic_class_init,
1655};
1656
1657static void openpic_register_types(void)
1658{
1659 type_register_static(&openpic_info);
1660}
1661
1662type_init(openpic_register_types)