Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/pc/dma.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname pc/dma.c 1991/0803
## diff -e /dev/null /n/bootesdump/1991/0803/sys/src/9/safari/dma.c
0a
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"

/*
 *  headland chip set for the safari.
 */
typedef struct DMAport	DMAport;
typedef struct DMA	DMA;
typedef struct DMAxfer	DMAxfer;

enum
{
	/*
	 *  the byte registers for DMA0 are all one byte apart
	 */
	Dma0=		0x00,
	Dma0status=	Dma0+0x8,	/* status port */
	Dma0reset=	Dma0+0xD,	/* reset port */

	/*
	 *  the byte registers for DMA1 are all two bytes apart (why?)
	 */
	Dma1=		0xC0,
	Dma1status=	Dma1+2*0x8,	/* status port */
	Dma1reset=	Dma1+2*0xD,	/* reset port */
};

/*
 *  state of a dma transfer
 */
struct DMAxfer
{
	Page	*pg;		/* page used by dma */
	void	*va;		/* virtual address destination/src */
	long	len;		/* bytes to be transferred */
	int	isread;
};

/*
 *  the dma controllers.  the first half of this structure specifies
 *  the I/O ports used by the DMA controllers.
 */
struct DMAport
{
	uchar	addr[4];	/* current address (4 channels) */
	uchar	count[4];	/* current count (4 channels) */
	uchar	page[4];	/* page registers (4 channels) */
	uchar	cmd;		/* command status register */
	uchar	req;		/* request registers */
	uchar	sbm;		/* single bit mask register */
	uchar	mode;		/* mode register */
	uchar	cbp;		/* clear byte pointer */
	uchar	mc;		/* master clear */
	uchar	cmask;		/* clear mask register */
	uchar	wam;		/* write all mask register bit */

	Lock;
};

struct DMA
{
	DMAport;
	Lock;
	DMAxfer	x[4];
};

DMA dma[2] = {
	{ 0x00, 0x02, 0x04, 0x06,
	  0x01, 0x03, 0x05, 0x07,
	  0x87, 0x83, 0x81, 0x82,
	  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
	{ 0xc0, 0xc6, 0xca, 0xce,
	  0xc4, 0xc8, 0xcc, 0xcf,
	  0x80, 0x8b, 0x89, 0x8a,
	  0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde },
};

/*
 *  setup a dma transfer.  if the destination is not in kernel
 *  memory, allocate a page for the transfer.
 *
 *  we assume BIOS has set up the command register before we
 *  are booted.
 *
 *  return the updated transfer length (we can't transfer across 64k
 *  boundaries)
 */
long
dmasetup(int chan, void *va, long len, int isread)
{
	DMA *dp;
	DMAxfer *xp;
	ulong pa;
	uchar mode;

	dp = &dma[(chan>>2)&1];
	chan = chan & 3;
	xp = &dp->x[chan];

	/*
	 *  if this isn't kernel memory (or crossing 64k boundary),
	 *  allocate a page for the DMA.
	 */
	pa = ((ulong)va) & ~KZERO;
	if(!isphys(va) || (pa&0xFFFF0000)!=((pa+len)&0xFFFF0000)){
		xp->pg = newpage(1, 0, 0);
		if(len > BY2PG)
			len = BY2PG;
		if(!isread)
			memmove((void*)(KZERO|xp->pg->pa), va, len);
		xp->va = va;
		xp->len = len;
		xp->isread = isread;
		pa = xp->pg->pa;
	}

	/*
	 * this setup must be atomic
	 */
	lock(dp);
	outb(dp->cbp, 0);		/* set count & address to their first byte */
	mode = (isread ? 0x44 : 0x48) | chan;
	outb(dp->mode, mode);		/* single mode dma (give CPU a chance at mem) */
	outb(dp->addr[chan], pa);		/* set address */
	outb(dp->addr[chan], pa>>8);
	outb(dp->page[chan], pa>>16);
	outb(dp->count[chan], len-1);		/* set count */
	outb(dp->count[chan], (len-1)>>8);
	outb(dp->sbm, chan);		/* enable the channel */
	unlock(dp);

	return len;
}

/*
 *  this must be called after a dma has been completed.
 *
 *  if a page has been allocated for the dma,
 *  copy the data into the actual destination
 *  and free the page.
 */
void
dmaend(int chan)
{
	DMA *dp;
	DMAxfer *xp;
	ulong addr;
	uchar mode;

	dp = &dma[(chan>>2)&1];
	chan = chan & 3;

	/*
	 *  disable the channel
	 */
	lock(dp);
	outb(dp->sbm, 4|chan);
	unlock(dp);

	xp = &dp->x[chan];
	if(xp->pg == 0)
		return;

	/*
	 *  copy out of temporary page
	 */
	memmove(xp->va, (void*)(KZERO|xp->pg->pa), xp->len);
	putpage(xp->pg);
	xp->pg = 0;
}
.
## diffname pc/dma.c 1991/0925
## diff -e /n/bootesdump/1991/0803/sys/src/9/safari/dma.c /n/bootesdump/1991/0925/sys/src/9/safari/dma.c
171,172c
	xp->len = 0;
.
164c
	if(xp->len == 0)
.
118c
	} else
		xp->len = 0;
