remember dma count, as can't calculate correctly on Emulex;
[unix-history] / usr / src / sys / vax / uba / uba.c
index 1540929..ba9d9ad 100644 (file)
@@ -1,26 +1,37 @@
-/*     uba.c   4.41    82/03/29        */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/cpu.h"
-#include "../h/map.h"
-#include "../h/pte.h"
-#include "../h/buf.h"
-#include "../h/vm.h"
-#include "../h/ubareg.h"
-#include "../h/ubavar.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/proc.h"
-#include "../h/conf.h"
-#include "../h/mtpr.h"
-#include "../h/nexus.h"
-#include "../h/dk.h"
-
-#if VAX780
+/*
+ * Copyright (c) 1982 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)uba.c       6.10 (Berkeley) %G%
+ */
+
+#include "../machine/pte.h"
+
+#include "param.h"
+#include "systm.h"
+#include "map.h"
+#include "buf.h"
+#include "vm.h"
+#include "dir.h"
+#include "user.h"
+#include "proc.h"
+#include "conf.h"
+#include "dk.h"
+#include "kernel.h"
+
+#include "../vax/cpu.h"
+#include "../vax/mtpr.h"
+#include "../vax/nexus.h"
+#include "ubareg.h"
+#include "ubavar.h"
+
+#if defined(VAX780) || defined(VAX8600)
 char   ubasr_bits[] = UBASR_BITS;
 #endif
 
 char   ubasr_bits[] = UBASR_BITS;
 #endif
 
+#define        spluba  spl7            /* IPL 17 */
+
 /*
  * Do transfer on device argument.  The controller
  * and uba involved are implied by the device.
 /*
  * Do transfer on device argument.  The controller
  * and uba involved are implied by the device.
@@ -42,7 +53,7 @@ ubago(ui)
        register int s, unit;
 
        uh = &uba_hd[um->um_ubanum];
        register int s, unit;
 
        uh = &uba_hd[um->um_ubanum];
-       s = spl6();
+       s = spluba();
        if (um->um_driver->ud_xclu && uh->uh_users > 0 || uh->uh_xclu)
                goto rwait;
        um->um_ubinfo = ubasetup(um->um_ubanum, um->um_tab.b_actf->b_actf,
        if (um->um_driver->ud_xclu && uh->uh_users > 0 || uh->uh_xclu)
                goto rwait;
        um->um_ubinfo = ubasetup(um->um_ubanum, um->um_tab.b_actf->b_actf,
@@ -102,39 +113,48 @@ ubasetup(uban, bp, flags)
        struct buf *bp;
 {
        register struct uba_hd *uh = &uba_hd[uban];
        struct buf *bp;
 {
        register struct uba_hd *uh = &uba_hd[uban];
-       register int temp, i;
+       int pfnum, temp;
        int npf, reg, bdp;
        unsigned v;
        register struct pte *pte, *io;
        struct proc *rp;
        int a, o, ubinfo;
 
        int npf, reg, bdp;
        unsigned v;
        register struct pte *pte, *io;
        struct proc *rp;
        int a, o, ubinfo;
 
-#if VAX7ZZ
-       if (cpu == VAX_7ZZ)
+#if VAX730
+       if (cpu == VAX_730)
                flags &= ~UBA_NEEDBDP;
 #endif
        v = btop(bp->b_un.b_addr);
        o = (int)bp->b_un.b_addr & PGOFSET;
        npf = btoc(bp->b_bcount + o) + 1;
                flags &= ~UBA_NEEDBDP;
 #endif
        v = btop(bp->b_un.b_addr);
        o = (int)bp->b_un.b_addr & PGOFSET;
        npf = btoc(bp->b_bcount + o) + 1;
-       a = spl6();
-       while ((reg = rmalloc(uh->uh_map, npf)) == 0) {
+       a = spluba();
+       while ((reg = rmalloc(uh->uh_map, (long)npf)) == 0) {
                if (flags & UBA_CANTWAIT) {
                        splx(a);
                        return (0);
                }
                uh->uh_mrwant++;
                if (flags & UBA_CANTWAIT) {
                        splx(a);
                        return (0);
                }
                uh->uh_mrwant++;
-               sleep((caddr_t)uh->uh_map, PSWP);
+               sleep((caddr_t)&uh->uh_mrwant, PSWP);
+       }
+       if ((flags & UBA_NEED16) && reg + npf > 128) {
+               /*
+                * Could hang around and try again (if we can ever succeed).
+                * Won't help any current device...
+                */
+               rmfree(uh->uh_map, (long)npf, (long)reg);
+               splx(a);
+               return (0);
        }
        bdp = 0;
        if (flags & UBA_NEEDBDP) {
        }
        bdp = 0;
        if (flags & UBA_NEEDBDP) {
-               while ((bdp = ffs(uh->uh_bdpfree)) == 0) {
+               while ((bdp = ffs((long)uh->uh_bdpfree)) == 0) {
                        if (flags & UBA_CANTWAIT) {
                        if (flags & UBA_CANTWAIT) {
-                               rmfree(uh->uh_map, npf, reg);
+                               rmfree(uh->uh_map, (long)npf, (long)reg);
                                splx(a);
                                return (0);
                        }
                        uh->uh_bdpwant++;
                                splx(a);
                                return (0);
                        }
                        uh->uh_bdpwant++;
-                       sleep((caddr_t)uh->uh_map, PSWP);
+                       sleep((caddr_t)&uh->uh_bdpwant, PSWP);
                }
                uh->uh_bdpfree &= ~(1 << (bdp-1));
        } else if (flags & UBA_HAVEBDP)
                }
                uh->uh_bdpfree &= ~(1 << (bdp-1));
        } else if (flags & UBA_HAVEBDP)
