openpic.c 33.8 KB
Newer Older
B
bellard 已提交
1 2
/*
 * OpenPIC emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2004 Jocelyn Mayer
5
 *               2011 Alexander Graf
6
 *
B
bellard 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
/*
 *
 * Based on OpenPic implementations:
28
 * - Intel GW80314 I/O companion chip developer's manual
B
bellard 已提交
29 30 31 32 33
 * - Motorola MPC8245 & MPC8540 user manuals.
 * - Motorola MCP750 (aka Raven) programmer manual.
 * - Motorola Harrier programmer manuel
 *
 * Serial interrupts, as implemented in Raven chipset are not supported yet.
34
 *
B
bellard 已提交
35
 */
P
pbrook 已提交
36 37 38
#include "hw.h"
#include "ppc_mac.h"
#include "pci.h"
39
#include "openpic.h"
B
bellard 已提交
40

B
bellard 已提交
41
//#define DEBUG_OPENPIC
B
bellard 已提交
42 43

#ifdef DEBUG_OPENPIC
44
#define DPRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
B
bellard 已提交
45
#else
46
#define DPRINTF(fmt, ...) do { } while (0)
B
bellard 已提交
47 48
#endif

A
Alexander Graf 已提交
49 50
#define MAX_CPU     15
#define MAX_SRC     256
B
bellard 已提交
51 52 53
#define MAX_TMR     4
#define VECTOR_BITS 8
#define MAX_IPI     4
A
Alexander Graf 已提交
54
#define MAX_IRQ     (MAX_SRC + MAX_IPI + MAX_TMR)
B
bellard 已提交
55 56 57 58 59 60 61
#define VID         0x03 /* MPIC version ID */

enum {
    IRQ_IPVP = 0,
    IRQ_IDE,
};

62 63 64 65 66 67
/* OpenPIC */
#define OPENPIC_MAX_CPU      2
#define OPENPIC_MAX_IRQ     64
#define OPENPIC_EXT_IRQ     48
#define OPENPIC_MAX_TMR      MAX_TMR
#define OPENPIC_MAX_IPI      MAX_IPI
B
bellard 已提交
68

69 70 71 72 73 74 75
/* Interrupt definitions */
#define OPENPIC_IRQ_FE     (OPENPIC_EXT_IRQ)     /* Internal functional IRQ */
#define OPENPIC_IRQ_ERR    (OPENPIC_EXT_IRQ + 1) /* Error IRQ */
#define OPENPIC_IRQ_TIM0   (OPENPIC_EXT_IRQ + 2) /* First timer IRQ */
#if OPENPIC_MAX_IPI > 0
#define OPENPIC_IRQ_IPI0   (OPENPIC_IRQ_TIM0 + OPENPIC_MAX_TMR) /* First IPI IRQ */
#define OPENPIC_IRQ_DBL0   (OPENPIC_IRQ_IPI0 + (OPENPIC_MAX_CPU * OPENPIC_MAX_IPI)) /* First doorbell IRQ */
B
bellard 已提交
76
#else
77 78
#define OPENPIC_IRQ_DBL0   (OPENPIC_IRQ_TIM0 + OPENPIC_MAX_TMR) /* First doorbell IRQ */
#define OPENPIC_IRQ_MBX0   (OPENPIC_IRQ_DBL0 + OPENPIC_MAX_DBL) /* First mailbox IRQ */
B
bellard 已提交
79 80
#endif

81 82 83 84 85 86 87 88 89
#define OPENPIC_GLB_REG_START        0x0
#define OPENPIC_GLB_REG_SIZE         0x10F0
#define OPENPIC_TMR_REG_START        0x10F0
#define OPENPIC_TMR_REG_SIZE         0x220
#define OPENPIC_SRC_REG_START        0x10000
#define OPENPIC_SRC_REG_SIZE         (MAX_SRC * 0x20)
#define OPENPIC_CPU_REG_START        0x20000
#define OPENPIC_CPU_REG_SIZE         0x100 + ((MAX_CPU - 1) * 0x1000)

90 91 92 93
/* MPIC */
#define MPIC_MAX_CPU      1
#define MPIC_MAX_EXT     12
#define MPIC_MAX_INT     64
A
Alexander Graf 已提交
94
#define MPIC_MAX_IRQ     MAX_IRQ
B
bellard 已提交
95 96

/* Interrupt definitions */
A
Alexander Graf 已提交
97 98 99 100 101 102 103 104 105
/* IRQs, accessible through the IRQ region */
#define MPIC_EXT_IRQ      0x00
#define MPIC_INT_IRQ      0x10
#define MPIC_MSG_IRQ      0xb0
#define MPIC_MSI_IRQ      0xe0
/* These are available through separate regions, but
   for simplicity's sake mapped into the same number space */
#define MPIC_TMR_IRQ      0x100
#define MPIC_IPI_IRQ      0x104
106 107 108 109 110

#define MPIC_GLB_REG_START        0x0
#define MPIC_GLB_REG_SIZE         0x10F0
#define MPIC_TMR_REG_START        0x10F0
#define MPIC_TMR_REG_SIZE         0x220
111 112
#define MPIC_SRC_REG_START        0x10000
#define MPIC_SRC_REG_SIZE         (MAX_SRC * 0x20)
113
#define MPIC_CPU_REG_START        0x20000
A
Alexander Graf 已提交
114
#define MPIC_CPU_REG_SIZE         0x100 + ((MAX_CPU - 1) * 0x1000)
115

B
Bharat Bhushan 已提交
116 117 118 119 120 121 122 123 124 125 126
/*
 * Block Revision Register1 (BRR1): QEMU does not fully emulate
 * any version on MPIC. So to start with, set the IP version to 0.
 *
 * NOTE: This is Freescale MPIC specific register. Keep it here till
 * this code is refactored for different variants of OPENPIC and MPIC.
 */
#define FSL_BRR1_IPID (0x0040 << 16) /* 16 bit IP-block ID */
#define FSL_BRR1_IPMJ (0x00 << 8) /* 8 bit IP major number */
#define FSL_BRR1_IPMN 0x00 /* 8 bit IP minor number */

127 128 129 130 131 132 133 134
#define FREP_NIRQ_SHIFT   16
#define FREP_NCPU_SHIFT    8
#define FREP_VID_SHIFT     0

#define VID_REVISION_1_2   2

#define VENI_GENERIC      0x00000000 /* Generic Vendor ID */

135
enum mpic_ide_bits {
A
Alexander Graf 已提交
136 137 138 139 140
    IDR_EP     = 31,
    IDR_CI0     = 30,
    IDR_CI1     = 29,
    IDR_P1     = 1,
    IDR_P0     = 0,
141 142
};

