4 * Copyright (c) 2004 Jocelyn Mayer
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * Based on OpenPic implementations:
27 * - Intel GW80314 I/O companion chip developer's manual
28 * - Motorola MPC8245 & MPC8540 user manuals.
29 * - Motorola MCP750 (aka Raven) programmer manual.
30 * - Motorola Harrier programmer manuel
32 * Serial interrupts, as implemented in Raven chipset are not supported yet.
40 //#define DEBUG_OPENPIC
43 #define DPRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
45 #define DPRINTF(fmt, ...) do { } while (0)
48 #define USE_MPCxxx /* Intel model is broken, for now */
50 #if defined (USE_INTEL_GW80314)
51 /* Intel GW80314 I/O Companion chip */
61 #define VID (0x00000000)
63 #elif defined(USE_MPCxxx)
72 #define VID 0x03 /* MPIC version ID */
73 #define VENI 0x00000000 /* Vendor ID */
81 #define OPENPIC_MAX_CPU 2
82 #define OPENPIC_MAX_IRQ 64
83 #define OPENPIC_EXT_IRQ 48
84 #define OPENPIC_MAX_TMR MAX_TMR
85 #define OPENPIC_MAX_IPI MAX_IPI
87 /* Interrupt definitions */
88 #define OPENPIC_IRQ_FE (OPENPIC_EXT_IRQ) /* Internal functional IRQ */
89 #define OPENPIC_IRQ_ERR (OPENPIC_EXT_IRQ + 1) /* Error IRQ */
90 #define OPENPIC_IRQ_TIM0 (OPENPIC_EXT_IRQ + 2) /* First timer IRQ */
91 #if OPENPIC_MAX_IPI > 0
92 #define OPENPIC_IRQ_IPI0 (OPENPIC_IRQ_TIM0 + OPENPIC_MAX_TMR) /* First IPI IRQ */
93 #define OPENPIC_IRQ_DBL0 (OPENPIC_IRQ_IPI0 + (OPENPIC_MAX_CPU * OPENPIC_MAX_IPI)) /* First doorbell IRQ */
95 #define OPENPIC_IRQ_DBL0 (OPENPIC_IRQ_TIM0 + OPENPIC_MAX_TMR) /* First doorbell IRQ */
96 #define OPENPIC_IRQ_MBX0 (OPENPIC_IRQ_DBL0 + OPENPIC_MAX_DBL) /* First mailbox IRQ */
100 #define MPIC_MAX_CPU 1
101 #define MPIC_MAX_EXT 12
102 #define MPIC_MAX_INT 64
103 #define MPIC_MAX_MSG 4
104 #define MPIC_MAX_MSI 8
105 #define MPIC_MAX_TMR MAX_TMR
106 #define MPIC_MAX_IPI MAX_IPI
107 #define MPIC_MAX_IRQ (MPIC_MAX_EXT + MPIC_MAX_INT + MPIC_MAX_TMR + MPIC_MAX_MSG + MPIC_MAX_MSI + (MPIC_MAX_IPI * MPIC_MAX_CPU))
109 /* Interrupt definitions */
110 #define MPIC_EXT_IRQ 0
111 #define MPIC_INT_IRQ (MPIC_EXT_IRQ + MPIC_MAX_EXT)
112 #define MPIC_TMR_IRQ (MPIC_INT_IRQ + MPIC_MAX_INT)
113 #define MPIC_MSG_IRQ (MPIC_TMR_IRQ + MPIC_MAX_TMR)
114 #define MPIC_MSI_IRQ (MPIC_MSG_IRQ + MPIC_MAX_MSG)
115 #define MPIC_IPI_IRQ (MPIC_MSI_IRQ + MPIC_MAX_MSI)
117 #define MPIC_GLB_REG_START 0x0
118 #define MPIC_GLB_REG_SIZE 0x10F0
119 #define MPIC_TMR_REG_START 0x10F0
120 #define MPIC_TMR_REG_SIZE 0x220
121 #define MPIC_EXT_REG_START 0x10000
122 #define MPIC_EXT_REG_SIZE 0x180
123 #define MPIC_INT_REG_START 0x10200
124 #define MPIC_INT_REG_SIZE 0x800
125 #define MPIC_MSG_REG_START 0x11600
126 #define MPIC_MSG_REG_SIZE 0x100
127 #define MPIC_MSI_REG_START 0x11C00
128 #define MPIC_MSI_REG_SIZE 0x100
129 #define MPIC_CPU_REG_START 0x20000
130 #define MPIC_CPU_REG_SIZE 0x100
141 #error "Please select which OpenPic implementation is to be emulated"
144 #define OPENPIC_PAGE_SIZE 4096
146 #define BF_WIDTH(_bits_) \
147 (((_bits_) + (sizeof(uint32_t) * 8) - 1) / (sizeof(uint32_t) * 8))
149 static inline void set_bit (uint32_t *field
, int bit
)
151 field
[bit
>> 5] |= 1 << (bit
& 0x1F);
154 static inline void reset_bit (uint32_t *field
, int bit
)
156 field
[bit
>> 5] &= ~(1 << (bit
& 0x1F));
159 static inline int test_bit (uint32_t *field
, int bit
)
161 return (field
[bit
>> 5] & 1 << (bit
& 0x1F)) != 0;
171 typedef struct IRQ_queue_t
{
172 uint32_t queue
[BF_WIDTH(MAX_IRQ
)];
177 typedef struct IRQ_src_t
{
178 uint32_t ipvp
; /* IRQ vector/priority register */
179 uint32_t ide
; /* IRQ destination register */
182 int pending
; /* TRUE if IRQ is pending */
192 #define IPVP_PRIORITY_MASK (0x1F << 16)
193 #define IPVP_PRIORITY(_ipvpr_) ((int)(((_ipvpr_) & IPVP_PRIORITY_MASK) >> 16))
194 #define IPVP_VECTOR_MASK ((1 << VECTOR_BITS) - 1)
195 #define IPVP_VECTOR(_ipvpr_) ((_ipvpr_) & IPVP_VECTOR_MASK)
197 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 openpic_t
{
209 /* Global registers */
210 uint32_t frep
; /* Feature reporting register */
211 uint32_t glbc
; /* Global configuration register */
212 uint32_t micr
; /* MPIC interrupt configuration register */
213 uint32_t veni
; /* Vendor identification register */
214 uint32_t pint
; /* Processor initialization register */
215 uint32_t spve
; /* Spurious vector register */
216 uint32_t tifr
; /* Timer frequency reporting register */
217 /* Source registers */
218 IRQ_src_t src
[MAX_IRQ
];
219 /* Local registers per output pin */
220 IRQ_dst_t dst
[MAX_CPU
];
222 /* Timer registers */
224 uint32_t ticc
; /* Global timer current count register */
225 uint32_t tibc
; /* Global timer base count register */
228 /* Doorbell registers */
229 uint32_t dar
; /* Doorbell activate register */
231 uint32_t dmr
; /* Doorbell messaging register */
232 } doorbells
[MAX_DBL
];
235 /* Mailbox registers */
237 uint32_t mbr
; /* Mailbox register */
238 } mailboxes
[MAX_MAILBOXES
];
240 /* IRQ out is used when in bypass mode (not implemented) */
246 void (*reset
) (void *);
247 void (*irq_raise
) (struct openpic_t
*, int, IRQ_src_t
*);
250 static inline uint32_t openpic_swap32(openpic_t
*opp
, uint32_t val
)
258 static inline void IRQ_setbit (IRQ_queue_t
*q
, int n_IRQ
)
260 set_bit(q
->queue
, n_IRQ
);
263 static inline void IRQ_resetbit (IRQ_queue_t
*q
, int n_IRQ
)
265 reset_bit(q
->queue
, n_IRQ
);
268 static inline int IRQ_testbit (IRQ_queue_t
*q
, int n_IRQ
)
270 return test_bit(q
->queue
, n_IRQ
);
273 static void IRQ_check (openpic_t
*opp
, IRQ_queue_t
*q
)
280 for (i
= 0; i
< opp
->max_irq
; i
++) {
281 if (IRQ_testbit(q
, i
)) {
282 DPRINTF("IRQ_check: irq %d set ipvp_pr=%d pr=%d\n",
283 i
, IPVP_PRIORITY(opp
->src
[i
].ipvp
), priority
);
284 if (IPVP_PRIORITY(opp
->src
[i
].ipvp
) > priority
) {
286 priority
= IPVP_PRIORITY(opp
->src
[i
].ipvp
);
291 q
->priority
= priority
;
294 static int IRQ_get_next (openpic_t
*opp
, IRQ_queue_t
*q
)
304 static void IRQ_local_pipe (openpic_t
*opp
, int n_CPU
, int n_IRQ
)
310 dst
= &opp
->dst
[n_CPU
];
311 src
= &opp
->src
[n_IRQ
];
312 priority
= IPVP_PRIORITY(src
->ipvp
);
313 if (priority
<= dst
->pctp
) {
314 /* Too low priority */
315 DPRINTF("%s: IRQ %d has too low priority on CPU %d\n",
316 __func__
, n_IRQ
, n_CPU
);
319 if (IRQ_testbit(&dst
->raised
, n_IRQ
)) {
321 DPRINTF("%s: IRQ %d was missed on CPU %d\n",
322 __func__
, n_IRQ
, n_CPU
);
325 set_bit(&src
->ipvp
, IPVP_ACTIVITY
);
326 IRQ_setbit(&dst
->raised
, n_IRQ
);
327 if (priority
< dst
->raised
.priority
) {
328 /* An higher priority IRQ is already raised */
329 DPRINTF("%s: IRQ %d is hidden by raised IRQ %d on CPU %d\n",
330 __func__
, n_IRQ
, dst
->raised
.next
, n_CPU
);
333 IRQ_get_next(opp
, &dst
->raised
);
334 if (IRQ_get_next(opp
, &dst
->servicing
) != -1 &&
335 priority
<= dst
->servicing
.priority
) {
336 DPRINTF("%s: IRQ %d is hidden by servicing IRQ %d on CPU %d\n",
337 __func__
, n_IRQ
, dst
->servicing
.next
, n_CPU
);
338 /* Already servicing a higher priority IRQ */
341 DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n", n_CPU
, n_IRQ
);
342 opp
->irq_raise(opp
, n_CPU
, src
);
345 /* update pic state because registers for n_IRQ have changed value */
346 static void openpic_update_irq(openpic_t
*opp
, int n_IRQ
)
351 src
= &opp
->src
[n_IRQ
];
355 DPRINTF("%s: IRQ %d is not pending\n", __func__
, n_IRQ
);
358 if (test_bit(&src
->ipvp
, IPVP_MASK
)) {
359 /* Interrupt source is disabled */
360 DPRINTF("%s: IRQ %d is disabled\n", __func__
, n_IRQ
);
363 if (IPVP_PRIORITY(src
->ipvp
) == 0) {
364 /* Priority set to zero */
365 DPRINTF("%s: IRQ %d has 0 priority\n", __func__
, n_IRQ
);
368 if (test_bit(&src
->ipvp
, IPVP_ACTIVITY
)) {
369 /* IRQ already active */
370 DPRINTF("%s: IRQ %d is already active\n", __func__
, n_IRQ
);
373 if (src
->ide
== 0x00000000) {
375 DPRINTF("%s: IRQ %d has no target\n", __func__
, n_IRQ
);
379 if (src
->ide
== (1 << src
->last_cpu
)) {
380 /* Only one CPU is allowed to receive this IRQ */
381 IRQ_local_pipe(opp
, src
->last_cpu
, n_IRQ
);
382 } else if (!test_bit(&src
->ipvp
, IPVP_MODE
)) {
383 /* Directed delivery mode */
384 for (i
= 0; i
< opp
->nb_cpus
; i
++) {
385 if (test_bit(&src
->ide
, i
))
386 IRQ_local_pipe(opp
, i
, n_IRQ
);
389 /* Distributed delivery mode */
390 for (i
= src
->last_cpu
+ 1; i
!= src
->last_cpu
; i
++) {
391 if (i
== opp
->nb_cpus
)
393 if (test_bit(&src
->ide
, i
)) {
394 IRQ_local_pipe(opp
, i
, n_IRQ
);
402 static void openpic_set_irq(void *opaque
, int n_IRQ
, int level
)
404 openpic_t
*opp
= opaque
;
407 src
= &opp
->src
[n_IRQ
];
408 DPRINTF("openpic: set irq %d = %d ipvp=%08x\n",
409 n_IRQ
, level
, src
->ipvp
);
410 if (test_bit(&src
->ipvp
, IPVP_SENSE
)) {
411 /* level-sensitive irq */
412 src
->pending
= level
;
414 reset_bit(&src
->ipvp
, IPVP_ACTIVITY
);
416 /* edge-sensitive irq */
420 openpic_update_irq(opp
, n_IRQ
);
423 static void openpic_reset (void *opaque
)
425 openpic_t
*opp
= (openpic_t
*)opaque
;
428 opp
->glbc
= 0x80000000;
429 /* Initialise controller registers */
430 opp
->frep
= ((OPENPIC_EXT_IRQ
- 1) << 16) | ((MAX_CPU
- 1) << 8) | VID
;
432 opp
->pint
= 0x00000000;
433 opp
->spve
= 0x000000FF;
434 opp
->tifr
= 0x003F7A00;
436 opp
->micr
= 0x00000000;
437 /* Initialise IRQ sources */
438 for (i
= 0; i
< opp
->max_irq
; i
++) {
439 opp
->src
[i
].ipvp
= 0xA0000000;
440 opp
->src
[i
].ide
= 0x00000000;
442 /* Initialise IRQ destinations */
443 for (i
= 0; i
< MAX_CPU
; i
++) {
444 opp
->dst
[i
].pctp
= 0x0000000F;
445 opp
->dst
[i
].pcsr
= 0x00000000;
446 memset(&opp
->dst
[i
].raised
, 0, sizeof(IRQ_queue_t
));
447 opp
->dst
[i
].raised
.next
= -1;
448 memset(&opp
->dst
[i
].servicing
, 0, sizeof(IRQ_queue_t
));
449 opp
->dst
[i
].servicing
.next
= -1;
451 /* Initialise timers */
452 for (i
= 0; i
< MAX_TMR
; i
++) {
453 opp
->timers
[i
].ticc
= 0x00000000;
454 opp
->timers
[i
].tibc
= 0x80000000;
456 /* Initialise doorbells */
458 opp
->dar
= 0x00000000;
459 for (i
= 0; i
< MAX_DBL
; i
++) {
460 opp
->doorbells
[i
].dmr
= 0x00000000;
463 /* Initialise mailboxes */
465 for (i
= 0; i
< MAX_MBX
; i
++) { /* ? */
466 opp
->mailboxes
[i
].mbr
= 0x00000000;
469 /* Go out of RESET state */
470 opp
->glbc
= 0x00000000;
473 static inline uint32_t read_IRQreg (openpic_t
*opp
, int n_IRQ
, uint32_t reg
)
479 retval
= opp
->src
[n_IRQ
].ipvp
;
482 retval
= opp
->src
[n_IRQ
].ide
;
489 static inline void write_IRQreg (openpic_t
*opp
, int n_IRQ
,
490 uint32_t reg
, uint32_t val
)
496 /* NOTE: not fully accurate for special IRQs, but simple and
498 /* ACTIVITY bit is read-only */
499 opp
->src
[n_IRQ
].ipvp
=
500 (opp
->src
[n_IRQ
].ipvp
& 0x40000000) |
502 openpic_update_irq(opp
, n_IRQ
);
503 DPRINTF("Set IPVP %d to 0x%08x -> 0x%08x\n",
504 n_IRQ
, val
, opp
->src
[n_IRQ
].ipvp
);
507 tmp
= val
& 0xC0000000;
508 tmp
|= val
& ((1 << MAX_CPU
) - 1);
509 opp
->src
[n_IRQ
].ide
= tmp
;
510 DPRINTF("Set IDE %d to 0x%08x\n", n_IRQ
, opp
->src
[n_IRQ
].ide
);
515 #if 0 // Code provision for Intel model
517 static uint32_t read_doorbell_register (openpic_t
*opp
,
518 int n_dbl
, uint32_t offset
)
523 case DBL_IPVP_OFFSET
:
524 retval
= read_IRQreg(opp
, IRQ_DBL0
+ n_dbl
, IRQ_IPVP
);
527 retval
= read_IRQreg(opp
, IRQ_DBL0
+ n_dbl
, IRQ_IDE
);
530 retval
= opp
->doorbells
[n_dbl
].dmr
;
537 static void write_doorbell_register (penpic_t
*opp
, int n_dbl
,
538 uint32_t offset
, uint32_t value
)
541 case DBL_IVPR_OFFSET
:
542 write_IRQreg(opp
, IRQ_DBL0
+ n_dbl
, IRQ_IPVP
, value
);
545 write_IRQreg(opp
, IRQ_DBL0
+ n_dbl
, IRQ_IDE
, value
);
548 opp
->doorbells
[n_dbl
].dmr
= value
;
555 static uint32_t read_mailbox_register (openpic_t
*opp
,
556 int n_mbx
, uint32_t offset
)
562 retval
= opp
->mailboxes
[n_mbx
].mbr
;
564 case MBX_IVPR_OFFSET
:
565 retval
= read_IRQreg(opp
, IRQ_MBX0
+ n_mbx
, IRQ_IPVP
);
568 retval
= read_IRQreg(opp
, IRQ_MBX0
+ n_mbx
, IRQ_IDE
);
575 static void write_mailbox_register (openpic_t
*opp
, int n_mbx
,
576 uint32_t address
, uint32_t value
)
580 opp
->mailboxes
[n_mbx
].mbr
= value
;
582 case MBX_IVPR_OFFSET
:
583 write_IRQreg(opp
, IRQ_MBX0
+ n_mbx
, IRQ_IPVP
, value
);
586 write_IRQreg(opp
, IRQ_MBX0
+ n_mbx
, IRQ_IDE
, value
);
591 #endif /* 0 : Code provision for Intel model */
593 static void openpic_gbl_write (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
595 openpic_t
*opp
= opaque
;
599 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
602 val
= openpic_swap32(opp
, val
);
605 case 0x00: /* FREP */
607 case 0x20: /* GLBC */
608 if (val
& 0x80000000 && opp
->reset
)
610 opp
->glbc
= val
& ~0x80000000;
612 case 0x80: /* VENI */
614 case 0x90: /* PINT */
615 for (idx
= 0; idx
< opp
->nb_cpus
; idx
++) {
616 if ((val
& (1 << idx
)) && !(opp
->pint
& (1 << idx
))) {
617 DPRINTF("Raise OpenPIC RESET output for CPU %d\n", idx
);
618 dst
= &opp
->dst
[idx
];
619 qemu_irq_raise(dst
->irqs
[OPENPIC_OUTPUT_RESET
]);
620 } else if (!(val
& (1 << idx
)) && (opp
->pint
& (1 << idx
))) {
621 DPRINTF("Lower OpenPIC RESET output for CPU %d\n", idx
);
622 dst
= &opp
->dst
[idx
];
623 qemu_irq_lower(dst
->irqs
[OPENPIC_OUTPUT_RESET
]);
629 case 0xA0: /* IPI_IPVP */
635 idx
= (addr
- 0xA0) >> 4;
636 write_IRQreg(opp
, opp
->irq_ipi0
+ idx
, IRQ_IPVP
, val
);
640 case 0xE0: /* SPVE */
641 opp
->spve
= val
& 0x000000FF;
643 case 0xF0: /* TIFR */
651 static uint32_t openpic_gbl_read (void *opaque
, target_phys_addr_t addr
)
653 openpic_t
*opp
= opaque
;
656 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
662 case 0x00: /* FREP */
665 case 0x20: /* GLBC */
668 case 0x80: /* VENI */
671 case 0x90: /* PINT */
675 case 0xA0: /* IPI_IPVP */
681 idx
= (addr
- 0xA0) >> 4;
682 retval
= read_IRQreg(opp
, opp
->irq_ipi0
+ idx
, IRQ_IPVP
);
686 case 0xE0: /* SPVE */
689 case 0xF0: /* TIFR */
695 DPRINTF("%s: => %08x\n", __func__
, retval
);
696 retval
= openpic_swap32(opp
, retval
);
701 static void openpic_timer_write (void *opaque
, uint32_t addr
, uint32_t val
)
703 openpic_t
*opp
= opaque
;
706 DPRINTF("%s: addr %08x <= %08x\n", __func__
, addr
, val
);
709 val
= openpic_swap32(opp
, val
);
712 idx
= (addr
& 0xFFF0) >> 6;
715 case 0x00: /* TICC */
717 case 0x10: /* TIBC */
718 if ((opp
->timers
[idx
].ticc
& 0x80000000) != 0 &&
719 (val
& 0x80000000) == 0 &&
720 (opp
->timers
[idx
].tibc
& 0x80000000) != 0)
721 opp
->timers
[idx
].ticc
&= ~0x80000000;
722 opp
->timers
[idx
].tibc
= val
;
724 case 0x20: /* TIVP */
725 write_IRQreg(opp
, opp
->irq_tim0
+ idx
, IRQ_IPVP
, val
);
727 case 0x30: /* TIDE */
728 write_IRQreg(opp
, opp
->irq_tim0
+ idx
, IRQ_IDE
, val
);
733 static uint32_t openpic_timer_read (void *opaque
, uint32_t addr
)
735 openpic_t
*opp
= opaque
;
739 DPRINTF("%s: addr %08x\n", __func__
, addr
);
745 idx
= (addr
& 0xFFF0) >> 6;
748 case 0x00: /* TICC */
749 retval
= opp
->timers
[idx
].ticc
;
751 case 0x10: /* TIBC */
752 retval
= opp
->timers
[idx
].tibc
;
754 case 0x20: /* TIPV */
755 retval
= read_IRQreg(opp
, opp
->irq_tim0
+ idx
, IRQ_IPVP
);
757 case 0x30: /* TIDE */
758 retval
= read_IRQreg(opp
, opp
->irq_tim0
+ idx
, IRQ_IDE
);
761 DPRINTF("%s: => %08x\n", __func__
, retval
);
762 retval
= openpic_swap32(opp
, retval
);
767 static void openpic_src_write (void *opaque
, uint32_t addr
, uint32_t val
)
769 openpic_t
*opp
= opaque
;
772 DPRINTF("%s: addr %08x <= %08x\n", __func__
, addr
, val
);
775 val
= openpic_swap32(opp
, val
);
776 addr
= addr
& 0xFFF0;
779 /* EXDE / IFEDE / IEEDE */
780 write_IRQreg(opp
, idx
, IRQ_IDE
, val
);
782 /* EXVP / IFEVP / IEEVP */
783 write_IRQreg(opp
, idx
, IRQ_IPVP
, val
);
787 static uint32_t openpic_src_read (void *opaque
, uint32_t addr
)
789 openpic_t
*opp
= opaque
;
793 DPRINTF("%s: addr %08x\n", __func__
, addr
);
797 addr
= addr
& 0xFFF0;
800 /* EXDE / IFEDE / IEEDE */
801 retval
= read_IRQreg(opp
, idx
, IRQ_IDE
);
803 /* EXVP / IFEVP / IEEVP */
804 retval
= read_IRQreg(opp
, idx
, IRQ_IPVP
);
806 DPRINTF("%s: => %08x\n", __func__
, retval
);
807 retval
= openpic_swap32(opp
, retval
);
812 static void openpic_cpu_write (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
814 openpic_t
*opp
= opaque
;
817 int idx
, s_IRQ
, n_IRQ
;
819 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
822 val
= openpic_swap32(opp
, val
);
825 dst
= &opp
->dst
[idx
];
829 case 0x40: /* PIPD */
833 idx
= (addr
- 0x40) >> 4;
834 write_IRQreg(opp
, opp
->irq_ipi0
+ idx
, IRQ_IDE
, val
);
835 openpic_set_irq(opp
, opp
->irq_ipi0
+ idx
, 1);
836 openpic_set_irq(opp
, opp
->irq_ipi0
+ idx
, 0);
839 case 0x80: /* PCTP */
840 dst
->pctp
= val
& 0x0000000F;
842 case 0x90: /* WHOAMI */
843 /* Read-only register */
845 case 0xA0: /* PIAC */
846 /* Read-only register */
848 case 0xB0: /* PEOI */
850 s_IRQ
= IRQ_get_next(opp
, &dst
->servicing
);
851 IRQ_resetbit(&dst
->servicing
, s_IRQ
);
852 dst
->servicing
.next
= -1;
853 /* Set up next servicing IRQ */
854 s_IRQ
= IRQ_get_next(opp
, &dst
->servicing
);
855 /* Check queued interrupts. */
856 n_IRQ
= IRQ_get_next(opp
, &dst
->raised
);
857 src
= &opp
->src
[n_IRQ
];
860 IPVP_PRIORITY(src
->ipvp
) > dst
->servicing
.priority
)) {
861 DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n",
863 opp
->irq_raise(opp
, idx
, src
);
871 static uint32_t openpic_cpu_read (void *opaque
, target_phys_addr_t addr
)
873 openpic_t
*opp
= opaque
;
879 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
885 dst
= &opp
->dst
[idx
];
888 case 0x80: /* PCTP */
891 case 0x90: /* WHOAMI */
894 case 0xA0: /* PIAC */
895 DPRINTF("Lower OpenPIC INT output\n");
896 qemu_irq_lower(dst
->irqs
[OPENPIC_OUTPUT_INT
]);
897 n_IRQ
= IRQ_get_next(opp
, &dst
->raised
);
898 DPRINTF("PIAC: irq=%d\n", n_IRQ
);
900 /* No more interrupt pending */
901 retval
= IPVP_VECTOR(opp
->spve
);
903 src
= &opp
->src
[n_IRQ
];
904 if (!test_bit(&src
->ipvp
, IPVP_ACTIVITY
) ||
905 !(IPVP_PRIORITY(src
->ipvp
) > dst
->pctp
)) {
906 /* - Spurious level-sensitive IRQ
907 * - Priorities has been changed
908 * and the pending IRQ isn't allowed anymore
910 reset_bit(&src
->ipvp
, IPVP_ACTIVITY
);
911 retval
= IPVP_VECTOR(opp
->spve
);
913 /* IRQ enter servicing state */
914 IRQ_setbit(&dst
->servicing
, n_IRQ
);
915 retval
= IPVP_VECTOR(src
->ipvp
);
917 IRQ_resetbit(&dst
->raised
, n_IRQ
);
918 dst
->raised
.next
= -1;
919 if (!test_bit(&src
->ipvp
, IPVP_SENSE
)) {
920 /* edge-sensitive IRQ */
921 reset_bit(&src
->ipvp
, IPVP_ACTIVITY
);
926 case 0xB0: /* PEOI */
932 idx
= (addr
- 0x40) >> 4;
933 retval
= read_IRQreg(opp
, opp
->irq_ipi0
+ idx
, IRQ_IDE
);
939 DPRINTF("%s: => %08x\n", __func__
, retval
);
940 retval
= openpic_swap32(opp
, retval
);
945 static void openpic_buggy_write (void *opaque
,
946 target_phys_addr_t addr
, uint32_t val
)
948 printf("Invalid OPENPIC write access !\n");
951 static uint32_t openpic_buggy_read (void *opaque
, target_phys_addr_t addr
)
953 printf("Invalid OPENPIC read access !\n");
958 static void openpic_writel (void *opaque
,
959 target_phys_addr_t addr
, uint32_t val
)
961 openpic_t
*opp
= opaque
;
964 DPRINTF("%s: offset %08x val: %08x\n", __func__
, (int)addr
, val
);
966 /* Global registers */
967 openpic_gbl_write(opp
, addr
, val
);
968 } else if (addr
< 0x10000) {
969 /* Timers registers */
970 openpic_timer_write(opp
, addr
, val
);
971 } else if (addr
< 0x20000) {
972 /* Source registers */
973 openpic_src_write(opp
, addr
, val
);
976 openpic_cpu_write(opp
, addr
, val
);
980 static uint32_t openpic_readl (void *opaque
,target_phys_addr_t addr
)
982 openpic_t
*opp
= opaque
;
986 DPRINTF("%s: offset %08x\n", __func__
, (int)addr
);
988 /* Global registers */
989 retval
= openpic_gbl_read(opp
, addr
);
990 } else if (addr
< 0x10000) {
991 /* Timers registers */
992 retval
= openpic_timer_read(opp
, addr
);
993 } else if (addr
< 0x20000) {
994 /* Source registers */
995 retval
= openpic_src_read(opp
, addr
);
998 retval
= openpic_cpu_read(opp
, addr
);
1004 static CPUWriteMemoryFunc
* const openpic_write
[] = {
1005 &openpic_buggy_write
,
1006 &openpic_buggy_write
,
1010 static CPUReadMemoryFunc
* const openpic_read
[] = {
1011 &openpic_buggy_read
,
1012 &openpic_buggy_read
,
1016 static void openpic_map(PCIDevice
*pci_dev
, int region_num
,
1017 pcibus_t addr
, pcibus_t size
, int type
)
1021 DPRINTF("Map OpenPIC\n");
1022 opp
= (openpic_t
*)pci_dev
;
1023 /* Global registers */
1024 DPRINTF("Register OPENPIC gbl %08x => %08x\n",
1025 addr
+ 0x1000, addr
+ 0x1000 + 0x100);
1026 /* Timer registers */
1027 DPRINTF("Register OPENPIC timer %08x => %08x\n",
1028 addr
+ 0x1100, addr
+ 0x1100 + 0x40 * MAX_TMR
);
1029 /* Interrupt source registers */
1030 DPRINTF("Register OPENPIC src %08x => %08x\n",
1031 addr
+ 0x10000, addr
+ 0x10000 + 0x20 * (OPENPIC_EXT_IRQ
+ 2));
1032 /* Per CPU registers */
1033 DPRINTF("Register OPENPIC dst %08x => %08x\n",
1034 addr
+ 0x20000, addr
+ 0x20000 + 0x1000 * MAX_CPU
);
1035 cpu_register_physical_memory(addr
, 0x40000, opp
->mem_index
);
1036 #if 0 // Don't implement ISU for now
1037 opp_io_memory
= cpu_register_io_memory(openpic_src_read
,
1039 cpu_register_physical_memory(isu_base
, 0x20 * (EXT_IRQ
+ 2),
1044 static void openpic_save_IRQ_queue(QEMUFile
* f
, IRQ_queue_t
*q
)
1048 for (i
= 0; i
< BF_WIDTH(MAX_IRQ
); i
++)
1049 qemu_put_be32s(f
, &q
->queue
[i
]);
1051 qemu_put_sbe32s(f
, &q
->next
);
1052 qemu_put_sbe32s(f
, &q
->priority
);
1055 static void openpic_save(QEMUFile
* f
, void *opaque
)
1057 openpic_t
*opp
= (openpic_t
*)opaque
;
1060 qemu_put_be32s(f
, &opp
->frep
);
1061 qemu_put_be32s(f
, &opp
->glbc
);
1062 qemu_put_be32s(f
, &opp
->micr
);
1063 qemu_put_be32s(f
, &opp
->veni
);
1064 qemu_put_be32s(f
, &opp
->pint
);
1065 qemu_put_be32s(f
, &opp
->spve
);
1066 qemu_put_be32s(f
, &opp
->tifr
);
1068 for (i
= 0; i
< opp
->max_irq
; i
++) {
1069 qemu_put_be32s(f
, &opp
->src
[i
].ipvp
);
1070 qemu_put_be32s(f
, &opp
->src
[i
].ide
);
1071 qemu_put_sbe32s(f
, &opp
->src
[i
].type
);
1072 qemu_put_sbe32s(f
, &opp
->src
[i
].last_cpu
);
1073 qemu_put_sbe32s(f
, &opp
->src
[i
].pending
);
1076 qemu_put_sbe32s(f
, &opp
->nb_cpus
);
1078 for (i
= 0; i
< opp
->nb_cpus
; i
++) {
1079 qemu_put_be32s(f
, &opp
->dst
[i
].tfrr
);
1080 qemu_put_be32s(f
, &opp
->dst
[i
].pctp
);
1081 qemu_put_be32s(f
, &opp
->dst
[i
].pcsr
);
1082 openpic_save_IRQ_queue(f
, &opp
->dst
[i
].raised
);
1083 openpic_save_IRQ_queue(f
, &opp
->dst
[i
].servicing
);
1086 for (i
= 0; i
< MAX_TMR
; i
++) {
1087 qemu_put_be32s(f
, &opp
->timers
[i
].ticc
);
1088 qemu_put_be32s(f
, &opp
->timers
[i
].tibc
);
1092 qemu_put_be32s(f
, &opp
->dar
);
1094 for (i
= 0; i
< MAX_DBL
; i
++) {
1095 qemu_put_be32s(f
, &opp
->doorbells
[i
].dmr
);
1100 for (i
= 0; i
< MAX_MAILBOXES
; i
++) {
1101 qemu_put_be32s(f
, &opp
->mailboxes
[i
].mbr
);
1105 pci_device_save(&opp
->pci_dev
, f
);
1108 static void openpic_load_IRQ_queue(QEMUFile
* f
, IRQ_queue_t
*q
)
1112 for (i
= 0; i
< BF_WIDTH(MAX_IRQ
); i
++)
1113 qemu_get_be32s(f
, &q
->queue
[i
]);
1115 qemu_get_sbe32s(f
, &q
->next
);
1116 qemu_get_sbe32s(f
, &q
->priority
);
1119 static int openpic_load(QEMUFile
* f
, void *opaque
, int version_id
)
1121 openpic_t
*opp
= (openpic_t
*)opaque
;
1124 if (version_id
!= 1)
1127 qemu_get_be32s(f
, &opp
->frep
);
1128 qemu_get_be32s(f
, &opp
->glbc
);
1129 qemu_get_be32s(f
, &opp
->micr
);
1130 qemu_get_be32s(f
, &opp
->veni
);
1131 qemu_get_be32s(f
, &opp
->pint
);
1132 qemu_get_be32s(f
, &opp
->spve
);
1133 qemu_get_be32s(f
, &opp
->tifr
);
1135 for (i
= 0; i
< opp
->max_irq
; i
++) {
1136 qemu_get_be32s(f
, &opp
->src
[i
].ipvp
);
1137 qemu_get_be32s(f
, &opp
->src
[i
].ide
);
1138 qemu_get_sbe32s(f
, &opp
->src
[i
].type
);
1139 qemu_get_sbe32s(f
, &opp
->src
[i
].last_cpu
);
1140 qemu_get_sbe32s(f
, &opp
->src
[i
].pending
);
1143 qemu_get_sbe32s(f
, &opp
->nb_cpus
);
1145 for (i
= 0; i
< opp
->nb_cpus
; i
++) {
1146 qemu_get_be32s(f
, &opp
->dst
[i
].tfrr
);
1147 qemu_get_be32s(f
, &opp
->dst
[i
].pctp
);
1148 qemu_get_be32s(f
, &opp
->dst
[i
].pcsr
);
1149 openpic_load_IRQ_queue(f
, &opp
->dst
[i
].raised
);
1150 openpic_load_IRQ_queue(f
, &opp
->dst
[i
].servicing
);
1153 for (i
= 0; i
< MAX_TMR
; i
++) {
1154 qemu_get_be32s(f
, &opp
->timers
[i
].ticc
);
1155 qemu_get_be32s(f
, &opp
->timers
[i
].tibc
);
1159 qemu_get_be32s(f
, &opp
->dar
);
1161 for (i
= 0; i
< MAX_DBL
; i
++) {
1162 qemu_get_be32s(f
, &opp
->doorbells
[i
].dmr
);
1167 for (i
= 0; i
< MAX_MAILBOXES
; i
++) {
1168 qemu_get_be32s(f
, &opp
->mailboxes
[i
].mbr
);
1172 return pci_device_load(&opp
->pci_dev
, f
);
1175 static void openpic_irq_raise(openpic_t
*opp
, int n_CPU
, IRQ_src_t
*src
)
1177 qemu_irq_raise(opp
->dst
[n_CPU
].irqs
[OPENPIC_OUTPUT_INT
]);
1180 qemu_irq
*openpic_init (PCIBus
*bus
, int *pmem_index
, int nb_cpus
,
1181 qemu_irq
**irqs
, qemu_irq irq_out
)
1187 /* XXX: for now, only one CPU is supported */
1191 opp
= (openpic_t
*)pci_register_device(bus
, "OpenPIC", sizeof(openpic_t
),
1193 pci_conf
= opp
->pci_dev
.config
;
1194 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_IBM
);
1195 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_IBM_OPENPIC2
);
1196 pci_config_set_class(pci_conf
, PCI_CLASS_SYSTEM_OTHER
); // FIXME?
1197 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
1198 pci_conf
[0x3d] = 0x00; // no interrupt pin
1200 /* Register I/O spaces */
1201 pci_register_bar((PCIDevice
*)opp
, 0, 0x40000,
1202 PCI_BASE_ADDRESS_SPACE_MEMORY
, &openpic_map
);
1204 opp
= qemu_mallocz(sizeof(openpic_t
));
1206 opp
->mem_index
= cpu_register_io_memory(openpic_read
,
1207 openpic_write
, opp
);
1209 // isu_base &= 0xFFFC0000;
1210 opp
->nb_cpus
= nb_cpus
;
1211 opp
->max_irq
= OPENPIC_MAX_IRQ
;
1212 opp
->irq_ipi0
= OPENPIC_IRQ_IPI0
;
1213 opp
->irq_tim0
= OPENPIC_IRQ_TIM0
;
1215 for (i
= 0; i
< OPENPIC_EXT_IRQ
; i
++) {
1216 opp
->src
[i
].type
= IRQ_EXTERNAL
;
1218 for (; i
< OPENPIC_IRQ_TIM0
; i
++) {
1219 opp
->src
[i
].type
= IRQ_SPECIAL
;
1222 m
= OPENPIC_IRQ_IPI0
;
1224 m
= OPENPIC_IRQ_DBL0
;
1226 for (; i
< m
; i
++) {
1227 opp
->src
[i
].type
= IRQ_TIMER
;
1229 for (; i
< OPENPIC_MAX_IRQ
; i
++) {
1230 opp
->src
[i
].type
= IRQ_INTERNAL
;
1232 for (i
= 0; i
< nb_cpus
; i
++)
1233 opp
->dst
[i
].irqs
= irqs
[i
];
1234 opp
->irq_out
= irq_out
;
1237 register_savevm("openpic", 0, 2, openpic_save
, openpic_load
, opp
);
1238 qemu_register_reset(openpic_reset
, opp
);
1240 opp
->irq_raise
= openpic_irq_raise
;
1241 opp
->reset
= openpic_reset
;
1244 *pmem_index
= opp
->mem_index
;
1246 return qemu_allocate_irqs(openpic_set_irq
, opp
, opp
->max_irq
);
1249 static void mpic_irq_raise(openpic_t
*mpp
, int n_CPU
, IRQ_src_t
*src
)
1251 int n_ci
= IDR_CI0
- n_CPU
;
1253 if(test_bit(&src
->ide
, n_ci
)) {
1254 qemu_irq_raise(mpp
->dst
[n_CPU
].irqs
[OPENPIC_OUTPUT_CINT
]);
1257 qemu_irq_raise(mpp
->dst
[n_CPU
].irqs
[OPENPIC_OUTPUT_INT
]);
1261 static void mpic_reset (void *opaque
)
1263 openpic_t
*mpp
= (openpic_t
*)opaque
;
1266 mpp
->glbc
= 0x80000000;
1267 /* Initialise controller registers */
1268 mpp
->frep
= 0x004f0002;
1270 mpp
->pint
= 0x00000000;
1271 mpp
->spve
= 0x0000FFFF;
1272 /* Initialise IRQ sources */
1273 for (i
= 0; i
< mpp
->max_irq
; i
++) {
1274 mpp
->src
[i
].ipvp
= 0x80800000;
1275 mpp
->src
[i
].ide
= 0x00000001;
1277 /* Initialise IRQ destinations */
1278 for (i
= 0; i
< MAX_CPU
; i
++) {
1279 mpp
->dst
[i
].pctp
= 0x0000000F;
1280 mpp
->dst
[i
].tfrr
= 0x00000000;
1281 memset(&mpp
->dst
[i
].raised
, 0, sizeof(IRQ_queue_t
));
1282 mpp
->dst
[i
].raised
.next
= -1;
1283 memset(&mpp
->dst
[i
].servicing
, 0, sizeof(IRQ_queue_t
));
1284 mpp
->dst
[i
].servicing
.next
= -1;
1286 /* Initialise timers */
1287 for (i
= 0; i
< MAX_TMR
; i
++) {
1288 mpp
->timers
[i
].ticc
= 0x00000000;
1289 mpp
->timers
[i
].tibc
= 0x80000000;
1291 /* Go out of RESET state */
1292 mpp
->glbc
= 0x00000000;
1295 static void mpic_timer_write (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1297 openpic_t
*mpp
= opaque
;
1300 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
1305 idx
= (addr
>> 6) & 0x3;
1306 switch (addr
& 0x30) {
1307 case 0x00: /* gtccr */
1309 case 0x10: /* gtbcr */
1310 if ((mpp
->timers
[idx
].ticc
& 0x80000000) != 0 &&
1311 (val
& 0x80000000) == 0 &&
1312 (mpp
->timers
[idx
].tibc
& 0x80000000) != 0)
1313 mpp
->timers
[idx
].ticc
&= ~0x80000000;
1314 mpp
->timers
[idx
].tibc
= val
;
1316 case 0x20: /* GTIVPR */
1317 write_IRQreg(mpp
, MPIC_TMR_IRQ
+ idx
, IRQ_IPVP
, val
);
1319 case 0x30: /* GTIDR & TFRR */
1320 if ((addr
& 0xF0) == 0xF0)
1321 mpp
->dst
[cpu
].tfrr
= val
;
1323 write_IRQreg(mpp
, MPIC_TMR_IRQ
+ idx
, IRQ_IDE
, val
);
1328 static uint32_t mpic_timer_read (void *opaque
, target_phys_addr_t addr
)
1330 openpic_t
*mpp
= opaque
;
1334 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1335 retval
= 0xFFFFFFFF;
1340 idx
= (addr
>> 6) & 0x3;
1341 switch (addr
& 0x30) {
1342 case 0x00: /* gtccr */
1343 retval
= mpp
->timers
[idx
].ticc
;
1345 case 0x10: /* gtbcr */
1346 retval
= mpp
->timers
[idx
].tibc
;
1348 case 0x20: /* TIPV */
1349 retval
= read_IRQreg(mpp
, MPIC_TMR_IRQ
+ idx
, IRQ_IPVP
);
1351 case 0x30: /* TIDR */
1352 if ((addr
&0xF0) == 0XF0)
1353 retval
= mpp
->dst
[cpu
].tfrr
;
1355 retval
= read_IRQreg(mpp
, MPIC_TMR_IRQ
+ idx
, IRQ_IDE
);
1358 DPRINTF("%s: => %08x\n", __func__
, retval
);
1363 static void mpic_src_ext_write (void *opaque
, target_phys_addr_t addr
,
1366 openpic_t
*mpp
= opaque
;
1367 int idx
= MPIC_EXT_IRQ
;
1369 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
1373 addr
-= MPIC_EXT_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1374 if (addr
< MPIC_EXT_REG_SIZE
) {
1375 idx
+= (addr
& 0xFFF0) >> 5;
1377 /* EXDE / IFEDE / IEEDE */
1378 write_IRQreg(mpp
, idx
, IRQ_IDE
, val
);
1380 /* EXVP / IFEVP / IEEVP */
1381 write_IRQreg(mpp
, idx
, IRQ_IPVP
, val
);
1386 static uint32_t mpic_src_ext_read (void *opaque
, target_phys_addr_t addr
)
1388 openpic_t
*mpp
= opaque
;
1390 int idx
= MPIC_EXT_IRQ
;
1392 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1393 retval
= 0xFFFFFFFF;
1397 addr
-= MPIC_EXT_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1398 if (addr
< MPIC_EXT_REG_SIZE
) {
1399 idx
+= (addr
& 0xFFF0) >> 5;
1401 /* EXDE / IFEDE / IEEDE */
1402 retval
= read_IRQreg(mpp
, idx
, IRQ_IDE
);
1404 /* EXVP / IFEVP / IEEVP */
1405 retval
= read_IRQreg(mpp
, idx
, IRQ_IPVP
);
1407 DPRINTF("%s: => %08x\n", __func__
, retval
);
1413 static void mpic_src_int_write (void *opaque
, target_phys_addr_t addr
,
1416 openpic_t
*mpp
= opaque
;
1417 int idx
= MPIC_INT_IRQ
;
1419 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
1423 addr
-= MPIC_INT_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1424 if (addr
< MPIC_INT_REG_SIZE
) {
1425 idx
+= (addr
& 0xFFF0) >> 5;
1427 /* EXDE / IFEDE / IEEDE */
1428 write_IRQreg(mpp
, idx
, IRQ_IDE
, val
);
1430 /* EXVP / IFEVP / IEEVP */
1431 write_IRQreg(mpp
, idx
, IRQ_IPVP
, val
);
1436 static uint32_t mpic_src_int_read (void *opaque
, target_phys_addr_t addr
)
1438 openpic_t
*mpp
= opaque
;
1440 int idx
= MPIC_INT_IRQ
;
1442 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1443 retval
= 0xFFFFFFFF;
1447 addr
-= MPIC_INT_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1448 if (addr
< MPIC_INT_REG_SIZE
) {
1449 idx
+= (addr
& 0xFFF0) >> 5;
1451 /* EXDE / IFEDE / IEEDE */
1452 retval
= read_IRQreg(mpp
, idx
, IRQ_IDE
);
1454 /* EXVP / IFEVP / IEEVP */
1455 retval
= read_IRQreg(mpp
, idx
, IRQ_IPVP
);
1457 DPRINTF("%s: => %08x\n", __func__
, retval
);
1463 static void mpic_src_msg_write (void *opaque
, target_phys_addr_t addr
,
1466 openpic_t
*mpp
= opaque
;
1467 int idx
= MPIC_MSG_IRQ
;
1469 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
1473 addr
-= MPIC_MSG_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1474 if (addr
< MPIC_MSG_REG_SIZE
) {
1475 idx
+= (addr
& 0xFFF0) >> 5;
1477 /* EXDE / IFEDE / IEEDE */
1478 write_IRQreg(mpp
, idx
, IRQ_IDE
, val
);
1480 /* EXVP / IFEVP / IEEVP */
1481 write_IRQreg(mpp
, idx
, IRQ_IPVP
, val
);
1486 static uint32_t mpic_src_msg_read (void *opaque
, target_phys_addr_t addr
)
1488 openpic_t
*mpp
= opaque
;
1490 int idx
= MPIC_MSG_IRQ
;
1492 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1493 retval
= 0xFFFFFFFF;
1497 addr
-= MPIC_MSG_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1498 if (addr
< MPIC_MSG_REG_SIZE
) {
1499 idx
+= (addr
& 0xFFF0) >> 5;
1501 /* EXDE / IFEDE / IEEDE */
1502 retval
= read_IRQreg(mpp
, idx
, IRQ_IDE
);
1504 /* EXVP / IFEVP / IEEVP */
1505 retval
= read_IRQreg(mpp
, idx
, IRQ_IPVP
);
1507 DPRINTF("%s: => %08x\n", __func__
, retval
);
1513 static void mpic_src_msi_write (void *opaque
, target_phys_addr_t addr
,
1516 openpic_t
*mpp
= opaque
;
1517 int idx
= MPIC_MSI_IRQ
;
1519 DPRINTF("%s: addr " TARGET_FMT_plx
" <= %08x\n", __func__
, addr
, val
);
1523 addr
-= MPIC_MSI_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1524 if (addr
< MPIC_MSI_REG_SIZE
) {
1525 idx
+= (addr
& 0xFFF0) >> 5;
1527 /* EXDE / IFEDE / IEEDE */
1528 write_IRQreg(mpp
, idx
, IRQ_IDE
, val
);
1530 /* EXVP / IFEVP / IEEVP */
1531 write_IRQreg(mpp
, idx
, IRQ_IPVP
, val
);
1535 static uint32_t mpic_src_msi_read (void *opaque
, target_phys_addr_t addr
)
1537 openpic_t
*mpp
= opaque
;
1539 int idx
= MPIC_MSI_IRQ
;
1541 DPRINTF("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
1542 retval
= 0xFFFFFFFF;
1546 addr
-= MPIC_MSI_REG_START
& (OPENPIC_PAGE_SIZE
- 1);
1547 if (addr
< MPIC_MSI_REG_SIZE
) {
1548 idx
+= (addr
& 0xFFF0) >> 5;
1550 /* EXDE / IFEDE / IEEDE */
1551 retval
= read_IRQreg(mpp
, idx
, IRQ_IDE
);
1553 /* EXVP / IFEVP / IEEVP */
1554 retval
= read_IRQreg(mpp
, idx
, IRQ_IPVP
);
1556 DPRINTF("%s: => %08x\n", __func__
, retval
);
1562 static CPUWriteMemoryFunc
* const mpic_glb_write
[] = {
1563 &openpic_buggy_write
,
1564 &openpic_buggy_write
,
1568 static CPUReadMemoryFunc
* const mpic_glb_read
[] = {
1569 &openpic_buggy_read
,
1570 &openpic_buggy_read
,
1574 static CPUWriteMemoryFunc
* const mpic_tmr_write
[] = {
1575 &openpic_buggy_write
,
1576 &openpic_buggy_write
,
1580 static CPUReadMemoryFunc
* const mpic_tmr_read
[] = {
1581 &openpic_buggy_read
,
1582 &openpic_buggy_read
,
1586 static CPUWriteMemoryFunc
* const mpic_cpu_write
[] = {
1587 &openpic_buggy_write
,
1588 &openpic_buggy_write
,
1592 static CPUReadMemoryFunc
* const mpic_cpu_read
[] = {
1593 &openpic_buggy_read
,
1594 &openpic_buggy_read
,
1598 static CPUWriteMemoryFunc
* const mpic_ext_write
[] = {
1599 &openpic_buggy_write
,
1600 &openpic_buggy_write
,
1601 &mpic_src_ext_write
,
1604 static CPUReadMemoryFunc
* const mpic_ext_read
[] = {
1605 &openpic_buggy_read
,
1606 &openpic_buggy_read
,
1610 static CPUWriteMemoryFunc
* const mpic_int_write
[] = {
1611 &openpic_buggy_write
,
1612 &openpic_buggy_write
,
1613 &mpic_src_int_write
,
1616 static CPUReadMemoryFunc
* const mpic_int_read
[] = {
1617 &openpic_buggy_read
,
1618 &openpic_buggy_read
,
1622 static CPUWriteMemoryFunc
* const mpic_msg_write
[] = {
1623 &openpic_buggy_write
,
1624 &openpic_buggy_write
,
1625 &mpic_src_msg_write
,
1628 static CPUReadMemoryFunc
* const mpic_msg_read
[] = {
1629 &openpic_buggy_read
,
1630 &openpic_buggy_read
,
1633 static CPUWriteMemoryFunc
* const mpic_msi_write
[] = {
1634 &openpic_buggy_write
,
1635 &openpic_buggy_write
,
1636 &mpic_src_msi_write
,
1639 static CPUReadMemoryFunc
* const mpic_msi_read
[] = {
1640 &openpic_buggy_read
,
1641 &openpic_buggy_read
,
1645 qemu_irq
*mpic_init (target_phys_addr_t base
, int nb_cpus
,
1646 qemu_irq
**irqs
, qemu_irq irq_out
)
1651 CPUReadMemoryFunc
* const *read
;
1652 CPUWriteMemoryFunc
* const *write
;
1653 target_phys_addr_t start_addr
;
1656 {mpic_glb_read
, mpic_glb_write
, MPIC_GLB_REG_START
, MPIC_GLB_REG_SIZE
},
1657 {mpic_tmr_read
, mpic_tmr_write
, MPIC_TMR_REG_START
, MPIC_TMR_REG_SIZE
},
1658 {mpic_ext_read
, mpic_ext_write
, MPIC_EXT_REG_START
, MPIC_EXT_REG_SIZE
},
1659 {mpic_int_read
, mpic_int_write
, MPIC_INT_REG_START
, MPIC_INT_REG_SIZE
},
1660 {mpic_msg_read
, mpic_msg_write
, MPIC_MSG_REG_START
, MPIC_MSG_REG_SIZE
},
1661 {mpic_msi_read
, mpic_msi_write
, MPIC_MSI_REG_START
, MPIC_MSI_REG_SIZE
},
1662 {mpic_cpu_read
, mpic_cpu_write
, MPIC_CPU_REG_START
, MPIC_CPU_REG_SIZE
},
1665 /* XXX: for now, only one CPU is supported */
1669 mpp
= qemu_mallocz(sizeof(openpic_t
));
1671 for (i
= 0; i
< sizeof(list
)/sizeof(list
[0]); i
++) {
1674 mem_index
= cpu_register_io_memory(list
[i
].read
, list
[i
].write
, mpp
);
1675 if (mem_index
< 0) {
1678 cpu_register_physical_memory(base
+ list
[i
].start_addr
,
1679 list
[i
].size
, mem_index
);
1682 mpp
->nb_cpus
= nb_cpus
;
1683 mpp
->max_irq
= MPIC_MAX_IRQ
;
1684 mpp
->irq_ipi0
= MPIC_IPI_IRQ
;
1685 mpp
->irq_tim0
= MPIC_TMR_IRQ
;
1687 for (i
= 0; i
< nb_cpus
; i
++)
1688 mpp
->dst
[i
].irqs
= irqs
[i
];
1689 mpp
->irq_out
= irq_out
;
1690 mpp
->need_swap
= 0; /* MPIC has the same endian as target */
1692 mpp
->irq_raise
= mpic_irq_raise
;
1693 mpp
->reset
= mpic_reset
;
1695 register_savevm("mpic", 0, 2, openpic_save
, openpic_load
, mpp
);
1696 qemu_register_reset(mpic_reset
, mpp
);
1698 return qemu_allocate_irqs(openpic_set_irq
, mpp
, mpp
->max_irq
);