1 1.36 ragge /* $NetBSD: ka43.c,v 1.36 2017/05/22 16:46:15 ragge Exp $ */ 2 1.1 ragge /* 3 1.1 ragge * Copyright (c) 1996 Ludd, University of Lule}, Sweden. 4 1.1 ragge * All rights reserved. 5 1.1 ragge * 6 1.1 ragge * This code is derived from software contributed to Ludd by Bertram Barth. 7 1.1 ragge * 8 1.1 ragge * Redistribution and use in source and binary forms, with or without 9 1.1 ragge * modification, are permitted provided that the following conditions 10 1.1 ragge * are met: 11 1.1 ragge * 1. Redistributions of source code must retain the above copyright 12 1.1 ragge * notice, this list of conditions and the following disclaimer. 13 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 ragge * notice, this list of conditions and the following disclaimer in the 15 1.1 ragge * documentation and/or other materials provided with the distribution. 16 1.1 ragge * 17 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 1.1 ragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 ragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 ragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 1.1 ragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 1.1 ragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 1.1 ragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 1.1 ragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 1.1 ragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 1.1 ragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 1.1 ragge */ 28 1.26 lukem 29 1.26 lukem #include <sys/cdefs.h> 30 1.36 ragge __KERNEL_RCSID(0, "$NetBSD: ka43.c,v 1.36 2017/05/22 16:46:15 ragge Exp $"); 31 1.1 ragge 32 1.1 ragge #include <sys/param.h> 33 1.35 matt #include <sys/systm.h> 34 1.35 matt #include <sys/cpu.h> 35 1.1 ragge #include <sys/device.h> 36 1.1 ragge #include <sys/kernel.h> 37 1.1 ragge 38 1.1 ragge #include <machine/sid.h> 39 1.1 ragge #include <machine/nexus.h> 40 1.6 ragge #include <machine/vsbus.h> 41 1.1 ragge #include <machine/ka43.h> 42 1.1 ragge #include <machine/clock.h> 43 1.1 ragge 44 1.33 matt static void ka43_conf(void); 45 1.33 matt static void ka43_steal_pages(void); 46 1.9 ragge 47 1.33 matt static int ka43_mchk(void *); 48 1.33 matt static void ka43_memerr(void); 49 1.14 ragge #if 0 50 1.33 matt static void ka43_clear_errors(void); 51 1.14 ragge #endif 52 1.33 matt static int ka43_cache_init(void); /* "int mapen" as argument? */ 53 1.33 matt static int ka43_cache_reset(void); 54 1.33 matt static int ka43_cache_enable(void); 55 1.33 matt static int ka43_cache_disable(void); 56 1.33 matt static int ka43_cache_invalidate(void); 57 1.33 matt static void ka43_halt(void); 58 1.33 matt static void ka43_reboot(int); 59 1.33 matt static void ka43_clrf(void); 60 1.33 matt 61 1.33 matt static const char * const ka43_devs[] = { "cpu", "vsbus", NULL }; 62 1.33 matt 63 1.33 matt const struct cpu_dep ka43_calls = { 64 1.33 matt .cpu_steal_pages = ka43_steal_pages, 65 1.33 matt .cpu_mchk = ka43_mchk, 66 1.33 matt .cpu_memerr = ka43_memerr, 67 1.33 matt .cpu_conf = ka43_conf, 68 1.33 matt .cpu_gettime = chip_gettime, 69 1.33 matt .cpu_settime = chip_settime, 70 1.33 matt .cpu_vups = 7, /* 7.6 VUP */ 71 1.33 matt .cpu_scbsz = 2, /* SCB pages */ 72 1.33 matt .cpu_halt = ka43_halt, 73 1.33 matt .cpu_reboot = ka43_reboot, 74 1.33 matt .cpu_clrf = ka43_clrf, 75 1.33 matt .cpu_devs = ka43_devs, 76 1.33 matt .cpu_flags = CPU_RAISEIPL, 77 1.4 ragge }; 78 1.4 ragge 79 1.5 ragge /* 80 1.5 ragge * ka43_steal_pages() is called with MMU disabled, after that call MMU gets 81 1.5 ragge * enabled. Thus we initialize these four pointers with physical addresses, 82 1.5 ragge * but before leving ka43_steal_pages() we reset them to virtual addresses. 83 1.5 ragge */ 84 1.16 ragge static volatile struct ka43_cpu *ka43_cpu = (void*)KA43_CPU_BASE; 85 1.16 ragge static volatile u_int *ka43_creg = (void*)KA43_CH2_CREG; 86 1.16 ragge static volatile u_int *ka43_ctag = (void*)KA43_CT2_BASE; 87 1.1 ragge 88 1.5 ragge #define KA43_MC_RESTART 0x00008000 /* Restart possible*/ 89 1.5 ragge #define KA43_PSL_FPDONE 0x00010000 /* First Part Done */ 90 1.1 ragge 91 1.5 ragge struct ka43_mcframe { /* Format of RigelMAX machine check frame: */ 92 1.5 ragge int mc43_bcnt; /* byte count, always 24 (0x18) */ 93 1.5 ragge int mc43_code; /* machine check type code and restart bit */ 94 1.5 ragge int mc43_addr; /* most recent (faulting?) virtual address */ 95 1.5 ragge int mc43_viba; /* contents of VIBA register */ 96 1.5 ragge int mc43_sisr; /* ICCS bit 6 and SISR bits 15:0 */ 97 1.5 ragge int mc43_istate; /* internal state */ 98 1.5 ragge int mc43_sc; /* shift count register */ 99 1.5 ragge int mc43_pc; /* trapped PC */ 100 1.5 ragge int mc43_psl; /* trapped PSL */ 101 1.5 ragge }; 102 1.1 ragge 103 1.33 matt static const char * const ka43_mctype[] = { 104 1.5 ragge "no error (0)", /* Code 0: No error */ 105 1.5 ragge "FPA: protocol error", /* Code 1-5: FPA errors */ 106 1.5 ragge "FPA: illegal opcode", 107 1.5 ragge "FPA: operand parity error", 108 1.5 ragge "FPA: unknown status", 109 1.5 ragge "FPA: result parity error", 110 1.5 ragge "unused (6)", /* Code 6-7: Unused */ 111 1.1 ragge "unused (7)", 112 1.5 ragge "MMU error (TLB miss)", /* Code 8-9: MMU errors */ 113 1.1 ragge "MMU error (TLB hit)", 114 1.5 ragge "HW interrupt at unused IPL", /* Code 10: Interrupt error */ 115 1.5 ragge "MOVCx impossible state", /* Code 11-13: Microcode errors */ 116 1.1 ragge "undefined trap code (i-box)", 117 1.1 ragge "undefined control store address", 118 1.5 ragge "unused (14)", /* Code 14-15: Unused */ 119 1.1 ragge "unused (15)", 120 1.5 ragge "PC tag or data parity error", /* Code 16: Cache error */ 121 1.5 ragge "data bus parity error", /* Code 17: Read error */ 122 1.5 ragge "data bus error (NXM)", /* Code 18: Write error */ 123 1.5 ragge "undefined data bus state", /* Code 19: Bus error */ 124 1.1 ragge }; 125 1.5 ragge #define MC43_MAX 19 126 1.5 ragge 127 1.5 ragge static int ka43_error_count = 0; 128 1.1 ragge 129 1.1 ragge int 130 1.33 matt ka43_mchk(void *addr) 131 1.1 ragge { 132 1.33 matt struct ka43_mcframe *mcf = (void*)addr; 133 1.5 ragge 134 1.5 ragge mtpr(0x00, PR_MCESR); /* Acknowledge the machine check */ 135 1.5 ragge printf("machine check %d (0x%x)\n", mcf->mc43_code, mcf->mc43_code); 136 1.5 ragge printf("reason: %s\n", ka43_mctype[mcf->mc43_code & 0xff]); 137 1.5 ragge if (++ka43_error_count > 10) { 138 1.5 ragge printf("error_count exceeded: %d\n", ka43_error_count); 139 1.5 ragge return (-1); 140 1.5 ragge } 141 1.1 ragge 142 1.5 ragge /* 143 1.5 ragge * If either the Restart flag is set or the First-Part-Done flag 144 1.20 soren * is set, and the TRAP2 (double error) bit is not set, then the 145 1.5 ragge * error is recoverable. 146 1.5 ragge */ 147 1.5 ragge if (mfpr(PR_PCSTS) & KA43_PCS_TRAP2) { 148 1.5 ragge printf("TRAP2 (double error) in ka43_mchk.\n"); 149 1.25 provos panic("unrecoverable state in ka43_mchk."); 150 1.5 ragge return (-1); 151 1.5 ragge } 152 1.5 ragge if ((mcf->mc43_code & KA43_MC_RESTART) || 153 1.5 ragge (mcf->mc43_psl & KA43_PSL_FPDONE)) { 154 1.5 ragge printf("ka43_mchk: recovering from machine-check.\n"); 155 1.5 ragge ka43_cache_reset(); /* reset caches */ 156 1.5 ragge return (0); /* go on; */ 157 1.5 ragge } 158 1.5 ragge 159 1.5 ragge /* 160 1.5 ragge * Unknown error state, panic/halt the machine! 161 1.5 ragge */ 162 1.5 ragge printf("ka43_mchk: unknown error state!\n"); 163 1.1 ragge return (-1); 164 1.1 ragge } 165 1.1 ragge 166 1.5 ragge void 167 1.33 matt ka43_memerr(void) 168 1.5 ragge { 169 1.24 tv char sbuf[256]; 170 1.24 tv 171 1.5 ragge /* 172 1.5 ragge * Don\'t know what to do here. So just print some messages 173 1.5 ragge * and try to go on... 174 1.5 ragge */ 175 1.24 tv 176 1.5 ragge printf("memory error!\n"); 177 1.24 tv 178 1.34 christos snprintb(sbuf, sizeof(sbuf), KA43_PCSTS_BITS, mfpr(PR_PCSTS)); 179 1.24 tv printf("primary cache status: %s\n", sbuf); 180 1.24 tv 181 1.34 christos snprintb(sbuf, sizeof(sbuf), KA43_SESR_BITS, *ka43_creg); 182 1.24 tv printf("secondary cache status: %s\n", sbuf); 183 1.5 ragge } 184 1.5 ragge 185 1.1 ragge int 186 1.33 matt ka43_cache_init(void) 187 1.1 ragge { 188 1.5 ragge return (ka43_cache_reset()); 189 1.5 ragge } 190 1.1 ragge 191 1.14 ragge #if 0 192 1.6 ragge void 193 1.33 matt ka43_clear_errors(void) 194 1.5 ragge { 195 1.5 ragge int val = *ka43_creg; 196 1.5 ragge val |= KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR; 197 1.5 ragge *ka43_creg = val; 198 1.1 ragge } 199 1.14 ragge #endif 200 1.1 ragge 201 1.5 ragge int 202 1.33 matt ka43_cache_reset(void) 203 1.1 ragge { 204 1.24 tv char sbuf[256]; 205 1.24 tv 206 1.5 ragge /* 207 1.5 ragge * resetting primary and secondary caches is done in three steps: 208 1.5 ragge * 1. disable both caches 209 1.5 ragge * 2. manually clear secondary cache 210 1.5 ragge * 3. enable both caches 211 1.5 ragge */ 212 1.5 ragge ka43_cache_disable(); 213 1.5 ragge ka43_cache_invalidate(); 214 1.5 ragge ka43_cache_enable(); 215 1.5 ragge 216 1.34 christos snprintb(sbuf, sizeof(sbuf), KA43_PCSTS_BITS, mfpr(PR_PCSTS)); 217 1.24 tv printf("primary cache status: %s\n", sbuf); 218 1.24 tv 219 1.34 christos snprintb(sbuf, sizeof(sbuf), KA43_SESR_BITS, *ka43_creg); 220 1.24 tv printf("secondary cache status: %s\n", sbuf); 221 1.1 ragge 222 1.1 ragge return (0); 223 1.5 ragge } 224 1.5 ragge 225 1.5 ragge int 226 1.33 matt ka43_cache_disable(void) 227 1.5 ragge { 228 1.6 ragge int val; 229 1.1 ragge 230 1.1 ragge /* 231 1.5 ragge * first disable primary cache and clear error flags 232 1.1 ragge */ 233 1.5 ragge mtpr(KA43_PCS_REFRESH, PR_PCSTS); /* disable primary cache */ 234 1.5 ragge val = mfpr(PR_PCSTS); 235 1.5 ragge mtpr(val, PR_PCSTS); /* clear error flags */ 236 1.5 ragge 237 1.1 ragge /* 238 1.5 ragge * now disable secondary cache and clear error flags 239 1.1 ragge */ 240 1.5 ragge val = *ka43_creg & ~KA43_SESR_CENB; /* BICL !!! */ 241 1.5 ragge *ka43_creg = val; /* disable secondary cache */ 242 1.5 ragge val = KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR; 243 1.5 ragge *ka43_creg = val; /* clear error flags */ 244 1.5 ragge 245 1.5 ragge return (0); 246 1.5 ragge } 247 1.5 ragge 248 1.5 ragge int 249 1.33 matt ka43_cache_invalidate(void) 250 1.5 ragge { 251 1.5 ragge int i, val; 252 1.5 ragge 253 1.5 ragge val = KA43_PCTAG_PARITY; /* clear valid flag, set parity bit */ 254 1.5 ragge for (i = 0; i < 256; i++) { /* 256 Quadword entries */ 255 1.5 ragge mtpr(i*8, PR_PCIDX); /* write index of tag */ 256 1.5 ragge mtpr(val, PR_PCTAG); /* write value into tag */ 257 1.5 ragge } 258 1.5 ragge val = KA43_PCS_FLUSH | KA43_PCS_REFRESH; 259 1.5 ragge mtpr(val, PR_PCSTS); /* flush primary cache */ 260 1.5 ragge 261 1.5 ragge /* 262 1.5 ragge * Rigel\'s secondary cache doesn\'t implement a valid-flag. 263 1.5 ragge * Thus we initialize all entries with out-of-range/dummy 264 1.5 ragge * addresses which will never be referenced (ie. never hit). 265 1.5 ragge * After enabling cache we also access 128K of memory starting 266 1.5 ragge * at 0x00 so that secondary cache will be filled with these 267 1.5 ragge * valid addresses... 268 1.5 ragge */ 269 1.5 ragge val = 0xff; 270 1.5 ragge /* if (memory > 28 MB) val = 0x55; */ 271 1.5 ragge for (i = 0; i < KA43_CT2_SIZE; i+= 4) { /* Quadword entries ?? */ 272 1.5 ragge ka43_ctag[i/4] = val; /* reset upper and lower */ 273 1.1 ragge } 274 1.5 ragge 275 1.5 ragge return (0); 276 1.1 ragge } 277 1.1 ragge 278 1.5 ragge 279 1.5 ragge int 280 1.33 matt ka43_cache_enable(void) 281 1.1 ragge { 282 1.5 ragge volatile char *membase = (void*)0x80000000; /* physical 0x00 */ 283 1.5 ragge int i, val; 284 1.5 ragge 285 1.5 ragge val = KA43_PCS_FLUSH | KA43_PCS_REFRESH; 286 1.5 ragge mtpr(val, PR_PCSTS); /* flush primary cache */ 287 1.1 ragge 288 1.1 ragge /* 289 1.5 ragge * now we enable secondary cache and access first 128K of memory 290 1.5 ragge * so that secondary cache gets really initialized and holds 291 1.5 ragge * valid addresses/data... 292 1.1 ragge */ 293 1.5 ragge *ka43_creg = KA43_SESR_CENB; /* enable secondary cache */ 294 1.5 ragge for (i=0; i<128*1024; i++) { 295 1.5 ragge val += membase[i]; /* some dummy operation... */ 296 1.1 ragge } 297 1.1 ragge 298 1.5 ragge val = KA43_PCS_ENABLE | KA43_PCS_REFRESH; 299 1.5 ragge mtpr(val, PR_PCSTS); /* enable primary cache */ 300 1.5 ragge 301 1.5 ragge return (0); 302 1.1 ragge } 303 1.1 ragge 304 1.1 ragge void 305 1.33 matt ka43_conf(void) 306 1.1 ragge { 307 1.33 matt curcpu()->ci_cpustr = "Rigel, 2KB L1 cache, 128KB L2 cache"; 308 1.33 matt 309 1.14 ragge ka43_cpu = (void *)vax_map_physmem(VS_REGS, 1); 310 1.14 ragge ka43_creg = (void *)vax_map_physmem(KA43_CH2_CREG, 1); 311 1.14 ragge ka43_ctag = (void *)vax_map_physmem(KA43_CT2_BASE, 312 1.14 ragge (KA43_CT2_SIZE/VAX_NBPG)); 313 1.14 ragge 314 1.5 ragge /* 315 1.5 ragge * ka43_conf() gets called with MMU enabled, now it's save to 316 1.5 ragge * init/reset the caches. 317 1.5 ragge */ 318 1.5 ragge ka43_cache_init(); 319 1.14 ragge 320 1.14 ragge clk_adrshift = 1; /* Addressed at long's... */ 321 1.14 ragge clk_tweak = 2; /* ...and shift two */ 322 1.14 ragge clk_page = (short *)vax_map_physmem(VS_CLOCK, 1); 323 1.1 ragge } 324 1.1 ragge 325 1.1 ragge 326 1.1 ragge /* 327 1.5 ragge * The interface for communication with the LANCE ethernet controller 328 1.5 ragge * is setup in the xxx_steal_pages() routine. We decrease highest 329 1.5 ragge * available address by 64K and use this area as communication buffer. 330 1.1 ragge */ 331 1.1 ragge 332 1.1 ragge void 333 1.33 matt ka43_steal_pages(void) 334 1.1 ragge { 335 1.14 ragge int val; 336 1.1 ragge 337 1.1 ragge 338 1.5 ragge /* 339 1.5 ragge * if LANCE\'s io-buffer is above 16 MB, then the appropriate flag 340 1.5 ragge * in the parity control register has to be set (it works as an 341 1.5 ragge * additional address bit). In any case, don\'t enable CPEN and 342 1.5 ragge * DPEN in the PARCTL register, somewhow they are internally managed 343 1.5 ragge * by the RIGEL chip itself!?! 344 1.5 ragge */ 345 1.5 ragge val = ka43_cpu->parctl & 0x03; /* read the old value */ 346 1.5 ragge ka43_cpu->parctl = val; /* and write new value */ 347 1.14 ragge } 348 1.14 ragge 349 1.33 matt void 350 1.33 matt ka43_clrf(void) 351 1.14 ragge { 352 1.28 ragge volatile struct ka43_clock *clk = (volatile void *)clk_page; 353 1.1 ragge 354 1.14 ragge /* 355 1.14 ragge * Clear restart and boot in progress flags in the CPMBX. 356 1.27 ragge * The cpmbx is split into two 4-bit fields. 357 1.27 ragge * One for the current restart/boot in progress flags, and 358 1.27 ragge * one for the permanent halt flag. 359 1.27 ragge * The restart/boot in progress flag is also used as the action request 360 1.27 ragge * for the CPU at a halt. /BQT 361 1.14 ragge */ 362 1.27 ragge clk->req = 0; 363 1.14 ragge } 364 1.14 ragge 365 1.33 matt void 366 1.33 matt ka43_halt(void) 367 1.14 ragge { 368 1.28 ragge volatile struct ka43_clock *clk = (volatile void *)clk_page; 369 1.27 ragge clk->req = 3; /* 3 is halt. */ 370 1.30 perry __asm("halt"); 371 1.14 ragge } 372 1.14 ragge 373 1.33 matt void 374 1.33 matt ka43_reboot(int arg) 375 1.14 ragge { 376 1.28 ragge volatile struct ka43_clock *clk = (volatile void *)clk_page; 377 1.27 ragge clk->req = 2; /* 2 is reboot. */ 378 1.30 perry __asm("halt"); 379 1.1 ragge } 380