B
bellard 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#define BF_WIDTH(_bits_) \
(((_bits_) + (sizeof(uint32_t) * 8) - 1) / (sizeof(uint32_t) * 8))

static inline void set_bit (uint32_t *field, int bit)
{
    field[bit >> 5] |= 1 << (bit & 0x1F);
}

static inline void reset_bit (uint32_t *field, int bit)
{
    field[bit >> 5] &= ~(1 << (bit & 0x1F));
}

static inline int test_bit (uint32_t *field, int bit)
{
    return (field[bit >> 5] & 1 << (bit & 0x1F)) != 0;
}

161 162 163 164 165
static int get_current_cpu(void)
{
  return cpu_single_env->cpu_index;
}

A
Avi Kivity 已提交
166
static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
167
                                          int idx);
A
Avi Kivity 已提交
168
static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
169 170
                                       uint32_t val, int idx);

B
bellard 已提交
171 172 173 174 175
enum {
    IRQ_EXTERNAL = 0x01,
    IRQ_INTERNAL = 0x02,
    IRQ_TIMER    = 0x04,
    IRQ_SPECIAL  = 0x08,
176
};
B
bellard 已提交
177

A
Anthony Liguori 已提交
178
typedef struct IRQ_queue_t {
B
bellard 已提交
179 180 181
    uint32_t queue[BF_WIDTH(MAX_IRQ)];
    int next;
    int priority;
A
Anthony Liguori 已提交
182
} IRQ_queue_t;
B
bellard 已提交
183

A
Anthony Liguori 已提交
184
typedef struct IRQ_src_t {
B
bellard 已提交
185 186 187 188
    uint32_t ipvp;  /* IRQ vector/priority register */
    uint32_t ide;   /* IRQ destination register */
    int type;
    int last_cpu;
B
bellard 已提交
189
    int pending;    /* TRUE if IRQ is pending */
A
Anthony Liguori 已提交
190
} IRQ_src_t;
B
bellard 已提交
191 192 193 194 195 196 197 198 199

enum IPVP_bits {
    IPVP_MASK     = 31,
    IPVP_ACTIVITY = 30,
    IPVP_MODE     = 29,
    IPVP_POLARITY = 23,
    IPVP_SENSE    = 22,
};
#define IPVP_PRIORITY_MASK     (0x1F << 16)
B
bellard 已提交
200
#define IPVP_PRIORITY(_ipvpr_) ((int)(((_ipvpr_) & IPVP_PRIORITY_MASK) >> 16))
B
bellard 已提交
201 202 203
#define IPVP_VECTOR_MASK       ((1 << VECTOR_BITS) - 1)
#define IPVP_VECTOR(_ipvpr_)   ((_ipvpr_) & IPVP_VECTOR_MASK)

A
Anthony Liguori 已提交
204
typedef struct IRQ_dst_t {
B
bellard 已提交
205 206
    uint32_t pctp; /* CPU current task priority */
    uint32_t pcsr; /* CPU sensitivity register */
A
Anthony Liguori 已提交
207 208
    IRQ_queue_t raised;
    IRQ_queue_t servicing;
209
    qemu_irq *irqs;
A
Anthony Liguori 已提交
210
} IRQ_dst_t;
B
bellard 已提交
211

A
Anthony Liguori 已提交
212
typedef struct openpic_t {
B
bellard 已提交
213
    PCIDevice pci_dev;
A
Avi Kivity 已提交
214
    MemoryRegion mem;
215

216 217
    /* Behavior control */
    uint32_t flags;
218 219 220 221 222 223 224
    uint32_t nb_irqs;
    uint32_t vid;
    uint32_t veni; /* Vendor identification register */
    uint32_t spve_mask;
    uint32_t tifr_reset;
    uint32_t ipvp_reset;
    uint32_t ide_reset;
225

226 227 228
    /* Sub-regions */
    MemoryRegion sub_io_mem[7];

B
bellard 已提交
229 230 231
    /* Global registers */
    uint32_t frep; /* Feature reporting register */
    uint32_t glbc; /* Global configuration register  */
232
    uint32_t pint; /* Processor initialization register */
B
bellard 已提交
233 234 235
    uint32_t spve; /* Spurious vector register */
    uint32_t tifr; /* Timer frequency reporting register */
    /* Source registers */
A
Anthony Liguori 已提交
236
    IRQ_src_t src[MAX_IRQ];
B
bellard 已提交
237
    /* Local registers per output pin */
A
Anthony Liguori 已提交
238
    IRQ_dst_t dst[MAX_CPU];
B
bellard 已提交
239 240 241
    int nb_cpus;
    /* Timer registers */
    struct {
242 243
        uint32_t ticc;  /* Global timer current count register */
        uint32_t tibc;  /* Global timer base count register */
B
bellard 已提交
244
    } timers[MAX_TMR];
245 246
    /* IRQ out is used when in bypass mode (not implemented) */
    qemu_irq irq_out;
247 248 249
    int max_irq;
    int irq_ipi0;
    int irq_tim0;
A
Anthony Liguori 已提交
250
} openpic_t;
B
bellard 已提交
251

252 253
static void openpic_irq_raise(openpic_t *opp, int n_CPU, IRQ_src_t *src);

A
Anthony Liguori 已提交
254
static inline void IRQ_setbit (IRQ_queue_t *q, int n_IRQ)
B
bellard 已提交
255 256 257 258
{
    set_bit(q->queue, n_IRQ);
}

A
Anthony Liguori 已提交
259
static inline void IRQ_resetbit (IRQ_queue_t *q, int n_IRQ)
B
bellard 已提交
260 261 262 263
{
    reset_bit(q->queue, n_IRQ);
}

A
Anthony Liguori 已提交
264
static inline int IRQ_testbit (IRQ_queue_t *q, int n_IRQ)
B
bellard 已提交
265 266 267 268
{
    return test_bit(q->queue, n_IRQ);
}

A
Anthony Liguori 已提交
269
static void IRQ_check (openpic_t *opp, IRQ_queue_t *q)
B
bellard 已提交
270 271 272 273 274 275
{
    int next, i;
    int priority;

    next = -1;
    priority = -1;
276
    for (i = 0; i < opp->max_irq; i++) {
277
        if (IRQ_testbit(q, i)) {
278
            DPRINTF("IRQ_check: irq %d set ipvp_pr=%d pr=%d\n",
B
bellard 已提交
279
                    i, IPVP_PRIORITY(opp->src[i].ipvp), priority);
280 281 282 283 284
            if (IPVP_PRIORITY(opp->src[i].ipvp) > priority) {
                next = i;
                priority = IPVP_PRIORITY(opp->src[i].ipvp);
            }
        }
B
bellard 已提交
285 286 287 288 289
    }
    q->next = next;
    q->priority = priority;
}