@@ -142,31 +162,25 @@ ubasetup(uban, bp, flags)
        splx(a);
        reg--;
        ubinfo = (bdp << 28) | (npf << 18) | (reg << 9) | o;
        splx(a);
        reg--;
        ubinfo = (bdp << 28) | (npf << 18) | (reg << 9) | o;
-       io = &uh->uh_uba->uba_map[reg];
        temp = (bdp << 21) | UBAMR_MRV;
        temp = (bdp << 21) | UBAMR_MRV;
-       rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc;
        if (bdp && (o & 01))
                temp |= UBAMR_BO;
        if (bdp && (o & 01))
                temp |= UBAMR_BO;
-       if (bp->b_flags & B_UAREA) {
-               for (i = UPAGES - bp->b_bcount / NBPG; i < UPAGES; i++) {
-                       if (rp->p_addr[i].pg_pfnum == 0)
-                               panic("uba: zero upage");
-                       *(int *)io++ = rp->p_addr[i].pg_pfnum | temp;
-               }
-       } else if ((bp->b_flags & B_PHYS) == 0) {
+       rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc;
+       if ((bp->b_flags & B_PHYS) == 0)
                pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)];
                pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)];
-               while (--npf != 0)
-                       *(int *)io++ = pte++->pg_pfnum | temp;
-       } else {
-               if (bp->b_flags & B_PAGET)
-                       pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
-               else
-                       pte = vtopte(rp, v);
-               while (--npf != 0) {
-                       if (pte->pg_pfnum == 0)
-                               panic("uba zero uentry");
-                       *(int *)io++ = pte++->pg_pfnum | temp;
-               }
+       else if (bp->b_flags & B_UAREA)
+               pte = &rp->p_addr[v];
+       else if (bp->b_flags & B_PAGET)
+               pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
+       else
+               pte = vtopte(rp, v);
+       io = &uh->uh_uba->uba_map[reg];
+       while (--npf != 0) {
+               pfnum = pte->pg_pfnum;
+               if (pfnum == 0)
+                       panic("uba zero uentry");
+               pte++;
+               *(int *)io++ = pfnum | temp;
        }
        *(int *)io++ = 0;
        return (ubinfo);
        }
        *(int *)io++ = 0;
        return (ubinfo);
