]> git.proxmox.com Git - qemu.git/blame - hw/intc/openpic.c
memory: add owner argument to initialization functions
[qemu.git] / hw / intc / openpic.c
CommitLineData
dbda808a
FB
1/*
2 * OpenPIC emulation
5fafdf24 3 *
dbda808a 4 * Copyright (c) 2004 Jocelyn Mayer
704c7e5d 5 * 2011 Alexander Graf
5fafdf24 6 *
dbda808a
FB
7 * 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:
67b55785 28 * - Intel GW80314 I/O companion chip developer's manual
dbda808a
FB
29 * - 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.
5fafdf24 34 *
dbda808a 35 */
83c9f4ca
PB
36#include "hw/hw.h"
37#include "hw/ppc/mac.h"
38#include "hw/pci/pci.h"
0d09e41a 39#include "hw/ppc/openpic.h"
83c9f4ca
PB
40#include "hw/sysbus.h"
41#include "hw/pci/msi.h"
e69a17f6 42#include "qemu/bitops.h"
0d09e41a 43#include "hw/ppc/ppc.h"
dbda808a 44
611493d9 45//#define DEBUG_OPENPIC
dbda808a
FB
46
47#ifdef DEBUG_OPENPIC
4c4f0e48 48static const int debug_openpic = 1;
dbda808a 49#else
4c4f0e48 50static const int debug_openpic = 0;
dbda808a 51#endif
dbda808a 52
4c4f0e48
SW
53#define DPRINTF(fmt, ...) do { \
54 if (debug_openpic) { \
55 printf(fmt , ## __VA_ARGS__); \
56 } \
57 } while (0)
58
e0dfe5b1 59#define MAX_CPU 32
732aa6ec 60#define MAX_MSI 8
dbda808a 61#define VID 0x03 /* MPIC version ID */
dbda808a 62
d0b72631 63/* OpenPIC capability flags */
be7c236f 64#define OPENPIC_FLAG_IDR_CRIT (1 << 0)
e0dfe5b1 65#define OPENPIC_FLAG_ILR (2 << 0)
dbda808a 66
d0b72631 67/* OpenPIC address map */
780d16b7
AG
68#define OPENPIC_GLB_REG_START 0x0
69#define OPENPIC_GLB_REG_SIZE 0x10F0
70#define OPENPIC_TMR_REG_START 0x10F0
71#define OPENPIC_TMR_REG_SIZE 0x220
732aa6ec
AG
72#define OPENPIC_MSI_REG_START 0x1600
73#define OPENPIC_MSI_REG_SIZE 0x200
e0dfe5b1
SW
74#define OPENPIC_SUMMARY_REG_START 0x3800
75#define OPENPIC_SUMMARY_REG_SIZE 0x800
780d16b7 76#define OPENPIC_SRC_REG_START 0x10000
8935a442 77#define OPENPIC_SRC_REG_SIZE (OPENPIC_MAX_SRC * 0x20)
780d16b7
AG
78#define OPENPIC_CPU_REG_START 0x20000
79#define OPENPIC_CPU_REG_SIZE 0x100 + ((MAX_CPU - 1) * 0x1000)
80
d0b72631
AG
81/* Raven */
82#define RAVEN_MAX_CPU 2
83#define RAVEN_MAX_EXT 48
84#define RAVEN_MAX_IRQ 64
8935a442
SW
85#define RAVEN_MAX_TMR OPENPIC_MAX_TMR
86#define RAVEN_MAX_IPI OPENPIC_MAX_IPI
d0b72631
AG
87
88/* Interrupt definitions */
89#define RAVEN_FE_IRQ (RAVEN_MAX_EXT) /* Internal functional IRQ */
90#define RAVEN_ERR_IRQ (RAVEN_MAX_EXT + 1) /* Error IRQ */
91#define RAVEN_TMR_IRQ (RAVEN_MAX_EXT + 2) /* First timer IRQ */
92#define RAVEN_IPI_IRQ (RAVEN_TMR_IRQ + RAVEN_MAX_TMR) /* First IPI IRQ */
93/* First doorbell IRQ */
94#define RAVEN_DBL_IRQ (RAVEN_IPI_IRQ + (RAVEN_MAX_CPU * RAVEN_MAX_IPI))
95
e0dfe5b1
SW
96typedef struct FslMpicInfo {
97 int max_ext;
98} FslMpicInfo;
dbda808a 99
e0dfe5b1
SW
100static FslMpicInfo fsl_mpic_20 = {
101 .max_ext = 12,
102};
b7169916 103
e0dfe5b1
SW
104static FslMpicInfo fsl_mpic_42 = {
105 .max_ext = 12,
106};
3e772232 107
be7c236f
SW
108#define FRR_NIRQ_SHIFT 16
109#define FRR_NCPU_SHIFT 8
110#define FRR_VID_SHIFT 0
825463b3
AG
111
112#define VID_REVISION_1_2 2
d0b72631 113#define VID_REVISION_1_3 3
825463b3 114
be7c236f 115#define VIR_GENERIC 0x00000000 /* Generic Vendor ID */
825463b3 116
be7c236f 117#define GCR_RESET 0x80000000
68c2dd70
AG
118#define GCR_MODE_PASS 0x00000000
119#define GCR_MODE_MIXED 0x20000000
120#define GCR_MODE_PROXY 0x60000000
71c6cacb 121
be7c236f
SW
122#define TBCR_CI 0x80000000 /* count inhibit */
123#define TCCR_TOG 0x80000000 /* toggles when decrement to zero */
825463b3 124
1945dbc1
AG
125#define IDR_EP_SHIFT 31
126#define IDR_EP_MASK (1 << IDR_EP_SHIFT)
127#define IDR_CI0_SHIFT 30
128#define IDR_CI1_SHIFT 29
129#define IDR_P1_SHIFT 1
130#define IDR_P0_SHIFT 0
b7169916 131
e0dfe5b1
SW
132#define ILR_INTTGT_MASK 0x000000ff
133#define ILR_INTTGT_INT 0x00
134#define ILR_INTTGT_CINT 0x01 /* critical */
135#define ILR_INTTGT_MCP 0x02 /* machine check */
136
137/* The currently supported INTTGT values happen to be the same as QEMU's
138 * openpic output codes, but don't depend on this. The output codes
139 * could change (unlikely, but...) or support could be added for
140 * more INTTGT values.
141 */
142static const int inttgt_output[][2] = {
143 { ILR_INTTGT_INT, OPENPIC_OUTPUT_INT },
144 { ILR_INTTGT_CINT, OPENPIC_OUTPUT_CINT },
145 { ILR_INTTGT_MCP, OPENPIC_OUTPUT_MCK },
146};
147
148static int inttgt_to_output(int inttgt)
149{
150 int i;
151
152 for (i = 0; i < ARRAY_SIZE(inttgt_output); i++) {
153 if (inttgt_output[i][0] == inttgt) {
154 return inttgt_output[i][1];
155 }
156 }
157
158 fprintf(stderr, "%s: unsupported inttgt %d\n", __func__, inttgt);
159 return OPENPIC_OUTPUT_INT;
160}
161
162static int output_to_inttgt(int output)
163{
164 int i;
165
166 for (i = 0; i < ARRAY_SIZE(inttgt_output); i++) {
167 if (inttgt_output[i][1] == output) {
168 return inttgt_output[i][0];
169 }
170 }
171
172 abort();
173}
174
732aa6ec
AG
175#define MSIIR_OFFSET 0x140
176#define MSIIR_SRS_SHIFT 29
177#define MSIIR_SRS_MASK (0x7 << MSIIR_SRS_SHIFT)
178#define MSIIR_IBS_SHIFT 24
179#define MSIIR_IBS_MASK (0x1f << MSIIR_IBS_SHIFT)
180
704c7e5d
AG
181static int get_current_cpu(void)
182{
55e5c285
AF
183 CPUState *cpu_single_cpu;
184
c3203fa5
SW
185 if (!cpu_single_env) {
186 return -1;
187 }
188
55e5c285
AF
189 cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
190 return cpu_single_cpu->cpu_index;
704c7e5d
AG
191}
192
a8170e5e 193static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
704c7e5d 194 int idx);
a8170e5e 195static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
704c7e5d
AG
196 uint32_t val, int idx);
197
6c5e84c2
SW
198typedef enum IRQType {
199 IRQ_TYPE_NORMAL = 0,
200 IRQ_TYPE_FSLINT, /* FSL internal interrupt -- level only */
201 IRQ_TYPE_FSLSPECIAL, /* FSL timer/IPI interrupt, edge, no polarity */
202} IRQType;
203
af7e9e74 204typedef struct IRQQueue {
e69a17f6
SW
205 /* Round up to the nearest 64 IRQs so that the queue length
206 * won't change when moving between 32 and 64 bit hosts.
207 */
8935a442 208 unsigned long queue[BITS_TO_LONGS((OPENPIC_MAX_IRQ + 63) & ~63)];
dbda808a
FB
209 int next;
210 int priority;
af7e9e74 211} IRQQueue;
dbda808a 212
af7e9e74 213typedef struct IRQSource {
be7c236f
SW
214 uint32_t ivpr; /* IRQ vector/priority register */
215 uint32_t idr; /* IRQ destination register */
5e22c276 216 uint32_t destmask; /* bitmap of CPU destinations */
dbda808a 217 int last_cpu;
5e22c276 218 int output; /* IRQ level, e.g. OPENPIC_OUTPUT_INT */
611493d9 219 int pending; /* TRUE if IRQ is pending */
6c5e84c2
SW
220 IRQType type;
221 bool level:1; /* level-triggered */
72c1da2c 222 bool nomask:1; /* critical interrupts ignore mask on some FSL MPICs */
af7e9e74 223} IRQSource;
dbda808a 224
be7c236f
SW
225#define IVPR_MASK_SHIFT 31
226#define IVPR_MASK_MASK (1 << IVPR_MASK_SHIFT)
227#define IVPR_ACTIVITY_SHIFT 30
228#define IVPR_ACTIVITY_MASK (1 << IVPR_ACTIVITY_SHIFT)
229#define IVPR_MODE_SHIFT 29
230#define IVPR_MODE_MASK (1 << IVPR_MODE_SHIFT)
231#define IVPR_POLARITY_SHIFT 23
232#define IVPR_POLARITY_MASK (1 << IVPR_POLARITY_SHIFT)
233#define IVPR_SENSE_SHIFT 22
234#define IVPR_SENSE_MASK (1 << IVPR_SENSE_SHIFT)
235
236#define IVPR_PRIORITY_MASK (0xF << 16)
237#define IVPR_PRIORITY(_ivprr_) ((int)(((_ivprr_) & IVPR_PRIORITY_MASK) >> 16))
238#define IVPR_VECTOR(opp, _ivprr_) ((_ivprr_) & (opp)->vector_mask)
239
240/* IDR[EP/CI] are only for FSL MPIC prior to v4.0 */
241#define IDR_EP 0x80000000 /* external pin */
242#define IDR_CI 0x40000000 /* critical interrupt */
71c6cacb 243
af7e9e74 244typedef struct IRQDest {
eb438427 245 int32_t ctpr; /* CPU current task priority */
af7e9e74
AG
246 IRQQueue raised;
247 IRQQueue servicing;
e9df014c 248 qemu_irq *irqs;
9f1d4b1d
SW
249
250 /* Count of IRQ sources asserting on non-INT outputs */
251 uint32_t outputs_active[OPENPIC_OUTPUT_NB];
af7e9e74 252} IRQDest;
dbda808a 253
e1766344
AF
254#define OPENPIC(obj) OBJECT_CHECK(OpenPICState, (obj), TYPE_OPENPIC)
255
6d544ee8 256typedef struct OpenPICState {
e1766344
AF
257 /*< private >*/
258 SysBusDevice parent_obj;
259 /*< public >*/
260
23c5e4ca 261 MemoryRegion mem;
71cf9e62 262
5861a338 263 /* Behavior control */
e0dfe5b1 264 FslMpicInfo *fsl;
d0b72631 265 uint32_t model;
5861a338 266 uint32_t flags;
825463b3
AG
267 uint32_t nb_irqs;
268 uint32_t vid;
be7c236f 269 uint32_t vir; /* Vendor identification register */
0fe04622 270 uint32_t vector_mask;
be7c236f
SW
271 uint32_t tfrr_reset;
272 uint32_t ivpr_reset;
273 uint32_t idr_reset;
dbbbfd60 274 uint32_t brr1;
68c2dd70 275 uint32_t mpic_mode_mask;
5861a338 276
71cf9e62 277 /* Sub-regions */
e0dfe5b1 278 MemoryRegion sub_io_mem[6];
71cf9e62 279
dbda808a 280 /* Global registers */
be7c236f
SW
281 uint32_t frr; /* Feature reporting register */
282 uint32_t gcr; /* Global configuration register */
283 uint32_t pir; /* Processor initialization register */
dbda808a 284 uint32_t spve; /* Spurious vector register */
be7c236f 285 uint32_t tfrr; /* Timer frequency reporting register */
dbda808a 286 /* Source registers */
8935a442 287 IRQSource src[OPENPIC_MAX_IRQ];
dbda808a 288 /* Local registers per output pin */
af7e9e74 289 IRQDest dst[MAX_CPU];
d0b72631 290 uint32_t nb_cpus;
dbda808a
FB
291 /* Timer registers */
292 struct {
be7c236f
SW
293 uint32_t tccr; /* Global timer current count register */
294 uint32_t tbcr; /* Global timer base count register */
8935a442 295 } timers[OPENPIC_MAX_TMR];
732aa6ec
AG
296 /* Shared MSI registers */
297 struct {
298 uint32_t msir; /* Shared Message Signaled Interrupt Register */
299 } msi[MAX_MSI];
d0b72631
AG
300 uint32_t max_irq;
301 uint32_t irq_ipi0;
302 uint32_t irq_tim0;
732aa6ec 303 uint32_t irq_msi;
6d544ee8 304} OpenPICState;
dbda808a 305
af7e9e74 306static inline void IRQ_setbit(IRQQueue *q, int n_IRQ)
dbda808a 307{
e69a17f6 308 set_bit(n_IRQ, q->queue);
dbda808a
FB
309}
310
af7e9e74 311static inline void IRQ_resetbit(IRQQueue *q, int n_IRQ)
dbda808a 312{
e69a17f6 313 clear_bit(n_IRQ, q->queue);
dbda808a
FB
314}
315
af7e9e74 316static inline int IRQ_testbit(IRQQueue *q, int n_IRQ)
dbda808a 317{
e69a17f6 318 return test_bit(n_IRQ, q->queue);
dbda808a
FB
319}
320
af7e9e74 321static void IRQ_check(OpenPICState *opp, IRQQueue *q)
dbda808a 322{
4417c733
SW
323 int irq = -1;
324 int next = -1;
325 int priority = -1;
326
327 for (;;) {
328 irq = find_next_bit(q->queue, opp->max_irq, irq + 1);
329 if (irq == opp->max_irq) {
330 break;
331 }
76aec1f8 332
4417c733
SW
333 DPRINTF("IRQ_check: irq %d set ivpr_pr=%d pr=%d\n",
334 irq, IVPR_PRIORITY(opp->src[irq].ivpr), priority);
76aec1f8 335
4417c733
SW
336 if (IVPR_PRIORITY(opp->src[irq].ivpr) > priority) {
337 next = irq;
338 priority = IVPR_PRIORITY(opp->src[irq].ivpr);
060fbfe1 339 }
dbda808a 340 }
76aec1f8 341
dbda808a
FB
342 q->next = next;
343 q->priority = priority;
344}
345
af7e9e74 346static int IRQ_get_next(OpenPICState *opp, IRQQueue *q)
dbda808a 347{
3c94378e
SW
348 /* XXX: optimize */
349 IRQ_check(opp, q);
dbda808a
FB
350
351 return q->next;
352}
353
9f1d4b1d
SW
354static void IRQ_local_pipe(OpenPICState *opp, int n_CPU, int n_IRQ,
355 bool active, bool was_active)
dbda808a 356{
af7e9e74
AG
357 IRQDest *dst;
358 IRQSource *src;
dbda808a
FB
359 int priority;
360
361 dst = &opp->dst[n_CPU];
362 src = &opp->src[n_IRQ];
5e22c276 363
9f1d4b1d
SW
364 DPRINTF("%s: IRQ %d active %d was %d\n",
365 __func__, n_IRQ, active, was_active);
366
5e22c276 367 if (src->output != OPENPIC_OUTPUT_INT) {
9f1d4b1d
SW
368 DPRINTF("%s: output %d irq %d active %d was %d count %d\n",
369 __func__, src->output, n_IRQ, active, was_active,
370 dst->outputs_active[src->output]);
371
5e22c276
SW
372 /* On Freescale MPIC, critical interrupts ignore priority,
373 * IACK, EOI, etc. Before MPIC v4.1 they also ignore
374 * masking.
375 */
9f1d4b1d
SW
376 if (active) {
377 if (!was_active && dst->outputs_active[src->output]++ == 0) {
378 DPRINTF("%s: Raise OpenPIC output %d cpu %d irq %d\n",
379 __func__, src->output, n_CPU, n_IRQ);
380 qemu_irq_raise(dst->irqs[src->output]);
381 }
382 } else {
383 if (was_active && --dst->outputs_active[src->output] == 0) {
384 DPRINTF("%s: Lower OpenPIC output %d cpu %d irq %d\n",
385 __func__, src->output, n_CPU, n_IRQ);
386 qemu_irq_lower(dst->irqs[src->output]);
387 }
388 }
389
060fbfe1 390 return;
dbda808a 391 }
5e22c276 392
be7c236f 393 priority = IVPR_PRIORITY(src->ivpr);
9f1d4b1d
SW
394
395 /* Even if the interrupt doesn't have enough priority,
396 * it is still raised, in case ctpr is lowered later.
397 */
398 if (active) {
399 IRQ_setbit(&dst->raised, n_IRQ);
400 } else {
401 IRQ_resetbit(&dst->raised, n_IRQ);
dbda808a 402 }
9f1d4b1d 403
3c94378e 404 IRQ_check(opp, &dst->raised);
9f1d4b1d
SW
405
406 if (active && priority <= dst->ctpr) {
407 DPRINTF("%s: IRQ %d priority %d too low for ctpr %d on CPU %d\n",
408 __func__, n_IRQ, priority, dst->ctpr, n_CPU);
409 active = 0;
e9df014c 410 }
9f1d4b1d
SW
411
412 if (active) {
413 if (IRQ_get_next(opp, &dst->servicing) >= 0 &&
414 priority <= dst->servicing.priority) {
415 DPRINTF("%s: IRQ %d is hidden by servicing IRQ %d on CPU %d\n",
416 __func__, n_IRQ, dst->servicing.next, n_CPU);
417 } else {
418 DPRINTF("%s: Raise OpenPIC INT output cpu %d irq %d/%d\n",
419 __func__, n_CPU, n_IRQ, dst->raised.next);
420 qemu_irq_raise(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_INT]);
421 }
422 } else {
423 IRQ_get_next(opp, &dst->servicing);
424 if (dst->raised.priority > dst->ctpr &&
425 dst->raised.priority > dst->servicing.priority) {
426 DPRINTF("%s: IRQ %d inactive, IRQ %d prio %d above %d/%d, CPU %d\n",
427 __func__, n_IRQ, dst->raised.next, dst->raised.priority,
428 dst->ctpr, dst->servicing.priority, n_CPU);
429 /* IRQ line stays asserted */
430 } else {
431 DPRINTF("%s: IRQ %d inactive, current prio %d/%d, CPU %d\n",
432 __func__, n_IRQ, dst->ctpr, dst->servicing.priority, n_CPU);
433 qemu_irq_lower(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_INT]);
434 }
dbda808a
FB
435 }
436}
437
611493d9 438/* update pic state because registers for n_IRQ have changed value */
6d544ee8 439static void openpic_update_irq(OpenPICState *opp, int n_IRQ)
dbda808a 440{
af7e9e74 441 IRQSource *src;
9f1d4b1d 442 bool active, was_active;
dbda808a
FB
443 int i;
444
445 src = &opp->src[n_IRQ];
9f1d4b1d 446 active = src->pending;
611493d9 447
72c1da2c 448 if ((src->ivpr & IVPR_MASK_MASK) && !src->nomask) {
060fbfe1 449 /* Interrupt source is disabled */
e9df014c 450 DPRINTF("%s: IRQ %d is disabled\n", __func__, n_IRQ);
9f1d4b1d 451 active = false;
dbda808a 452 }
9f1d4b1d
SW
453
454 was_active = !!(src->ivpr & IVPR_ACTIVITY_MASK);
455
456 /*
457 * We don't have a similar check for already-active because
458 * ctpr may have changed and we need to withdraw the interrupt.
459 */
460 if (!active && !was_active) {
461 DPRINTF("%s: IRQ %d is already inactive\n", __func__, n_IRQ);
060fbfe1 462 return;
dbda808a 463 }
9f1d4b1d
SW
464
465 if (active) {
466 src->ivpr |= IVPR_ACTIVITY_MASK;
467 } else {
468 src->ivpr &= ~IVPR_ACTIVITY_MASK;
611493d9 469 }
9f1d4b1d 470
f40c360c 471 if (src->destmask == 0) {
060fbfe1 472 /* No target */
e9df014c 473 DPRINTF("%s: IRQ %d has no target\n", __func__, n_IRQ);
060fbfe1 474 return;
dbda808a 475 }
611493d9 476
f40c360c 477 if (src->destmask == (1 << src->last_cpu)) {
e9df014c 478 /* Only one CPU is allowed to receive this IRQ */
9f1d4b1d 479 IRQ_local_pipe(opp, src->last_cpu, n_IRQ, active, was_active);
be7c236f 480 } else if (!(src->ivpr & IVPR_MODE_MASK)) {
611493d9
FB
481 /* Directed delivery mode */
482 for (i = 0; i < opp->nb_cpus; i++) {
5e22c276 483 if (src->destmask & (1 << i)) {
9f1d4b1d 484 IRQ_local_pipe(opp, i, n_IRQ, active, was_active);
1945dbc1 485 }
611493d9 486 }
dbda808a 487 } else {
611493d9 488 /* Distributed delivery mode */
e9df014c 489 for (i = src->last_cpu + 1; i != src->last_cpu; i++) {
af7e9e74 490 if (i == opp->nb_cpus) {
611493d9 491 i = 0;
af7e9e74 492 }
5e22c276 493 if (src->destmask & (1 << i)) {
9f1d4b1d 494 IRQ_local_pipe(opp, i, n_IRQ, active, was_active);
611493d9
FB
495 src->last_cpu = i;
496 break;
497 }
498 }
499 }
500}
501
d537cf6c 502static void openpic_set_irq(void *opaque, int n_IRQ, int level)
611493d9 503{
6d544ee8 504 OpenPICState *opp = opaque;
af7e9e74 505 IRQSource *src;
611493d9 506
8935a442 507 if (n_IRQ >= OPENPIC_MAX_IRQ) {
65b9d0d5
SW
508 fprintf(stderr, "%s: IRQ %d out of range\n", __func__, n_IRQ);
509 abort();
510 }
611493d9
FB
511
512 src = &opp->src[n_IRQ];
be7c236f
SW
513 DPRINTF("openpic: set irq %d = %d ivpr=0x%08x\n",
514 n_IRQ, level, src->ivpr);
6c5e84c2 515 if (src->level) {
611493d9
FB
516 /* level-sensitive irq */
517 src->pending = level;
9f1d4b1d 518 openpic_update_irq(opp, n_IRQ);
611493d9
FB
519 } else {
520 /* edge-sensitive irq */
af7e9e74 521 if (level) {
611493d9 522 src->pending = 1;
9f1d4b1d
SW
523 openpic_update_irq(opp, n_IRQ);
524 }
525
526 if (src->output != OPENPIC_OUTPUT_INT) {
527 /* Edge-triggered interrupts shouldn't be used
528 * with non-INT delivery, but just in case,
529 * try to make it do something sane rather than
530 * cause an interrupt storm. This is close to
531 * what you'd probably see happen in real hardware.
532 */
533 src->pending = 0;
534 openpic_update_irq(opp, n_IRQ);
af7e9e74 535 }
dbda808a
FB
536 }
537}
538
d0b72631 539static void openpic_reset(DeviceState *d)
dbda808a 540{
e1766344 541 OpenPICState *opp = OPENPIC(d);
dbda808a
FB
542 int i;
543
be7c236f 544 opp->gcr = GCR_RESET;
f8407028 545 /* Initialise controller registers */
be7c236f
SW
546 opp->frr = ((opp->nb_irqs - 1) << FRR_NIRQ_SHIFT) |
547 ((opp->nb_cpus - 1) << FRR_NCPU_SHIFT) |
548 (opp->vid << FRR_VID_SHIFT);
825463b3 549
be7c236f 550 opp->pir = 0;
0fe04622 551 opp->spve = -1 & opp->vector_mask;
be7c236f 552 opp->tfrr = opp->tfrr_reset;
dbda808a 553 /* Initialise IRQ sources */
b7169916 554 for (i = 0; i < opp->max_irq; i++) {
be7c236f
SW
555 opp->src[i].ivpr = opp->ivpr_reset;
556 opp->src[i].idr = opp->idr_reset;
6c5e84c2
SW
557
558 switch (opp->src[i].type) {
559 case IRQ_TYPE_NORMAL:
560 opp->src[i].level = !!(opp->ivpr_reset & IVPR_SENSE_MASK);
561 break;
562
563 case IRQ_TYPE_FSLINT:
564 opp->src[i].ivpr |= IVPR_POLARITY_MASK;
565 break;
566
567 case IRQ_TYPE_FSLSPECIAL:
568 break;
569 }
dbda808a
FB
570 }
571 /* Initialise IRQ destinations */
e9df014c 572 for (i = 0; i < MAX_CPU; i++) {
be7c236f 573 opp->dst[i].ctpr = 15;
af7e9e74 574 memset(&opp->dst[i].raised, 0, sizeof(IRQQueue));
d14ed254 575 opp->dst[i].raised.next = -1;
af7e9e74 576 memset(&opp->dst[i].servicing, 0, sizeof(IRQQueue));
d14ed254 577 opp->dst[i].servicing.next = -1;
dbda808a
FB
578 }
579 /* Initialise timers */
8935a442 580 for (i = 0; i < OPENPIC_MAX_TMR; i++) {
be7c236f
SW
581 opp->timers[i].tccr = 0;
582 opp->timers[i].tbcr = TBCR_CI;
dbda808a 583 }
dbda808a 584 /* Go out of RESET state */
be7c236f 585 opp->gcr = 0;
dbda808a
FB
586}
587
be7c236f 588static inline uint32_t read_IRQreg_idr(OpenPICState *opp, int n_IRQ)
dbda808a 589{
be7c236f 590 return opp->src[n_IRQ].idr;
8d3a8c1e 591}
dbda808a 592
e0dfe5b1
SW
593static inline uint32_t read_IRQreg_ilr(OpenPICState *opp, int n_IRQ)
594{
595 if (opp->flags & OPENPIC_FLAG_ILR) {
596 return output_to_inttgt(opp->src[n_IRQ].output);
597 }
598
599 return 0xffffffff;
600}
601
be7c236f 602static inline uint32_t read_IRQreg_ivpr(OpenPICState *opp, int n_IRQ)
8d3a8c1e 603{
be7c236f 604 return opp->src[n_IRQ].ivpr;
dbda808a
FB
605}
606
be7c236f 607static inline void write_IRQreg_idr(OpenPICState *opp, int n_IRQ, uint32_t val)
dbda808a 608{
5e22c276
SW
609 IRQSource *src = &opp->src[n_IRQ];
610 uint32_t normal_mask = (1UL << opp->nb_cpus) - 1;
611 uint32_t crit_mask = 0;
612 uint32_t mask = normal_mask;
613 int crit_shift = IDR_EP_SHIFT - opp->nb_cpus;
614 int i;
615
616 if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
617 crit_mask = mask << crit_shift;
618 mask |= crit_mask | IDR_EP;
619 }
620
621 src->idr = val & mask;
622 DPRINTF("Set IDR %d to 0x%08x\n", n_IRQ, src->idr);
623
624 if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
625 if (src->idr & crit_mask) {
626 if (src->idr & normal_mask) {
627 DPRINTF("%s: IRQ configured for multiple output types, using "
628 "critical\n", __func__);
629 }
dbda808a 630
5e22c276 631 src->output = OPENPIC_OUTPUT_CINT;
72c1da2c 632 src->nomask = true;
5e22c276
SW
633 src->destmask = 0;
634
635 for (i = 0; i < opp->nb_cpus; i++) {
636 int n_ci = IDR_CI0_SHIFT - i;
dbda808a 637
5e22c276
SW
638 if (src->idr & (1UL << n_ci)) {
639 src->destmask |= 1UL << i;
640 }
641 }
642 } else {
643 src->output = OPENPIC_OUTPUT_INT;
72c1da2c 644 src->nomask = false;
5e22c276
SW
645 src->destmask = src->idr & normal_mask;
646 }
647 } else {
648 src->destmask = src->idr;
649 }
11de8b71
AG
650}
651
e0dfe5b1
SW
652static inline void write_IRQreg_ilr(OpenPICState *opp, int n_IRQ, uint32_t val)
653{
654 if (opp->flags & OPENPIC_FLAG_ILR) {
655 IRQSource *src = &opp->src[n_IRQ];
656
657 src->output = inttgt_to_output(val & ILR_INTTGT_MASK);
658 DPRINTF("Set ILR %d to 0x%08x, output %d\n", n_IRQ, src->idr,
659 src->output);
660
661 /* TODO: on MPIC v4.0 only, set nomask for non-INT */
662 }
663}
664
be7c236f 665static inline void write_IRQreg_ivpr(OpenPICState *opp, int n_IRQ, uint32_t val)
11de8b71 666{
6c5e84c2
SW
667 uint32_t mask;
668
669 /* NOTE when implementing newer FSL MPIC models: starting with v4.0,
670 * the polarity bit is read-only on internal interrupts.
671 */
672 mask = IVPR_MASK_MASK | IVPR_PRIORITY_MASK | IVPR_SENSE_MASK |
673 IVPR_POLARITY_MASK | opp->vector_mask;
674
11de8b71 675 /* ACTIVITY bit is read-only */
6c5e84c2
SW
676 opp->src[n_IRQ].ivpr =
677 (opp->src[n_IRQ].ivpr & IVPR_ACTIVITY_MASK) | (val & mask);
678
679 /* For FSL internal interrupts, The sense bit is reserved and zero,
680 * and the interrupt is always level-triggered. Timers and IPIs
681 * have no sense or polarity bits, and are edge-triggered.
682 */
683 switch (opp->src[n_IRQ].type) {
684 case IRQ_TYPE_NORMAL:
685 opp->src[n_IRQ].level = !!(opp->src[n_IRQ].ivpr & IVPR_SENSE_MASK);
686 break;
687
688 case IRQ_TYPE_FSLINT:
689 opp->src[n_IRQ].ivpr &= ~IVPR_SENSE_MASK;
690 break;
691
692 case IRQ_TYPE_FSLSPECIAL:
693 opp->src[n_IRQ].ivpr &= ~(IVPR_POLARITY_MASK | IVPR_SENSE_MASK);
694 break;
695 }
696
11de8b71 697 openpic_update_irq(opp, n_IRQ);
be7c236f
SW
698 DPRINTF("Set IVPR %d to 0x%08x -> 0x%08x\n", n_IRQ, val,
699 opp->src[n_IRQ].ivpr);
dbda808a
FB
700}
701
7f11573b
AG
702static void openpic_gcr_write(OpenPICState *opp, uint64_t val)
703{
e49798b1 704 bool mpic_proxy = false;
1ac3d713 705
7f11573b 706 if (val & GCR_RESET) {
e1766344 707 openpic_reset(DEVICE(opp));
1ac3d713
AG
708 return;
709 }
7f11573b 710
1ac3d713
AG
711 opp->gcr &= ~opp->mpic_mode_mask;
712 opp->gcr |= val & opp->mpic_mode_mask;
7f11573b 713
1ac3d713
AG
714 /* Set external proxy mode */
715 if ((val & opp->mpic_mode_mask) == GCR_MODE_PROXY) {
e49798b1 716 mpic_proxy = true;
7f11573b 717 }
e49798b1
AG
718
719 ppce500_set_mpic_proxy(mpic_proxy);
7f11573b
AG
720}
721
b9b2aaa3
AG
722static void openpic_gbl_write(void *opaque, hwaddr addr, uint64_t val,
723 unsigned len)
dbda808a 724{
6d544ee8 725 OpenPICState *opp = opaque;
af7e9e74 726 IRQDest *dst;
e9df014c 727 int idx;
dbda808a 728
4c4f0e48
SW
729 DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
730 __func__, addr, val);
af7e9e74 731 if (addr & 0xF) {
dbda808a 732 return;
af7e9e74 733 }
dbda808a 734 switch (addr) {
3e772232
BB
735 case 0x00: /* Block Revision Register1 (BRR1) is Readonly */
736 break;
704c7e5d
AG
737 case 0x40:
738 case 0x50:
739 case 0x60:
740 case 0x70:
741 case 0x80:
742 case 0x90:
743 case 0xA0:
744 case 0xB0:
745 openpic_cpu_write_internal(opp, addr, val, get_current_cpu());
dbda808a 746 break;
be7c236f 747 case 0x1000: /* FRR */
dbda808a 748 break;
be7c236f 749 case 0x1020: /* GCR */
7f11573b 750 openpic_gcr_write(opp, val);
060fbfe1 751 break;
be7c236f 752 case 0x1080: /* VIR */
060fbfe1 753 break;
be7c236f 754 case 0x1090: /* PIR */
e9df014c 755 for (idx = 0; idx < opp->nb_cpus; idx++) {
be7c236f 756 if ((val & (1 << idx)) && !(opp->pir & (1 << idx))) {
e9df014c
JM
757 DPRINTF("Raise OpenPIC RESET output for CPU %d\n", idx);
758 dst = &opp->dst[idx];
759 qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_RESET]);
be7c236f 760 } else if (!(val & (1 << idx)) && (opp->pir & (1 << idx))) {
e9df014c
JM
761 DPRINTF("Lower OpenPIC RESET output for CPU %d\n", idx);
762 dst = &opp->dst[idx];
763 qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_RESET]);
764 }
dbda808a 765 }
be7c236f 766 opp->pir = val;
060fbfe1 767 break;
be7c236f 768 case 0x10A0: /* IPI_IVPR */
704c7e5d
AG
769 case 0x10B0:
770 case 0x10C0:
771 case 0x10D0:
dbda808a
FB
772 {
773 int idx;
704c7e5d 774 idx = (addr - 0x10A0) >> 4;
be7c236f 775 write_IRQreg_ivpr(opp, opp->irq_ipi0 + idx, val);
dbda808a
FB
776 }
777 break;
704c7e5d 778 case 0x10E0: /* SPVE */
0fe04622 779 opp->spve = val & opp->vector_mask;
dbda808a 780 break;
dbda808a
FB
781 default:
782 break;
783 }
784}
785
b9b2aaa3 786static uint64_t openpic_gbl_read(void *opaque, hwaddr addr, unsigned len)
dbda808a 787{
6d544ee8 788 OpenPICState *opp = opaque;
dbda808a
FB
789 uint32_t retval;
790
4c4f0e48 791 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
dbda808a 792 retval = 0xFFFFFFFF;
af7e9e74 793 if (addr & 0xF) {
dbda808a 794 return retval;
af7e9e74 795 }
dbda808a 796 switch (addr) {
be7c236f
SW
797 case 0x1000: /* FRR */
798 retval = opp->frr;
dbda808a 799 break;
be7c236f
SW
800 case 0x1020: /* GCR */
801 retval = opp->gcr;
060fbfe1 802 break;
be7c236f
SW
803 case 0x1080: /* VIR */
804 retval = opp->vir;
060fbfe1 805 break;
be7c236f 806 case 0x1090: /* PIR */
dbda808a 807 retval = 0x00000000;
060fbfe1 808 break;
3e772232 809 case 0x00: /* Block Revision Register1 (BRR1) */
0d404683
SW
810 retval = opp->brr1;
811 break;
704c7e5d
AG
812 case 0x40:
813 case 0x50:
814 case 0x60:
815 case 0x70:
816 case 0x80:
817 case 0x90:
818 case 0xA0:
dbda808a 819 case 0xB0:
704c7e5d
AG
820 retval = openpic_cpu_read_internal(opp, addr, get_current_cpu());
821 break;
be7c236f 822 case 0x10A0: /* IPI_IVPR */
704c7e5d
AG
823 case 0x10B0:
824 case 0x10C0:
825 case 0x10D0:
dbda808a
FB
826 {
827 int idx;
704c7e5d 828 idx = (addr - 0x10A0) >> 4;
be7c236f 829 retval = read_IRQreg_ivpr(opp, opp->irq_ipi0 + idx);
dbda808a 830 }
060fbfe1 831 break;
704c7e5d 832 case 0x10E0: /* SPVE */
dbda808a
FB
833 retval = opp->spve;
834 break;
dbda808a
FB
835 default:
836 break;
837 }
4c4f0e48 838 DPRINTF("%s: => 0x%08x\n", __func__, retval);
dbda808a
FB
839
840 return retval;
841}
842
6d544ee8 843static void openpic_tmr_write(void *opaque, hwaddr addr, uint64_t val,
b9b2aaa3 844 unsigned len)
dbda808a 845{
6d544ee8 846 OpenPICState *opp = opaque;
dbda808a
FB
847 int idx;
848
03274d44
SW
849 addr += 0x10f0;
850
4c4f0e48
SW
851 DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
852 __func__, addr, val);
af7e9e74 853 if (addr & 0xF) {
dbda808a 854 return;
af7e9e74 855 }
c38c0b8a 856
03274d44 857 if (addr == 0x10f0) {
be7c236f
SW
858 /* TFRR */
859 opp->tfrr = val;
c38c0b8a
AG
860 return;
861 }
03274d44
SW
862
863 idx = (addr >> 6) & 0x3;
864 addr = addr & 0x30;
865
c38c0b8a 866 switch (addr & 0x30) {
be7c236f 867 case 0x00: /* TCCR */
dbda808a 868 break;
be7c236f
SW
869 case 0x10: /* TBCR */
870 if ((opp->timers[idx].tccr & TCCR_TOG) != 0 &&
871 (val & TBCR_CI) == 0 &&
872 (opp->timers[idx].tbcr & TBCR_CI) != 0) {
873 opp->timers[idx].tccr &= ~TCCR_TOG;
71c6cacb 874 }
be7c236f 875 opp->timers[idx].tbcr = val;
060fbfe1 876 break;
be7c236f
SW
877 case 0x20: /* TVPR */
878 write_IRQreg_ivpr(opp, opp->irq_tim0 + idx, val);
060fbfe1 879 break;
be7c236f
SW
880 case 0x30: /* TDR */
881 write_IRQreg_idr(opp, opp->irq_tim0 + idx, val);
060fbfe1 882 break;
dbda808a
FB
883 }
884}
885
6d544ee8 886static uint64_t openpic_tmr_read(void *opaque, hwaddr addr, unsigned len)
dbda808a 887{
6d544ee8 888 OpenPICState *opp = opaque;
c38c0b8a 889 uint32_t retval = -1;
dbda808a
FB
890 int idx;
891
4c4f0e48 892 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
c38c0b8a
AG
893 if (addr & 0xF) {
894 goto out;
895 }
896 idx = (addr >> 6) & 0x3;
897 if (addr == 0x0) {
be7c236f
SW
898 /* TFRR */
899 retval = opp->tfrr;
c38c0b8a
AG
900 goto out;
901 }
902 switch (addr & 0x30) {
be7c236f
SW
903 case 0x00: /* TCCR */
904 retval = opp->timers[idx].tccr;
dbda808a 905 break;
be7c236f
SW
906 case 0x10: /* TBCR */
907 retval = opp->timers[idx].tbcr;
060fbfe1 908 break;
be7c236f
SW
909 case 0x20: /* TIPV */
910 retval = read_IRQreg_ivpr(opp, opp->irq_tim0 + idx);
060fbfe1 911 break;
c38c0b8a 912 case 0x30: /* TIDE (TIDR) */
be7c236f 913 retval = read_IRQreg_idr(opp, opp->irq_tim0 + idx);
060fbfe1 914 break;
dbda808a 915 }
c38c0b8a
AG
916
917out:
4c4f0e48 918 DPRINTF("%s: => 0x%08x\n", __func__, retval);
dbda808a
FB
919
920 return retval;
921}
922
b9b2aaa3
AG
923static void openpic_src_write(void *opaque, hwaddr addr, uint64_t val,
924 unsigned len)
dbda808a 925{
6d544ee8 926 OpenPICState *opp = opaque;
dbda808a
FB
927 int idx;
928
4c4f0e48
SW
929 DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
930 __func__, addr, val);
e0dfe5b1
SW
931
932 addr = addr & 0xffff;
dbda808a 933 idx = addr >> 5;
e0dfe5b1
SW
934
935 switch (addr & 0x1f) {
936 case 0x00:
be7c236f 937 write_IRQreg_ivpr(opp, idx, val);
e0dfe5b1
SW
938 break;
939 case 0x10:
940 write_IRQreg_idr(opp, idx, val);
941 break;
942 case 0x18:
943 write_IRQreg_ilr(opp, idx, val);
944 break;
dbda808a
FB
945 }
946}
947
b9b2aaa3 948static uint64_t openpic_src_read(void *opaque, uint64_t addr, unsigned len)
dbda808a 949{
6d544ee8 950 OpenPICState *opp = opaque;
dbda808a
FB
951 uint32_t retval;
952 int idx;
953
4c4f0e48 954 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
dbda808a 955 retval = 0xFFFFFFFF;
e0dfe5b1
SW
956
957 addr = addr & 0xffff;
dbda808a 958 idx = addr >> 5;
e0dfe5b1
SW
959
960 switch (addr & 0x1f) {
961 case 0x00:
be7c236f 962 retval = read_IRQreg_ivpr(opp, idx);
e0dfe5b1
SW
963 break;
964 case 0x10:
965 retval = read_IRQreg_idr(opp, idx);
966 break;
967 case 0x18:
968 retval = read_IRQreg_ilr(opp, idx);
969 break;
dbda808a 970 }
dbda808a 971
e0dfe5b1 972 DPRINTF("%s: => 0x%08x\n", __func__, retval);
dbda808a
FB
973 return retval;
974}
975
732aa6ec
AG
976static void openpic_msi_write(void *opaque, hwaddr addr, uint64_t val,
977 unsigned size)
978{
979 OpenPICState *opp = opaque;
980 int idx = opp->irq_msi;
981 int srs, ibs;
982
4c4f0e48
SW
983 DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64 "\n",
984 __func__, addr, val);
732aa6ec
AG
985 if (addr & 0xF) {
986 return;
987 }
988
989 switch (addr) {
990 case MSIIR_OFFSET:
991 srs = val >> MSIIR_SRS_SHIFT;
992 idx += srs;
993 ibs = (val & MSIIR_IBS_MASK) >> MSIIR_IBS_SHIFT;
994 opp->msi[srs].msir |= 1 << ibs;
995 openpic_set_irq(opp, idx, 1);
996 break;
997 default:
998 /* most registers are read-only, thus ignored */
999 break;
1000 }
1001}
1002
1003static uint64_t openpic_msi_read(void *opaque, hwaddr addr, unsigned size)
1004{
1005 OpenPICState *opp = opaque;
1006 uint64_t r = 0;
1007 int i, srs;
1008
4c4f0e48 1009 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
732aa6ec
AG
1010 if (addr & 0xF) {
1011 return -1;
1012 }
1013
1014 srs = addr >> 4;
1015
1016 switch (addr) {
1017 case 0x00:
1018 case 0x10:
1019 case 0x20:
1020 case 0x30:
1021 case 0x40:
1022 case 0x50:
1023 case 0x60:
1024 case 0x70: /* MSIRs */
1025 r = opp->msi[srs].msir;
1026 /* Clear on read */
1027 opp->msi[srs].msir = 0;
e99fd8af 1028 openpic_set_irq(opp, opp->irq_msi + srs, 0);
732aa6ec
AG
1029 break;
1030 case 0x120: /* MSISR */
1031 for (i = 0; i < MAX_MSI; i++) {
1032 r |= (opp->msi[i].msir ? 1 : 0) << i;
1033 }
1034 break;
1035 }
1036
1037 return r;
1038}
1039
e0dfe5b1
SW
1040static uint64_t openpic_summary_read(void *opaque, hwaddr addr, unsigned size)
1041{
1042 uint64_t r = 0;
1043
1044 DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
1045
1046 /* TODO: EISR/EIMR */
1047
1048 return r;
1049}
1050
1051static void openpic_summary_write(void *opaque, hwaddr addr, uint64_t val,
1052 unsigned size)
1053{
1054 DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64 "\n",
1055 __func__, addr, val);
1056
1057 /* TODO: EISR/EIMR */
1058}
1059
a8170e5e 1060static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
704c7e5d 1061 uint32_t val, int idx)
dbda808a 1062{
6d544ee8 1063 OpenPICState *opp = opaque;
af7e9e74
AG
1064 IRQSource *src;
1065 IRQDest *dst;
704c7e5d 1066 int s_IRQ, n_IRQ;
dbda808a 1067
4c4f0e48 1068 DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx " <= 0x%08x\n", __func__, idx,
704c7e5d 1069 addr, val);
c3203fa5
SW
1070
1071 if (idx < 0) {
dbda808a 1072 return;
c3203fa5
SW
1073 }
1074
af7e9e74 1075 if (addr & 0xF) {
dbda808a 1076 return;
af7e9e74 1077 }
dbda808a
FB
1078 dst = &opp->dst[idx];
1079 addr &= 0xFF0;
1080 switch (addr) {
704c7e5d 1081 case 0x40: /* IPIDR */
dbda808a
FB
1082 case 0x50:
1083 case 0x60:
1084 case 0x70:
1085 idx = (addr - 0x40) >> 4;
a675155e 1086 /* we use IDE as mask which CPUs to deliver the IPI to still. */
f40c360c 1087 opp->src[opp->irq_ipi0 + idx].destmask |= val;
b7169916
AJ
1088 openpic_set_irq(opp, opp->irq_ipi0 + idx, 1);
1089 openpic_set_irq(opp, opp->irq_ipi0 + idx, 0);
dbda808a 1090 break;
be7c236f
SW
1091 case 0x80: /* CTPR */
1092 dst->ctpr = val & 0x0000000F;
9f1d4b1d
SW
1093
1094 DPRINTF("%s: set CPU %d ctpr to %d, raised %d servicing %d\n",
1095 __func__, idx, dst->ctpr, dst->raised.priority,
1096 dst->servicing.priority);
1097
1098 if (dst->raised.priority <= dst->ctpr) {
1099 DPRINTF("%s: Lower OpenPIC INT output cpu %d due to ctpr\n",
1100 __func__, idx);
1101 qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
1102 } else if (dst->raised.priority > dst->servicing.priority) {
1103 DPRINTF("%s: Raise OpenPIC INT output cpu %d irq %d\n",
1104 __func__, idx, dst->raised.next);
1105 qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_INT]);
1106 }
1107
060fbfe1 1108 break;
dbda808a 1109 case 0x90: /* WHOAMI */
060fbfe1
AJ
1110 /* Read-only register */
1111 break;
be7c236f 1112 case 0xA0: /* IACK */
060fbfe1
AJ
1113 /* Read-only register */
1114 break;
be7c236f
SW
1115 case 0xB0: /* EOI */
1116 DPRINTF("EOI\n");
060fbfe1 1117 s_IRQ = IRQ_get_next(opp, &dst->servicing);
65b9d0d5
SW
1118
1119 if (s_IRQ < 0) {
1120 DPRINTF("%s: EOI with no interrupt in service\n", __func__);
1121 break;
1122 }
1123
060fbfe1 1124 IRQ_resetbit(&dst->servicing, s_IRQ);
060fbfe1
AJ
1125 /* Set up next servicing IRQ */
1126 s_IRQ = IRQ_get_next(opp, &dst->servicing);
e9df014c
JM
1127 /* Check queued interrupts. */
1128 n_IRQ = IRQ_get_next(opp, &dst->raised);
1129 src = &opp->src[n_IRQ];
1130 if (n_IRQ != -1 &&
1131 (s_IRQ == -1 ||
be7c236f 1132 IVPR_PRIORITY(src->ivpr) > dst->servicing.priority)) {
e9df014c
JM
1133 DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n",
1134 idx, n_IRQ);
5e22c276 1135 qemu_irq_raise(opp->dst[idx].irqs[OPENPIC_OUTPUT_INT]);
e9df014c 1136 }
060fbfe1 1137 break;
dbda808a
FB
1138 default:
1139 break;
1140 }
1141}
1142
b9b2aaa3
AG
1143static void openpic_cpu_write(void *opaque, hwaddr addr, uint64_t val,
1144 unsigned len)
704c7e5d
AG
1145{
1146 openpic_cpu_write_internal(opaque, addr, val, (addr & 0x1f000) >> 12);
1147}
1148
a898a8fc
SW
1149
1150static uint32_t openpic_iack(OpenPICState *opp, IRQDest *dst, int cpu)
1151{
1152 IRQSource *src;
1153 int retval, irq;
1154
1155 DPRINTF("Lower OpenPIC INT output\n");
1156 qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
1157
1158 irq = IRQ_get_next(opp, &dst->raised);
1159 DPRINTF("IACK: irq=%d\n", irq);
1160
1161 if (irq == -1) {
1162 /* No more interrupt pending */
1163 return opp->spve;
1164 }
1165
1166 src = &opp->src[irq];
1167 if (!(src->ivpr & IVPR_ACTIVITY_MASK) ||
1168 !(IVPR_PRIORITY(src->ivpr) > dst->ctpr)) {
9f1d4b1d
SW
1169 fprintf(stderr, "%s: bad raised IRQ %d ctpr %d ivpr 0x%08x\n",
1170 __func__, irq, dst->ctpr, src->ivpr);
1171 openpic_update_irq(opp, irq);
a898a8fc
SW
1172 retval = opp->spve;
1173 } else {
1174 /* IRQ enter servicing state */
1175 IRQ_setbit(&dst->servicing, irq);
1176 retval = IVPR_VECTOR(opp, src->ivpr);
1177 }
9f1d4b1d 1178
a898a8fc
SW
1179 if (!src->level) {
1180 /* edge-sensitive IRQ */
1181 src->ivpr &= ~IVPR_ACTIVITY_MASK;
1182 src->pending = 0;
9f1d4b1d 1183 IRQ_resetbit(&dst->raised, irq);
a898a8fc
SW
1184 }
1185
8935a442 1186 if ((irq >= opp->irq_ipi0) && (irq < (opp->irq_ipi0 + OPENPIC_MAX_IPI))) {
f40c360c
SW
1187 src->destmask &= ~(1 << cpu);
1188 if (src->destmask && !src->level) {
a898a8fc
SW
1189 /* trigger on CPUs that didn't know about it yet */
1190 openpic_set_irq(opp, irq, 1);
1191 openpic_set_irq(opp, irq, 0);
1192 /* if all CPUs knew about it, set active bit again */
1193 src->ivpr |= IVPR_ACTIVITY_MASK;
1194 }
1195 }
1196
1197 return retval;
1198}
1199
a8170e5e 1200static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
704c7e5d 1201 int idx)
dbda808a 1202{
6d544ee8 1203 OpenPICState *opp = opaque;
af7e9e74 1204 IRQDest *dst;
dbda808a 1205 uint32_t retval;
3b46e624 1206
4c4f0e48 1207 DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx "\n", __func__, idx, addr);
dbda808a 1208 retval = 0xFFFFFFFF;
c3203fa5
SW
1209
1210 if (idx < 0) {
1211 return retval;
1212 }
1213
af7e9e74 1214 if (addr & 0xF) {
dbda808a 1215 return retval;
af7e9e74 1216 }
dbda808a
FB
1217 dst = &opp->dst[idx];
1218 addr &= 0xFF0;
1219 switch (addr) {
be7c236f
SW
1220 case 0x80: /* CTPR */
1221 retval = dst->ctpr;
060fbfe1 1222 break;
dbda808a 1223 case 0x90: /* WHOAMI */
060fbfe1
AJ
1224 retval = idx;
1225 break;
be7c236f 1226 case 0xA0: /* IACK */
a898a8fc 1227 retval = openpic_iack(opp, dst, idx);
060fbfe1 1228 break;
be7c236f 1229 case 0xB0: /* EOI */
060fbfe1
AJ
1230 retval = 0;
1231 break;
dbda808a
FB
1232 default:
1233 break;
1234 }
4c4f0e48 1235 DPRINTF("%s: => 0x%08x\n", __func__, retval);
dbda808a
FB
1236
1237 return retval;
1238}
1239
b9b2aaa3 1240static uint64_t openpic_cpu_read(void *opaque, hwaddr addr, unsigned len)
704c7e5d
AG
1241{
1242 return openpic_cpu_read_internal(opaque, addr, (addr & 0x1f000) >> 12);
1243}
1244
35732cb4 1245static const MemoryRegionOps openpic_glb_ops_le = {
780d16b7
AG
1246 .write = openpic_gbl_write,
1247 .read = openpic_gbl_read,
1248 .endianness = DEVICE_LITTLE_ENDIAN,
1249 .impl = {
1250 .min_access_size = 4,
1251 .max_access_size = 4,
1252 },
1253};
dbda808a 1254
35732cb4
AG
1255static const MemoryRegionOps openpic_glb_ops_be = {
1256 .write = openpic_gbl_write,
1257 .read = openpic_gbl_read,
1258 .endianness = DEVICE_BIG_ENDIAN,
1259 .impl = {
1260 .min_access_size = 4,
1261 .max_access_size = 4,
1262 },
1263};
1264
1265static const MemoryRegionOps openpic_tmr_ops_le = {
6d544ee8
AG
1266 .write = openpic_tmr_write,
1267 .read = openpic_tmr_read,
780d16b7
AG
1268 .endianness = DEVICE_LITTLE_ENDIAN,
1269 .impl = {
1270 .min_access_size = 4,
1271 .max_access_size = 4,
1272 },
1273};
dbda808a 1274
35732cb4 1275static const MemoryRegionOps openpic_tmr_ops_be = {
6d544ee8
AG
1276 .write = openpic_tmr_write,
1277 .read = openpic_tmr_read,
35732cb4
AG
1278 .endianness = DEVICE_BIG_ENDIAN,
1279 .impl = {
1280 .min_access_size = 4,
1281 .max_access_size = 4,
1282 },
1283};
1284
1285static const MemoryRegionOps openpic_cpu_ops_le = {
780d16b7
AG
1286 .write = openpic_cpu_write,
1287 .read = openpic_cpu_read,
1288 .endianness = DEVICE_LITTLE_ENDIAN,
1289 .impl = {
1290 .min_access_size = 4,
1291 .max_access_size = 4,
1292 },
1293};
dbda808a 1294
35732cb4
AG
1295static const MemoryRegionOps openpic_cpu_ops_be = {
1296 .write = openpic_cpu_write,
1297 .read = openpic_cpu_read,
1298 .endianness = DEVICE_BIG_ENDIAN,
1299 .impl = {
1300 .min_access_size = 4,
1301 .max_access_size = 4,
1302 },
1303};
1304
1305static const MemoryRegionOps openpic_src_ops_le = {
780d16b7
AG
1306 .write = openpic_src_write,
1307 .read = openpic_src_read,
23c5e4ca 1308 .endianness = DEVICE_LITTLE_ENDIAN,
b9b2aaa3
AG
1309 .impl = {
1310 .min_access_size = 4,
1311 .max_access_size = 4,
1312 },
23c5e4ca
AK
1313};
1314
35732cb4
AG
1315static const MemoryRegionOps openpic_src_ops_be = {
1316 .write = openpic_src_write,
1317 .read = openpic_src_read,
1318 .endianness = DEVICE_BIG_ENDIAN,
1319 .impl = {
1320 .min_access_size = 4,
1321 .max_access_size = 4,
1322 },
1323};
1324
e0dfe5b1 1325static const MemoryRegionOps openpic_msi_ops_be = {
732aa6ec
AG
1326 .read = openpic_msi_read,
1327 .write = openpic_msi_write,
e0dfe5b1 1328 .endianness = DEVICE_BIG_ENDIAN,
732aa6ec
AG
1329 .impl = {
1330 .min_access_size = 4,
1331 .max_access_size = 4,
1332 },
1333};
1334
e0dfe5b1
SW
1335static const MemoryRegionOps openpic_summary_ops_be = {
1336 .read = openpic_summary_read,
1337 .write = openpic_summary_write,
732aa6ec
AG
1338 .endianness = DEVICE_BIG_ENDIAN,
1339 .impl = {
1340 .min_access_size = 4,
1341 .max_access_size = 4,
1342 },
1343};
1344
af7e9e74 1345static void openpic_save_IRQ_queue(QEMUFile* f, IRQQueue *q)
67b55785
BS
1346{
1347 unsigned int i;
1348
e69a17f6
SW
1349 for (i = 0; i < ARRAY_SIZE(q->queue); i++) {
1350 /* Always put the lower half of a 64-bit long first, in case we
1351 * restore on a 32-bit host. The least significant bits correspond
1352 * to lower IRQ numbers in the bitmap.
1353 */
1354 qemu_put_be32(f, (uint32_t)q->queue[i]);
1355#if LONG_MAX > 0x7FFFFFFF
1356 qemu_put_be32(f, (uint32_t)(q->queue[i] >> 32));
1357#endif
1358 }
67b55785
BS
1359
1360 qemu_put_sbe32s(f, &q->next);
1361 qemu_put_sbe32s(f, &q->priority);
1362}
1363
1364static void openpic_save(QEMUFile* f, void *opaque)
1365{
6d544ee8 1366 OpenPICState *opp = (OpenPICState *)opaque;
67b55785
BS
1367 unsigned int i;
1368
be7c236f
SW
1369 qemu_put_be32s(f, &opp->gcr);
1370 qemu_put_be32s(f, &opp->vir);
1371 qemu_put_be32s(f, &opp->pir);
67b55785 1372 qemu_put_be32s(f, &opp->spve);
be7c236f 1373 qemu_put_be32s(f, &opp->tfrr);
67b55785 1374
d0b72631 1375 qemu_put_be32s(f, &opp->nb_cpus);
b7169916
AJ
1376
1377 for (i = 0; i < opp->nb_cpus; i++) {
eb438427 1378 qemu_put_sbe32s(f, &opp->dst[i].ctpr);
67b55785
BS
1379 openpic_save_IRQ_queue(f, &opp->dst[i].raised);
1380 openpic_save_IRQ_queue(f, &opp->dst[i].servicing);
9f1d4b1d
SW
1381 qemu_put_buffer(f, (uint8_t *)&opp->dst[i].outputs_active,
1382 sizeof(opp->dst[i].outputs_active));
67b55785
BS
1383 }
1384
8935a442 1385 for (i = 0; i < OPENPIC_MAX_TMR; i++) {
be7c236f
SW
1386 qemu_put_be32s(f, &opp->timers[i].tccr);
1387 qemu_put_be32s(f, &opp->timers[i].tbcr);
67b55785 1388 }
5e22c276
SW
1389
1390 for (i = 0; i < opp->max_irq; i++) {
1391 qemu_put_be32s(f, &opp->src[i].ivpr);
1392 qemu_put_be32s(f, &opp->src[i].idr);
f40c360c 1393 qemu_get_be32s(f, &opp->src[i].destmask);
5e22c276
SW
1394 qemu_put_sbe32s(f, &opp->src[i].last_cpu);
1395 qemu_put_sbe32s(f, &opp->src[i].pending);
67b55785 1396 }
67b55785
BS
1397}
1398
af7e9e74 1399static void openpic_load_IRQ_queue(QEMUFile* f, IRQQueue *q)
67b55785
BS
1400{
1401 unsigned int i;
1402
e69a17f6
SW
1403 for (i = 0; i < ARRAY_SIZE(q->queue); i++) {
1404 unsigned long val;
1405
1406 val = qemu_get_be32(f);
1407#if LONG_MAX > 0x7FFFFFFF
1408 val <<= 32;
1409 val |= qemu_get_be32(f);
1410#endif
1411
1412 q->queue[i] = val;
1413 }
67b55785
BS
1414
1415 qemu_get_sbe32s(f, &q->next);
1416 qemu_get_sbe32s(f, &q->priority);
1417}
1418
1419static int openpic_load(QEMUFile* f, void *opaque, int version_id)
1420{
6d544ee8 1421 OpenPICState *opp = (OpenPICState *)opaque;
67b55785
BS
1422 unsigned int i;
1423
af7e9e74 1424 if (version_id != 1) {
67b55785 1425 return -EINVAL;
af7e9e74 1426 }
67b55785 1427
be7c236f
SW
1428 qemu_get_be32s(f, &opp->gcr);
1429 qemu_get_be32s(f, &opp->vir);
1430 qemu_get_be32s(f, &opp->pir);
67b55785 1431 qemu_get_be32s(f, &opp->spve);
be7c236f 1432 qemu_get_be32s(f, &opp->tfrr);
67b55785 1433
d0b72631 1434 qemu_get_be32s(f, &opp->nb_cpus);
b7169916
AJ
1435
1436 for (i = 0; i < opp->nb_cpus; i++) {
eb438427 1437 qemu_get_sbe32s(f, &opp->dst[i].ctpr);
67b55785
BS
1438 openpic_load_IRQ_queue(f, &opp->dst[i].raised);
1439 openpic_load_IRQ_queue(f, &opp->dst[i].servicing);
9f1d4b1d
SW
1440 qemu_get_buffer(f, (uint8_t *)&opp->dst[i].outputs_active,
1441 sizeof(opp->dst[i].outputs_active));
67b55785
BS
1442 }
1443
8935a442 1444 for (i = 0; i < OPENPIC_MAX_TMR; i++) {
be7c236f
SW
1445 qemu_get_be32s(f, &opp->timers[i].tccr);
1446 qemu_get_be32s(f, &opp->timers[i].tbcr);
67b55785
BS
1447 }
1448
5e22c276
SW
1449 for (i = 0; i < opp->max_irq; i++) {
1450 uint32_t val;
67b55785 1451
5e22c276
SW
1452 val = qemu_get_be32(f);
1453 write_IRQreg_idr(opp, i, val);
1454 val = qemu_get_be32(f);
1455 write_IRQreg_ivpr(opp, i, val);
5861a338 1456
5e22c276
SW
1457 qemu_get_be32s(f, &opp->src[i].ivpr);
1458 qemu_get_be32s(f, &opp->src[i].idr);
f40c360c 1459 qemu_get_be32s(f, &opp->src[i].destmask);
5e22c276
SW
1460 qemu_get_sbe32s(f, &opp->src[i].last_cpu);
1461 qemu_get_sbe32s(f, &opp->src[i].pending);
5861a338 1462 }
5e22c276
SW
1463
1464 return 0;
b7169916
AJ
1465}
1466
af7e9e74 1467typedef struct MemReg {
d0b72631
AG
1468 const char *name;
1469 MemoryRegionOps const *ops;
1470 hwaddr start_addr;
1471 ram_addr_t size;
af7e9e74 1472} MemReg;
d0b72631 1473
e0dfe5b1
SW
1474static void fsl_common_init(OpenPICState *opp)
1475{
1476 int i;
8935a442 1477 int virq = OPENPIC_MAX_SRC;
e0dfe5b1
SW
1478
1479 opp->vid = VID_REVISION_1_2;
1480 opp->vir = VIR_GENERIC;
1481 opp->vector_mask = 0xFFFF;
1482 opp->tfrr_reset = 0;
1483 opp->ivpr_reset = IVPR_MASK_MASK;
1484 opp->idr_reset = 1 << 0;
8935a442 1485 opp->max_irq = OPENPIC_MAX_IRQ;
e0dfe5b1
SW
1486
1487 opp->irq_ipi0 = virq;
8935a442 1488 virq += OPENPIC_MAX_IPI;
e0dfe5b1 1489 opp->irq_tim0 = virq;
8935a442 1490 virq += OPENPIC_MAX_TMR;
e0dfe5b1 1491
8935a442 1492 assert(virq <= OPENPIC_MAX_IRQ);
e0dfe5b1
SW
1493
1494 opp->irq_msi = 224;
1495
1496 msi_supported = true;
1497 for (i = 0; i < opp->fsl->max_ext; i++) {
1498 opp->src[i].level = false;
1499 }
1500
1501 /* Internal interrupts, including message and MSI */
8935a442 1502 for (i = 16; i < OPENPIC_MAX_SRC; i++) {
e0dfe5b1
SW
1503 opp->src[i].type = IRQ_TYPE_FSLINT;
1504 opp->src[i].level = true;
1505 }
1506
1507 /* timers and IPIs */
8935a442 1508 for (i = OPENPIC_MAX_SRC; i < virq; i++) {
e0dfe5b1
SW
1509 opp->src[i].type = IRQ_TYPE_FSLSPECIAL;
1510 opp->src[i].level = false;
1511 }
1512}
1513
1514static void map_list(OpenPICState *opp, const MemReg *list, int *count)
1515{
1516 while (list->name) {
1517 assert(*count < ARRAY_SIZE(opp->sub_io_mem));
1518
2c9b15ca 1519 memory_region_init_io(&opp->sub_io_mem[*count], NULL, list->ops, opp,
e0dfe5b1
SW
1520 list->name, list->size);
1521
1522 memory_region_add_subregion(&opp->mem, list->start_addr,
1523 &opp->sub_io_mem[*count]);
1524
1525 (*count)++;
1526 list++;
1527 }
1528}
1529
cbe72019 1530static void openpic_init(Object *obj)
dbda808a 1531{
cbe72019
AF
1532 OpenPICState *opp = OPENPIC(obj);
1533
2c9b15ca 1534 memory_region_init(&opp->mem, NULL, "openpic", 0x40000);
cbe72019
AF
1535}
1536
1537static void openpic_realize(DeviceState *dev, Error **errp)
1538{
1539 SysBusDevice *d = SYS_BUS_DEVICE(dev);
e1766344 1540 OpenPICState *opp = OPENPIC(dev);
d0b72631 1541 int i, j;
e0dfe5b1
SW
1542 int list_count = 0;
1543 static const MemReg list_le[] = {
1544 {"glb", &openpic_glb_ops_le,
732aa6ec 1545 OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
e0dfe5b1 1546 {"tmr", &openpic_tmr_ops_le,
732aa6ec 1547 OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
e0dfe5b1 1548 {"src", &openpic_src_ops_le,
732aa6ec 1549 OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
e0dfe5b1 1550 {"cpu", &openpic_cpu_ops_le,
732aa6ec 1551 OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
e0dfe5b1 1552 {NULL}
780d16b7 1553 };
e0dfe5b1
SW
1554 static const MemReg list_be[] = {
1555 {"glb", &openpic_glb_ops_be,
732aa6ec 1556 OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
e0dfe5b1 1557 {"tmr", &openpic_tmr_ops_be,
732aa6ec 1558 OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
e0dfe5b1 1559 {"src", &openpic_src_ops_be,
732aa6ec 1560 OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
e0dfe5b1 1561 {"cpu", &openpic_cpu_ops_be,
732aa6ec 1562 OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
e0dfe5b1 1563 {NULL}
d0b72631 1564 };
e0dfe5b1
SW
1565 static const MemReg list_fsl[] = {
1566 {"msi", &openpic_msi_ops_be,
1567 OPENPIC_MSI_REG_START, OPENPIC_MSI_REG_SIZE},
1568 {"summary", &openpic_summary_ops_be,
1569 OPENPIC_SUMMARY_REG_START, OPENPIC_SUMMARY_REG_SIZE},
1570 {NULL}
1571 };
1572
d0b72631
AG
1573 switch (opp->model) {
1574 case OPENPIC_MODEL_FSL_MPIC_20:
1575 default:
e0dfe5b1
SW
1576 opp->fsl = &fsl_mpic_20;
1577 opp->brr1 = 0x00400200;
be7c236f 1578 opp->flags |= OPENPIC_FLAG_IDR_CRIT;
d0b72631 1579 opp->nb_irqs = 80;
e0dfe5b1 1580 opp->mpic_mode_mask = GCR_MODE_MIXED;
68c2dd70 1581
e0dfe5b1
SW
1582 fsl_common_init(opp);
1583 map_list(opp, list_be, &list_count);
1584 map_list(opp, list_fsl, &list_count);
6c5e84c2 1585
e0dfe5b1 1586 break;
6c5e84c2 1587
e0dfe5b1
SW
1588 case OPENPIC_MODEL_FSL_MPIC_42:
1589 opp->fsl = &fsl_mpic_42;
1590 opp->brr1 = 0x00400402;
1591 opp->flags |= OPENPIC_FLAG_ILR;
1592 opp->nb_irqs = 196;
1593 opp->mpic_mode_mask = GCR_MODE_PROXY;
6c5e84c2 1594
e0dfe5b1
SW
1595 fsl_common_init(opp);
1596 map_list(opp, list_be, &list_count);
1597 map_list(opp, list_fsl, &list_count);
6c5e84c2 1598
d0b72631 1599 break;
6c5e84c2 1600
d0b72631
AG
1601 case OPENPIC_MODEL_RAVEN:
1602 opp->nb_irqs = RAVEN_MAX_EXT;
1603 opp->vid = VID_REVISION_1_3;
be7c236f 1604 opp->vir = VIR_GENERIC;
0fe04622 1605 opp->vector_mask = 0xFF;
be7c236f
SW
1606 opp->tfrr_reset = 4160000;
1607 opp->ivpr_reset = IVPR_MASK_MASK | IVPR_MODE_MASK;
1608 opp->idr_reset = 0;
d0b72631
AG
1609 opp->max_irq = RAVEN_MAX_IRQ;
1610 opp->irq_ipi0 = RAVEN_IPI_IRQ;
1611 opp->irq_tim0 = RAVEN_TMR_IRQ;
dbbbfd60 1612 opp->brr1 = -1;
86e56a88 1613 opp->mpic_mode_mask = GCR_MODE_MIXED;
d0b72631 1614
d0b72631 1615 if (opp->nb_cpus != 1) {
cbe72019
AF
1616 error_setg(errp, "Only UP supported today");
1617 return;
d0b72631 1618 }
780d16b7 1619
e0dfe5b1
SW
1620 map_list(opp, list_le, &list_count);
1621 break;
780d16b7 1622 }
3b46e624 1623
d0b72631
AG
1624 for (i = 0; i < opp->nb_cpus; i++) {
1625 opp->dst[i].irqs = g_new(qemu_irq, OPENPIC_OUTPUT_NB);
1626 for (j = 0; j < OPENPIC_OUTPUT_NB; j++) {
cbe72019 1627 sysbus_init_irq(d, &opp->dst[i].irqs[j]);
d0b72631
AG
1628 }
1629 }
1630
cbe72019 1631 register_savevm(dev, "openpic", 0, 2,
0be71e32 1632 openpic_save, openpic_load, opp);
b7169916 1633
cbe72019
AF
1634 sysbus_init_mmio(d, &opp->mem);
1635 qdev_init_gpio_in(dev, openpic_set_irq, opp->max_irq);
b7169916
AJ
1636}
1637
d0b72631
AG
1638static Property openpic_properties[] = {
1639 DEFINE_PROP_UINT32("model", OpenPICState, model, OPENPIC_MODEL_FSL_MPIC_20),
1640 DEFINE_PROP_UINT32("nb_cpus", OpenPICState, nb_cpus, 1),
1641 DEFINE_PROP_END_OF_LIST(),
1642};
71cf9e62 1643
cbe72019 1644static void openpic_class_init(ObjectClass *oc, void *data)
d0b72631 1645{
cbe72019 1646 DeviceClass *dc = DEVICE_CLASS(oc);
b7169916 1647
cbe72019 1648 dc->realize = openpic_realize;
d0b72631
AG
1649 dc->props = openpic_properties;
1650 dc->reset = openpic_reset;
1651}
71cf9e62 1652
8c43a6f0 1653static const TypeInfo openpic_info = {
e1766344 1654 .name = TYPE_OPENPIC,
d0b72631
AG
1655 .parent = TYPE_SYS_BUS_DEVICE,
1656 .instance_size = sizeof(OpenPICState),
cbe72019 1657 .instance_init = openpic_init,
d0b72631
AG
1658 .class_init = openpic_class_init,
1659};
b7169916 1660
d0b72631
AG
1661static void openpic_register_types(void)
1662{
1663 type_register_static(&openpic_info);
dbda808a 1664}
d0b72631
AG
1665
1666type_init(openpic_register_types)