A
Anthony Liguori 已提交
290
static int IRQ_get_next (openpic_t *opp, IRQ_queue_t *q)
B
bellard 已提交
291 292
{
    if (q->next == -1) {
B
bellard 已提交
293
        /* XXX: optimize */
294
        IRQ_check(opp, q);
B
bellard 已提交
295 296 297 298 299
    }

    return q->next;
}

A
Anthony Liguori 已提交
300
static void IRQ_local_pipe (openpic_t *opp, int n_CPU, int n_IRQ)
B
bellard 已提交
301
{
A
Anthony Liguori 已提交
302 303
    IRQ_dst_t *dst;
    IRQ_src_t *src;
B
bellard 已提交
304 305 306 307 308 309
    int priority;

    dst = &opp->dst[n_CPU];
    src = &opp->src[n_IRQ];
    priority = IPVP_PRIORITY(src->ipvp);
    if (priority <= dst->pctp) {
310
        /* Too low priority */
311 312
        DPRINTF("%s: IRQ %d has too low priority on CPU %d\n",
                __func__, n_IRQ, n_CPU);
313
        return;
B
bellard 已提交
314 315
    }
    if (IRQ_testbit(&dst->raised, n_IRQ)) {
316
        /* Interrupt miss */
317 318
        DPRINTF("%s: IRQ %d was missed on CPU %d\n",
                __func__, n_IRQ, n_CPU);
319
        return;
B
bellard 已提交
320 321 322
    }
    set_bit(&src->ipvp, IPVP_ACTIVITY);
    IRQ_setbit(&dst->raised, n_IRQ);
323 324 325 326 327 328 329 330
    if (priority < dst->raised.priority) {
        /* An higher priority IRQ is already raised */
        DPRINTF("%s: IRQ %d is hidden by raised IRQ %d on CPU %d\n",
                __func__, n_IRQ, dst->raised.next, n_CPU);
        return;
    }
    IRQ_get_next(opp, &dst->raised);
    if (IRQ_get_next(opp, &dst->servicing) != -1 &&
331
        priority <= dst->servicing.priority) {
332 333 334 335
        DPRINTF("%s: IRQ %d is hidden by servicing IRQ %d on CPU %d\n",
                __func__, n_IRQ, dst->servicing.next, n_CPU);
        /* Already servicing a higher priority IRQ */
        return;
B
bellard 已提交
336
    }
337
    DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n", n_CPU, n_IRQ);
338
    openpic_irq_raise(opp, n_CPU, src);
B
bellard 已提交
339 340
}

B
bellard 已提交
341
/* update pic state because registers for n_IRQ have changed value */
A
Anthony Liguori 已提交
342
static void openpic_update_irq(openpic_t *opp, int n_IRQ)
B
bellard 已提交
343
{
A
Anthony Liguori 已提交
344
    IRQ_src_t *src;
B
bellard 已提交
345 346 347
    int i;

    src = &opp->src[n_IRQ];
B
bellard 已提交
348 349 350

    if (!src->pending) {
        /* no irq pending */
351
        DPRINTF("%s: IRQ %d is not pending\n", __func__, n_IRQ);
B
bellard 已提交
352 353 354
        return;
    }
    if (test_bit(&src->ipvp, IPVP_MASK)) {
355
        /* Interrupt source is disabled */
356
        DPRINTF("%s: IRQ %d is disabled\n", __func__, n_IRQ);
357
        return;
B
bellard 已提交
358 359
    }
    if (IPVP_PRIORITY(src->ipvp) == 0) {
360
        /* Priority set to zero */
361
        DPRINTF("%s: IRQ %d has 0 priority\n", __func__, n_IRQ);
362
        return;
B
bellard 已提交
363
    }
B
bellard 已提交
364 365
    if (test_bit(&src->ipvp, IPVP_ACTIVITY)) {
        /* IRQ already active */
366
        DPRINTF("%s: IRQ %d is already active\n", __func__, n_IRQ);
B
bellard 已提交
367 368
        return;
    }
B
bellard 已提交
369
    if (src->ide == 0x00000000) {
370
        /* No target */
371
        DPRINTF("%s: IRQ %d has no target\n", __func__, n_IRQ);
372
        return;
B
bellard 已提交
373
    }
B
bellard 已提交
374

375 376 377 378
    if (src->ide == (1 << src->last_cpu)) {
        /* Only one CPU is allowed to receive this IRQ */
        IRQ_local_pipe(opp, src->last_cpu, n_IRQ);
    } else if (!test_bit(&src->ipvp, IPVP_MODE)) {
B
bellard 已提交
379 380 381 382 383
        /* Directed delivery mode */
        for (i = 0; i < opp->nb_cpus; i++) {
            if (test_bit(&src->ide, i))
                IRQ_local_pipe(opp, i, n_IRQ);
        }
B
bellard 已提交
384
    } else {
B
bellard 已提交
385
        /* Distributed delivery mode */
386 387
        for (i = src->last_cpu + 1; i != src->last_cpu; i++) {
            if (i == opp->nb_cpus)
B
bellard 已提交
388 389 390 391 392 393 394 395 396 397
                i = 0;
            if (test_bit(&src->ide, i)) {
                IRQ_local_pipe(opp, i, n_IRQ);
                src->last_cpu = i;
                break;
            }
        }
    }
}

P
pbrook 已提交
398
static void openpic_set_irq(void *opaque, int n_IRQ, int level)
B
bellard 已提交
399
{
A
Anthony Liguori 已提交
400 401
    openpic_t *opp = opaque;
    IRQ_src_t *src;
B
bellard 已提交
402 403

    src = &opp->src[n_IRQ];
404
    DPRINTF("openpic: set irq %d = %d ipvp=%08x\n",
B
bellard 已提交
405 406 407 408 409 410 411 412 413 414
            n_IRQ, level, src->ipvp);
    if (test_bit(&src->ipvp, IPVP_SENSE)) {
        /* level-sensitive irq */
        src->pending = level;
        if (!level)
            reset_bit(&src->ipvp, IPVP_ACTIVITY);
    } else {
        /* edge-sensitive irq */
        if (level)
            src->pending = 1;
B
bellard 已提交
415
    }
B
bellard 已提交
416
    openpic_update_irq(opp, n_IRQ);
B
bellard 已提交
417 418
}