@@ -205,7 +219,7 @@ ubarelse(uban, amr)
         * Carefully see if we should release the space, since
         * it may be released asynchronously at uba reset time.
         */
         * Carefully see if we should release the space, since
         * it may be released asynchronously at uba reset time.
         */
-       s = spl6();
+       s = spluba();
        mr = *amr;
        if (mr == 0) {
                /*
        mr = *amr;
        if (mr == 0) {
                /*
@@ -216,11 +230,11 @@ ubarelse(uban, amr)
                return;
        }
        *amr = 0;
                return;
        }
        *amr = 0;
-       splx(s);                /* let interrupts in, we're safe for a while */
        bdp = (mr >> 28) & 0x0f;
        if (bdp) {
                switch (cpu) {
        bdp = (mr >> 28) & 0x0f;
        if (bdp) {
                switch (cpu) {
-#if VAX780
+#if defined(VAX780) || defined(VAX8600)
+               case VAX_8600:
                case VAX_780:
                        uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE;
                        break;
                case VAX_780:
                        uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE;
                        break;
@@ -235,18 +249,18 @@ ubarelse(uban, amr)
                uh->uh_bdpfree |= 1 << (bdp-1);         /* atomic */
                if (uh->uh_bdpwant) {
                        uh->uh_bdpwant = 0;
                uh->uh_bdpfree |= 1 << (bdp-1);         /* atomic */
                if (uh->uh_bdpwant) {
                        uh->uh_bdpwant = 0;
-                       wakeup((caddr_t)uh->uh_map);
+                       wakeup((caddr_t)&uh->uh_bdpwant);
                }
        }
        /*
         * Put back the registers in the resource map.
                }
        }
        /*
         * Put back the registers in the resource map.
-        * The map code must not be reentered, so we do this
-        * at high ipl.
+        * The map code must not be reentered,
+        * nor can the registers be freed twice.
+        * Unblock interrupts once this is done.
         */
        npf = (mr >> 18) & 0x3ff;
        reg = ((mr >> 9) & 0x1ff) + 1;
         */
        npf = (mr >> 18) & 0x3ff;
        reg = ((mr >> 9) & 0x1ff) + 1;
-       s = spl6();
-       rmfree(uh->uh_map, npf, reg);
+       rmfree(uh->uh_map, (long)npf, (long)reg);
        splx(s);
 
        /*
        splx(s);
 
        /*
@@ -256,7 +270,7 @@ ubarelse(uban, amr)
         */
        if (uh->uh_mrwant) {
                uh->uh_mrwant = 0;
         */
        if (uh->uh_mrwant) {
                uh->uh_mrwant = 0;
-               wakeup((caddr_t)uh->uh_map);
+               wakeup((caddr_t)&uh->uh_mrwant);
        }
        while (uh->uh_actf && ubago(uh->uh_actf))
                ;
        }
        while (uh->uh_actf && ubago(uh->uh_actf))
                ;
@@ -269,7 +283,8 @@ ubapurge(um)
        register int bdp = (um->um_ubinfo >> 28) & 0x0f;
 
        switch (cpu) {
        register int bdp = (um->um_ubinfo >> 28) & 0x0f;
 
        switch (cpu) {
-#if VAX780
+#if defined(VAX780) || defined(VAX8600)
+       case VAX_8600:
        case VAX_780:
                uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE;
                break;
        case VAX_780:
                uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE;
                break;
@@ -282,6 +297,30 @@ ubapurge(um)
        }
 }
 
        }
 }
 
+ubainitmaps(uhp)
+       register struct uba_hd *uhp;
+{
+
+       rminit(uhp->uh_map, (long)NUBMREG, (long)1, "uba", UAMSIZ);
+       switch (cpu) {
+#if defined(VAX780) || defined(VAX8600)
+       case VAX_8600:
+       case VAX_780:
+               uhp->uh_bdpfree = (1<<NBDP780) - 1;
+               break;
+#endif
+#if VAX750
+       case VAX_750:
+               uhp->uh_bdpfree = (1<<NBDP750) - 1;
+               break;
+#endif
+#if VAX730
+       case VAX_730:
+               break;
+#endif
+       }
+}
+
 /*
  * Generate a reset on uba number uban.  Then
  * call each device in the character device table,
 /*
  * Generate a reset on uba number uban.  Then
  * call each device in the character device table,
@@ -294,23 +333,22 @@ ubareset(uban)
        register struct uba_hd *uh = &uba_hd[uban];
        int s;
 
        register struct uba_hd *uh = &uba_hd[uban];
        int s;
 
-       s = spl6();
+       s = spluba();
        uh->uh_users = 0;
        uh->uh_zvcnt = 0;
        uh->uh_xclu = 0;
        uh->uh_users = 0;
        uh->uh_zvcnt = 0;
        uh->uh_xclu = 0;
-       uh->uh_hangcnt = 0;
        uh->uh_actf = uh->uh_actl = 0;
        uh->uh_bdpwant = 0;
        uh->uh_mrwant = 0;
        uh->uh_actf = uh->uh_actl = 0;
        uh->uh_bdpwant = 0;
        uh->uh_mrwant = 0;
+       ubainitmaps(uh);
        wakeup((caddr_t)&uh->uh_bdpwant);
        wakeup((caddr_t)&uh->uh_mrwant);
        printf("uba%d: reset", uban);
        ubainit(uh->uh_uba);
        wakeup((caddr_t)&uh->uh_bdpwant);
        wakeup((caddr_t)&uh->uh_mrwant);
        printf("uba%d: reset", uban);
        ubainit(uh->uh_uba);
-       for (cdp = cdevsw; cdp->d_open; cdp++)
+       ubameminit(uban);
+       for (cdp = cdevsw; cdp < cdevsw + nchrdev; cdp++)
                (*cdp->d_reset)(uban);
                (*cdp->d_reset)(uban);
-#ifdef INET
        ifubareset(uban);
        ifubareset(uban);
-#endif
        printf("\n");
        splx(s);
 }
        printf("\n");
        splx(s);
 }
@@ -328,7 +366,8 @@ ubainit(uba)
 {
 
        switch (cpu) {
 {
 
        switch (cpu) {
-#if VAX780
+#if defined(VAX780) || defined(VAX8600)
+       case VAX_8600:
        case VAX_780:
                uba->uba_cr = UBACR_ADINIT;
                uba->uba_cr = UBACR_IFS|UBACR_BRIE|UBACR_USEFIE|UBACR_SUEFIE;
        case VAX_780:
                uba->uba_cr = UBACR_ADINIT;
                uba->uba_cr = UBACR_IFS|UBACR_BRIE|UBACR_USEFIE|UBACR_SUEFIE;
@@ -339,10 +378,10 @@ ubainit(uba)
 #if VAX750
        case VAX_750:
 #endif
 #if VAX750
        case VAX_750:
 #endif
-#if VAX7ZZ
-       case VAX_7ZZ:
+#if VAX730
+       case VAX_730:
 #endif
 #endif
-#if defined(VAX750) || defined(VAX7ZZ)
+#if defined(VAX750) || defined(VAX730)
                mtpr(IUR, 0);
                /* give devices time to recover from power fail */
 /* THIS IS PROBABLY UNNECESSARY */
                mtpr(IUR, 0);
                /* give devices time to recover from power fail */
 /* THIS IS PROBABLY UNNECESSARY */
@@ -353,78 +392,50 @@ ubainit(uba)
        }
 }
 
        }
 }
 
