xref: /csrg-svn/sys/vax/uba/uba.c (revision 2646)
1*2646Swnj /*	uba.c	4.14	02/23/81	*/
240Sbill 
32570Swnj #define	DELAY(N)	{ register int d; d = N; while (--d > 0); }
42570Swnj 
540Sbill #include "../h/param.h"
62395Swnj #include "../h/systm.h"
72395Swnj #include "../h/cpu.h"
840Sbill #include "../h/map.h"
940Sbill #include "../h/pte.h"
102395Swnj #include "../h/buf.h"
112570Swnj #include "../h/vm.h"
1240Sbill #include "../h/uba.h"
1340Sbill #include "../h/dir.h"
1440Sbill #include "../h/user.h"
1540Sbill #include "../h/proc.h"
16284Sbill #include "../h/conf.h"
171901Swnj #include "../h/mtpr.h"
182395Swnj #include "../h/nexus.h"
192570Swnj #include "../h/dk.h"
2040Sbill 
2140Sbill /*
222570Swnj  * Do transfer on device argument.  The controller
232570Swnj  * and uba involved are implied by the device.
242570Swnj  * We queue for resource wait in the uba code if necessary.
252570Swnj  * We return 1 if the transfer was started, 0 if it was not.
262570Swnj  * If you call this routine with the head of the queue for a
272570Swnj  * UBA, it will automatically remove the device from the UBA
282570Swnj  * queue before it returns.  If some other device is given
292570Swnj  * as argument, it will be added to the request queue if the
302570Swnj  * request cannot be started immediately.  This means that
312570Swnj  * passing a device which is on the queue but not at the head
322570Swnj  * of the request queue is likely to be a disaster.
332570Swnj  */
342570Swnj ubago(ui)
352570Swnj 	register struct uba_dinfo *ui;
362570Swnj {
372570Swnj 	register struct uba_minfo *um = ui->ui_mi;
382570Swnj 	register struct uba_hd *uh;
392570Swnj 	register int s, unit;
402570Swnj 
412570Swnj 	uh = &uba_hd[um->um_ubanum];
422570Swnj 	s = spl6();
432628Swnj 	if (um->um_driver->ud_xclu && uh->uh_users > 0 || uh->uh_xclu)
442616Swnj 		goto rwait;
452570Swnj 	um->um_ubinfo = ubasetup(um->um_ubanum, um->um_tab.b_actf->b_actf,
462570Swnj 	    UBA_NEEDBDP|UBA_CANTWAIT);
472616Swnj 	if (um->um_ubinfo == 0)
482616Swnj 		goto rwait;
492616Swnj 	uh->uh_users++;
502628Swnj 	if (um->um_driver->ud_xclu)
512616Swnj 		uh->uh_xclu = 1;
522570Swnj 	splx(s);
532570Swnj 	if (ui->ui_dk >= 0) {
542570Swnj 		unit = ui->ui_dk;
552570Swnj 		dk_busy |= 1<<unit;
562570Swnj 	}
572570Swnj 	if (uh->uh_actf == ui)
582570Swnj 		uh->uh_actf = ui->ui_forw;
592570Swnj 	(*um->um_driver->ud_dgo)(um);
602570Swnj 	if (ui->ui_dk >= 0) {
612570Swnj 		dk_xfer[unit]++;
622570Swnj 		dk_wds[unit] += um->um_tab.b_actf->b_bcount>>6;
632570Swnj 	}
642570Swnj 	return (1);
652616Swnj rwait:
662616Swnj 	if (uh->uh_actf != ui) {
672616Swnj 		ui->ui_forw = NULL;
682616Swnj 		if (uh->uh_actf == NULL)
692616Swnj 			uh->uh_actf = ui;
702616Swnj 		else
712616Swnj 			uh->uh_actl->ui_forw = ui;
722616Swnj 		uh->uh_actl = ui;
732616Swnj 	}
742616Swnj 	splx(s);
752616Swnj 	return (0);
762570Swnj }
772570Swnj 
782616Swnj ubadone(um)
792616Swnj 	register struct uba_minfo *um;
802616Swnj {
812616Swnj 	register struct uba_hd *uh = &uba_hd[um->um_ubanum];
822616Swnj 
832628Swnj 	if (um->um_driver->ud_xclu)
842616Swnj 		uh->uh_xclu = 0;
852616Swnj 	uh->uh_users--;
862616Swnj 	ubarelse(um->um_ubanum, &um->um_ubinfo);
872616Swnj }
882616Swnj 
892570Swnj /*
902395Swnj  * Allocate and setup UBA map registers, and bdp's
912395Swnj  * Flags says whether bdp is needed, whether the caller can't
922395Swnj  * wait (e.g. if the caller is at interrupt level).
9340Sbill  *
942570Swnj  * Return value:
9540Sbill  *	Bits 0-8	Byte offset
9640Sbill  *	Bits 9-17	Start map reg. no.
9740Sbill  *	Bits 18-27	No. mapping reg's
9840Sbill  *	Bits 28-31	BDP no.
9940Sbill  */
1002395Swnj ubasetup(uban, bp, flags)
1012395Swnj 	struct buf *bp;
10240Sbill {
1032395Swnj 	register struct uba_hd *uh = &uba_hd[uban];
10440Sbill 	register int temp, i;
10540Sbill 	int npf, reg, bdp;
10640Sbill 	unsigned v;
10740Sbill 	register struct pte *pte, *io;
10840Sbill 	struct proc *rp;
10940Sbill 	int a, o, ubinfo;
11040Sbill 
11140Sbill 	v = btop(bp->b_un.b_addr);
11240Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
11340Sbill 	npf = btoc(bp->b_bcount + o) + 1;
11440Sbill 	a = spl6();
1152395Swnj 	while ((reg = malloc(uh->uh_map, npf)) == 0) {
1162395Swnj 		if (flags & UBA_CANTWAIT)
1172395Swnj 			return (0);
1182395Swnj 		uh->uh_mrwant++;
1192395Swnj 		sleep((caddr_t)uh->uh_map, PSWP);
12040Sbill 	}
12140Sbill 	bdp = 0;
1222395Swnj 	if (flags & UBA_NEEDBDP) {
1232395Swnj 		while ((bdp = ffs(uh->uh_bdpfree)) == 0) {
1242395Swnj 			if (flags & UBA_CANTWAIT) {
1252395Swnj 				mfree(uh->uh_map, npf, reg);
1262395Swnj 				return (0);
1272395Swnj 			}
1282395Swnj 			uh->uh_bdpwant++;
1292395Swnj 			sleep((caddr_t)uh->uh_map, PSWP);
13040Sbill 		}
1312463Swnj 		uh->uh_bdpfree &= ~(1 << (bdp-1));
1322395Swnj 	}
13340Sbill 	splx(a);
1342463Swnj 	reg--;
13540Sbill 	ubinfo = (bdp << 28) | (npf << 18) | (reg << 9) | o;
1362395Swnj 	io = &uh->uh_uba->uba_map[reg];
1372395Swnj 	temp = (bdp << 21) | UBA_MRV;
13840Sbill 	rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc;
13940Sbill 	if (bdp && (o & 01))
1402395Swnj 		temp |= UBA_BO;
14140Sbill 	if (bp->b_flags & B_UAREA) {
14240Sbill 		for (i = UPAGES - bp->b_bcount / NBPG; i < UPAGES; i++) {
14340Sbill 			if (rp->p_addr[i].pg_pfnum == 0)
14440Sbill 				panic("uba: zero upage");
14540Sbill 			*(int *)io++ = rp->p_addr[i].pg_pfnum | temp;
14640Sbill 		}
14740Sbill 	} else if ((bp->b_flags & B_PHYS) == 0) {
148728Sbill 		pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)];
14940Sbill 		while (--npf != 0)
150728Sbill 			*(int *)io++ = pte++->pg_pfnum | temp;
15140Sbill 	} else {
15240Sbill 		if (bp->b_flags & B_PAGET)
15340Sbill 			pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
15440Sbill 		else
15540Sbill 			pte = vtopte(rp, v);
15640Sbill 		while (--npf != 0) {
15740Sbill 			if (pte->pg_pfnum == 0)
15840Sbill 				panic("uba zero uentry");
15940Sbill 			*(int *)io++ = pte++->pg_pfnum | temp;
16040Sbill 		}
16140Sbill 	}
16240Sbill 	*(int *)io++ = 0;
16340Sbill 	return (ubinfo);
16440Sbill }
16540Sbill 
16640Sbill /*
1672570Swnj  * Non buffer setup interface... set up a buffer and call ubasetup.
16840Sbill  */
1692395Swnj uballoc(uban, addr, bcnt, flags)
17040Sbill 	caddr_t addr;
17140Sbill 	unsigned short bcnt;
17240Sbill {
173883Sbill 	struct buf ubabuf;
17440Sbill 
17540Sbill 	ubabuf.b_un.b_addr = addr;
17640Sbill 	ubabuf.b_flags = B_BUSY;
17740Sbill 	ubabuf.b_bcount = bcnt;
178883Sbill 	/* that's all the fields ubasetup() needs */
1792395Swnj 	return (ubasetup(uban, &ubabuf, flags));
18040Sbill }
18140Sbill 
1822053Swnj /*
1832570Swnj  * Release resources on uba uban, and then unblock resource waiters.
1842570Swnj  * The map register parameter is by value since we need to block
1852570Swnj  * against uba resets on 11/780's.
1862053Swnj  */
1872395Swnj ubarelse(uban, amr)
1882053Swnj 	int *amr;
18940Sbill {
1902395Swnj 	register struct uba_hd *uh = &uba_hd[uban];
1912570Swnj 	register int bdp, reg, npf, s;
1922053Swnj 	int mr;
19340Sbill 
1942570Swnj 	/*
1952570Swnj 	 * Carefully see if we should release the space, since
1962570Swnj 	 * it may be released asynchronously at uba reset time.
1972570Swnj 	 */
1982570Swnj 	s = spl6();
1992053Swnj 	mr = *amr;
2002053Swnj 	if (mr == 0) {
2012570Swnj 		/*
2022570Swnj 		 * A ubareset() occurred before we got around
2032570Swnj 		 * to releasing the space... no need to bother.
2042570Swnj 		 */
2052570Swnj 		splx(s);
2062053Swnj 		return;
2072053Swnj 	}
2082067Swnj 	*amr = 0;
2092570Swnj 	splx(s);		/* let interrupts in, we're safe for a while */
21040Sbill 	bdp = (mr >> 28) & 0x0f;
21140Sbill 	if (bdp) {
2122423Skre 		switch (cpu)
2132423Skre 		{
2142423Skre #if VAX780
2152423Skre 		case VAX_780:
2162423Skre 			uh->uh_uba->uba_dpr[bdp] |= UBA_BNE;
2172423Skre 			break;
2182423Skre #endif
2192423Skre #if VAX750
2202423Skre 		case VAX_750:
2212423Skre 			uh->uh_uba->uba_dpr[bdp] |= UBA_PURGE|UBA_NXM|UBA_UCE;
2222423Skre 			break;
2232423Skre #endif
2242423Skre 		}
2252570Swnj 		uh->uh_bdpfree |= 1 << (bdp-1);		/* atomic */
2262395Swnj 		if (uh->uh_bdpwant) {
2272395Swnj 			uh->uh_bdpwant = 0;
2282395Swnj 			wakeup((caddr_t)uh->uh_map);
22940Sbill 		}
23040Sbill 	}
2312570Swnj 	/*
2322570Swnj 	 * Put back the registers in the resource map.
2332570Swnj 	 * The map code must not be reentered, so we do this
2342570Swnj 	 * at high ipl.
2352570Swnj 	 */
23640Sbill 	npf = (mr >> 18) & 0x3ff;
23740Sbill 	reg = ((mr >> 9) & 0x1ff) + 1;
2382570Swnj 	s = spl6();
2392395Swnj 	mfree(uh->uh_map, npf, reg);
2402570Swnj 	splx(s);
2412570Swnj 
2422570Swnj 	/*
2432570Swnj 	 * Wakeup sleepers for map registers,
2442570Swnj 	 * and also, if there are processes blocked in dgo(),
2452570Swnj 	 * give them a chance at the UNIBUS.
2462570Swnj 	 */
2472395Swnj 	if (uh->uh_mrwant) {
2482395Swnj 		uh->uh_mrwant = 0;
2492395Swnj 		wakeup((caddr_t)uh->uh_map);
25040Sbill 	}
2512570Swnj 	while (uh->uh_actf && ubago(uh->uh_actf))
2522570Swnj 		;
25340Sbill }
25440Sbill 
2552570Swnj /*
2562570Swnj  * Generate a reset on uba number uban.  Then
2572570Swnj  * call each device in the character device table,
2582570Swnj  * giving it a chance to clean up so as to be able to continue.
2592570Swnj  */
2602395Swnj ubareset(uban)
2612570Swnj 	int uban;
262284Sbill {
263284Sbill 	register struct cdevsw *cdp;
264*2646Swnj 	register struct uba_hd *uh = &uba_hd[uban];
2651781Sbill 	int s;
266284Sbill 
267302Sbill 	s = spl6();
268*2646Swnj 	uh->uh_users = 0;
269*2646Swnj 	uh->uh_zvcnt = 0;
270*2646Swnj 	uh->uh_xclu = 0;
271*2646Swnj 	uh->uh_hangcnt = 0;
272*2646Swnj 	uh->uh_actf = uh->uh_actl = 0;
273*2646Swnj 	uh->uh_bdpwant = 0;
274*2646Swnj 	uh->uh_mrwant = 0;
275*2646Swnj 	wakeup((caddr_t)&uh->uh_bdpwant);
276*2646Swnj 	wakeup((caddr_t)&uh->uh_mrwant);
2772395Swnj 	switch (cpu) {
2781901Swnj #if VAX==780
2792395Swnj 	case VAX_780:
2802395Swnj 		printf("UBA RESET %d:", uban);
281*2646Swnj 		ubainit(uh->uh_uba);
2822395Swnj 		break;
2831901Swnj #endif
2841901Swnj #if VAX==750
2852395Swnj 	case VAX_750:
2862395Swnj 		printf("UNIBUS INIT:");
2872395Swnj 		mtpr(IUR, 1);
2882395Swnj 		DELAY(100000);
2892395Swnj 		break;
2901901Swnj #endif
2912395Swnj 	}
292284Sbill 	for (cdp = cdevsw; cdp->d_open; cdp++)
2932395Swnj 		(*cdp->d_reset)(uban);
294284Sbill 	printf("\n");
295302Sbill 	splx(s);
296284Sbill }
2972395Swnj 
2982570Swnj /*
2992570Swnj  * Init a uba.  This is called with a pointer
3002570Swnj  * rather than a virtual address since it is called
3012570Swnj  * by code which runs with memory mapping disabled.
3022570Swnj  * In these cases we really don't need the interrupts
3032570Swnj  * enabled, but since we run with ipl high, we don't care
3042570Swnj  * if they are, they will never happen anyways.
3052570Swnj  */
3062423Skre ubainit(uba)
3072423Skre 	register struct uba_regs *uba;
3082395Swnj {
3092395Swnj 
3102423Skre 	uba->uba_cr = UBA_ADINIT;
3112423Skre 	uba->uba_cr = UBA_IFS|UBA_BRIE|UBA_USEFIE|UBA_SUEFIE;
3122423Skre 	while ((uba->uba_cnfgr & UBA_UBIC) == 0)
3132395Swnj 		;
3142395Swnj }
3152395Swnj 
3162395Swnj #if VAX780
3172570Swnj /*
3182570Swnj  * Check to make sure the UNIBUS adaptor is not hung,
3192570Swnj  * with an interrupt in the register to be presented,
3202570Swnj  * but not presenting it for an extended period (5 seconds).
3212570Swnj  */
3222395Swnj unhang()
3232395Swnj {
3242395Swnj 	register int uban;
3252395Swnj 
3262395Swnj 	for (uban = 0; uban < numuba; uban++) {
3272395Swnj 		register struct uba_hd *uh = &uba_hd[uban];
3282395Swnj 		register struct uba_regs *up = uh->uh_uba;
3292395Swnj 
3302395Swnj 		if (up->uba_sr == 0)
3312395Swnj 			return;
3322395Swnj 		uh->uh_hangcnt++;
3332395Swnj 		if (uh->uh_hangcnt > 5*HZ) {
3342395Swnj 			uh->uh_hangcnt = 0;
3352395Swnj 			printf("HANG ");
3362395Swnj 			ubareset(uban);
3372395Swnj 		}
3382395Swnj 	}
3392395Swnj }
3402395Swnj 
3412570Swnj /*
3422570Swnj  * This is a timeout routine which decrements the ``i forgot to
3432570Swnj  * interrupt'' counts, on an 11/780.  This prevents slowly growing
3442570Swnj  * counts from causing a UBA reset since we are interested only
3452570Swnj  * in hang situations.
3462570Swnj  */
3472395Swnj ubawatch()
3482395Swnj {
3492395Swnj 	register struct uba_hd *uh;
3502395Swnj 	register int uban;
3512395Swnj 
3522395Swnj 	for (uban = 0; uban < numuba; uban++) {
3532395Swnj 		uh = &uba_hd[uban];
3542395Swnj 		if (uh->uh_hangcnt)
3552395Swnj 			uh->uh_hangcnt--;
3562395Swnj 	}
3572395Swnj }
3582395Swnj 
3592570Swnj /*
3602570Swnj  * This routine is called by the locore code to
3612570Swnj  * process a UBA error on an 11/780.  The arguments are passed
3622570Swnj  * on the stack, and value-result (through some trickery).
3632570Swnj  * In particular, the uvec argument is used for further
3642570Swnj  * uba processing so the result aspect of it is very important.
3652570Swnj  * It must not be declared register.
3662570Swnj  */
3672423Skre /*ARGSUSED*/
3682395Swnj ubaerror(uban, uh, xx, uvec, uba)
3692395Swnj 	register int uban;
3702395Swnj 	register struct uba_hd *uh;
3712395Swnj 	int uvec;
3722395Swnj 	register struct uba_regs *uba;
3732395Swnj {
3742395Swnj 	register sr, s;
3752395Swnj 
3762395Swnj 	if (uvec == 0) {
3772395Swnj 		uh->uh_zvcnt++;
3782395Swnj 		if (uh->uh_zvcnt > 250000) {
3792395Swnj 			printf("ZERO VECTOR ");
3802395Swnj 			ubareset(uban);
3812395Swnj 		}
3822395Swnj 		uvec = 0;
3832395Swnj 		return;
3842395Swnj 	}
3852395Swnj 	if (uba->uba_cnfgr & NEX_CFGFLT) {
3862395Swnj 		printf("UBA%d SBI FAULT sr %x cnfgr %x\n",
3872395Swnj 		    uban, uba->uba_sr, uba->uba_cnfgr);
3882395Swnj 		ubareset(uban);
3892395Swnj 		uvec = 0;
3902395Swnj 		return;
3912395Swnj 	}
3922395Swnj 	sr = uba->uba_sr;
3932395Swnj 	s = spl7();
3942395Swnj 	printf("UBA%d ERROR SR %x FMER %x FUBAR %o\n",
3952470Swnj 	    uban, uba->uba_sr, uba->uba_fmer, 4*uba->uba_fubar);
3962395Swnj 	splx(s);
3972395Swnj 	uba->uba_sr = sr;
3982395Swnj 	uvec &= UBA_DIV;
3992395Swnj 	return;
4002395Swnj }
4012395Swnj #endif
402