419
static void openpic_reset (void *opaque)
B
bellard 已提交
420
{
A
Anthony Liguori 已提交
421
    openpic_t *opp = (openpic_t *)opaque;
B
bellard 已提交
422 423 424
    int i;

    opp->glbc = 0x80000000;
B
bellard 已提交
425
    /* Initialise controller registers */
426 427 428 429
    opp->frep = ((opp->nb_irqs -1) << FREP_NIRQ_SHIFT) |
                ((opp->nb_cpus -1) << FREP_NCPU_SHIFT) |
                (opp->vid << FREP_VID_SHIFT);

430
    opp->pint = 0x00000000;
431 432
    opp->spve = -1 & opp->spve_mask;
    opp->tifr = opp->tifr_reset;
B
bellard 已提交
433
    /* Initialise IRQ sources */
434
    for (i = 0; i < opp->max_irq; i++) {
435 436
        opp->src[i].ipvp = opp->ipvp_reset;
        opp->src[i].ide  = opp->ide_reset;
B
bellard 已提交
437 438
    }
    /* Initialise IRQ destinations */
439
    for (i = 0; i < MAX_CPU; i++) {
440 441 442
        opp->dst[i].pctp      = 0x0000000F;
        opp->dst[i].pcsr      = 0x00000000;
        memset(&opp->dst[i].raised, 0, sizeof(IRQ_queue_t));
443
        opp->dst[i].raised.next = -1;
444
        memset(&opp->dst[i].servicing, 0, sizeof(IRQ_queue_t));
445
        opp->dst[i].servicing.next = -1;
B
bellard 已提交
446 447 448
    }
    /* Initialise timers */
    for (i = 0; i < MAX_TMR; i++) {
449 450
        opp->timers[i].ticc = 0x00000000;
        opp->timers[i].tibc = 0x80000000;
B
bellard 已提交
451 452 453 454 455
    }
    /* Go out of RESET state */
    opp->glbc = 0x00000000;
}

A
Alexander Graf 已提交
456
static inline uint32_t read_IRQreg_ide(openpic_t *opp, int n_IRQ)
B
bellard 已提交
457
{
A
Alexander Graf 已提交
458 459
    return opp->src[n_IRQ].ide;
}
B
bellard 已提交
460

A
Alexander Graf 已提交
461 462 463
static inline uint32_t read_IRQreg_ipvp(openpic_t *opp, int n_IRQ)
{
    return opp->src[n_IRQ].ipvp;
B
bellard 已提交
464 465
}

A
Alexander Graf 已提交
466
static inline void write_IRQreg_ide(openpic_t *opp, int n_IRQ, uint32_t val)
B
bellard 已提交
467 468 469
{
    uint32_t tmp;

A
Alexander Graf 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    tmp = val & 0xC0000000;
    tmp |= val & ((1ULL << MAX_CPU) - 1);
    opp->src[n_IRQ].ide = tmp;
    DPRINTF("Set IDE %d to 0x%08x\n", n_IRQ, opp->src[n_IRQ].ide);
}

static inline void write_IRQreg_ipvp(openpic_t *opp, int n_IRQ, uint32_t val)
{
    /* NOTE: not fully accurate for special IRQs, but simple and sufficient */
    /* ACTIVITY bit is read-only */
    opp->src[n_IRQ].ipvp = (opp->src[n_IRQ].ipvp & 0x40000000)
                         | (val & 0x800F00FF);
    openpic_update_irq(opp, n_IRQ);
    DPRINTF("Set IPVP %d to 0x%08x -> 0x%08x\n", n_IRQ, val,
            opp->src[n_IRQ].ipvp);
B
bellard 已提交
485 486
}

487 488
static void openpic_gbl_write(void *opaque, hwaddr addr, uint64_t val,
                              unsigned len)
B
bellard 已提交
489
{
A
Anthony Liguori 已提交
490 491
    openpic_t *opp = opaque;
    IRQ_dst_t *dst;
492
    int idx;
B
bellard 已提交
493

494
    DPRINTF("%s: addr " TARGET_FMT_plx " <= %08x\n", __func__, addr, val);
B
bellard 已提交
495 496 497
    if (addr & 0xF)
        return;
    switch (addr) {
B
Bharat Bhushan 已提交
498 499
    case 0x00: /* Block Revision Register1 (BRR1) is Readonly */
        break;
500 501 502 503 504 505 506 507 508
    case 0x40:
    case 0x50:
    case 0x60:
    case 0x70:
    case 0x80:
    case 0x90:
    case 0xA0:
    case 0xB0:
        openpic_cpu_write_internal(opp, addr, val, get_current_cpu());
B
bellard 已提交
509
        break;
510
    case 0x1000: /* FREP */
B
bellard 已提交
511
        break;
512
    case 0x1020: /* GLBC */
513 514 515
        if (val & 0x80000000) {
            openpic_reset(opp);
        }
516
        break;
517
    case 0x1080: /* VENI */
518
        break;
519
    case 0x1090: /* PINT */
520 521 522 523 524 525 526 527 528 529
        for (idx = 0; idx < opp->nb_cpus; idx++) {
            if ((val & (1 << idx)) && !(opp->pint & (1 << idx))) {
                DPRINTF("Raise OpenPIC RESET output for CPU %d\n", idx);
                dst = &opp->dst[idx];
                qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_RESET]);
            } else if (!(val & (1 << idx)) && (opp->pint & (1 << idx))) {
                DPRINTF("Lower OpenPIC RESET output for CPU %d\n", idx);
                dst = &opp->dst[idx];
                qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_RESET]);
            }
B
bellard 已提交
530
        }
531
        opp->pint = val;
532
        break;
533 534 535 536
    case 0x10A0: /* IPI_IPVP */
    case 0x10B0:
    case 0x10C0:
    case 0x10D0:
B
bellard 已提交
537 538
        {
            int idx;
539
            idx = (addr - 0x10A0) >> 4;
A
Alexander Graf 已提交
540
            write_IRQreg_ipvp(opp, opp->irq_ipi0 + idx, val);
B
bellard 已提交
541 542
        }
        break;
543
    case 0x10E0: /* SPVE */
544
        opp->spve = val & opp->spve_mask;
B
bellard 已提交
545 546 547 548 549 550
        break;
    default:
        break;
    }
}