.
109c
		if(xp->pg == 0)
			xp->pg = newpage(1, 0, 0);
.
## diffname pc/dma.c 1992/0321
## diff -e /n/bootesdump/1991/0925/sys/src/9/safari/dma.c /n/bootesdump/1992/0321/sys/src/9/safari/dma.c
2c
#include	"../port/lib.h"
.
## diffname pc/dma.c 1992/0711
## diff -e /n/bootesdump/1992/0321/sys/src/9/safari/dma.c /n/bootesdump/1992/0711/sys/src/9/safari/dma.c
152,153d
## diffname pc/dma.c 1992/1013
## diff -e /n/bootesdump/1992/0808/sys/src/9/safari/dma.c /n/bootesdump/1992/1013/sys/src/9/pc/dma.c
108c
	if((((ulong)va)&0xF0000000) != KZERO
	|| (pa&0xFFFF0000) != ((pa+len)&0xFFFF0000)){
.
## diffname pc/dma.c 1993/0915
## diff -e /n/bootesdump/1992/1013/sys/src/9/pc/dma.c /n/fornaxdump/1993/0915/sys/src/brazil/pc/dma.c
171c
	memmove(xp->va, (void*)(xp->pg.va), xp->len);
.
119c
		pa = xp->pg.pa;
.
115c
			memmove((void*)(xp->pg.va), va, len);
.
109,111c
	|| (pa&0xFFFF0000) != ((pa+len)&0xFFFF0000)
	|| pa > 16*MB){
.
107c
	pa = PADDR(va);
.
104,105c
	 *  if this isn't kernel memory or crossing 64k boundary or above 16 meg
	 *  use the allocated low memory page.
.
81a
 *  DMA must be in the first 16 meg.  This gets called early by main() to
 *  ensure that.
 */
void
dmainit(void)
{
	int i, chan;
	DMA *dp;
	DMAxfer *xp;

	for(i = 0; i < 2; i++){
		dp = &dma[i];
		for(chan = 0; chan < 4; chan++){
			xp = &dp->x[chan];
			xp->pg.pa = (ulong)xspanalloc(BY2PG, BY2PG, 0);
			xp->pg.va = KZERO|xp->pg.pa;
		}
	}
}

/*
.
36c
	Page	pg;		/* page used by dma */
.
## diffname pc/dma.c 1994/1202
## diff -e /n/fornaxdump/1993/0915/sys/src/brazil/pc/dma.c /n/fornaxdump/1994/1202/sys/src/brazil/pc/dma.c
185c
	if(xp->len == 0 || xp->isread)
.
## diffname pc/dma.c 1994/1206
## diff -e /n/fornaxdump/1994/1202/sys/src/brazil/pc/dma.c /n/fornaxdump/1994/1206/sys/src/brazil/pc/dma.c
185c
	if(xp->len == 0 || !xp->isread)
.
## diffname pc/dma.c 1995/0214
## diff -e /n/fornaxdump/1994/1206/sys/src/brazil/pc/dma.c /n/fornaxdump/1995/0214/sys/src/brazil/pc/dma.c
182c
	iunlock(dp);
.
180c
	ilock(dp);
.
156c
	iunlock(dp);
.
153,154c
	outb(dp->cbp, 0);		/* set count & address to their first byte */
	outb(dp->addr[chan], pa>>dp->shift);		/* set address */
	outb(dp->addr[chan], pa>>(8+dp->shift));
	outb(dp->count[chan], (len>>dp->shift)-1);		/* set count */
	outb(dp->count[chan], ((len>>dp->shift)-1)>>8);
.
150,151d
146,147c
	ilock(dp);
.
97a
			xp->len = 0;
			xp->isread = 0;
.
74,78c
	  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
	 0 },

	{ 0xc0, 0xc4, 0xc8, 0xcc,
	  0xc2, 0xc6, 0xca, 0xce,
	  0x8f, 0x8b, 0x89, 0x8a,
	  0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
	 1 },
