eccmemctl.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * QEMU Sparc Sun4m ECC memory controller emulation
 *
 * Copyright (c) 2007 Robert Reif
 *
 * 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.
 */
#include "hw.h"
#include "sun4m.h"
#include "sysemu.h"

//#define DEBUG_ECC

#ifdef DEBUG_ECC
#define DPRINTF(fmt, args...)                           \
    do { printf("ECC: " fmt , ##args); } while (0)
#else
#define DPRINTF(fmt, args...)
#endif

/* There are 3 versions of this chip used in SMP sun4m systems:
 * MCC (version 0, implementation 0) SS-600MP
 * EMC (version 0, implementation 1) SS-10
 * SMC (version 0, implementation 2) SS-10SX and SS-20
 */

43 44 45 46 47 48 49 50 51 52
/* Register indexes */
#define ECC_MER        0               /* Memory Enable Register */
#define ECC_MDR        1               /* Memory Delay Register */
#define ECC_MFSR       2               /* Memory Fault Status Register */
#define ECC_VCR        3               /* Video Configuration Register */
#define ECC_MFAR0      4               /* Memory Fault Address Register 0 */
#define ECC_MFAR1      5               /* Memory Fault Address Register 1 */
#define ECC_DR         6               /* Diagnostic Register */
#define ECC_ECR0       7               /* Event Count Register 0 */
#define ECC_ECR1       8               /* Event Count Register 1 */
53 54

/* ECC fault control register */
55
#define ECC_MER_EE     0x00000001      /* Enable ECC checking */
B
blueswir1 已提交
56 57
#define ECC_MER_EI     0x00000002      /* Enable Interrupts on
                                          correctable errors */
58 59 60 61 62 63 64 65 66 67 68
#define ECC_MER_MRR0   0x00000004      /* SIMM 0 */
#define ECC_MER_MRR1   0x00000008      /* SIMM 1 */
#define ECC_MER_MRR2   0x00000010      /* SIMM 2 */
#define ECC_MER_MRR3   0x00000020      /* SIMM 3 */
#define ECC_MER_MRR4   0x00000040      /* SIMM 4 */
#define ECC_MER_MRR5   0x00000080      /* SIMM 5 */
#define ECC_MER_MRR6   0x00000100      /* SIMM 6 */
#define ECC_MER_MRR7   0x00000200      /* SIMM 7 */
#define ECC_MER_REU    0x00000200      /* Memory Refresh Enable (600MP) */
#define ECC_MER_MRR    0x000003fc      /* MRR mask */
#define ECC_MEM_A      0x00000400      /* Memory controller addr map select */
B
blueswir1 已提交
69
#define ECC_MER_DCI    0x00000800      /* Disables Coherent Invalidate ACK */
70 71 72 73 74 75 76 77 78 79 80 81
#define ECC_MER_VER    0x0f000000      /* Version */
#define ECC_MER_IMPL   0xf0000000      /* Implementation */

/* ECC memory delay register */
#define ECC_MDR_RRI    0x000003ff      /* Refresh Request Interval */
#define ECC_MDR_MI     0x00001c00      /* MIH Delay */
#define ECC_MDR_CI     0x0000e000      /* Coherent Invalidate Delay */
#define ECC_MDR_MDL    0x001f0000      /* MBus Master arbitration delay */
#define ECC_MDR_MDH    0x03e00000      /* MBus Master arbitration delay */
#define ECC_MDR_GAD    0x7c000000      /* Graphics Arbitration Delay */
#define ECC_MDR_RSC    0x80000000      /* Refresh load control */
#define ECC_MDR_MASK   0x7fffffff
82 83

/* ECC fault status register */
84 85 86 87 88 89 90 91
#define ECC_MFSR_CE    0x00000001      /* Correctable error */
#define ECC_MFSR_BS    0x00000002      /* C2 graphics bad slot access */
#define ECC_MFSR_TO    0x00000004      /* Timeout on write */
#define ECC_MFSR_UE    0x00000008      /* Uncorrectable error */
#define ECC_MFSR_DW    0x000000f0      /* Index of double word in block */
#define ECC_MFSR_SYND  0x0000ff00      /* Syndrome for correctable error */
#define ECC_MFSR_ME    0x00010000      /* Multiple errors */
#define ECC_MFSR_C2ERR 0x00020000      /* C2 graphics error */
92 93

/* ECC fault address register 0 */
94 95 96 97 98 99 100 101 102
#define ECC_MFAR0_PADDR 0x0000000f     /* PA[32-35] */
#define ECC_MFAR0_TYPE  0x000000f0     /* Transaction type */
#define ECC_MFAR0_SIZE  0x00000700     /* Transaction size */
#define ECC_MFAR0_CACHE 0x00000800     /* Mapped cacheable */
#define ECC_MFAR0_LOCK  0x00001000     /* Error occurred in atomic cycle */
#define ECC_MFAR0_BMODE 0x00002000     /* Boot mode */
#define ECC_MFAR0_VADDR 0x003fc000     /* VA[12-19] (superset bits) */
#define ECC_MFAR0_S     0x08000000     /* Supervisor mode */
#define ECC_MFARO_MID   0xf0000000     /* Module ID */
103 104

/* ECC diagnostic register */
105 106 107 108 109 110 111 112 113 114 115
#define ECC_DR_CBX     0x00000001
#define ECC_DR_CB0     0x00000002
#define ECC_DR_CB1     0x00000004
#define ECC_DR_CB2     0x00000008
#define ECC_DR_CB4     0x00000010
#define ECC_DR_CB8     0x00000020
#define ECC_DR_CB16    0x00000040
#define ECC_DR_CB32    0x00000080
#define ECC_DR_DMODE   0x00000c00

#define ECC_NREGS      9
116
#define ECC_SIZE       (ECC_NREGS * sizeof(uint32_t))
117 118 119

#define ECC_DIAG_SIZE  4
#define ECC_DIAG_MASK  (ECC_DIAG_SIZE - 1)
120 121

typedef struct ECCState {
122
    qemu_irq irq;
123
    uint32_t regs[ECC_NREGS];
124
    uint8_t diag[ECC_DIAG_SIZE];
125 126 127 128 129 130
} ECCState;

static void ecc_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
{
    ECCState *s = opaque;

B
blueswir1 已提交
131
    switch (addr >> 2) {
132
    case ECC_MER:
133 134
        s->regs[ECC_MER] = (s->regs[ECC_MER] & (ECC_MER_VER | ECC_MER_IMPL)) |
            (val & ~(ECC_MER_VER | ECC_MER_IMPL));
135
        DPRINTF("Write memory enable %08x\n", val);
136
        break;
137
    case ECC_MDR:
138
        s->regs[ECC_MDR] =  val & ECC_MDR_MASK;
139
        DPRINTF("Write memory delay %08x\n", val);
140
        break;
141
    case ECC_MFSR:
142
        s->regs[ECC_MFSR] =  val;
143
        DPRINTF("Write memory fault status %08x\n", val);
144
        break;
145
    case ECC_VCR:
146
        s->regs[ECC_VCR] =  val;
147
        DPRINTF("Write slot configuration %08x\n", val);
148
        break;
149
    case ECC_DR:
150
        s->regs[ECC_DR] =  val;
151 152 153
        DPRINTF("Write diagnosiic %08x\n", val);
        break;
    case ECC_ECR0:
154
        s->regs[ECC_ECR0] =  val;
155
        DPRINTF("Write event count 1 %08x\n", val);
156
        break;
157
    case ECC_ECR1:
158
        s->regs[ECC_ECR0] =  val;
159
        DPRINTF("Write event count 2 %08x\n", val);
160 161 162 163 164 165 166 167 168
        break;
    }
}

static uint32_t ecc_mem_readl(void *opaque, target_phys_addr_t addr)
{
    ECCState *s = opaque;
    uint32_t ret = 0;

B
blueswir1 已提交
169
    switch (addr >> 2) {
170
    case ECC_MER:
171
        ret = s->regs[ECC_MER];
172
        DPRINTF("Read memory enable %08x\n", ret);
173
        break;
174
    case ECC_MDR:
175
        ret = s->regs[ECC_MDR];
176
        DPRINTF("Read memory delay %08x\n", ret);
177
        break;
178
    case ECC_MFSR:
179
        ret = s->regs[ECC_MFSR];
180
        DPRINTF("Read memory fault status %08x\n", ret);
181
        break;
182
    case ECC_VCR:
183
        ret = s->regs[ECC_VCR];
184
        DPRINTF("Read slot configuration %08x\n", ret);
185
        break;
186
    case ECC_MFAR0:
187
        ret = s->regs[ECC_MFAR0];
188
        DPRINTF("Read memory fault address 0 %08x\n", ret);
189
        break;
190
    case ECC_MFAR1:
191
        ret = s->regs[ECC_MFAR1];
192
        DPRINTF("Read memory fault address 1 %08x\n", ret);
193
        break;
194
    case ECC_DR:
195
        ret = s->regs[ECC_DR];
196
        DPRINTF("Read diagnostic %08x\n", ret);
197
        break;
198
    case ECC_ECR0:
199
        ret = s->regs[ECC_ECR0];
200 201 202
        DPRINTF("Read event count 1 %08x\n", ret);
        break;
    case ECC_ECR1:
203
        ret = s->regs[ECC_ECR0];
204
        DPRINTF("Read event count 2 %08x\n", ret);
205 206 207 208 209 210
        break;
    }
    return ret;
}

static CPUReadMemoryFunc *ecc_mem_read[3] = {
211 212
    NULL,
    NULL,
213 214 215 216
    ecc_mem_readl,
};

static CPUWriteMemoryFunc *ecc_mem_write[3] = {
217 218
    NULL,
    NULL,
219 220 221
    ecc_mem_writel,
};

222 223 224 225 226
static void ecc_diag_mem_writeb(void *opaque, target_phys_addr_t addr,
                                uint32_t val)
{
    ECCState *s = opaque;

B
blueswir1 已提交
227
    DPRINTF("Write diagnostic[%d] = %02x\n", (int)addr, val);
228 229 230 231 232 233
    s->diag[addr & ECC_DIAG_MASK] = val;
}

static uint32_t ecc_diag_mem_readb(void *opaque, target_phys_addr_t addr)
{
    ECCState *s = opaque;
B
blueswir1 已提交
234 235 236
    uint32_t ret = s->diag[(int)addr];

    DPRINTF("Read diagnostic[%d] = %02x\n", (int)addr, ret);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    return ret;
}

static CPUReadMemoryFunc *ecc_diag_mem_read[3] = {
    ecc_diag_mem_readb,
    NULL,
    NULL,
};

static CPUWriteMemoryFunc *ecc_diag_mem_write[3] = {
    ecc_diag_mem_writeb,
    NULL,
    NULL,
};

252 253 254 255 256
static int ecc_load(QEMUFile *f, void *opaque, int version_id)
{
    ECCState *s = opaque;
    int i;

257
    if (version_id != 2)
258 259 260 261 262
        return -EINVAL;

    for (i = 0; i < ECC_NREGS; i++)
        qemu_get_be32s(f, &s->regs[i]);

263 264 265
    for (i = 0; i < ECC_DIAG_SIZE; i++)
        qemu_get_8s(f, &s->diag[i]);

266 267 268 269 270 271 272 273 274 275
    return 0;
}

static void ecc_save(QEMUFile *f, void *opaque)
{
    ECCState *s = opaque;
    int i;

    for (i = 0; i < ECC_NREGS; i++)
        qemu_put_be32s(f, &s->regs[i]);
276 277 278

    for (i = 0; i < ECC_DIAG_SIZE; i++)
        qemu_put_8s(f, &s->diag[i]);
279 280 281 282 283 284
}

static void ecc_reset(void *opaque)
{
    ECCState *s = opaque;

285 286 287 288 289 290 291 292 293 294
    s->regs[ECC_MER] &= (ECC_MER_VER | ECC_MER_IMPL);
    s->regs[ECC_MER] |= ECC_MER_MRR;
    s->regs[ECC_MDR] = 0x20;
    s->regs[ECC_MFSR] = 0;
    s->regs[ECC_VCR] = 0;
    s->regs[ECC_MFAR0] = 0x07c00000;
    s->regs[ECC_MFAR1] = 0;
    s->regs[ECC_DR] = 0;
    s->regs[ECC_ECR0] = 0;
    s->regs[ECC_ECR1] = 0;
295 296
}

297
void * ecc_init(target_phys_addr_t base, qemu_irq irq, uint32_t version)
298 299 300 301 302 303 304 305 306
{
    int ecc_io_memory;
    ECCState *s;

    s = qemu_mallocz(sizeof(ECCState));
    if (!s)
        return NULL;

    s->regs[0] = version;
307
    s->irq = irq;
308 309 310

    ecc_io_memory = cpu_register_io_memory(0, ecc_mem_read, ecc_mem_write, s);
    cpu_register_physical_memory(base, ECC_SIZE, ecc_io_memory);
311 312 313 314 315 316 317
    if (version == 0) { // SS-600MP only
        ecc_io_memory = cpu_register_io_memory(0, ecc_diag_mem_read,
                                               ecc_diag_mem_write, s);
        cpu_register_physical_memory(base + 0x1000, ECC_DIAG_SIZE,
                                     ecc_io_memory);
    }
    register_savevm("ECC", base, 2, ecc_save, ecc_load, s);
318 319 320 321
    qemu_register_reset(ecc_reset, s);
    ecc_reset(s);
    return s;
}