551
static uint64_t openpic_gbl_read(void *opaque, hwaddr addr, unsigned len)
B
bellard 已提交
552
{
A
Anthony Liguori 已提交
553
    openpic_t *opp = opaque;
B
bellard 已提交
554 555
    uint32_t retval;

556
    DPRINTF("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
B
bellard 已提交
557 558 559 560
    retval = 0xFFFFFFFF;
    if (addr & 0xF)
        return retval;
    switch (addr) {
561
    case 0x1000: /* FREP */
B
bellard 已提交
562 563
        retval = opp->frep;
        break;
564
    case 0x1020: /* GLBC */
B
bellard 已提交
565
        retval = opp->glbc;
566
        break;
567
    case 0x1080: /* VENI */
B
bellard 已提交
568
        retval = opp->veni;
569
        break;
570
    case 0x1090: /* PINT */
B
bellard 已提交
571
        retval = 0x00000000;
572
        break;
B
Bharat Bhushan 已提交
573
    case 0x00: /* Block Revision Register1 (BRR1) */
574 575 576 577 578 579 580
    case 0x40:
    case 0x50:
    case 0x60:
    case 0x70:
    case 0x80:
    case 0x90:
    case 0xA0:
B
bellard 已提交
581
    case 0xB0:
582 583 584 585 586 587
        retval = openpic_cpu_read_internal(opp, addr, get_current_cpu());
        break;
    case 0x10A0: /* IPI_IPVP */
    case 0x10B0:
    case 0x10C0:
    case 0x10D0:
B
bellard 已提交
588 589
        {
            int idx;
590
            idx = (addr - 0x10A0) >> 4;
A
Alexander Graf 已提交
591
            retval = read_IRQreg_ipvp(opp, opp->irq_ipi0 + idx);
B
bellard 已提交
592
        }
593
        break;
594
    case 0x10E0: /* SPVE */
B
bellard 已提交
595 596 597 598 599 600 601 602 603 604
        retval = opp->spve;
        break;
    default:
        break;
    }
    DPRINTF("%s: => %08x\n", __func__, retval);

    return retval;
}

605 606
static void openpic_timer_write(void *opaque, hwaddr addr, uint64_t val,
                                unsigned len)
B
bellard 已提交
607
{
A
Anthony Liguori 已提交
608
    openpic_t *opp = opaque;
B
bellard 已提交
609 610 611 612 613
    int idx;

    DPRINTF("%s: addr %08x <= %08x\n", __func__, addr, val);
    if (addr & 0xF)
        return;
614
    idx = (addr >> 6) & 0x3;
B
bellard 已提交
615
    addr = addr & 0x30;
616 617 618 619 620 621 622 623

    if (addr == 0x0) {
        /* TIFR (TFRR) */
        opp->tifr = val;
        return;
    }
    switch (addr & 0x30) {
    case 0x00: /* TICC (GTCCR) */
B
bellard 已提交
624
        break;
625
    case 0x10: /* TIBC (GTBCR) */
626 627
        if ((opp->timers[idx].ticc & 0x80000000) != 0 &&
            (val & 0x80000000) == 0 &&
B
bellard 已提交
628
            (opp->timers[idx].tibc & 0x80000000) != 0)
629 630 631
            opp->timers[idx].ticc &= ~0x80000000;
        opp->timers[idx].tibc = val;
        break;
632
    case 0x20: /* TIVP (GTIVPR) */
A
Alexander Graf 已提交
633
        write_IRQreg_ipvp(opp, opp->irq_tim0 + idx, val);
634
        break;
635
    case 0x30: /* TIDE (GTIDR) */
A
Alexander Graf 已提交
636
        write_IRQreg_ide(opp, opp->irq_tim0 + idx, val);
637
        break;
B
bellard 已提交
638 639 640
    }
}

641
static uint64_t openpic_timer_read(void *opaque, hwaddr addr, unsigned len)
B
bellard 已提交
642
{
A
Anthony Liguori 已提交
643
    openpic_t *opp = opaque;
644
    uint32_t retval = -1;
B
bellard 已提交
645 646 647
    int idx;

    DPRINTF("%s: addr %08x\n", __func__, addr);
648 649 650 651 652 653 654 655 656 657 658
    if (addr & 0xF) {
        goto out;
    }
    idx = (addr >> 6) & 0x3;
    if (addr == 0x0) {
        /* TIFR (TFRR) */
        retval = opp->tifr;
        goto out;
    }
    switch (addr & 0x30) {
    case 0x00: /* TICC (GTCCR) */
659
        retval = opp->timers[idx].ticc;
B
bellard 已提交
660
        break;
661
    case 0x10: /* TIBC (GTBCR) */
662 663
        retval = opp->timers[idx].tibc;
        break;
664
    case 0x20: /* TIPV (TIPV) */
A
Alexander Graf 已提交
665
        retval = read_IRQreg_ipvp(opp, opp->irq_tim0 + idx);
666
        break;
667
    case 0x30: /* TIDE (TIDR) */
A
Alexander Graf 已提交
668
        retval = read_IRQreg_ide(opp, opp->irq_tim0 + idx);
669
        break;
B
bellard 已提交
670
    }
671 672

out:
B
bellard 已提交
673 674 675 676 677
    DPRINTF("%s: => %08x\n", __func__, retval);

    return retval;
}

678 679
static void openpic_src_write(void *opaque, hwaddr addr, uint64_t val,
                              unsigned len)
B
bellard 已提交
680
{
A
Anthony Liguori 已提交
681
    openpic_t *opp = opaque;
B
bellard 已提交
682 683 684 685 686 687 688 689 690
    int idx;

    DPRINTF("%s: addr %08x <= %08x\n", __func__, addr, val);
    if (addr & 0xF)
        return;
    addr = addr & 0xFFF0;
    idx = addr >> 5;
    if (addr & 0x10) {
        /* EXDE / IFEDE / IEEDE */
A
Alexander Graf 已提交
691
        write_IRQreg_ide(opp, idx, val);
B
bellard 已提交
692 693
    } else {
        /* EXVP / IFEVP / IEEVP */
A
Alexander Graf 已提交
694
        write_IRQreg_ipvp(opp, idx, val);
B
bellard 已提交
695 696 697
    }
}

698
static uint64_t openpic_src_read(void *opaque, uint64_t addr, unsigned len)
B
bellard 已提交
699
{
A
Anthony Liguori 已提交
700
    openpic_t *opp = opaque;
B
bellard 已提交
701 702 703 704 705 706 707 708 709 710 711
    uint32_t retval;
    int idx;

    DPRINTF("%s: addr %08x\n", __func__, addr);
    retval = 0xFFFFFFFF;
    if (addr & 0xF)
        return retval;
    addr = addr & 0xFFF0;
    idx = addr >> 5;
    if (addr & 0x10) {
        /* EXDE / IFEDE / IEEDE */
A
Alexander Graf 已提交
712
        retval = read_IRQreg_ide(opp, idx);
B
bellard 已提交
713 714
    } else {
        /* EXVP / IFEVP / IEEVP */
A
Alexander Graf 已提交
715
        retval = read_IRQreg_ipvp(opp, idx);
B
bellard 已提交
716 717 718 719 720 721
    }
    DPRINTF("%s: => %08x\n", __func__, retval);

    return retval;
}

A
Avi Kivity 已提交
722
static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
723
                                       uint32_t val, int idx)