.
65a
	int	shift;
.
59,60d
## diffname pc/dma.c 1995/0502
## diff -e /n/fornaxdump/1995/0214/sys/src/brazil/pc/dma.c /n/fornaxdump/1995/0502/sys/src/brazil/pc/dma.c
164a
int
dmadone(int chan)
{
	DMA *dp;

	dp = &dma[(chan>>2)&1];
	chan = chan & 3;

	return inb(dp->cmd) & (1<<chan);
}

.
## diffname pc/dma.c 1995/0701
## diff -e /n/fornaxdump/1995/0502/sys/src/brazil/pc/dma.c /n/fornaxdump/1995/0701/sys/src/brazil/pc/dma.c
208a

void
dmaemode(int chan, int value)
{
	if(chan < 4)
		outb(0x40b, value|chan);
	else
		outb(0x4d6, value|chan);

}
.
## diffname pc/dma.c 1996/0607
## diff -e /n/fornaxdump/1995/0701/sys/src/brazil/pc/dma.c /n/fornaxdump/1996/0607/sys/src/brazil/pc/dma.c
208,217d
206c
	memmove(xp->va, xp->bva, xp->len);
.
152c
	outb(dp->mode, mode);	/* single mode dma (give CPU a chance at mem) */
.
143,144c
		pa = xp->bpa;
	}
	else
.
139c
			memmove(xp->bva, va, len);