-#if VAX780
-/*
- * Check to make sure the UNIBUS adaptor is not hung,
- * with an interrupt in the register to be presented,
- * but not presenting it for an extended period (5 seconds).
- */
-unhang()
-{
-       register int uban;
-
-       for (uban = 0; uban < numuba; uban++) {
-               register struct uba_hd *uh = &uba_hd[uban];
-               register struct uba_regs *up = uh->uh_uba;
-
-               if (up->uba_sr == 0)
-                       return;
-               up->uba_sr = UBASR_CRD|UBASR_LEB;
-               uh->uh_hangcnt++;
-               if (uh->uh_hangcnt > 5*hz) {
-                       uh->uh_hangcnt = 0;
-                       printf("uba%d: hung\n", uban);
-                       ubareset(uban);
-               }
-       }
-}
-
-/*
- * This is a timeout routine which decrements the ``i forgot to
- * interrupt'' counts, on an 11/780.  This prevents slowly growing
- * counts from causing a UBA reset since we are interested only
- * in hang situations.
- */
-ubawatch()
-{
-       register struct uba_hd *uh;
-       register int uban;
-
-       if (panicstr)
-               return;
-       for (uban = 0; uban < numuba; uban++) {
-               uh = &uba_hd[uban];
-               if (uh->uh_hangcnt)
-                       uh->uh_hangcnt--;
-       }
-}
-
+#if defined(VAX780) || defined(VAX8600)
 int    ubawedgecnt = 10;
 int    ubacrazy = 500;
 int    ubawedgecnt = 10;
 int    ubacrazy = 500;