B
bellard 已提交
724
{
A
Anthony Liguori 已提交
725 726 727
    openpic_t *opp = opaque;
    IRQ_src_t *src;
    IRQ_dst_t *dst;
728
    int s_IRQ, n_IRQ;
B
bellard 已提交
729

730 731
    DPRINTF("%s: cpu %d addr " TARGET_FMT_plx " <= %08x\n", __func__, idx,
            addr, val);
B
bellard 已提交
732 733 734 735 736
    if (addr & 0xF)
        return;
    dst = &opp->dst[idx];
    addr &= 0xFF0;
    switch (addr) {
737
    case 0x40: /* IPIDR */
B
bellard 已提交
738 739 740 741
    case 0x50:
    case 0x60:
    case 0x70:
        idx = (addr - 0x40) >> 4;
A
Alexander Graf 已提交
742
        /* we use IDE as mask which CPUs to deliver the IPI to still. */
A
Alexander Graf 已提交
743 744
        write_IRQreg_ide(opp, opp->irq_ipi0 + idx,
                         opp->src[opp->irq_ipi0 + idx].ide | val);
745 746
        openpic_set_irq(opp, opp->irq_ipi0 + idx, 1);
        openpic_set_irq(opp, opp->irq_ipi0 + idx, 0);
B
bellard 已提交
747 748
        break;
    case 0x80: /* PCTP */
749 750
        dst->pctp = val & 0x0000000F;
        break;
B
bellard 已提交
751
    case 0x90: /* WHOAMI */
752 753
        /* Read-only register */
        break;
B
bellard 已提交
754
    case 0xA0: /* PIAC */
755 756
        /* Read-only register */
        break;
B
bellard 已提交
757 758
    case 0xB0: /* PEOI */
        DPRINTF("PEOI\n");
759 760 761 762 763
        s_IRQ = IRQ_get_next(opp, &dst->servicing);
        IRQ_resetbit(&dst->servicing, s_IRQ);
        dst->servicing.next = -1;
        /* Set up next servicing IRQ */
        s_IRQ = IRQ_get_next(opp, &dst->servicing);
764 765 766 767 768 769 770 771
        /* Check queued interrupts. */
        n_IRQ = IRQ_get_next(opp, &dst->raised);
        src = &opp->src[n_IRQ];
        if (n_IRQ != -1 &&
            (s_IRQ == -1 ||
             IPVP_PRIORITY(src->ipvp) > dst->servicing.priority)) {
            DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n",
                    idx, n_IRQ);
772
            openpic_irq_raise(opp, idx, src);
773
        }
774
        break;
B
bellard 已提交
775 776 777 778 779
    default:
        break;
    }
}

780 781
static void openpic_cpu_write(void *opaque, hwaddr addr, uint64_t val,
                              unsigned len)
782 783 784 785
{
    openpic_cpu_write_internal(opaque, addr, val, (addr & 0x1f000) >> 12);
}

A
Avi Kivity 已提交
786
static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
787
                                          int idx)
B
bellard 已提交
788
{
A
Anthony Liguori 已提交
789 790 791
    openpic_t *opp = opaque;
    IRQ_src_t *src;
    IRQ_dst_t *dst;
B
bellard 已提交
792
    uint32_t retval;
793
    int n_IRQ;
794

795
    DPRINTF("%s: cpu %d addr " TARGET_FMT_plx "\n", __func__, idx, addr);
B
bellard 已提交
796 797 798 799 800 801
    retval = 0xFFFFFFFF;
    if (addr & 0xF)
        return retval;
    dst = &opp->dst[idx];
    addr &= 0xFF0;
    switch (addr) {
B
Bharat Bhushan 已提交
802 803 804
    case 0x00: /* Block Revision Register1 (BRR1) */
        retval = FSL_BRR1_IPID | FSL_BRR1_IPMJ | FSL_BRR1_IPMN;
        break;
B
bellard 已提交
805
    case 0x80: /* PCTP */
806 807
        retval = dst->pctp;
        break;
B
bellard 已提交
808
    case 0x90: /* WHOAMI */
809 810
        retval = idx;
        break;
B
bellard 已提交
811
    case 0xA0: /* PIAC */
812 813
        DPRINTF("Lower OpenPIC INT output\n");
        qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
814
        n_IRQ = IRQ_get_next(opp, &dst->raised);
B
bellard 已提交
815
        DPRINTF("PIAC: irq=%d\n", n_IRQ);
816 817
        if (n_IRQ == -1) {
            /* No more interrupt pending */
818
            retval = IPVP_VECTOR(opp->spve);
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
        } else {
            src = &opp->src[n_IRQ];
            if (!test_bit(&src->ipvp, IPVP_ACTIVITY) ||
                !(IPVP_PRIORITY(src->ipvp) > dst->pctp)) {
                /* - Spurious level-sensitive IRQ
                 * - Priorities has been changed
                 *   and the pending IRQ isn't allowed anymore
                 */
                reset_bit(&src->ipvp, IPVP_ACTIVITY);
                retval = IPVP_VECTOR(opp->spve);
            } else {
                /* IRQ enter servicing state */
                IRQ_setbit(&dst->servicing, n_IRQ);
                retval = IPVP_VECTOR(src->ipvp);
            }
            IRQ_resetbit(&dst->raised, n_IRQ);
            dst->raised.next = -1;
            if (!test_bit(&src->ipvp, IPVP_SENSE)) {
B
bellard 已提交
837
                /* edge-sensitive IRQ */
838
                reset_bit(&src->ipvp, IPVP_ACTIVITY);
B
bellard 已提交
839 840
                src->pending = 0;
            }
A
Alexander Graf 已提交
841 842 843 844 845 846 847 848 849 850 851

            if ((n_IRQ >= opp->irq_ipi0) &&  (n_IRQ < (opp->irq_ipi0 + MAX_IPI))) {
                src->ide &= ~(1 << idx);
                if (src->ide && !test_bit(&src->ipvp, IPVP_SENSE)) {
                    /* trigger on CPUs that didn't know about it yet */
                    openpic_set_irq(opp, n_IRQ, 1);
                    openpic_set_irq(opp, n_IRQ, 0);
                    /* if all CPUs knew about it, set active bit again */
                    set_bit(&src->ipvp, IPVP_ACTIVITY);
                }
            }
852 853
        }
        break;
B
bellard 已提交
854
    case 0xB0: /* PEOI */
855 856
        retval = 0;
        break;
B
bellard 已提交
857 858 859 860 861 862 863 864
    default:
        break;
    }
    DPRINTF("%s: => %08x\n", __func__, retval);

    return retval;
}

865
static uint64_t openpic_cpu_read(void *opaque, hwaddr addr, unsigned len)
866 867 868 869
{
    return openpic_cpu_read_internal(opaque, addr, (addr & 0x1f000) >> 12);
}

870 871 872 873 874 875 876 877 878
static const MemoryRegionOps openpic_glb_ops = {
    .write = openpic_gbl_write,
    .read  = openpic_gbl_read,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
};
B
bellard 已提交
879

