Statistics
| Branch: | Revision:

root / hw / openpic.c @ 7830cf78

History | View | Annotate | Download (45.2 kB)

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