xref: /netbsd/sys/arch/alpha/alpha/dec_kn300.c (revision 5f87ffb2)
1 /* $NetBSD: dec_kn300.c,v 1.43 2021/06/19 16:36:35 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1998 by Matthew Jacob
5  * NASA AMES Research Center.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "opt_kgdb.h"
34 
35 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
36 
37 __KERNEL_RCSID(0, "$NetBSD: dec_kn300.c,v 1.43 2021/06/19 16:36:35 thorpej Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/termios.h>
43 #include <sys/conf.h>
44 #include <dev/cons.h>
45 
46 #include <machine/rpb.h>
47 #include <machine/alpha.h>
48 #include <machine/autoconf.h>
49 #include <machine/frame.h>
50 #include <machine/cpuconf.h>
51 
52 #include <dev/ic/comreg.h>
53 #include <dev/ic/comvar.h>
54 
55 #include <dev/isa/isareg.h>
56 #include <dev/isa/isavar.h>
57 #include <dev/ic/i8042reg.h>
58 #include <dev/ic/pckbcvar.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 
62 #include <uvm/uvm_extern.h>
63 
64 #include <alpha/mcbus/mcbusreg.h>
65 #include <alpha/mcbus/mcbusvar.h>
66 #include <alpha/pci/mcpciareg.h>
67 #include <alpha/pci/mcpciavar.h>
68 #include <machine/logout.h>
69 
70 #include <dev/scsipi/scsi_all.h>
71 #include <dev/scsipi/scsipi_all.h>
72 #include <dev/scsipi/scsiconf.h>
73 
74 #include <dev/ic/mlxio.h>
75 #include <dev/ic/mlxvar.h>
76 
77 
78 #include "pckbd.h"
79 
80 #ifndef	CONSPEED
81 #define	CONSPEED	TTYDEF_SPEED
82 #endif
83 static int comcnrate = CONSPEED;
84 
85 void dec_kn300_init(void);
86 void dec_kn300_cons_init(void);
87 static void dec_kn300_device_register(device_t, void *);
88 static void dec_kn300_mcheck_handler
89 (unsigned long, struct trapframe *, unsigned long, unsigned long);
90 
91 #ifdef KGDB
92 #include <machine/db_machdep.h>
93 
94 static const char *kgdb_devlist[] = {
95 	"com",
96 	NULL,
97 };
98 #endif /* KGDB */
99 
100 #define	ALPHASERVER_4100	"AlphaServer 4100"
101 
102 const struct alpha_variation_table dec_kn300_variations[] = {
103 	{ 0, ALPHASERVER_4100 },
104 	{ 0, NULL },
105 };
106 
107 void
dec_kn300_init(void)108 dec_kn300_init(void)
109 {
110 	uint64_t variation;
111 	int cachesize;
112 
113 	platform.family = ALPHASERVER_4100;
114 
115 	if ((platform.model = alpha_dsr_sysname()) == NULL) {
116 		variation = hwrpb->rpb_variation & SV_ST_MASK;
117 		if ((platform.model = alpha_variation_name(variation,
118 		    dec_kn300_variations)) == NULL)
119 			platform.model = alpha_unknown_sysname();
120 	}
121 
122 	platform.iobus = "mcbus";
123 	platform.cons_init = dec_kn300_cons_init;
124 	platform.device_register = dec_kn300_device_register;
125 	platform.mcheck_handler = dec_kn300_mcheck_handler;
126 
127 	/*
128 	 * Determine B-cache size by looking at the primary (console)
129 	 * MCPCIA's WHOAMI register.
130 	 */
131 	mcpcia_init();
132 
133 	if (mcbus_primary.mcbus_valid) {
134 		switch (mcbus_primary.mcbus_bcache) {
135 		default:
136 		case CPU_BCache_0MB:
137 			/* No B-cache or invalid; default to 1MB. */
138 			/* FALLTHROUGH */
139 
140 		case CPU_BCache_1MB:
141 			cachesize = (1 * 1024 * 1024);
142 			break;
143 
144 		case CPU_BCache_2MB:
145 			cachesize = (2 * 1024 * 1024);
146 			break;
147 
148 		case CPU_BCache_4MB:
149 			cachesize = (4 * 1024 * 1024);
150 			break;
151 		}
152 	} else {
153 		/* Default to 1MB. */
154 		cachesize = (1 * 1024 * 1024);
155 	}
156 
157 	uvmexp.ncolors = atop(cachesize);
158 }
159 
160 void
dec_kn300_cons_init(void)161 dec_kn300_cons_init(void)
162 {
163 	struct ctb *ctb;
164 	struct mcpcia_config *ccp;
165 	extern struct mcpcia_config mcpcia_console_configuration;
166 
167 	ccp = &mcpcia_console_configuration;
168 	/* It's already initialized. */
169 
170 	ctb = (struct ctb *)(((char *)hwrpb) + hwrpb->rpb_ctb_off);
171 
172 	switch (ctb->ctb_term_type) {
173 	case CTB_PRINTERPORT:
174 		/* serial console ... */
175 		/*
176 		 * Delay to allow PROM putchars to complete.
177 		 * FIFO depth * character time,
178 		 * character time = (1000000 / (defaultrate / 10))
179 		 */
180 		DELAY(160000000 / comcnrate);
181 		if (comcnattach(&ccp->cc_iot, 0x3f8, comcnrate,
182 		    COM_FREQ, COM_TYPE_NORMAL,
183 		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)) {
184 			panic("can't init serial console");
185 
186 		}
187 		break;
188 
189 	case CTB_GRAPHICS:
190 #if NPCKBD > 0
191 		/* display console ... */
192 		/* XXX */
193 		(void) pckbc_cnattach(&ccp->cc_iot, IO_KBD, KBCMDP,
194 		    PCKBC_KBD_SLOT, 0);
195 
196 		if (CTB_TURBOSLOT_TYPE(ctb->ctb_turboslot) ==
197 		    CTB_TURBOSLOT_TYPE_ISA)
198 			isa_display_console(&ccp->cc_iot, &ccp->cc_memt);
199 		else
200 			pci_display_console(&ccp->cc_iot, &ccp->cc_memt,
201 			    &ccp->cc_pc, CTB_TURBOSLOT_BUS(ctb->ctb_turboslot),
202 			    CTB_TURBOSLOT_SLOT(ctb->ctb_turboslot), 0);
203 #else
204 		panic("not configured to use display && keyboard console");
205 #endif
206 		break;
207 
208 	default:
209 		printf("ctb->ctb_term_type = 0x%lx\n", ctb->ctb_term_type);
210 		printf("ctb->ctb_turboslot = 0x%lx\n", ctb->ctb_turboslot);
211 
212 		panic("consinit: unknown console type %ld",
213 		    ctb->ctb_term_type);
214 	}
215 #ifdef KGDB
216 	/* Attach the KGDB device. */
217 	alpha_kgdb_init(kgdb_devlist, &ccp->cc_iot);
218 #endif /* KGDB */
219 }
220 
221 /* #define	BDEBUG	1 */
222 static void
dec_kn300_device_register(device_t dev,void * aux)223 dec_kn300_device_register(device_t dev, void *aux)
224 {
225 	static int found, initted, diskboot, netboot;
226 	static device_t primarydev, pcidev, ctrlrdev;
227 	struct bootdev_data *b = bootdev_data;
228 	device_t parent = device_parent(dev);
229 
230 	if (found)
231 		return;
232 
233 	if (!initted) {
234 		diskboot = (strcasecmp(b->protocol, "SCSI") == 0) ||
235 		    (strcasecmp(b->protocol, "RAID") == 0);
236 		netboot = (strcasecmp(b->protocol, "BOOTP") == 0) ||
237 		    (strcasecmp(b->protocol, "MOP") == 0);
238 #ifdef BDEBUG
239 		printf("proto:%s bus:%d slot:%d chan:%d", b->protocol,
240 		    b->bus, b->slot, b->channel);
241 		if (b->remote_address)
242 			printf(" remote_addr:%s", b->remote_address);
243 		printf(" un:%d bdt:%d", b->unit, b->boot_dev_type);
244 		if (b->ctrl_dev_type)
245 			printf(" cdt:%s\n", b->ctrl_dev_type);
246 		else
247 			printf("\n");
248 		printf("diskboot = %d, netboot = %d\n", diskboot, netboot);
249 #endif
250 		initted = 1;
251 	}
252 
253 	if (primarydev == NULL) {
254 		if (!device_is_a(dev, "mcpcia"))
255 			return;
256 		else {
257 			struct mcbus_dev_attach_args *ma = aux;
258 
259 			if (b->bus != ma->ma_mid - 4)
260 				return;
261 			primarydev = dev;
262 #ifdef BDEBUG
263 			printf("\nprimarydev = %s\n", device_xname(dev));
264 #endif
265 			return;
266 		}
267 	}
268 
269 	if (pcidev == NULL) {
270 		if (!device_is_a(dev, "pci"))
271 			return;
272 		/*
273 		 * Try to find primarydev anywhere in the ancestry.  This is
274 		 * necessary if the PCI bus is hidden behind a bridge.
275 		 */
276 		while (parent) {
277 			if (parent == primarydev)
278 				break;
279 			parent = device_parent(parent);
280 		}
281 		if (!parent)
282 			return;
283 		else {
284 			struct pcibus_attach_args *pba = aux;
285 
286 			if ((b->slot / 1000) != pba->pba_bus)
287 				return;
288 
289 			pcidev = dev;
290 #ifdef BDEBUG
291 			printf("\npcidev = %s\n", device_xname(dev));
292 #endif
293 			return;
294 		}
295 	}
296 
297 	if (ctrlrdev == NULL) {
298 		if (parent != pcidev)
299 			return;
300 		else {
301 			struct pci_attach_args *pa = aux;
302 			int slot;
303 
304 			slot = pa->pa_bus * 1000 + pa->pa_function * 100 +
305 			    pa->pa_device;
306 			if (b->slot != slot)
307 				return;
308 
309 			if (netboot) {
310 				booted_device = dev;
311 #ifdef BDEBUG
312 				printf("\nbooted_device = %s\n", device_xname(dev));
313 #endif
314 				found = 1;
315 			} else {
316 				ctrlrdev = dev;
317 #ifdef BDEBUG
318 				printf("\nctrlrdev = %s\n", device_xname(dev));
319 #endif
320 			}
321 			return;
322 		}
323 	}
324 
325 	if (!diskboot)
326 		return;
327 
328 	if (device_is_a(dev, "sd") ||
329 	    device_is_a(dev, "st") ||
330 	    device_is_a(dev, "cd")) {
331 		struct scsipibus_attach_args *sa = aux;
332 		struct scsipi_periph *periph = sa->sa_periph;
333 		int unit;
334 
335 		if (device_parent(parent) != ctrlrdev)
336 			return;
337 
338 		unit = periph->periph_target * 100 + periph->periph_lun;
339 		if (b->unit != unit)
340 			return;
341 		if (b->channel != periph->periph_channel->chan_channel)
342 			return;
343 
344 		/* we've found it! */
345 		booted_device = dev;
346 #ifdef BDEBUG
347 		printf("\nbooted_device = %s\n", device_xname(dev));
348 #endif
349 		found = 1;
350 	}
351 
352 	if (device_is_a(dev, "ld") && device_is_a(parent, "mlx")) {
353 		/*
354 		 * Argh!  The attach arguments for ld devices is not
355 		 * consistent, so each supported raid controller requires
356 		 * different checks.
357 		 */
358 		struct mlx_attach_args *mlxa = aux;
359 
360 		if (parent != ctrlrdev)
361 			return;
362 
363 		if (b->unit != mlxa->mlxa_unit)
364 			return;
365 		/* we've found it! */
366 		booted_device = dev;
367 #if 0
368 		printf("\nbooted_device = %s\n", device_xname(dev));
369 #endif
370 		found = 1;
371 	}
372 }
373 
374 
375 /*
376  * KN300 Machine Check Handlers.
377  */
378 static void kn300_softerr(unsigned long, unsigned long,
379     unsigned long, struct trapframe *);
380 
381 static void kn300_mcheck(unsigned long, unsigned long,
382     unsigned long, struct trapframe *);
383 
384 /*
385  * "soft" error structure in system area for KN300 processor.
386  * It differs from the EV5 'common' structure in a minor but
387  * exceedingly stupid and annoying fashion.
388  */
389 
390 typedef struct {
391 	/*
392 	 * Should be mc_cc_ev5 structure. Contents are the same,
393 	 * just in different places.
394 	 */
395 	uint64_t	ei_stat;
396 	uint64_t	ei_addr;
397 	uint64_t	fill_syndrome;
398 	uint64_t	isr;
399 	/*
400 	 * Platform Specific Area
401 	 */
402 	uint32_t	whami;
403 	uint32_t	sys_env;
404 	uint64_t	mcpcia_regs;
405 	uint32_t	pci_rev;
406 	uint32_t	mc_err0;
407 	uint32_t	mc_err1;
408 	uint32_t	cap_err;
409 	uint32_t	mdpa_stat;
410 	uint32_t	mdpa_syn;
411 	uint32_t	mdpb_stat;
412 	uint32_t	mdpb_syn;
413 	uint64_t	end_rsvd;
414 } mc_soft300;
415 #define	CAP_ERR_CRDX	204
416 
417 static void
kn300_softerr(unsigned long mces,unsigned long type,unsigned long logout,struct trapframe * framep)418 kn300_softerr(unsigned long mces, unsigned long type, unsigned long logout, struct trapframe *framep)
419 {
420 	static const char *sys = "system";
421 	static const char *proc = "processor";
422 	int whami;
423 	mc_hdr_ev5 *hdr;
424 	mc_soft300 *ptr;
425 	static const char *fmt1 = "        %-25s = 0x%l016x\n";
426 
427 	hdr = (mc_hdr_ev5 *) logout;
428 	ptr = (mc_soft300 *) (logout + sizeof (*hdr));
429 	whami = alpha_pal_whami();
430 
431 	printf("kn300: CPU ID %d %s correctable error corrected by %s\n", whami,
432 	    (type == ALPHA_SYS_ERROR)?  sys : proc,
433 	    ((hdr->mcheck_code & 0xff00) == (EV5_CORRECTED << 8))? proc :
434 	    (((hdr->mcheck_code & 0xff00) == (CAP_ERR_CRDX << 8)) ?
435 		"I/O Bridge Module" : sys));
436 
437 	printf("    Machine Check Code 0x%lx\n", hdr->mcheck_code);
438 	printf("    Physical Address of Error 0x%lx\n", ptr->ei_addr);
439 	if (ptr->ei_stat & 0x80000000L)
440 		printf("    Corrected ECC Error ");
441 	else
442 		printf("    Other Error");
443 	if (ptr->ei_stat & 0x40000000L)
444 		printf("in Memory ");
445 	else
446 		printf("in B-Cache ");
447 	if (ptr->ei_stat & 0x400000000L)
448 		printf("during I-Cache fill\n");
449 	else
450 		printf("during D-Cache fill\n");
451 
452 	printf(fmt1, "EI Status", ptr->ei_stat);
453 	printf(fmt1, "Fill Syndrome", ptr->fill_syndrome);
454 	printf(fmt1, "Interrupt Status Reg.", ptr->isr);
455 	printf("\n");
456 	printf(fmt1, "Whami Reg.", ptr->whami);
457 	printf(fmt1, "Sys. Env. Reg.", ptr->sys_env);
458 	printf(fmt1, "MCPCIA Regs.", ptr->mcpcia_regs);
459 	printf(fmt1, "PCI Rev. Reg.", ptr->pci_rev);
460 	printf(fmt1, "MC_ERR0 Reg.", ptr->mc_err0);
461 	printf(fmt1, "MC_ERR1 Reg.", ptr->mc_err1);
462 	printf(fmt1, "CAP_ERR Reg.", ptr->cap_err);
463 	printf(fmt1, "MDPA_STAT Reg.", ptr->mdpa_stat);
464 	printf(fmt1, "MDPA_SYN Reg.", ptr->mdpa_syn);
465 	printf(fmt1, "MDPB_STAT Reg.", ptr->mdpb_stat);
466 	printf(fmt1, "MDPB_SYN Reg.", ptr->mdpb_syn);
467 
468 	/*
469 	 * Clear error by rewriting register.
470 	 */
471 	alpha_pal_wrmces(mces);
472 }
473 
474 /*
475  * KN300 specific machine check handler
476  */
477 
478 static void
kn300_mcheck(unsigned long mces,unsigned long type,unsigned long logout,struct trapframe * framep)479 kn300_mcheck(unsigned long mces, unsigned long type, unsigned long logout, struct trapframe *framep)
480 {
481 	struct mchkinfo *mcp;
482 	static const char *fmt1 = "        %-25s = 0x%l016x\n";
483 	int i;
484 	mc_hdr_ev5 *hdr;
485 	mc_uc_ev5 *ptr;
486 	struct mcpcia_iodsnap *iodsnp;
487 
488 	/*
489 	 * If we expected a machine check, just go handle it in common code.
490 	 */
491 	mcp = &curcpu()->ci_mcinfo;
492 	if (mcp->mc_expected) {
493 		machine_check(mces, framep, type, logout);
494 		return;
495 	}
496 
497 	hdr = (mc_hdr_ev5 *) logout;
498 	ptr = (mc_uc_ev5 *) (logout + sizeof (*hdr));
499 	ev5_logout_print(hdr, ptr);
500 
501 	iodsnp = (struct mcpcia_iodsnap *) ((unsigned long) hdr +
502 	    (unsigned long) hdr->la_system_offset);
503 	for (i = 0; i < MCPCIA_PER_MCBUS; i++, iodsnp++) {
504 		if (!IS_MCPCIA_MAGIC(iodsnp->pci_rev)) {
505 			continue;
506 		}
507 		printf("        IOD %d register dump:\n", i);
508 		printf(fmt1, "Base Addr of PCI bridge", iodsnp->base_addr);
509 		printf(fmt1, "Whami Reg.", iodsnp->whami);
510 		printf(fmt1, "Sys. Env. Reg.", iodsnp->sys_env);
511 		printf(fmt1, "PCI Rev. Reg.", iodsnp->pci_rev);
512 		printf(fmt1, "CAP_CTL Reg.", iodsnp->cap_ctrl);
513 		printf(fmt1, "HAE_MEM Reg.", iodsnp->hae_mem);
514 		printf(fmt1, "HAE_IO Reg.", iodsnp->hae_io);
515 		printf(fmt1, "INT_CTL Reg.", iodsnp->int_ctl);
516 		printf(fmt1, "INT_REG Reg.", iodsnp->int_reg);
517 		printf(fmt1, "INT_MASK0 Reg.", iodsnp->int_mask0);
518 		printf(fmt1, "INT_MASK1 Reg.", iodsnp->int_mask1);
519 		printf(fmt1, "MC_ERR0 Reg.", iodsnp->mc_err0);
520 		printf(fmt1, "MC_ERR1 Reg.", iodsnp->mc_err1);
521 		printf(fmt1, "CAP_ERR Reg.", iodsnp->cap_err);
522 		printf(fmt1, "PCI_ERR1 Reg.", iodsnp->pci_err1);
523 		printf(fmt1, "MDPA_STAT Reg.", iodsnp->mdpa_stat);
524 		printf(fmt1, "MDPA_SYN Reg.", iodsnp->mdpa_syn);
525 		printf(fmt1, "MDPB_STAT Reg.", iodsnp->mdpb_stat);
526 		printf(fmt1, "MDPB_SYN Reg.", iodsnp->mdpb_syn);
527 
528 	}
529 	/*
530 	 * Now that we've printed all sorts of useful information
531 	 * and have decided that we really can't do any more to
532 	 * respond to the error, go on to the common code for
533 	 * final disposition. Usually this means that we die.
534 	 */
535 	/*
536 	 * XXX: HANDLE PCI ERRORS HERE?
537 	 */
538 	machine_check(mces, framep, type, logout);
539 }
540 
541 static void
dec_kn300_mcheck_handler(unsigned long mces,struct trapframe * framep,unsigned long vector,unsigned long param)542 dec_kn300_mcheck_handler(unsigned long mces, struct trapframe *framep, unsigned long vector, unsigned long param)
543 {
544 	switch (vector) {
545 	case ALPHA_SYS_ERROR:
546 	case ALPHA_PROC_ERROR:
547 		kn300_softerr(mces, vector, param, framep);
548 		break;
549 
550 	case ALPHA_SYS_MCHECK:
551 	case ALPHA_PROC_MCHECK:
552 		kn300_mcheck(mces, vector, param, framep);
553 		break;
554 	default:
555 		printf("KN300_MCHECK: unknown check vector 0x%lx\n", vector);
556 		machine_check(mces, framep, vector, param);
557 		break;
558 	}
559 }
560