880 881 882 883 884 885 886 887 888
static const MemoryRegionOps openpic_tmr_ops = {
    .write = openpic_timer_write,
    .read  = openpic_timer_read,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
};
B
bellard 已提交
889

890 891 892 893 894 895 896 897 898
static const MemoryRegionOps openpic_cpu_ops = {
    .write = openpic_cpu_write,
    .read  = openpic_cpu_read,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
};
B
bellard 已提交
899

900 901 902
static const MemoryRegionOps openpic_src_ops = {
    .write = openpic_src_write,
    .read  = openpic_src_read,
A
Avi Kivity 已提交
903
    .endianness = DEVICE_LITTLE_ENDIAN,
904 905 906 907
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
A
Avi Kivity 已提交
908 909
};

A
Anthony Liguori 已提交
910
static void openpic_save_IRQ_queue(QEMUFile* f, IRQ_queue_t *q)
911 912 913 914 915 916 917 918 919 920 921 922
{
    unsigned int i;

    for (i = 0; i < BF_WIDTH(MAX_IRQ); i++)
        qemu_put_be32s(f, &q->queue[i]);

    qemu_put_sbe32s(f, &q->next);
    qemu_put_sbe32s(f, &q->priority);
}

static void openpic_save(QEMUFile* f, void *opaque)
{
A
Anthony Liguori 已提交
923
    openpic_t *opp = (openpic_t *)opaque;
924 925 926 927 928 929 930 931
    unsigned int i;

    qemu_put_be32s(f, &opp->glbc);
    qemu_put_be32s(f, &opp->veni);
    qemu_put_be32s(f, &opp->pint);
    qemu_put_be32s(f, &opp->spve);
    qemu_put_be32s(f, &opp->tifr);

932
    for (i = 0; i < opp->max_irq; i++) {
933 934 935 936 937 938 939
        qemu_put_be32s(f, &opp->src[i].ipvp);
        qemu_put_be32s(f, &opp->src[i].ide);
        qemu_put_sbe32s(f, &opp->src[i].type);
        qemu_put_sbe32s(f, &opp->src[i].last_cpu);
        qemu_put_sbe32s(f, &opp->src[i].pending);
    }

940 941 942
    qemu_put_sbe32s(f, &opp->nb_cpus);

    for (i = 0; i < opp->nb_cpus; i++) {
943 944 945 946 947 948 949 950 951 952 953 954 955 956
        qemu_put_be32s(f, &opp->dst[i].pctp);
        qemu_put_be32s(f, &opp->dst[i].pcsr);
        openpic_save_IRQ_queue(f, &opp->dst[i].raised);
        openpic_save_IRQ_queue(f, &opp->dst[i].servicing);
    }

    for (i = 0; i < MAX_TMR; i++) {
        qemu_put_be32s(f, &opp->timers[i].ticc);
        qemu_put_be32s(f, &opp->timers[i].tibc);
    }

    pci_device_save(&opp->pci_dev, f);
}

A
Anthony Liguori 已提交
957
static void openpic_load_IRQ_queue(QEMUFile* f, IRQ_queue_t *q)
958 959 960 961 962 963 964 965 966 967 968 969
{
    unsigned int i;

    for (i = 0; i < BF_WIDTH(MAX_IRQ); i++)
        qemu_get_be32s(f, &q->queue[i]);

    qemu_get_sbe32s(f, &q->next);
    qemu_get_sbe32s(f, &q->priority);
}

static int openpic_load(QEMUFile* f, void *opaque, int version_id)
{
A
Anthony Liguori 已提交
970
    openpic_t *opp = (openpic_t *)opaque;
971 972 973 974 975 976 977 978 979 980 981
    unsigned int i;

    if (version_id != 1)
        return -EINVAL;

    qemu_get_be32s(f, &opp->glbc);
    qemu_get_be32s(f, &opp->veni);
    qemu_get_be32s(f, &opp->pint);
    qemu_get_be32s(f, &opp->spve);
    qemu_get_be32s(f, &opp->tifr);

982
    for (i = 0; i < opp->max_irq; i++) {
983 984 985 986 987 988 989
        qemu_get_be32s(f, &opp->src[i].ipvp);
        qemu_get_be32s(f, &opp->src[i].ide);
        qemu_get_sbe32s(f, &opp->src[i].type);
        qemu_get_sbe32s(f, &opp->src[i].last_cpu);
        qemu_get_sbe32s(f, &opp->src[i].pending);
    }

990 991 992
    qemu_get_sbe32s(f, &opp->nb_cpus);

    for (i = 0; i < opp->nb_cpus; i++) {
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        qemu_get_be32s(f, &opp->dst[i].pctp);
        qemu_get_be32s(f, &opp->dst[i].pcsr);
        openpic_load_IRQ_queue(f, &opp->dst[i].raised);
        openpic_load_IRQ_queue(f, &opp->dst[i].servicing);
    }

    for (i = 0; i < MAX_TMR; i++) {
        qemu_get_be32s(f, &opp->timers[i].ticc);
        qemu_get_be32s(f, &opp->timers[i].tibc);
    }

    return pci_device_load(&opp->pci_dev, f);
}

A
Anthony Liguori 已提交
1007
static void openpic_irq_raise(openpic_t *opp, int n_CPU, IRQ_src_t *src)
1008
{
1009 1010 1011 1012 1013 1014 1015
    int n_ci = IDR_CI0 - n_CPU;

    if ((opp->flags & OPENPIC_FLAG_IDE_CRIT) && test_bit(&src->ide, n_ci)) {
        qemu_irq_raise(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_CINT]);
    } else {
        qemu_irq_raise(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_INT]);
    }
1016 1017
}

1018
qemu_irq *openpic_init (MemoryRegion **pmem, int nb_cpus,
1019
                        qemu_irq **irqs, qemu_irq irq_out)
