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