.
135c
	|| pa > 16*MB) {
.
122a
	DMAxfer *xp;
.
120d
98,99c
			xp->bva = xspanalloc(BY2PG, BY2PG, 0);
			xp->bpa = PADDR(xp->bva);
.
36,37c
	ulong	bpa;		/* bounce buffer physical address */
	void*	bva;		/* bounce buffer virtual address */
	void*	va;		/* virtual address destination/src */
.
7,9d
## diffname pc/dma.c 1997/0327
## diff -e /n/fornaxdump/1996/0607/sys/src/brazil/pc/dma.c /n/emeliedump/1997/0327/sys/src/brazil/pc/dma.c
207a
 
int
dmacount(int chan)
{
	int     retval;
	DMA     *dp;
 
	dp = &dma[(chan>>2)&1];
	outb(dp->cbp, 0);
	retval = inb(dp->count[chan]);
	retval |= inb(dp->count[chan]) << 8;
	return((retval<<dp->shift)+1);
}
.
## diffname pc/dma.c 1997/0404
## diff -e /n/emeliedump/1997/0327/sys/src/brazil/pc/dma.c /n/emeliedump/1997/0404/sys/src/brazil/pc/dma.c
220a
 */
.
208c

/*
.
133a
		if(xp->bva == nil)
			return -1;
.
101a
	xp->bva = (void*)ROUND(v, BY2PG);
	xp->bpa = PADDR(xp->bva);
	xp->len = 0;
	xp->isread = 0;
.
92,100c
	dp = &dma[(chan>>2)&1];
	chan = chan & 3;
	xp = &dp->x[chan];
	if(xp->bva != nil)
		return;

	v = (ulong)xalloc(BY2PG+BY2PG);
	if(v == 0 || PADDR(v) >= 16*MB){
		print("dmainit: chan %d: 0x%luX out of range\n", chan, v);
		xfree((void*)v);
		v = 0;
.
90a
	ulong v;
.
88d
86c
dmainit(int chan)
.
82,83c
 *  DMA must be in the first 16MB.  This gets called early by the
 *  initialisation routines of any devices which require DMA to ensure
 *  the allocated bounce buffers are below the 16MB limit.
.
## diffname pc/dma.c 1998/1125
## diff -e /n/emeliedump/1997/0404/sys/src/brazil/pc/dma.c /n/emeliedump/1998/1125/sys/src/brazil/pc/dma.c
140c
	|| pa >= 16*MB) {
.
## diffname pc/dma.c 1999/0403
## diff -e /n/emeliedump/1998/1125/sys/src/brazil/pc/dma.c /n/emeliedump/1999/0403/sys/src/brazil/pc/dma.c
143,144c
		if(len > xp->blen)
			len = xp->blen;
.
135c
	 *  use the bounce buffer.
.
108a

	return 0;
.
106a
	if(xp->bpa >= 16*MB){
		xfree(xp->bva);		/* doesn't work... */
		xp->bva = nil;
		return 1;
	}
	xp->blen = maxtransfer;
.
105c

	xp->bva = xspanalloc(maxtransfer, BY2PG, 64*1024);
	if(xp->bva == nil)
		return 1;
.
96,103c
	if(xp->bva != nil){
		if(xp->blen < maxtransfer)
			return 1;
		return 0;
.
92a
	if(maxtransfer > 64*1024)
		maxtransfer = 64*1024;

.
91d
86,87c
int
dmainit(int chan, int maxtransfer)
.
34a
	int	blen;		/* bounce buffer length */
.
## diffname pc/dma.c 1999/0422
## diff -e /n/emeliedump/1999/0403/sys/src/brazil/pc/dma.c /n/emeliedump/1999/0422/sys/src/brazil/pc/dma.c
110c
		/*
		 * This will panic with the current
		 * implementation of xspanalloc().
		xfree(xp->bva);
		 */
.
## diffname pc/dma.c 1999/0713
## diff -e /n/emeliedump/1999/0422/sys/src/brazil/pc/dma.c /n/emeliedump/1999/0713/sys/src/brazil/pc/dma.c
92a
	if(ioalloc(0x00, 0x10, 0) < 0
	|| ioalloc(0x80, 0x10, 0) < 0
	|| ioalloc(0xd0, 0x10, 0) < 0)
		panic("dmainit");

.
## diffname pc/dma.c 1999/0714
## diff -e /n/emeliedump/1999/0713/sys/src/brazil/pc/dma.c /n/emeliedump/1999/0714/sys/src/brazil/pc/dma.c
93,96c
	if(once == 0){
		if(ioalloc(0x00, 0x10, 0, "dma") < 0
		|| ioalloc(0x80, 0x10, 0, "dma") < 0
		|| ioalloc(0xd0, 0x10, 0, "dma") < 0)
			panic("dmainit");
		once = 1;
	}
.
91a
	static int once;
.
11,27d

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].