B
bellard 已提交
1020
{
A
Anthony Liguori 已提交
1021
    openpic_t *opp;
B
bellard 已提交
1022
    int i, m;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    struct {
        const char             *name;
        MemoryRegionOps const  *ops;
        hwaddr      start_addr;
        ram_addr_t              size;
    } const list[] = {
        {"glb", &openpic_glb_ops, OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
        {"tmr", &openpic_tmr_ops, OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
        {"src", &openpic_src_ops, OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
        {"cpu", &openpic_cpu_ops, OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
    };
1034

B
bellard 已提交
1035 1036 1037
    /* XXX: for now, only one CPU is supported */
    if (nb_cpus != 1)
        return NULL;
1038
    opp = g_malloc0(sizeof(openpic_t));
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

    memory_region_init(&opp->mem, "openpic", 0x40000);

    for (i = 0; i < ARRAY_SIZE(list); i++) {

        memory_region_init_io(&opp->sub_io_mem[i], list[i].ops, opp,
                              list[i].name, list[i].size);

        memory_region_add_subregion(&opp->mem, list[i].start_addr,
                                    &opp->sub_io_mem[i]);
    }
1050

B
bellard 已提交
1051
    //    isu_base &= 0xFFFC0000;
B
bellard 已提交
1052
    opp->nb_cpus = nb_cpus;
1053 1054 1055 1056 1057
    opp->nb_irqs = OPENPIC_EXT_IRQ;
    opp->vid = VID;
    opp->veni = VENI_GENERIC;
    opp->spve_mask = 0xFF;
    opp->tifr_reset = 0x003F7A00;
1058 1059 1060
    opp->max_irq = OPENPIC_MAX_IRQ;
    opp->irq_ipi0 = OPENPIC_IRQ_IPI0;
    opp->irq_tim0 = OPENPIC_IRQ_TIM0;
B
bellard 已提交
1061
    /* Set IRQ types */
1062
    for (i = 0; i < OPENPIC_EXT_IRQ; i++) {
B
bellard 已提交
1063 1064
        opp->src[i].type = IRQ_EXTERNAL;
    }
1065
    for (; i < OPENPIC_IRQ_TIM0; i++) {
B
bellard 已提交
1066 1067
        opp->src[i].type = IRQ_SPECIAL;
    }
1068
    m = OPENPIC_IRQ_IPI0;
B
bellard 已提交
1069 1070 1071
    for (; i < m; i++) {
        opp->src[i].type = IRQ_TIMER;
    }
1072
    for (; i < OPENPIC_MAX_IRQ; i++) {
B
bellard 已提交
1073 1074
        opp->src[i].type = IRQ_INTERNAL;
    }
B
bellard 已提交
1075
    for (i = 0; i < nb_cpus; i++)
1076 1077
        opp->dst[i].irqs = irqs[i];
    opp->irq_out = irq_out;
1078

A
Alex Williamson 已提交
1079 1080
    register_savevm(&opp->pci_dev.qdev, "openpic", 0, 2,
                    openpic_save, openpic_load, opp);
1081
    qemu_register_reset(openpic_reset, opp);
1082

A
Avi Kivity 已提交
1083 1084
    if (pmem)
        *pmem = &opp->mem;
1085

1086 1087 1088
    return qemu_allocate_irqs(openpic_set_irq, opp, opp->max_irq);
}

1089
static const MemoryRegionOps mpic_glb_ops = {
1090 1091
    .write = openpic_gbl_write,
    .read  = openpic_gbl_read,
1092
    .endianness = DEVICE_BIG_ENDIAN,
1093 1094 1095 1096
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
1097 1098
};

1099
static const MemoryRegionOps mpic_tmr_ops = {
1100 1101
    .write = openpic_timer_write,
    .read  = openpic_timer_read,
1102
    .endianness = DEVICE_BIG_ENDIAN,
1103 1104 1105 1106
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
1107 1108
};

1109
static const MemoryRegionOps mpic_cpu_ops = {
1110 1111
    .write = openpic_cpu_write,
    .read  = openpic_cpu_read,
1112
    .endianness = DEVICE_BIG_ENDIAN,
1113 1114 1115 1116
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
1117 1118
};

A
Alexander Graf 已提交
1119
static const MemoryRegionOps mpic_irq_ops = {
1120 1121
    .write = openpic_src_write,
    .read  = openpic_src_read,
1122
    .endianness = DEVICE_BIG_ENDIAN,
A
Alexander Graf 已提交
1123 1124 1125
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
1126
    },
1127 1128
};

A
Avi Kivity 已提交
1129
qemu_irq *mpic_init (MemoryRegion *address_space, hwaddr base,
1130
                     int nb_cpus, qemu_irq **irqs, qemu_irq irq_out)
1131
{
1132 1133
    openpic_t    *mpp;
    int           i;
1134
    struct {
1135 1136
        const char             *name;
        MemoryRegionOps const  *ops;
A
Avi Kivity 已提交
1137
        hwaddr      start_addr;
1138
        ram_addr_t              size;
1139
    } const list[] = {
1140 1141
        {"glb", &mpic_glb_ops, MPIC_GLB_REG_START, MPIC_GLB_REG_SIZE},
        {"tmr", &mpic_tmr_ops, MPIC_TMR_REG_START, MPIC_TMR_REG_SIZE},
1142
        {"src", &mpic_irq_ops, MPIC_SRC_REG_START, MPIC_SRC_REG_SIZE},
1143
        {"cpu", &mpic_cpu_ops, MPIC_CPU_REG_START, MPIC_CPU_REG_SIZE},
1144 1145
    };

1146
    mpp = g_malloc0(sizeof(openpic_t));
1147

1148 1149 1150
    memory_region_init(&mpp->mem, "mpic", 0x40000);
    memory_region_add_subregion(address_space, base, &mpp->mem);

1151 1152
    for (i = 0; i < sizeof(list)/sizeof(list[0]); i++) {

1153 1154 1155 1156 1157
        memory_region_init_io(&mpp->sub_io_mem[i], list[i].ops, mpp,
                              list[i].name, list[i].size);

        memory_region_add_subregion(&mpp->mem, list[i].start_addr,
                                    &mpp->sub_io_mem[i]);
1158 1159 1160
    }

    mpp->nb_cpus = nb_cpus;
1161 1162 1163 1164 1165 1166 1167 1168 1169
    /* 12 external sources, 48 internal sources , 4 timer sources,
       4 IPI sources, 4 messaging sources, and 8 Shared MSI sources */
    mpp->nb_irqs = 80;
    mpp->vid = VID_REVISION_1_2;
    mpp->veni = VENI_GENERIC;
    mpp->spve_mask = 0xFFFF;
    mpp->tifr_reset = 0x00000000;
    mpp->ipvp_reset = 0x80000000;
    mpp->ide_reset = 0x00000001;
1170 1171 1172 1173 1174 1175 1176 1177
    mpp->max_irq = MPIC_MAX_IRQ;
    mpp->irq_ipi0 = MPIC_IPI_IRQ;
    mpp->irq_tim0 = MPIC_TMR_IRQ;

    for (i = 0; i < nb_cpus; i++)
        mpp->dst[i].irqs = irqs[i];
    mpp->irq_out = irq_out;

1178 1179
    /* Enable critical interrupt support */
    mpp->flags |= OPENPIC_FLAG_IDE_CRIT;
1180

A
Alex Williamson 已提交
1181
    register_savevm(NULL, "mpic", 0, 2, openpic_save, openpic_load, mpp);
1182
    qemu_register_reset(openpic_reset, mpp);
1183 1184

    return qemu_allocate_irqs(openpic_set_irq, mpp, mpp->max_irq);
B
bellard 已提交
1185
}