+int    zvcnt_max = 5000;       /* in 8 sec */
 /*
 /*
- * This routine is called by the locore code to
- * process a UBA error on an 11/780.  The arguments are passed
+ * This routine is called by the locore code to process a UBA
+ * error on an 11/780 or 8600.  The arguments are passed
  * on the stack, and value-result (through some trickery).
  * In particular, the uvec argument is used for further
  * uba processing so the result aspect of it is very important.
  * It must not be declared register.
  */
 /*ARGSUSED*/
  * on the stack, and value-result (through some trickery).
  * In particular, the uvec argument is used for further
  * uba processing so the result aspect of it is very important.
  * It must not be declared register.
  */
 /*ARGSUSED*/
-ubaerror(uban, uh, xx, uvec, uba)
+ubaerror(uban, uh, ipl, uvec, uba)
        register int uban;
        register struct uba_hd *uh;
        register int uban;
        register struct uba_hd *uh;
-       int uvec;
+       int ipl, uvec;
        register struct uba_regs *uba;
 {
        register sr, s;
 
        if (uvec == 0) {
        register struct uba_regs *uba;
 {
        register sr, s;
 
        if (uvec == 0) {
-               uh->uh_zvcnt++;
-               if (uh->uh_zvcnt > 250000) {
-                       printf("uba%d: too many zero vectors\n");
+               /*
+                * Declare dt as unsigned so that negative values
+                * are handled as >8 below, in case time was set back.
+                */
+               u_long  dt = time.tv_sec - uh->uh_zvtime;
+
+               uh->uh_zvtotal++;
+               if (dt > 8) {
+                       uh->uh_zvtime = time.tv_sec;
+                       uh->uh_zvcnt = 0;
+               }
+               if (++uh->uh_zvcnt > zvcnt_max) {
+                       printf("uba%d: too many zero vectors (%d in <%d sec)\n",
+                               uban, uh->uh_zvcnt, dt + 1);
+                       printf("\tIPL 0x%x\n\tcnfgr: %b  Adapter Code: 0x%x\n",
+                               ipl, uba->uba_cnfgr&(~0xff), UBACNFGR_BITS,
+                               uba->uba_cnfgr&0xff);
+                       printf("\tsr: %b\n\tdcr: %x (MIC %sOK)\n",
+                               uba->uba_sr, ubasr_bits, uba->uba_dcr,
+                               (uba->uba_dcr&0x8000000)?"":"NOT ");
                        ubareset(uban);
                }
                        ubareset(uban);
                }
-               uvec = 0;
                return;
        }
        if (uba->uba_cnfgr & NEX_CFGFLT) {
                return;
        }
        if (uba->uba_cnfgr & NEX_CFGFLT) {
@@ -436,7 +447,7 @@ ubaerror(uban, uh, xx, uvec, uba)
                return;
        }
        sr = uba->uba_sr;
                return;
        }
        sr = uba->uba_sr;
-       s = spl7();
+       s = spluba();
        printf("uba%d: uba error sr=%b fmer=%x fubar=%o\n",
            uban, uba->uba_sr, ubasr_bits, uba->uba_fmer, 4*uba->uba_fubar);
        splx(s);
        printf("uba%d: uba error sr=%b fmer=%x fubar=%o\n",
            uban, uba->uba_sr, ubasr_bits, uba->uba_fmer, 4*uba->uba_fubar);
        splx(s);
@@ -454,67 +465,130 @@ ubaerror(uban, uh, xx, uvec, uba)
 }
 #endif
 
 }
 #endif
 
