1*4024Swnj /* uba.c 4.35 81/07/22 */ 240Sbill 340Sbill #include "../h/param.h" 42395Swnj #include "../h/systm.h" 52395Swnj #include "../h/cpu.h" 640Sbill #include "../h/map.h" 740Sbill #include "../h/pte.h" 82395Swnj #include "../h/buf.h" 92570Swnj #include "../h/vm.h" 102958Swnj #include "../h/ubareg.h" 112958Swnj #include "../h/ubavar.h" 1240Sbill #include "../h/dir.h" 1340Sbill #include "../h/user.h" 1440Sbill #include "../h/proc.h" 15284Sbill #include "../h/conf.h" 161901Swnj #include "../h/mtpr.h" 172395Swnj #include "../h/nexus.h" 182570Swnj #include "../h/dk.h" 1940Sbill 202929Swnj #if VAX780 212929Swnj char ubasr_bits[] = UBASR_BITS; 222929Swnj #endif 232929Swnj 2440Sbill /* 252570Swnj * Do transfer on device argument. The controller 262570Swnj * and uba involved are implied by the device. 272570Swnj * We queue for resource wait in the uba code if necessary. 282570Swnj * We return 1 if the transfer was started, 0 if it was not. 292570Swnj * If you call this routine with the head of the queue for a 302570Swnj * UBA, it will automatically remove the device from the UBA 312570Swnj * queue before it returns. If some other device is given 322570Swnj * as argument, it will be added to the request queue if the 332570Swnj * request cannot be started immediately. This means that 342570Swnj * passing a device which is on the queue but not at the head 352570Swnj * of the request queue is likely to be a disaster. 362570Swnj */ 372570Swnj ubago(ui) 382958Swnj register struct uba_device *ui; 392570Swnj { 402958Swnj register struct uba_ctlr *um = ui->ui_mi; 412570Swnj register struct uba_hd *uh; 422570Swnj register int s, unit; 432570Swnj 442570Swnj uh = &uba_hd[um->um_ubanum]; 452570Swnj s = spl6(); 462628Swnj if (um->um_driver->ud_xclu && uh->uh_users > 0 || uh->uh_xclu) 472616Swnj goto rwait; 482570Swnj um->um_ubinfo = ubasetup(um->um_ubanum, um->um_tab.b_actf->b_actf, 492570Swnj UBA_NEEDBDP|UBA_CANTWAIT); 502616Swnj if (um->um_ubinfo == 0) 512616Swnj goto rwait; 522616Swnj uh->uh_users++; 532628Swnj if (um->um_driver->ud_xclu) 542616Swnj uh->uh_xclu = 1; 552570Swnj splx(s); 562570Swnj if (ui->ui_dk >= 0) { 572570Swnj unit = ui->ui_dk; 582570Swnj dk_busy |= 1<<unit; 592570Swnj } 602570Swnj if (uh->uh_actf == ui) 612570Swnj uh->uh_actf = ui->ui_forw; 622570Swnj (*um->um_driver->ud_dgo)(um); 632570Swnj if (ui->ui_dk >= 0) { 642570Swnj dk_xfer[unit]++; 653353Swnj dk_wds[unit] += um->um_tab.b_actf->b_actf->b_bcount>>6; 662570Swnj } 672570Swnj return (1); 682616Swnj rwait: 692616Swnj if (uh->uh_actf != ui) { 702616Swnj ui->ui_forw = NULL; 712616Swnj if (uh->uh_actf == NULL) 722616Swnj uh->uh_actf = ui; 732616Swnj else 742616Swnj uh->uh_actl->ui_forw = ui; 752616Swnj uh->uh_actl = ui; 762616Swnj } 772616Swnj splx(s); 782616Swnj return (0); 792570Swnj } 802570Swnj 812616Swnj ubadone(um) 822958Swnj register struct uba_ctlr *um; 832616Swnj { 842616Swnj register struct uba_hd *uh = &uba_hd[um->um_ubanum]; 852616Swnj 862628Swnj if (um->um_driver->ud_xclu) 872616Swnj uh->uh_xclu = 0; 882616Swnj uh->uh_users--; 892616Swnj ubarelse(um->um_ubanum, &um->um_ubinfo); 902616Swnj } 912616Swnj 922570Swnj /* 932395Swnj * Allocate and setup UBA map registers, and bdp's 942395Swnj * Flags says whether bdp is needed, whether the caller can't 952395Swnj * wait (e.g. if the caller is at interrupt level). 9640Sbill * 972570Swnj * Return value: 9840Sbill * Bits 0-8 Byte offset 9940Sbill * Bits 9-17 Start map reg. no. 10040Sbill * Bits 18-27 No. mapping reg's 10140Sbill * Bits 28-31 BDP no. 10240Sbill */ 1032395Swnj ubasetup(uban, bp, flags) 1042395Swnj struct buf *bp; 10540Sbill { 1062395Swnj register struct uba_hd *uh = &uba_hd[uban]; 10740Sbill register int temp, i; 10840Sbill int npf, reg, bdp; 10940Sbill unsigned v; 11040Sbill register struct pte *pte, *io; 11140Sbill struct proc *rp; 11240Sbill int a, o, ubinfo; 11340Sbill 1143498Swnj #if VAX7ZZ 1153498Swnj if (cpu == VAX_7ZZ) 1163332Swnj flags &= ~UBA_NEEDBDP; 1173332Swnj #endif 11840Sbill v = btop(bp->b_un.b_addr); 11940Sbill o = (int)bp->b_un.b_addr & PGOFSET; 12040Sbill npf = btoc(bp->b_bcount + o) + 1; 12140Sbill a = spl6(); 1222784Swnj while ((reg = rmalloc(uh->uh_map, npf)) == 0) { 1233913Swnj if (flags & UBA_CANTWAIT) { 1243913Swnj splx(a); 1252395Swnj return (0); 1263913Swnj } 1272395Swnj uh->uh_mrwant++; 1282395Swnj sleep((caddr_t)uh->uh_map, PSWP); 12940Sbill } 13040Sbill bdp = 0; 1312395Swnj if (flags & UBA_NEEDBDP) { 1322395Swnj while ((bdp = ffs(uh->uh_bdpfree)) == 0) { 1332395Swnj if (flags & UBA_CANTWAIT) { 1342784Swnj rmfree(uh->uh_map, npf, reg); 1353913Swnj splx(a); 1362395Swnj return (0); 1372395Swnj } 1382395Swnj uh->uh_bdpwant++; 1392395Swnj sleep((caddr_t)uh->uh_map, PSWP); 14040Sbill } 1412463Swnj uh->uh_bdpfree &= ~(1 << (bdp-1)); 1422395Swnj } 14340Sbill splx(a); 1442463Swnj reg--; 14540Sbill ubinfo = (bdp << 28) | (npf << 18) | (reg << 9) | o; 1462395Swnj io = &uh->uh_uba->uba_map[reg]; 1472958Swnj temp = (bdp << 21) | UBAMR_MRV; 14840Sbill rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc; 14940Sbill if (bdp && (o & 01)) 1502958Swnj temp |= UBAMR_BO; 15140Sbill if (bp->b_flags & B_UAREA) { 15240Sbill for (i = UPAGES - bp->b_bcount / NBPG; i < UPAGES; i++) { 15340Sbill if (rp->p_addr[i].pg_pfnum == 0) 15440Sbill panic("uba: zero upage"); 15540Sbill *(int *)io++ = rp->p_addr[i].pg_pfnum | temp; 15640Sbill } 15740Sbill } else if ((bp->b_flags & B_PHYS) == 0) { 158728Sbill pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)]; 15940Sbill while (--npf != 0) 160728Sbill *(int *)io++ = pte++->pg_pfnum | temp; 16140Sbill } else { 16240Sbill if (bp->b_flags & B_PAGET) 16340Sbill pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)]; 16440Sbill else 16540Sbill pte = vtopte(rp, v); 16640Sbill while (--npf != 0) { 16740Sbill if (pte->pg_pfnum == 0) 16840Sbill panic("uba zero uentry"); 16940Sbill *(int *)io++ = pte++->pg_pfnum | temp; 17040Sbill } 17140Sbill } 17240Sbill *(int *)io++ = 0; 17340Sbill return (ubinfo); 17440Sbill } 17540Sbill 17640Sbill /* 1772570Swnj * Non buffer setup interface... set up a buffer and call ubasetup. 17840Sbill */ 1792395Swnj uballoc(uban, addr, bcnt, flags) 1803107Swnj int uban; 18140Sbill caddr_t addr; 1823107Swnj int bcnt, flags; 18340Sbill { 184883Sbill struct buf ubabuf; 18540Sbill 18640Sbill ubabuf.b_un.b_addr = addr; 18740Sbill ubabuf.b_flags = B_BUSY; 18840Sbill ubabuf.b_bcount = bcnt; 189883Sbill /* that's all the fields ubasetup() needs */ 1902395Swnj return (ubasetup(uban, &ubabuf, flags)); 19140Sbill } 19240Sbill 1932053Swnj /* 1942570Swnj * Release resources on uba uban, and then unblock resource waiters. 1952570Swnj * The map register parameter is by value since we need to block 1962570Swnj * against uba resets on 11/780's. 1972053Swnj */ 1982395Swnj ubarelse(uban, amr) 1992053Swnj int *amr; 20040Sbill { 2012395Swnj register struct uba_hd *uh = &uba_hd[uban]; 2022570Swnj register int bdp, reg, npf, s; 2032053Swnj int mr; 20440Sbill 2052570Swnj /* 2062570Swnj * Carefully see if we should release the space, since 2072570Swnj * it may be released asynchronously at uba reset time. 2082570Swnj */ 2092570Swnj s = spl6(); 2102053Swnj mr = *amr; 2112053Swnj if (mr == 0) { 2122570Swnj /* 2132570Swnj * A ubareset() occurred before we got around 2142570Swnj * to releasing the space... no need to bother. 2152570Swnj */ 2162570Swnj splx(s); 2172053Swnj return; 2182053Swnj } 2192067Swnj *amr = 0; 2202570Swnj splx(s); /* let interrupts in, we're safe for a while */ 22140Sbill bdp = (mr >> 28) & 0x0f; 22240Sbill if (bdp) { 2232729Swnj switch (cpu) { 2242423Skre #if VAX780 2252423Skre case VAX_780: 2262958Swnj uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE; 2272423Skre break; 2282423Skre #endif 2292423Skre #if VAX750 2302423Skre case VAX_750: 2312958Swnj uh->uh_uba->uba_dpr[bdp] |= 2322958Swnj UBADPR_PURGE|UBADPR_NXM|UBADPR_UCE; 2332423Skre break; 2342423Skre #endif 2352423Skre } 2362570Swnj uh->uh_bdpfree |= 1 << (bdp-1); /* atomic */ 2372395Swnj if (uh->uh_bdpwant) { 2382395Swnj uh->uh_bdpwant = 0; 2392395Swnj wakeup((caddr_t)uh->uh_map); 24040Sbill } 24140Sbill } 2422570Swnj /* 2432570Swnj * Put back the registers in the resource map. 2442570Swnj * The map code must not be reentered, so we do this 2452570Swnj * at high ipl. 2462570Swnj */ 24740Sbill npf = (mr >> 18) & 0x3ff; 24840Sbill reg = ((mr >> 9) & 0x1ff) + 1; 2492570Swnj s = spl6(); 2502784Swnj rmfree(uh->uh_map, npf, reg); 2512570Swnj splx(s); 2522570Swnj 2532570Swnj /* 2542570Swnj * Wakeup sleepers for map registers, 2552570Swnj * and also, if there are processes blocked in dgo(), 2562570Swnj * give them a chance at the UNIBUS. 2572570Swnj */ 2582395Swnj if (uh->uh_mrwant) { 2592395Swnj uh->uh_mrwant = 0; 2602395Swnj wakeup((caddr_t)uh->uh_map); 26140Sbill } 2622570Swnj while (uh->uh_actf && ubago(uh->uh_actf)) 2632570Swnj ; 26440Sbill } 26540Sbill 2662729Swnj ubapurge(um) 2672958Swnj register struct uba_ctlr *um; 2682729Swnj { 2692729Swnj register struct uba_hd *uh = um->um_hd; 2702729Swnj register int bdp = (um->um_ubinfo >> 28) & 0x0f; 2712729Swnj 2722729Swnj switch (cpu) { 2732729Swnj #if VAX780 2742729Swnj case VAX_780: 2752958Swnj uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE; 2762729Swnj break; 2772729Swnj #endif 2782729Swnj #if VAX750 2792729Swnj case VAX_750: 2802958Swnj uh->uh_uba->uba_dpr[bdp] |= UBADPR_PURGE|UBADPR_NXM|UBADPR_UCE; 2812729Swnj break; 2822729Swnj #endif 2832729Swnj } 2842729Swnj } 2852729Swnj 2862570Swnj /* 2872570Swnj * Generate a reset on uba number uban. Then 2882570Swnj * call each device in the character device table, 2892570Swnj * giving it a chance to clean up so as to be able to continue. 2902570Swnj */ 2912395Swnj ubareset(uban) 2922570Swnj int uban; 293284Sbill { 294284Sbill register struct cdevsw *cdp; 2952646Swnj register struct uba_hd *uh = &uba_hd[uban]; 2961781Sbill int s; 297284Sbill 298302Sbill s = spl6(); 2992646Swnj uh->uh_users = 0; 3002646Swnj uh->uh_zvcnt = 0; 3012646Swnj uh->uh_xclu = 0; 3022646Swnj uh->uh_hangcnt = 0; 3032646Swnj uh->uh_actf = uh->uh_actl = 0; 3042646Swnj uh->uh_bdpwant = 0; 3052646Swnj uh->uh_mrwant = 0; 3062646Swnj wakeup((caddr_t)&uh->uh_bdpwant); 3072646Swnj wakeup((caddr_t)&uh->uh_mrwant); 3082958Swnj printf("uba%d: reset", uban); 3092958Swnj ubainit(uh->uh_uba); 310284Sbill for (cdp = cdevsw; cdp->d_open; cdp++) 3112395Swnj (*cdp->d_reset)(uban); 312284Sbill printf("\n"); 313302Sbill splx(s); 314284Sbill } 3152395Swnj 3162570Swnj /* 3172570Swnj * Init a uba. This is called with a pointer 3182570Swnj * rather than a virtual address since it is called 3192570Swnj * by code which runs with memory mapping disabled. 3202570Swnj * In these cases we really don't need the interrupts 3212570Swnj * enabled, but since we run with ipl high, we don't care 3222570Swnj * if they are, they will never happen anyways. 3232570Swnj */ 3242423Skre ubainit(uba) 3252423Skre register struct uba_regs *uba; 3262395Swnj { 3272395Swnj 3282958Swnj switch (cpu) { 3292958Swnj #if VAX780 3303248Swnj case VAX_780: 3312958Swnj uba->uba_cr = UBACR_ADINIT; 3322958Swnj uba->uba_cr = UBACR_IFS|UBACR_BRIE|UBACR_USEFIE|UBACR_SUEFIE; 3332958Swnj while ((uba->uba_cnfgr & UBACNFGR_UBIC) == 0) 3342958Swnj ; 3352958Swnj break; 3362958Swnj #endif 3372958Swnj #if VAX750 3383248Swnj case VAX_750: 3393352Swnj #endif 3403498Swnj #if VAX7ZZ 3413498Swnj case VAX_7ZZ: 3423352Swnj #endif 3433498Swnj #if defined(VAX750) || defined(VAX7ZZ) 3443352Swnj mtpr(IUR, 0); 3452958Swnj /* give devices time to recover from power fail */ 3463332Swnj /* THIS IS PROBABLY UNNECESSARY */ 3473352Swnj DELAY(500000); 3483332Swnj /* END PROBABLY UNNECESSARY */ 3492958Swnj break; 3502958Swnj #endif 3512958Swnj } 3522395Swnj } 3532395Swnj 3542958Swnj #if VAX780 3552570Swnj /* 3562570Swnj * Check to make sure the UNIBUS adaptor is not hung, 3572570Swnj * with an interrupt in the register to be presented, 3582570Swnj * but not presenting it for an extended period (5 seconds). 3592570Swnj */ 3602395Swnj unhang() 3612395Swnj { 3622395Swnj register int uban; 3632395Swnj 3642395Swnj for (uban = 0; uban < numuba; uban++) { 3652395Swnj register struct uba_hd *uh = &uba_hd[uban]; 3662395Swnj register struct uba_regs *up = uh->uh_uba; 3672395Swnj 3682395Swnj if (up->uba_sr == 0) 3692395Swnj return; 3703945Sroot up->uba_sr = UBASR_CRD|UBASR_LEB; 3712395Swnj uh->uh_hangcnt++; 3722759Swnj if (uh->uh_hangcnt > 5*hz) { 3732395Swnj uh->uh_hangcnt = 0; 3742929Swnj printf("uba%d: hung\n", uban); 3752395Swnj ubareset(uban); 3762395Swnj } 3772395Swnj } 3782395Swnj } 3792395Swnj 3802570Swnj /* 3812570Swnj * This is a timeout routine which decrements the ``i forgot to 3822570Swnj * interrupt'' counts, on an 11/780. This prevents slowly growing 3832570Swnj * counts from causing a UBA reset since we are interested only 3842570Swnj * in hang situations. 3852570Swnj */ 3862395Swnj ubawatch() 3872395Swnj { 3882395Swnj register struct uba_hd *uh; 3892395Swnj register int uban; 3902395Swnj 3912784Swnj if (panicstr) 3922784Swnj return; 3932395Swnj for (uban = 0; uban < numuba; uban++) { 3942395Swnj uh = &uba_hd[uban]; 3952395Swnj if (uh->uh_hangcnt) 3962395Swnj uh->uh_hangcnt--; 3972395Swnj } 3982395Swnj } 3992395Swnj 400*4024Swnj int ubawedgecnt = 10; 401*4024Swnj int ubacrazy = 500; 4022570Swnj /* 4032570Swnj * This routine is called by the locore code to 4042570Swnj * process a UBA error on an 11/780. The arguments are passed 4052570Swnj * on the stack, and value-result (through some trickery). 4062570Swnj * In particular, the uvec argument is used for further 4072570Swnj * uba processing so the result aspect of it is very important. 4082570Swnj * It must not be declared register. 4092570Swnj */ 4102423Skre /*ARGSUSED*/ 4112395Swnj ubaerror(uban, uh, xx, uvec, uba) 4122395Swnj register int uban; 4132395Swnj register struct uba_hd *uh; 4142395Swnj int uvec; 4152395Swnj register struct uba_regs *uba; 4162395Swnj { 4172395Swnj register sr, s; 4182395Swnj 4192395Swnj if (uvec == 0) { 4202395Swnj uh->uh_zvcnt++; 4212395Swnj if (uh->uh_zvcnt > 250000) { 4222929Swnj printf("uba%d: too many zero vectors\n"); 4232395Swnj ubareset(uban); 4242395Swnj } 4252395Swnj uvec = 0; 4262395Swnj return; 4272395Swnj } 4282395Swnj if (uba->uba_cnfgr & NEX_CFGFLT) { 4292929Swnj printf("uba%d: sbi fault sr=%b cnfgr=%b\n", 4302929Swnj uban, uba->uba_sr, ubasr_bits, 4313248Swnj uba->uba_cnfgr, NEXFLT_BITS); 4322395Swnj ubareset(uban); 4332395Swnj uvec = 0; 4342395Swnj return; 4352395Swnj } 4362395Swnj sr = uba->uba_sr; 4372395Swnj s = spl7(); 4383473Swnj printf("uba%d: uba error sr=%b fmer=%x fubar=%o\n", 4393473Swnj uban, uba->uba_sr, ubasr_bits, uba->uba_fmer, 4*uba->uba_fubar); 4402395Swnj splx(s); 4412395Swnj uba->uba_sr = sr; 4422958Swnj uvec &= UBABRRVR_DIV; 443*4024Swnj if (++uh->uh_errcnt % ubawedgecnt == 0) { 444*4024Swnj if (uh->uh_errcnt > ubacrazy) 445*4024Swnj panic("uba crazy"); 446*4024Swnj printf("ERROR LIMIT "); 447*4024Swnj ubareset(uban); 448*4024Swnj uvec = 0; 449*4024Swnj return; 450*4024Swnj } 4512395Swnj return; 4522395Swnj } 4532395Swnj #endif 4543745Sroot 4553745Sroot /* 4563745Sroot * This routine allows remapping of previously 4573745Sroot * allocated UNIBUS bdp and map resources 4583745Sroot * onto different memory addresses. 4593745Sroot * It should only be used by routines which need 4603745Sroot * small fixed length mappings for long periods of time 4613745Sroot * (like the ARPANET ACC IMP interface). 4623745Sroot * It only maps kernel addresses. 4633745Sroot */ 4643745Sroot ubaremap(uban, ubinfo, addr) 4653745Sroot int uban; 4663745Sroot register unsigned ubinfo; 4673745Sroot caddr_t addr; 4683745Sroot { 4693745Sroot register struct uba_hd *uh = &uba_hd[uban]; 4703745Sroot register struct pte *pte, *io; 4713745Sroot register int temp, bdp; 4723745Sroot int npf, o; 4733745Sroot 4743745Sroot o = (int)addr & PGOFSET; 4753745Sroot bdp = (ubinfo >> 28) & 0xf; 4763745Sroot npf = (ubinfo >> 18) & 0x3ff; 4773745Sroot io = &uh->uh_uba->uba_map[(ubinfo >> 9) & 0x1ff]; 4783745Sroot temp = (bdp << 21) | UBAMR_MRV; 4793745Sroot 4803745Sroot /* 4813745Sroot * If using buffered data path initiate purge 4823745Sroot * of old data and set byte offset bit if next 4833745Sroot * transfer will be from odd address. 4843745Sroot */ 4853745Sroot if (bdp) { 4863745Sroot switch (cpu) { 4873745Sroot #if VAX780 4883745Sroot case VAX_780: 4893745Sroot uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE; 4903745Sroot break; 4913745Sroot #endif 4923745Sroot #if VAX750 4933745Sroot case VAX_750: 4943745Sroot uh->uh_uba->uba_dpr[bdp] |= 4953745Sroot UBADPR_PURGE|UBADPR_NXM|UBADPR_UCE; 4963745Sroot break; 4973745Sroot #endif 4983745Sroot } 4993745Sroot if (o & 1) 5003745Sroot temp |= UBAMR_BO; 5013745Sroot } 5023745Sroot 5033745Sroot /* 5043745Sroot * Set up the map registers, leaving an invalid reg 5053745Sroot * at the end to guard against wild unibus transfers. 5063745Sroot */ 5073745Sroot pte = &Sysmap[btop(((int)addr)&0x7fffffff)]; 5083745Sroot while (--npf != 0) 5093745Sroot *(int *)io++ = pte++->pg_pfnum | temp; 5103745Sroot *(int *)io = 0; 5113745Sroot 5123745Sroot /* 5133745Sroot * Return effective UNIBUS address. 5143745Sroot */ 5153745Sroot return (ubinfo | o); 5163745Sroot } 517