Fix error reporting under Win32 (CreateFile does not set errno)
[qemu-kvm/fedora.git] / hw / eccmemctl.c
blobfe7f27e06c2eb738aeae3cd6abebd471d88be602
1 /*
2 * QEMU Sparc Sun4m ECC memory controller emulation
4 * Copyright (c) 2007 Robert Reif
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "sun4m.h"
26 #include "sysemu.h"
28 //#define DEBUG_ECC
30 #ifdef DEBUG_ECC
31 #define DPRINTF(fmt, args...) \
32 do { printf("ECC: " fmt , ##args); } while (0)
33 #else
34 #define DPRINTF(fmt, args...)
35 #endif
37 /* There are 3 versions of this chip used in SMP sun4m systems:
38 * MCC (version 0, implementation 0) SS-600MP
39 * EMC (version 0, implementation 1) SS-10
40 * SMC (version 0, implementation 2) SS-10SX and SS-20
43 /* Register offsets */
44 #define ECC_MER 0 /* Memory Enable Register */
45 #define ECC_MDR 4 /* Memory Delay Register */
46 #define ECC_MFSR 8 /* Memory Fault Status Register */
47 #define ECC_VCR 12 /* Video Configuration Register */
48 #define ECC_MFAR0 16 /* Memory Fault Address Register 0 */
49 #define ECC_MFAR1 20 /* Memory Fault Address Register 1 */
50 #define ECC_DR 24 /* Diagnostic Register */
51 #define ECC_ECR0 28 /* Event Count Register 0 */
52 #define ECC_ECR1 32 /* Event Count Register 1 */
54 /* ECC fault control register */
55 #define ECC_MER_EE 0x00000001 /* Enable ECC checking */
56 #define ECC_MER_EI 0x00000002 /* Enable Interrupts on
57 correctable errors */
58 #define ECC_MER_MRR0 0x00000004 /* SIMM 0 */
59 #define ECC_MER_MRR1 0x00000008 /* SIMM 1 */
60 #define ECC_MER_MRR2 0x00000010 /* SIMM 2 */
61 #define ECC_MER_MRR3 0x00000020 /* SIMM 3 */
62 #define ECC_MER_MRR4 0x00000040 /* SIMM 4 */
63 #define ECC_MER_MRR5 0x00000080 /* SIMM 5 */
64 #define ECC_MER_MRR6 0x00000100 /* SIMM 6 */
65 #define ECC_MER_MRR7 0x00000200 /* SIMM 7 */
66 #define ECC_MER_REU 0x00000200 /* Memory Refresh Enable (600MP) */
67 #define ECC_MER_MRR 0x000003fc /* MRR mask */
68 #define ECC_MEM_A 0x00000400 /* Memory controller addr map select */
69 #define ECC_MER_DCI 0x00000800 /* Disables Coherent Invalidate ACK */
70 #define ECC_MER_VER 0x0f000000 /* Version */
71 #define ECC_MER_IMPL 0xf0000000 /* Implementation */
73 /* ECC memory delay register */
74 #define ECC_MDR_RRI 0x000003ff /* Refresh Request Interval */
75 #define ECC_MDR_MI 0x00001c00 /* MIH Delay */
76 #define ECC_MDR_CI 0x0000e000 /* Coherent Invalidate Delay */
77 #define ECC_MDR_MDL 0x001f0000 /* MBus Master arbitration delay */
78 #define ECC_MDR_MDH 0x03e00000 /* MBus Master arbitration delay */
79 #define ECC_MDR_GAD 0x7c000000 /* Graphics Arbitration Delay */
80 #define ECC_MDR_RSC 0x80000000 /* Refresh load control */
81 #define ECC_MDR_MASK 0x7fffffff
83 /* ECC fault status register */
84 #define ECC_MFSR_CE 0x00000001 /* Correctable error */
85 #define ECC_MFSR_BS 0x00000002 /* C2 graphics bad slot access */
86 #define ECC_MFSR_TO 0x00000004 /* Timeout on write */
87 #define ECC_MFSR_UE 0x00000008 /* Uncorrectable error */
88 #define ECC_MFSR_DW 0x000000f0 /* Index of double word in block */
89 #define ECC_MFSR_SYND 0x0000ff00 /* Syndrome for correctable error */
90 #define ECC_MFSR_ME 0x00010000 /* Multiple errors */
91 #define ECC_MFSR_C2ERR 0x00020000 /* C2 graphics error */
93 /* ECC fault address register 0 */
94 #define ECC_MFAR0_PADDR 0x0000000f /* PA[32-35] */
95 #define ECC_MFAR0_TYPE 0x000000f0 /* Transaction type */
96 #define ECC_MFAR0_SIZE 0x00000700 /* Transaction size */
97 #define ECC_MFAR0_CACHE 0x00000800 /* Mapped cacheable */
98 #define ECC_MFAR0_LOCK 0x00001000 /* Error occurred in atomic cycle */
99 #define ECC_MFAR0_BMODE 0x00002000 /* Boot mode */
100 #define ECC_MFAR0_VADDR 0x003fc000 /* VA[12-19] (superset bits) */
101 #define ECC_MFAR0_S 0x08000000 /* Supervisor mode */
102 #define ECC_MFARO_MID 0xf0000000 /* Module ID */
104 /* ECC diagnostic register */
105 #define ECC_DR_CBX 0x00000001
106 #define ECC_DR_CB0 0x00000002
107 #define ECC_DR_CB1 0x00000004
108 #define ECC_DR_CB2 0x00000008
109 #define ECC_DR_CB4 0x00000010
110 #define ECC_DR_CB8 0x00000020
111 #define ECC_DR_CB16 0x00000040
112 #define ECC_DR_CB32 0x00000080
113 #define ECC_DR_DMODE 0x00000c00
115 #define ECC_NREGS 9
116 #define ECC_SIZE (ECC_NREGS * sizeof(uint32_t))
117 #define ECC_ADDR_MASK 0x1f
119 #define ECC_DIAG_SIZE 4
120 #define ECC_DIAG_MASK (ECC_DIAG_SIZE - 1)
122 typedef struct ECCState {
123 qemu_irq irq;
124 uint32_t regs[ECC_NREGS];
125 uint8_t diag[ECC_DIAG_SIZE];
126 } ECCState;
128 static void ecc_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
130 ECCState *s = opaque;
132 switch (addr & ECC_ADDR_MASK) {
133 case ECC_MER:
134 s->regs[0] = (s->regs[0] & (ECC_MER_VER | ECC_MER_IMPL)) |
135 (val & ~(ECC_MER_VER | ECC_MER_IMPL));
136 DPRINTF("Write memory enable %08x\n", val);
137 break;
138 case ECC_MDR:
139 s->regs[1] = val & ECC_MDR_MASK;
140 DPRINTF("Write memory delay %08x\n", val);
141 break;
142 case ECC_MFSR:
143 s->regs[2] = val;
144 DPRINTF("Write memory fault status %08x\n", val);
145 break;
146 case ECC_VCR:
147 s->regs[3] = val;
148 DPRINTF("Write slot configuration %08x\n", val);
149 break;
150 case ECC_DR:
151 s->regs[6] = val;
152 DPRINTF("Write diagnosiic %08x\n", val);
153 break;
154 case ECC_ECR0:
155 s->regs[7] = val;
156 DPRINTF("Write event count 1 %08x\n", val);
157 break;
158 case ECC_ECR1:
159 s->regs[7] = val;
160 DPRINTF("Write event count 2 %08x\n", val);
161 break;
165 static uint32_t ecc_mem_readl(void *opaque, target_phys_addr_t addr)
167 ECCState *s = opaque;
168 uint32_t ret = 0;
170 switch (addr & ECC_ADDR_MASK) {
171 case ECC_MER:
172 ret = s->regs[0];
173 DPRINTF("Read memory enable %08x\n", ret);
174 break;
175 case ECC_MDR:
176 ret = s->regs[1];
177 DPRINTF("Read memory delay %08x\n", ret);
178 break;
179 case ECC_MFSR:
180 ret = s->regs[2];
181 DPRINTF("Read memory fault status %08x\n", ret);
182 break;
183 case ECC_VCR:
184 ret = s->regs[3];
185 DPRINTF("Read slot configuration %08x\n", ret);
186 break;
187 case ECC_MFAR0:
188 ret = s->regs[4];
189 DPRINTF("Read memory fault address 0 %08x\n", ret);
190 break;
191 case ECC_MFAR1:
192 ret = s->regs[5];
193 DPRINTF("Read memory fault address 1 %08x\n", ret);
194 break;
195 case ECC_DR:
196 ret = s->regs[6];
197 DPRINTF("Read diagnostic %08x\n", ret);
198 break;
199 case ECC_ECR0:
200 ret = s->regs[7];
201 DPRINTF("Read event count 1 %08x\n", ret);
202 break;
203 case ECC_ECR1:
204 ret = s->regs[7];
205 DPRINTF("Read event count 2 %08x\n", ret);
206 break;
208 return ret;
211 static CPUReadMemoryFunc *ecc_mem_read[3] = {
212 NULL,
213 NULL,
214 ecc_mem_readl,
217 static CPUWriteMemoryFunc *ecc_mem_write[3] = {
218 NULL,
219 NULL,
220 ecc_mem_writel,
223 static void ecc_diag_mem_writeb(void *opaque, target_phys_addr_t addr,
224 uint32_t val)
226 ECCState *s = opaque;
228 DPRINTF("Write diagnostic[%d] = %02x\n", (int)(addr & ECC_DIAG_MASK), val);
229 s->diag[addr & ECC_DIAG_MASK] = val;
232 static uint32_t ecc_diag_mem_readb(void *opaque, target_phys_addr_t addr)
234 ECCState *s = opaque;
235 uint32_t ret = s->diag[addr & ECC_DIAG_MASK];
236 DPRINTF("Read diagnostic[%d] = %02x\n", (int)(addr & ECC_DIAG_MASK), ret);
237 return ret;
240 static CPUReadMemoryFunc *ecc_diag_mem_read[3] = {
241 ecc_diag_mem_readb,
242 NULL,
243 NULL,
246 static CPUWriteMemoryFunc *ecc_diag_mem_write[3] = {
247 ecc_diag_mem_writeb,
248 NULL,
249 NULL,
252 static int ecc_load(QEMUFile *f, void *opaque, int version_id)
254 ECCState *s = opaque;
255 int i;
257 if (version_id != 2)
258 return -EINVAL;
260 for (i = 0; i < ECC_NREGS; i++)
261 qemu_get_be32s(f, &s->regs[i]);
263 for (i = 0; i < ECC_DIAG_SIZE; i++)
264 qemu_get_8s(f, &s->diag[i]);
266 return 0;
269 static void ecc_save(QEMUFile *f, void *opaque)
271 ECCState *s = opaque;
272 int i;
274 for (i = 0; i < ECC_NREGS; i++)
275 qemu_put_be32s(f, &s->regs[i]);
277 for (i = 0; i < ECC_DIAG_SIZE; i++)
278 qemu_put_8s(f, &s->diag[i]);
281 static void ecc_reset(void *opaque)
283 ECCState *s = opaque;
284 int i;
286 s->regs[ECC_MER] &= (ECC_MER_VER | ECC_MER_IMPL);
287 s->regs[ECC_MER] |= ECC_MER_MRR;
288 s->regs[ECC_MDR] = 0x20;
289 s->regs[ECC_MFSR] = 0;
290 s->regs[ECC_VCR] = 0;
291 s->regs[ECC_MFAR0] = 0x07c00000;
292 s->regs[ECC_MFAR1] = 0;
293 s->regs[ECC_DR] = 0;
294 s->regs[ECC_ECR0] = 0;
295 s->regs[ECC_ECR1] = 0;
297 for (i = 1; i < ECC_NREGS; i++)
298 s->regs[i] = 0;
301 void * ecc_init(target_phys_addr_t base, qemu_irq irq, uint32_t version)
303 int ecc_io_memory;
304 ECCState *s;
306 s = qemu_mallocz(sizeof(ECCState));
307 if (!s)
308 return NULL;
310 s->regs[0] = version;
311 s->irq = irq;
313 ecc_io_memory = cpu_register_io_memory(0, ecc_mem_read, ecc_mem_write, s);
314 cpu_register_physical_memory(base, ECC_SIZE, ecc_io_memory);
315 if (version == 0) { // SS-600MP only
316 ecc_io_memory = cpu_register_io_memory(0, ecc_diag_mem_read,
317 ecc_diag_mem_write, s);
318 cpu_register_physical_memory(base + 0x1000, ECC_DIAG_SIZE,
319 ecc_io_memory);
321 register_savevm("ECC", base, 2, ecc_save, ecc_load, s);
322 qemu_register_reset(ecc_reset, s);
323 ecc_reset(s);
324 return s;