-#ifdef notdef
 /*
 /*
- * This routine allows remapping of previously
- * allocated UNIBUS bdp and map resources
- * onto different memory addresses.
- * It should only be used by routines which need
- * small fixed length mappings for long periods of time
- * (like the ARPANET ACC IMP interface).
- * It only maps kernel addresses.
+ * Look for devices with unibus memory, allow them to configure, then disable
+ * map registers as necessary.  Called during autoconfiguration and ubareset.
+ * The device ubamem routine returns 0 on success, 1 on success if it is fully
+ * configured (has no csr or interrupt, so doesn't need to be probed),
+ * and -1 on failure.
  */
  */
-ubaremap(uban, ubinfo, addr)
-       int uban;
-       register unsigned ubinfo;
-       caddr_t addr;
+ubameminit(uban)
 {
 {
+       register struct uba_device *ui;
        register struct uba_hd *uh = &uba_hd[uban];
        register struct uba_hd *uh = &uba_hd[uban];
-       register struct pte *pte, *io;
-       register int temp, bdp;
-       int npf, o;
-
-       o = (int)addr & PGOFSET;
-       bdp = (ubinfo >> 28) & 0xf;
-       npf = (ubinfo >> 18) & 0x3ff;
-       io = &uh->uh_uba->uba_map[(ubinfo >> 9) & 0x1ff];
-       temp = (bdp << 21) | UBAMR_MRV;
+       caddr_t umembase = umem[uban] + 0x3e000, addr;
+#define        ubaoff(off)     ((int)(off) & 0x1fff)
 
 
+       uh->uh_lastmem = 0;
+       for (ui = ubdinit; ui->ui_driver; ui++) {
+               if (ui->ui_ubanum != uban && ui->ui_ubanum != '?')
+                       continue;
+               if (ui->ui_driver->ud_ubamem) {
+                       /*
+                        * During autoconfiguration, need to fudge ui_addr.
+                        */
+                       addr = ui->ui_addr;
+                       ui->ui_addr = umembase + ubaoff(addr);
+                       switch ((*ui->ui_driver->ud_ubamem)(ui, uban)) {
+                       case 1:
+                               ui->ui_alive = 1;
+                               /* FALLTHROUGH */
+                       case 0:
+                               ui->ui_ubanum = uban;
+                               break;
+                       }
+                       ui->ui_addr = addr;
+               }
+       }
+#if defined(VAX780) || defined(VAX8600)
        /*
        /*
-        * If using buffered data path initiate purge
-        * of old data and set byte offset bit if next
-        * transfer will be from odd address.
+        * On a 780, throw away any map registers disabled by rounding
+        * the map disable in the configuration register
+        * up to the next 8K boundary, or below the last unibus memory.
         */
         */
-       if (bdp) {
-               switch (cpu) {
-#if VAX780
-               case VAX_780:
-                       uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE;
-                       break;
-#endif
-#if VAX750
-               case VAX_750:
-                       uh->uh_uba->uba_dpr[bdp] |=
-                           UBADPR_PURGE|UBADPR_NXM|UBADPR_UCE;
-                       break;
+       if ((cpu == VAX_780) || (cpu == VAX_8600)) {
+               register i;
+
+               i = btop(((uh->uh_lastmem + 8191) / 8192) * 8192);
+               while (i)
+                       (void) rmget(uh->uh_map, 1, i--);
+       }
 #endif
 #endif
+}
+
+/*
+ * Allocate UNIBUS memory.  Allocates and initializes
+ * sufficient mapping registers for access.  On a 780,
+ * the configuration register is setup to disable UBA
+ * response on DMA transfers to addresses controlled
+ * by the disabled mapping registers.
+ * On a 780, should only be called from ubameminit, or in ascending order
+ * from 0 with 8K-sized and -aligned addresses; freeing memory that isn't
+ * the last unibus memory would free unusable map registers.
+ * Doalloc is 1 to allocate, 0 to deallocate.
+ */
+ubamem(uban, addr, npg, doalloc)
+       int uban, addr, npg, doalloc;
+{
+       register struct uba_hd *uh = &uba_hd[uban];
+       register int a;
+       int s;
+
+       a = (addr >> 9) + 1;
+       s = spluba();
+       if (doalloc)
+               a = rmget(uh->uh_map, npg, a);
+       else
+               rmfree(uh->uh_map, (long)npg, (long)a);
+       splx(s);
+       if (a) {
+               register int i, *m;
+
+               m = (int *)&uh->uh_uba->uba_map[a - 1];
+               for (i = 0; i < npg; i++)
+                       *m++ = 0;       /* All off, especially 'valid' */
+               i = addr + npg * 512;
+               if (doalloc && i > uh->uh_lastmem)
+                       uh->uh_lastmem = i;
+               else if (doalloc == 0 && i == uh->uh_lastmem)
+                       uh->uh_lastmem = addr;
+#if defined(VAX780) || defined(VAX8600)
+               /*
+                * On a 780, set up the map register disable
+                * field in the configuration register.  Beware
+                * of callers that request memory ``out of order''
+                * or in sections other than 8K multiples.
+                * Ubameminit handles such requests properly, however.
+                */
+               if ((cpu == VAX_780) || (cpu == VAX_8600)) {
+                       i = uh->uh_uba->uba_cr &~ 0x7c000000;
+                       i |= ((uh->uh_lastmem + 8191) / 8192) << 26;
+                       uh->uh_uba->uba_cr = i;
                }
                }
-               if (o & 1)
-                       temp |= UBAMR_BO;
+#endif
        }
        }
+       return (a);
+}
 
 
-       /*
-        * Set up the map registers, leaving an invalid reg
-        * at the end to guard against wild unibus transfers.
-        */
-       pte = &Sysmap[btop(((int)addr)&0x7fffffff)];
-       while (--npf != 0)
-               *(int *)io++ = pte++->pg_pfnum | temp;
-       *(int *)io = 0;
+#include "ik.h"
+#include "vs.h"
+#if NIK > 0 || NVS > 0
+/*
+ * Map a virtual address into users address space. Actually all we
+ * do is turn on the user mode write protection bits for the particular
+ * page of memory involved.
+ */
+maptouser(vaddress)
+       caddr_t vaddress;
+{
 
 
-       /*
-        * Return effective UNIBUS address.
-        */
-       return (ubinfo | o);
+       Sysmap[(((unsigned)(vaddress))-0x80000000) >> 9].pg_prot = (PG_UW>>27);
+}
+
+unmaptouser(vaddress)
+       caddr_t vaddress;
+{
+
+       Sysmap[(((unsigned)(vaddress))-0x80000000) >> 9].pg_prot = (PG_KW>>27);
 }
 #endif
 }
 #endif