4 * Copyright (c) 2004 Jocelyn Mayer
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
27 * Based on OpenPic implementations:
28 * - Intel GW80314 I/O companion chip developer's manual
29 * - Motorola MPC8245 & MPC8540 user manuals.
30 * - Motorola MCP750 (aka Raven) programmer manual.
31 * - Motorola Harrier programmer manuel
33 * Serial interrupts, as implemented in Raven chipset are not supported yet.
43 //#define DEBUG_OPENPIC
46 #define DPRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
48 #define DPRINTF(fmt, ...) do { } while (0)
57 #define MAX_IRQ (MAX_SRC + MAX_IPI + MAX_TMR)
58 #define VID 0x03 /* MPIC version ID */
60 /* OpenPIC capability flags */
61 #define OPENPIC_FLAG_IDE_CRIT (1 << 0)
63 /* OpenPIC address map */
64 #define OPENPIC_GLB_REG_START 0x0
65 #define OPENPIC_GLB_REG_SIZE 0x10F0
66 #define OPENPIC_TMR_REG_START 0x10F0
67 #define OPENPIC_TMR_REG_SIZE 0x220
68 #define OPENPIC_MSI_REG_START 0x1600
69 #define OPENPIC_MSI_REG_SIZE 0x200
70 #define OPENPIC_SRC_REG_START 0x10000
71 #define OPENPIC_SRC_REG_SIZE (MAX_SRC * 0x20)
72 #define OPENPIC_CPU_REG_START 0x20000
73 #define OPENPIC_CPU_REG_SIZE 0x100 + ((MAX_CPU - 1) * 0x1000)
76 #define RAVEN_MAX_CPU 2
77 #define RAVEN_MAX_EXT 48
78 #define RAVEN_MAX_IRQ 64
79 #define RAVEN_MAX_TMR MAX_TMR
80 #define RAVEN_MAX_IPI MAX_IPI
82 /* Interrupt definitions */
83 #define RAVEN_FE_IRQ (RAVEN_MAX_EXT) /* Internal functional IRQ */
84 #define RAVEN_ERR_IRQ (RAVEN_MAX_EXT + 1) /* Error IRQ */
85 #define RAVEN_TMR_IRQ (RAVEN_MAX_EXT + 2) /* First timer IRQ */
86 #define RAVEN_IPI_IRQ (RAVEN_TMR_IRQ + RAVEN_MAX_TMR) /* First IPI IRQ */
87 /* First doorbell IRQ */
88 #define RAVEN_DBL_IRQ (RAVEN_IPI_IRQ + (RAVEN_MAX_CPU * RAVEN_MAX_IPI))
91 #define FSL_MPIC_20_MAX_CPU 1
92 #define FSL_MPIC_20_MAX_EXT 12
93 #define FSL_MPIC_20_MAX_INT 64
94 #define FSL_MPIC_20_MAX_IRQ MAX_IRQ
96 /* Interrupt definitions */
97 /* IRQs, accessible through the IRQ region */
98 #define FSL_MPIC_20_EXT_IRQ 0x00
99 #define FSL_MPIC_20_INT_IRQ 0x10
100 #define FSL_MPIC_20_MSG_IRQ 0xb0
101 #define FSL_MPIC_20_MSI_IRQ 0xe0
102 /* These are available through separate regions, but
103 for simplicity's sake mapped into the same number space */
104 #define FSL_MPIC_20_TMR_IRQ 0x100
105 #define FSL_MPIC_20_IPI_IRQ 0x104
108 * Block Revision Register1 (BRR1): QEMU does not fully emulate
109 * any version on MPIC. So to start with, set the IP version to 0.
111 * NOTE: This is Freescale MPIC specific register. Keep it here till
112 * this code is refactored for different variants of OPENPIC and MPIC.
114 #define FSL_BRR1_IPID (0x0040 << 16) /* 16 bit IP-block ID */
115 #define FSL_BRR1_IPMJ (0x00 << 8) /* 8 bit IP major number */
116 #define FSL_BRR1_IPMN 0x00 /* 8 bit IP minor number */
118 #define FREP_NIRQ_SHIFT 16
119 #define FREP_NCPU_SHIFT 8
120 #define FREP_VID_SHIFT 0
122 #define VID_REVISION_1_2 2
123 #define VID_REVISION_1_3 3
125 #define VENI_GENERIC 0x00000000 /* Generic Vendor ID */
127 #define IDR_EP_SHIFT 31
128 #define IDR_EP_MASK (1 << IDR_EP_SHIFT)
129 #define IDR_CI0_SHIFT 30
130 #define IDR_CI1_SHIFT 29
131 #define IDR_P1_SHIFT 1
132 #define IDR_P0_SHIFT 0
134 #define MSIIR_OFFSET 0x140
135 #define MSIIR_SRS_SHIFT 29
136 #define MSIIR_SRS_MASK (0x7 << MSIIR_SRS_SHIFT)
137 #define MSIIR_IBS_SHIFT 24
138 #define MSIIR_IBS_MASK (0x1f << MSIIR_IBS_SHIFT)
140 #define BF_WIDTH(_bits_) \
141 (((_bits_) + (sizeof(uint32_t) * 8) - 1) / (sizeof(uint32_t) * 8))
143 static inline void set_bit(uint32_t *field
, int bit
)
145 field
[bit
>> 5] |= 1 << (bit
& 0x1F);
148 static inline void reset_bit(uint32_t *field
, int bit
)
150 field
[bit
>> 5] &= ~(1 << (bit
& 0x1F));
153 static inline int test_bit(uint32_t *field
, int bit
)
155 return (field
[bit
>> 5] & 1 << (bit
& 0x1F)) != 0;
158 static int get_current_cpu(void)
160 return cpu_single_env
->cpu_index
;
163 static uint32_t openpic_cpu_read_internal(void *opaque
, hwaddr addr
,
165 static void openpic_cpu_write_internal(void *opaque
, hwaddr addr
,
166 uint32_t val
, int idx
);
168 typedef struct IRQ_queue_t
{
169 uint32_t queue
[BF_WIDTH(MAX_IRQ
)];
172 int pending
; /* nr of pending bits in queue */
175 typedef struct IRQ_src_t
{
176 uint32_t ipvp
; /* IRQ vector/priority register */
177 uint32_t ide
; /* IRQ destination register */
179 int pending
; /* TRUE if IRQ is pending */
182 #define IPVP_MASK_SHIFT 31
183 #define IPVP_MASK_MASK (1 << IPVP_MASK_SHIFT)
184 #define IPVP_ACTIVITY_SHIFT 30
185 #define IPVP_ACTIVITY_MASK (1 << IPVP_ACTIVITY_SHIFT)
186 #define IPVP_MODE_SHIFT 29
187 #define IPVP_MODE_MASK (1 << IPVP_MODE_SHIFT)
188 #define IPVP_POLARITY_SHIFT 23
189 #define IPVP_POLARITY_MASK (1 << IPVP_POLARITY_SHIFT)
190 #define IPVP_SENSE_SHIFT 22
191 #define IPVP_SENSE_MASK (1 << IPVP_SENSE_SHIFT)
193 #define IPVP_PRIORITY_MASK (0x1F << 16)
194 #define IPVP_PRIORITY(_ipvpr_) ((int)(((_ipvpr_) & IPVP_PRIORITY_MASK) >> 16))
195 #define IPVP_VECTOR_MASK ((1 << VECTOR_BITS) - 1)
196 #define IPVP_VECTOR(_ipvpr_) ((_ipvpr_) & IPVP_VECTOR_MASK)
198 typedef struct IRQ_dst_t
{
199 uint32_t pctp
; /* CPU current task priority */
200 uint32_t pcsr
; /* CPU sensitivity register */
202 IRQ_queue_t servicing
;
206 typedef struct OpenPICState
{
210 /* Behavior control */
215 uint32_t veni
; /* Vendor identification register */
223 MemoryRegion sub_io_mem
[5];
225 /* Global registers */
226 uint32_t frep
; /* Feature reporting register */
227 uint32_t glbc
; /* Global configuration register */
228 uint32_t pint
; /* Processor initialization register */
229 uint32_t spve
; /* Spurious vector register */
230 uint32_t tifr
; /* Timer frequency reporting register */
231 /* Source registers */
232 IRQ_src_t src
[MAX_IRQ
];
233 /* Local registers per output pin */
234 IRQ_dst_t dst
[MAX_CPU
];
236 /* Timer registers */
238 uint32_t ticc
; /* Global timer current count register */
239 uint32_t tibc
; /* Global timer base count register */
241 /* Shared MSI registers */
243 uint32_t msir
; /* Shared Message Signaled Interrupt Register */
251 static void openpic_irq_raise(OpenPICState
*opp
, int n_CPU
, IRQ_src_t
*src
);
253 static inline void IRQ_setbit(IRQ_queue_t
*q
, int n_IRQ
)
256 set_bit(q
->queue
, n_IRQ
);
259 static inline void IRQ_resetbit(IRQ_queue_t
*q
, int n_IRQ
)
262 reset_bit(q
->queue
, n_IRQ
);
265 static inline int IRQ_testbit(IRQ_queue_t
*q
, int n_IRQ
)
267 return test_bit(q
->queue
, n_IRQ
);
270 static void IRQ_check(OpenPICState
*opp
, IRQ_queue_t
*q
)
279 /* IRQ bitmap is empty */
283 for (i
= 0; i
< opp
->max_irq
; i
++) {
284 if (IRQ_testbit(q
, i
)) {
285 DPRINTF("IRQ_check: irq %d set ipvp_pr=%d pr=%d\n",
286 i
, IPVP_PRIORITY(opp
->src
[i
].ipvp
), priority
);
287 if (IPVP_PRIORITY(opp
->src
[i
].ipvp
) > priority
) {
289 priority
= IPVP_PRIORITY(opp
->src
[i
].ipvp
);
296 q
->priority
= priority
;
299 static int IRQ_get_next(OpenPICState
*opp
, IRQ_queue_t
*q
)
309 static void IRQ_local_pipe(OpenPICState
*opp
, int n_CPU
, int n_IRQ
)
315 dst
= &opp
->dst
[n_CPU
];
316 src
= &opp
->src
[n_IRQ
];
317 priority
= IPVP_PRIORITY(src
->ipvp
);
318 if (priority
<= dst
->pctp
) {
319 /* Too low priority */
320 DPRINTF("%s: IRQ %d has too low priority on CPU %d\n",
321 __func__
, n_IRQ
, n_CPU
);
324 if (IRQ_testbit(&dst
->raised
, n_IRQ
)) {
326 DPRINTF("%s: IRQ %d was missed on CPU %d\n",
327 __func__
, n_IRQ
, n_CPU
);
330 src
->ipvp
|= IPVP_ACTIVITY_MASK
;
331 IRQ_setbit(&dst
->raised
, n_IRQ
);
332 if (priority
< dst
->raised
.priority
) {
333 /* An higher priority IRQ is already raised */
334 DPRINTF("%s: IRQ %d is hidden by raised IRQ %d on CPU %d\n",
335 __func__
, n_IRQ
, dst
->raised
.next
, n_CPU
);
338 IRQ_get_next(opp
, &dst
->raised
);
339 if (IRQ_get_next(opp
, &dst
->servicing
) != -1 &&
340 priority
<= dst
->servicing
.priority
) {
341 DPRINTF("%s: IRQ %d is hidden by servicing IRQ %d on CPU %d\n",
342 __func__
, n_IRQ
, dst
->servicing
.next
, n_CPU
);
343 /* Already servicing a higher priority IRQ */
346 DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n", n_CPU
, n_IRQ
);
347 openpic_irq_raise(opp
, n_CPU
, src
);
350 /* update pic state because registers for n_IRQ have changed value */
351 static void openpic_update_irq(OpenPICState
*opp
, int n_IRQ
)
356 src
= &opp
->src
[n_IRQ
];
360 DPRINTF("%s: IRQ %d is not pending\n", __func__
, n_IRQ
);
363 if (src
->ipvp
& IPVP_MASK_MASK
) {
364 /* Interrupt source is disabled */
365 DPRINTF("%s: IRQ %d is disabled\n", __func__
, n_IRQ
);
368 if (IPVP_PRIORITY(src
->ipvp
) == 0) {
369 /* Priority set to zero */
370 DPRINTF("%s: IRQ %d has 0 priority\n", __func__
, n_IRQ
);
373 if (src
->ipvp
& IPVP_ACTIVITY_MASK
) {
374 /* IRQ already active */
375 DPRINTF("%s: IRQ %d is already active\n", __func__
, n_IRQ
);
378 if (src
->ide
== 0x00000000) {
380 DPRINTF("%s: IRQ %d has no target\n", __func__
, n_IRQ
);
384 if (src
->ide
== (1 << src
->last_cpu
)) {
385 /* Only one CPU is allowed to receive this IRQ */
386 IRQ_local_pipe(opp
, src
->last_cpu
, n_IRQ
);
387 } else if (!(src
->ipvp
& IPVP_MODE_MASK
)) {
388 /* Directed delivery mode */
389 for (i
= 0; i
< opp
->nb_cpus
; i
++) {
390 if (src
->ide
& (1 << i
)) {
391 IRQ_local_pipe(opp
, i
, n_IRQ
);
395 /* Distributed delivery mode */
396 for (i
= src
->last_cpu
+ 1; i
!= src
->last_cpu
; i
++) {
397 if (i
== opp
->nb_cpus
)
399 if (src
->ide
& (1 << i
)) {
400 IRQ_local_pipe(opp
, i
, n_IRQ
);
408 static void openpic_set_irq(void *opaque
, int n_IRQ
, int level
)
410 OpenPICState
*opp
= opaque
;
413 src
= &opp
->src
[n_IRQ
];
414 DPRINTF("openpic: set irq %d = %d ipvp=%08x\n",
415 n_IRQ
, level
, src
->ipvp
);
416 if (src
->ipvp
& IPVP_SENSE_MASK
) {
417 /* level-sensitive irq */
418 src
->pending
= level
;
420 src
->ipvp
&= ~IPVP_ACTIVITY_MASK
;
423 /* edge-sensitive irq */
427 openpic_update_irq(opp
, n_IRQ
);
430 static void openpic_reset(DeviceState
*d
)
432 OpenPICState
*opp
= FROM_SYSBUS(typeof (*opp
), sysbus_from_qdev(d
));
435 opp
->glbc
= 0x80000000;
436 /* Initialise controller registers */
437 opp
->frep
= ((opp
->nb_irqs
-1) << FREP_NIRQ_SHIFT
) |
438 ((opp
->nb_cpus
-1) << FREP_NCPU_SHIFT
) |
439 (opp
->vid
<< FREP_VID_SHIFT
);
441 opp
->pint
= 0x00000000;
442 opp
->spve
= -1 & opp
->spve_mask
;
443 opp
->tifr
= opp
->tifr_reset
;
444 /* Initialise IRQ sources */
445 for (i
= 0; i
< opp
->max_irq
; i
++) {
446 opp
->src
[i
].ipvp
= opp
->ipvp_reset
;
447 opp
->src
[i
].ide
= opp
->ide_reset
;
449 /* Initialise IRQ destinations */
450 for (i
= 0; i
< MAX_CPU
; i
++) {
451 opp
->dst
[i
].pctp
= 0x0000000F;
452 opp
->dst
[i
].pcsr
= 0x00000000;
453 memset(&opp
->dst
[i
].raised
, 0, sizeof(IRQ_queue_t
));
454 opp
->dst
[i
].raised
.next
= -1;
455 memset(&opp
->dst
[i
].servicing
, 0, sizeof(IRQ_queue_t
));
456 opp
->dst
[i
].servicing
.next
= -1;
458 /* Initialise timers */
459 for (i
= 0; i
< MAX_TMR
; i
++) {
460 opp
->timers
[i
].ticc
= 0x00000000;
461 opp
->timers
[i
].tibc
= 0x80000000;
463 /* Go out of RESET state */
464 opp
->glbc
= 0x00000000;
467 static inline uint32_t read_IRQreg_ide(OpenPICState
*opp
, int n_IRQ
)
469 return opp
->src
[n_IRQ
].ide
;
472 static inline uint32_t read_IRQreg_ipvp(OpenPICState
*opp
, int n_IRQ
)
474 return opp
->src
[n_IRQ
].ipvp
;
477 static inline void write_IRQreg_ide(OpenPICState
*opp
, int n_IRQ
, uint32_t val
)
481 tmp
= val
& 0xC0000000;
482 tmp
|= val
& ((1ULL << MAX_CPU
) - 1);
483 opp
->src
[n_IRQ
].ide
= tmp
;
484 DPRINTF("Set IDE %d to 0x%08x\n", n_IRQ
, opp
->src
[n_IRQ
].ide
);
487 static inline void write_IRQreg_ipvp(OpenPICState
*opp
, int n_IRQ
, uint32_t val
)
489 /* NOTE: not fully accurate for special IRQs, but simple and sufficient */
490 /* ACTIVITY bit is read-only */
491 opp
->src
[n_IRQ
].ipvp
= (opp
->src
[n_IRQ
].ipvp
& 0x40000000)
492 | (val
& 0x800F00FF);
493 openpic_update_irq(opp
, n_IRQ
);
494 DPRINTF("Set IPVP %d to 0x%08x -> 0x%08x\n", n_IRQ
, val
,
495 opp
->src
[n_IRQ
].ipvp
);
498 static void openpic_gbl_write(void *opaque
, hwaddr addr
, uint64_t val
,
501 OpenPICState
*opp
= opaque
;
505 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
509 case 0x00: /* Block Revision Register1 (BRR1) is Readonly */
519 openpic_cpu_write_internal(opp
, addr
, val
, get_current_cpu());
521 case 0x1000: /* FREP */
523 case 0x1020: /* GLBC */
524 if (val
& 0x80000000) {
525 openpic_reset(&opp
->busdev
.qdev
);
528 case 0x1080: /* VENI */
530 case 0x1090: /* PINT */
531 for (idx
= 0; idx
< opp
->nb_cpus
; idx
++) {
532 if ((val
& (1 << idx
)) && !(opp
->pint
& (1 << idx
))) {
533 DPRINTF("Raise OpenPIC RESET output for CPU %d\n", idx
);
534 dst
= &opp
->dst
[idx
];
535 qemu_irq_raise(dst
->irqs
[OPENPIC_OUTPUT_RESET
]);
536 } else if (!(val
& (1 << idx
)) && (opp
->pint
& (1 << idx
))) {
537 DPRINTF("Lower OpenPIC RESET output for CPU %d\n", idx
);
538 dst
= &opp
->dst
[idx
];
539 qemu_irq_lower(dst
->irqs
[OPENPIC_OUTPUT_RESET
]);
544 case 0x10A0: /* IPI_IPVP */
550 idx
= (addr
- 0x10A0) >> 4;
551 write_IRQreg_ipvp(opp
, opp
->irq_ipi0
+ idx
, val
);
554 case 0x10E0: /* SPVE */
555 opp
->spve
= val
& opp
->spve_mask
;
562 static uint64_t openpic_gbl_read(void *opaque
, hwaddr addr
, unsigned len
)
564 OpenPICState
*opp
= opaque
;
567 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
572 case 0x1000: /* FREP */
575 case 0x1020: /* GLBC */
578 case 0x1080: /* VENI */
581 case 0x1090: /* PINT */
584 case 0x00: /* Block Revision Register1 (BRR1) */
593 retval
= openpic_cpu_read_internal(opp
, addr
, get_current_cpu());
595 case 0x10A0: /* IPI_IPVP */
601 idx
= (addr
- 0x10A0) >> 4;
602 retval
= read_IRQreg_ipvp(opp
, opp
->irq_ipi0
+ idx
);
605 case 0x10E0: /* SPVE */
611 DPRINTF("%s: => %08x\n", __func__
, retval
);
616 static void openpic_tmr_write(void *opaque
, hwaddr addr
, uint64_t val
,
619 OpenPICState
*opp
= opaque
;
622 DPRINTF("%s: addr %08x <= %08x\n", __func__
, addr
, val
);
625 idx
= (addr
>> 6) & 0x3;
633 switch (addr
& 0x30) {
634 case 0x00: /* TICC (GTCCR) */
636 case 0x10: /* TIBC (GTBCR) */
637 if ((opp
->timers
[idx
].ticc
& 0x80000000) != 0 &&
638 (val
& 0x80000000) == 0 &&
639 (opp
->timers
[idx
].tibc
& 0x80000000) != 0)
640 opp
->timers
[idx
].ticc
&= ~0x80000000;
641 opp
->timers
[idx
].tibc
= val
;
643 case 0x20: /* TIVP (GTIVPR) */
644 write_IRQreg_ipvp(opp
, opp
->irq_tim0
+ idx
, val
);
646 case 0x30: /* TIDE (GTIDR) */
647 write_IRQreg_ide(opp
, opp
->irq_tim0
+ idx
, val
);
652 static uint64_t openpic_tmr_read(void *opaque
, hwaddr addr
, unsigned len
)
654 OpenPICState
*opp
= opaque
;
655 uint32_t retval
= -1;
658 DPRINTF("%s: addr %08x\n", __func__
, addr
);
662 idx
= (addr
>> 6) & 0x3;
668 switch (addr
& 0x30) {
669 case 0x00: /* TICC (GTCCR) */
670 retval
= opp
->timers
[idx
].ticc
;
672 case 0x10: /* TIBC (GTBCR) */
673 retval
= opp
->timers
[idx
].tibc
;
675 case 0x20: /* TIPV (TIPV) */
676 retval
= read_IRQreg_ipvp(opp
, opp
->irq_tim0
+ idx
);
678 case 0x30: /* TIDE (TIDR) */
679 retval
= read_IRQreg_ide(opp
, opp
->irq_tim0
+ idx
);
684 DPRINTF("%s: => %08x\n", __func__
, retval
);
689 static void openpic_src_write(void *opaque
, hwaddr addr
, uint64_t val
,
692 OpenPICState
*opp
= opaque
;
695 DPRINTF("%s: addr %08x <= %08x\n", __func__
, addr
, val
);
698 addr
= addr
& 0xFFF0;
701 /* EXDE / IFEDE / IEEDE */
702 write_IRQreg_ide(opp
, idx
, val
);
704 /* EXVP / IFEVP / IEEVP */
705 write_IRQreg_ipvp(opp
, idx
, val
);
709 static uint64_t openpic_src_read(void *opaque
, uint64_t addr
, unsigned len
)
711 OpenPICState
*opp
= opaque
;
715 DPRINTF("%s: addr %08x\n", __func__
, addr
);
719 addr
= addr
& 0xFFF0;
722 /* EXDE / IFEDE / IEEDE */
723 retval
= read_IRQreg_ide(opp
, idx
);
725 /* EXVP / IFEVP / IEEVP */
726 retval
= read_IRQreg_ipvp(opp
, idx
);
728 DPRINTF("%s: => %08x\n", __func__
, retval
);
733 static void openpic_msi_write(void *opaque
, hwaddr addr
, uint64_t val
,
736 OpenPICState
*opp
= opaque
;
737 int idx
= opp
->irq_msi
;
740 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
747 srs
= val
>> MSIIR_SRS_SHIFT
;
749 ibs
= (val
& MSIIR_IBS_MASK
) >> MSIIR_IBS_SHIFT
;
750 opp
->msi
[srs
].msir
|= 1 << ibs
;
751 openpic_set_irq(opp
, idx
, 1);
754 /* most registers are read-only, thus ignored */
759 static uint64_t openpic_msi_read(void *opaque
, hwaddr addr
, unsigned size
)
761 OpenPICState
*opp
= opaque
;
765 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
780 case 0x70: /* MSIRs */
781 r
= opp
->msi
[srs
].msir
;
783 opp
->msi
[srs
].msir
= 0;
785 case 0x120: /* MSISR */
786 for (i
= 0; i
< MAX_MSI
; i
++) {
787 r
|= (opp
->msi
[i
].msir
? 1 : 0) << i
;
795 static void openpic_cpu_write_internal(void *opaque
, hwaddr addr
,
796 uint32_t val
, int idx
)
798 OpenPICState
*opp
= opaque
;
803 DPRINTF("%s: cpu %d addr " TARGET_FMT_plx
" <= %08x\n", __func__
, idx
,
807 dst
= &opp
->dst
[idx
];
810 case 0x40: /* IPIDR */
814 idx
= (addr
- 0x40) >> 4;
815 /* we use IDE as mask which CPUs to deliver the IPI to still. */
816 write_IRQreg_ide(opp
, opp
->irq_ipi0
+ idx
,
817 opp
->src
[opp
->irq_ipi0
+ idx
].ide
| val
);
818 openpic_set_irq(opp
, opp
->irq_ipi0
+ idx
, 1);
819 openpic_set_irq(opp
, opp
->irq_ipi0
+ idx
, 0);
821 case 0x80: /* PCTP */
822 dst
->pctp
= val
& 0x0000000F;
824 case 0x90: /* WHOAMI */
825 /* Read-only register */
827 case 0xA0: /* PIAC */
828 /* Read-only register */
830 case 0xB0: /* PEOI */
832 s_IRQ
= IRQ_get_next(opp
, &dst
->servicing
);
833 IRQ_resetbit(&dst
->servicing
, s_IRQ
);
834 dst
->servicing
.next
= -1;
835 /* Set up next servicing IRQ */
836 s_IRQ
= IRQ_get_next(opp
, &dst
->servicing
);
837 /* Check queued interrupts. */
838 n_IRQ
= IRQ_get_next(opp
, &dst
->raised
);
839 src
= &opp
->src
[n_IRQ
];
842 IPVP_PRIORITY(src
->ipvp
) > dst
->servicing
.priority
)) {
843 DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n",
845 openpic_irq_raise(opp
, idx
, src
);
853 static void openpic_cpu_write(void *opaque
, hwaddr addr
, uint64_t val
,
856 openpic_cpu_write_internal(opaque
, addr
, val
, (addr
& 0x1f000) >> 12);
859 static uint32_t openpic_cpu_read_internal(void *opaque
, hwaddr addr
,
862 OpenPICState
*opp
= opaque
;
868 DPRINTF("%s: cpu %d addr " TARGET_FMT_plx
"\n", __func__
, idx
, addr
);
872 dst
= &opp
->dst
[idx
];
875 case 0x00: /* Block Revision Register1 (BRR1) */
878 case 0x80: /* PCTP */
881 case 0x90: /* WHOAMI */
884 case 0xA0: /* PIAC */
885 DPRINTF("Lower OpenPIC INT output\n");
886 qemu_irq_lower(dst
->irqs
[OPENPIC_OUTPUT_INT
]);
887 n_IRQ
= IRQ_get_next(opp
, &dst
->raised
);
888 DPRINTF("PIAC: irq=%d\n", n_IRQ
);
890 /* No more interrupt pending */
891 retval
= IPVP_VECTOR(opp
->spve
);
893 src
= &opp
->src
[n_IRQ
];
894 if (!(src
->ipvp
& IPVP_ACTIVITY_MASK
) ||
895 !(IPVP_PRIORITY(src
->ipvp
) > dst
->pctp
)) {
896 /* - Spurious level-sensitive IRQ
897 * - Priorities has been changed
898 * and the pending IRQ isn't allowed anymore
900 src
->ipvp
&= ~IPVP_ACTIVITY_MASK
;
901 retval
= IPVP_VECTOR(opp
->spve
);
903 /* IRQ enter servicing state */
904 IRQ_setbit(&dst
->servicing
, n_IRQ
);
905 retval
= IPVP_VECTOR(src
->ipvp
);
907 IRQ_resetbit(&dst
->raised
, n_IRQ
);
908 dst
->raised
.next
= -1;
909 if (!(src
->ipvp
& IPVP_SENSE_MASK
)) {
910 /* edge-sensitive IRQ */
911 src
->ipvp
&= ~IPVP_ACTIVITY_MASK
;
915 if ((n_IRQ
>= opp
->irq_ipi0
) && (n_IRQ
< (opp
->irq_ipi0
+ MAX_IPI
))) {
916 src
->ide
&= ~(1 << idx
);
917 if (src
->ide
&& !(src
->ipvp
& IPVP_SENSE_MASK
)) {
918 /* trigger on CPUs that didn't know about it yet */
919 openpic_set_irq(opp
, n_IRQ
, 1);
920 openpic_set_irq(opp
, n_IRQ
, 0);
921 /* if all CPUs knew about it, set active bit again */
922 src
->ipvp
|= IPVP_ACTIVITY_MASK
;
927 case 0xB0: /* PEOI */
933 DPRINTF("%s: => %08x\n", __func__
, retval
);
938 static uint64_t openpic_cpu_read(void *opaque
, hwaddr addr
, unsigned len
)
940 return openpic_cpu_read_internal(opaque
, addr
, (addr
& 0x1f000) >> 12);
943 static const MemoryRegionOps openpic_glb_ops_le
= {
944 .write
= openpic_gbl_write
,
945 .read
= openpic_gbl_read
,
946 .endianness
= DEVICE_LITTLE_ENDIAN
,
948 .min_access_size
= 4,
949 .max_access_size
= 4,
953 static const MemoryRegionOps openpic_glb_ops_be
= {
954 .write
= openpic_gbl_write
,
955 .read
= openpic_gbl_read
,
956 .endianness
= DEVICE_BIG_ENDIAN
,
958 .min_access_size
= 4,
959 .max_access_size
= 4,
963 static const MemoryRegionOps openpic_tmr_ops_le
= {
964 .write
= openpic_tmr_write
,
965 .read
= openpic_tmr_read
,
966 .endianness
= DEVICE_LITTLE_ENDIAN
,
968 .min_access_size
= 4,
969 .max_access_size
= 4,
973 static const MemoryRegionOps openpic_tmr_ops_be
= {
974 .write
= openpic_tmr_write
,
975 .read
= openpic_tmr_read
,
976 .endianness
= DEVICE_BIG_ENDIAN
,
978 .min_access_size
= 4,
979 .max_access_size
= 4,
983 static const MemoryRegionOps openpic_cpu_ops_le
= {
984 .write
= openpic_cpu_write
,
985 .read
= openpic_cpu_read
,
986 .endianness
= DEVICE_LITTLE_ENDIAN
,
988 .min_access_size
= 4,
989 .max_access_size
= 4,
993 static const MemoryRegionOps openpic_cpu_ops_be
= {
994 .write
= openpic_cpu_write
,
995 .read
= openpic_cpu_read
,
996 .endianness
= DEVICE_BIG_ENDIAN
,
998 .min_access_size
= 4,
999 .max_access_size
= 4,
1003 static const MemoryRegionOps openpic_src_ops_le
= {
1004 .write
= openpic_src_write
,
1005 .read
= openpic_src_read
,
1006 .endianness
= DEVICE_LITTLE_ENDIAN
,
1008 .min_access_size
= 4,
1009 .max_access_size
= 4,
1013 static const MemoryRegionOps openpic_src_ops_be
= {
1014 .write
= openpic_src_write
,
1015 .read
= openpic_src_read
,
1016 .endianness
= DEVICE_BIG_ENDIAN
,
1018 .min_access_size
= 4,
1019 .max_access_size
= 4,
1023 static const MemoryRegionOps openpic_msi_ops_le
= {
1024 .read
= openpic_msi_read
,
1025 .write
= openpic_msi_write
,
1026 .endianness
= DEVICE_LITTLE_ENDIAN
,
1028 .min_access_size
= 4,
1029 .max_access_size
= 4,
1033 static const MemoryRegionOps openpic_msi_ops_be
= {
1034 .read
= openpic_msi_read
,
1035 .write
= openpic_msi_write
,
1036 .endianness
= DEVICE_BIG_ENDIAN
,
1038 .min_access_size
= 4,
1039 .max_access_size
= 4,
1043 static void openpic_save_IRQ_queue(QEMUFile
* f
, IRQ_queue_t
*q
)
1047 for (i
= 0; i
< BF_WIDTH(MAX_IRQ
); i
++)
1048 qemu_put_be32s(f
, &q
->queue
[i
]);
1050 qemu_put_sbe32s(f
, &q
->next
);
1051 qemu_put_sbe32s(f
, &q
->priority
);
1054 static void openpic_save(QEMUFile
* f
, void *opaque
)
1056 OpenPICState
*opp
= (OpenPICState
*)opaque
;
1059 qemu_put_be32s(f
, &opp
->glbc
);
1060 qemu_put_be32s(f
, &opp
->veni
);
1061 qemu_put_be32s(f
, &opp
->pint
);
1062 qemu_put_be32s(f
, &opp
->spve
);
1063 qemu_put_be32s(f
, &opp
->tifr
);
1065 for (i
= 0; i
< opp
->max_irq
; i
++) {
1066 qemu_put_be32s(f
, &opp
->src
[i
].ipvp
);
1067 qemu_put_be32s(f
, &opp
->src
[i
].ide
);
1068 qemu_put_sbe32s(f
, &opp
->src
[i
].last_cpu
);
1069 qemu_put_sbe32s(f
, &opp
->src
[i
].pending
);
1072 qemu_put_be32s(f
, &opp
->nb_cpus
);
1074 for (i
= 0; i
< opp
->nb_cpus
; i
++) {
1075 qemu_put_be32s(f
, &opp
->dst
[i
].pctp
);
1076 qemu_put_be32s(f
, &opp
->dst
[i
].pcsr
);
1077 openpic_save_IRQ_queue(f
, &opp
->dst
[i
].raised
);
1078 openpic_save_IRQ_queue(f
, &opp
->dst
[i
].servicing
);
1081 for (i
= 0; i
< MAX_TMR
; i
++) {
1082 qemu_put_be32s(f
, &opp
->timers
[i
].ticc
);
1083 qemu_put_be32s(f
, &opp
->timers
[i
].tibc
);
1087 static void openpic_load_IRQ_queue(QEMUFile
* f
, IRQ_queue_t
*q
)
1091 for (i
= 0; i
< BF_WIDTH(MAX_IRQ
); i
++)
1092 qemu_get_be32s(f
, &q
->queue
[i
]);
1094 qemu_get_sbe32s(f
, &q
->next
);
1095 qemu_get_sbe32s(f
, &q
->priority
);
1098 static int openpic_load(QEMUFile
* f
, void *opaque
, int version_id
)
1100 OpenPICState
*opp
= (OpenPICState
*)opaque
;
1103 if (version_id
!= 1)
1106 qemu_get_be32s(f
, &opp
->glbc
);
1107 qemu_get_be32s(f
, &opp
->veni
);
1108 qemu_get_be32s(f
, &opp
->pint
);
1109 qemu_get_be32s(f
, &opp
->spve
);
1110 qemu_get_be32s(f
, &opp
->tifr
);
1112 for (i
= 0; i
< opp
->max_irq
; i
++) {
1113 qemu_get_be32s(f
, &opp
->src
[i
].ipvp
);
1114 qemu_get_be32s(f
, &opp
->src
[i
].ide
);
1115 qemu_get_sbe32s(f
, &opp
->src
[i
].last_cpu
);
1116 qemu_get_sbe32s(f
, &opp
->src
[i
].pending
);
1119 qemu_get_be32s(f
, &opp
->nb_cpus
);
1121 for (i
= 0; i
< opp
->nb_cpus
; i
++) {
1122 qemu_get_be32s(f
, &opp
->dst
[i
].pctp
);
1123 qemu_get_be32s(f
, &opp
->dst
[i
].pcsr
);
1124 openpic_load_IRQ_queue(f
, &opp
->dst
[i
].raised
);
1125 openpic_load_IRQ_queue(f
, &opp
->dst
[i
].servicing
);
1128 for (i
= 0; i
< MAX_TMR
; i
++) {
1129 qemu_get_be32s(f
, &opp
->timers
[i
].ticc
);
1130 qemu_get_be32s(f
, &opp
->timers
[i
].tibc
);
1136 static void openpic_irq_raise(OpenPICState
*opp
, int n_CPU
, IRQ_src_t
*src
)
1138 int n_ci
= IDR_CI0_SHIFT
- n_CPU
;
1140 if ((opp
->flags
& OPENPIC_FLAG_IDE_CRIT
) && (src
->ide
& (1 << n_ci
))) {
1141 qemu_irq_raise(opp
->dst
[n_CPU
].irqs
[OPENPIC_OUTPUT_CINT
]);
1143 qemu_irq_raise(opp
->dst
[n_CPU
].irqs
[OPENPIC_OUTPUT_INT
]);
1149 MemoryRegionOps
const *ops
;
1155 static int openpic_init(SysBusDevice
*dev
)
1157 OpenPICState
*opp
= FROM_SYSBUS(typeof (*opp
), dev
);
1159 struct memreg list_le
[] = {
1160 {"glb", &openpic_glb_ops_le
, true,
1161 OPENPIC_GLB_REG_START
, OPENPIC_GLB_REG_SIZE
},
1162 {"tmr", &openpic_tmr_ops_le
, true,
1163 OPENPIC_TMR_REG_START
, OPENPIC_TMR_REG_SIZE
},
1164 {"msi", &openpic_msi_ops_le
, true,
1165 OPENPIC_MSI_REG_START
, OPENPIC_MSI_REG_SIZE
},
1166 {"src", &openpic_src_ops_le
, true,
1167 OPENPIC_SRC_REG_START
, OPENPIC_SRC_REG_SIZE
},
1168 {"cpu", &openpic_cpu_ops_le
, true,
1169 OPENPIC_CPU_REG_START
, OPENPIC_CPU_REG_SIZE
},
1171 struct memreg list_be
[] = {
1172 {"glb", &openpic_glb_ops_be
, true,
1173 OPENPIC_GLB_REG_START
, OPENPIC_GLB_REG_SIZE
},
1174 {"tmr", &openpic_tmr_ops_be
, true,
1175 OPENPIC_TMR_REG_START
, OPENPIC_TMR_REG_SIZE
},
1176 {"msi", &openpic_msi_ops_be
, true,
1177 OPENPIC_MSI_REG_START
, OPENPIC_MSI_REG_SIZE
},
1178 {"src", &openpic_src_ops_be
, true,
1179 OPENPIC_SRC_REG_START
, OPENPIC_SRC_REG_SIZE
},
1180 {"cpu", &openpic_cpu_ops_be
, true,
1181 OPENPIC_CPU_REG_START
, OPENPIC_CPU_REG_SIZE
},
1183 struct memreg
*list
;
1185 switch (opp
->model
) {
1186 case OPENPIC_MODEL_FSL_MPIC_20
:
1188 opp
->flags
|= OPENPIC_FLAG_IDE_CRIT
;
1190 opp
->vid
= VID_REVISION_1_2
;
1191 opp
->veni
= VENI_GENERIC
;
1192 opp
->spve_mask
= 0xFFFF;
1193 opp
->tifr_reset
= 0x00000000;
1194 opp
->ipvp_reset
= 0x80000000;
1195 opp
->ide_reset
= 0x00000001;
1196 opp
->max_irq
= FSL_MPIC_20_MAX_IRQ
;
1197 opp
->irq_ipi0
= FSL_MPIC_20_IPI_IRQ
;
1198 opp
->irq_tim0
= FSL_MPIC_20_TMR_IRQ
;
1199 opp
->irq_msi
= FSL_MPIC_20_MSI_IRQ
;
1200 opp
->brr1
= FSL_BRR1_IPID
| FSL_BRR1_IPMJ
| FSL_BRR1_IPMN
;
1201 msi_supported
= true;
1204 case OPENPIC_MODEL_RAVEN
:
1205 opp
->nb_irqs
= RAVEN_MAX_EXT
;
1206 opp
->vid
= VID_REVISION_1_3
;
1207 opp
->veni
= VENI_GENERIC
;
1208 opp
->spve_mask
= 0xFF;
1209 opp
->tifr_reset
= 0x003F7A00;
1210 opp
->ipvp_reset
= 0xA0000000;
1211 opp
->ide_reset
= 0x00000000;
1212 opp
->max_irq
= RAVEN_MAX_IRQ
;
1213 opp
->irq_ipi0
= RAVEN_IPI_IRQ
;
1214 opp
->irq_tim0
= RAVEN_TMR_IRQ
;
1217 /* Don't map MSI region */
1218 list
[2].map
= false;
1220 /* Only UP supported today */
1221 if (opp
->nb_cpus
!= 1) {
1227 memory_region_init(&opp
->mem
, "openpic", 0x40000);
1229 for (i
= 0; i
< ARRAY_SIZE(list_le
); i
++) {
1234 memory_region_init_io(&opp
->sub_io_mem
[i
], list
[i
].ops
, opp
,
1235 list
[i
].name
, list
[i
].size
);
1237 memory_region_add_subregion(&opp
->mem
, list
[i
].start_addr
,
1238 &opp
->sub_io_mem
[i
]);
1241 for (i
= 0; i
< opp
->nb_cpus
; i
++) {
1242 opp
->dst
[i
].irqs
= g_new(qemu_irq
, OPENPIC_OUTPUT_NB
);
1243 for (j
= 0; j
< OPENPIC_OUTPUT_NB
; j
++) {
1244 sysbus_init_irq(dev
, &opp
->dst
[i
].irqs
[j
]);
1248 register_savevm(&opp
->busdev
.qdev
, "openpic", 0, 2,
1249 openpic_save
, openpic_load
, opp
);
1251 sysbus_init_mmio(dev
, &opp
->mem
);
1252 qdev_init_gpio_in(&dev
->qdev
, openpic_set_irq
, opp
->max_irq
);
1257 static Property openpic_properties
[] = {
1258 DEFINE_PROP_UINT32("model", OpenPICState
, model
, OPENPIC_MODEL_FSL_MPIC_20
),
1259 DEFINE_PROP_UINT32("nb_cpus", OpenPICState
, nb_cpus
, 1),
1260 DEFINE_PROP_END_OF_LIST(),
1263 static void openpic_class_init(ObjectClass
*klass
, void *data
)
1265 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1266 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1268 k
->init
= openpic_init
;
1269 dc
->props
= openpic_properties
;
1270 dc
->reset
= openpic_reset
;
1273 static TypeInfo openpic_info
= {
1275 .parent
= TYPE_SYS_BUS_DEVICE
,
1276 .instance_size
= sizeof(OpenPICState
),
1277 .class_init
= openpic_class_init
,
1280 static void openpic_register_types(void)
1282 type_register_static(&openpic_info
);
1285 type_init(openpic_register_types
)