xref: /freebsd/sys/x86/isa/isa_dma.c (revision d6b66397)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * William Jolitz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 /*
41  * code to manage AT bus
42  *
43  * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
44  * Fixed uninitialized variable problem and added code to deal
45  * with DMA page boundaries in isa_dmarangecheck().  Fixed word
46  * mode DMA count compution and reorganized DMA setup code in
47  * isa_dmastart()
48  */
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/bus.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/lock.h>
56 #include <sys/proc.h>
57 #include <sys/mutex.h>
58 #include <sys/module.h>
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <vm/pmap.h>
62 #include <isa/isareg.h>
63 #include <isa/isavar.h>
64 #include <isa/isa_dmareg.h>
65 
66 #define	ISARAM_END	0x1000000
67 
68 static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
69 
70 static caddr_t	dma_bouncebuf[8];
71 static u_int	dma_bouncebufsize[8];
72 static u_int8_t	dma_bounced = 0;
73 static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
74 static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
75 static u_int8_t dma_auto_mode = 0;
76 static struct mtx isa_dma_lock;
77 MTX_SYSINIT(isa_dma_lock, &isa_dma_lock, "isa DMA lock", MTX_DEF);
78 
79 #define VALID_DMA_MASK (7)
80 
81 /* high byte of address is stored in this port for i-th dma channel */
82 static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
83 
84 /*
85  * Setup a DMA channel's bounce buffer.
86  */
87 int
88 isa_dma_init(int chan, u_int bouncebufsize, int flag)
89 {
90 	void *buf;
91 	int contig;
92 
93 #ifdef DIAGNOSTIC
94 	if (chan & ~VALID_DMA_MASK)
95 		panic("isa_dma_init: channel out of range");
96 #endif
97 
98 
99 	/* Try malloc() first.  It works better if it works. */
100 	buf = malloc(bouncebufsize, M_DEVBUF, flag);
101 	if (buf != NULL) {
102 		if (isa_dmarangecheck(buf, bouncebufsize, chan) != 0) {
103 			free(buf, M_DEVBUF);
104 			buf = NULL;
105 		}
106 		contig = 0;
107 	}
108 
109 	if (buf == NULL) {
110 		buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
111 			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
112 		contig = 1;
113 	}
114 
115 	if (buf == NULL)
116 		return (ENOMEM);
117 
118 	mtx_lock(&isa_dma_lock);
119 	/*
120 	 * If a DMA channel is shared, both drivers have to call isa_dma_init
121 	 * since they don't know that the other driver will do it.
122 	 * Just return if we're already set up good.
123 	 * XXX: this only works if they agree on the bouncebuf size.  This
124 	 * XXX: is typically the case since they are multiple instances of
125 	 * XXX: the same driver.
126 	 */
127 	if (dma_bouncebuf[chan] != NULL) {
128 		if (contig)
129 			contigfree(buf, bouncebufsize, M_DEVBUF);
130 		else
131 			free(buf, M_DEVBUF);
132 		mtx_unlock(&isa_dma_lock);
133 		return (0);
134 	}
135 
136 	dma_bouncebufsize[chan] = bouncebufsize;
137 	dma_bouncebuf[chan] = buf;
138 
139 	mtx_unlock(&isa_dma_lock);
140 
141 	return (0);
142 }
143 
144 /*
145  * Register a DMA channel's usage.  Usually called from a device driver
146  * in open() or during its initialization.
147  */
148 int
149 isa_dma_acquire(chan)
150 	int chan;
151 {
152 #ifdef DIAGNOSTIC
153 	if (chan & ~VALID_DMA_MASK)
154 		panic("isa_dma_acquire: channel out of range");
155 #endif
156 
157 	mtx_lock(&isa_dma_lock);
158 	if (dma_inuse & (1 << chan)) {
159 		printf("isa_dma_acquire: channel %d already in use\n", chan);
160 		mtx_unlock(&isa_dma_lock);
161 		return (EBUSY);
162 	}
163 	dma_inuse |= (1 << chan);
164 	dma_auto_mode &= ~(1 << chan);
165 	mtx_unlock(&isa_dma_lock);
166 
167 	return (0);
168 }
169 
170 /*
171  * Unregister a DMA channel's usage.  Usually called from a device driver
172  * during close() or during its shutdown.
173  */
174 void
175 isa_dma_release(chan)
176 	int chan;
177 {
178 #ifdef DIAGNOSTIC
179 	if (chan & ~VALID_DMA_MASK)
180 		panic("isa_dma_release: channel out of range");
181 
182 	mtx_lock(&isa_dma_lock);
183 	if ((dma_inuse & (1 << chan)) == 0)
184 		printf("isa_dma_release: channel %d not in use\n", chan);
185 #else
186 	mtx_lock(&isa_dma_lock);
187 #endif
188 
189 	if (dma_busy & (1 << chan)) {
190 		dma_busy &= ~(1 << chan);
191 		/*
192 		 * XXX We should also do "dma_bounced &= (1 << chan);"
193 		 * because we are acting on behalf of isa_dmadone() which
194 		 * was not called to end the last DMA operation.  This does
195 		 * not matter now, but it may in the future.
196 		 */
197 	}
198 
199 	dma_inuse &= ~(1 << chan);
200 	dma_auto_mode &= ~(1 << chan);
201 
202 	mtx_unlock(&isa_dma_lock);
203 }
204 
205 /*
206  * isa_dmacascade(): program 8237 DMA controller channel to accept
207  * external dma control by a board.
208  */
209 void
210 isa_dmacascade(chan)
211 	int chan;
212 {
213 #ifdef DIAGNOSTIC
214 	if (chan & ~VALID_DMA_MASK)
215 		panic("isa_dmacascade: channel out of range");
216 #endif
217 
218 	mtx_lock(&isa_dma_lock);
219 	/* set dma channel mode, and set dma channel mode */
220 	if ((chan & 4) == 0) {
221 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
222 		outb(DMA1_SMSK, chan);
223 	} else {
224 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
225 		outb(DMA2_SMSK, chan & 3);
226 	}
227 	mtx_unlock(&isa_dma_lock);
228 }
229 
230 /*
231  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
232  * problems by using a bounce buffer.
233  */
234 void
235 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
236 {
237 	vm_paddr_t phys;
238 	int waport;
239 	caddr_t newaddr;
240 	int dma_range_checked;
241 
242 	dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
243 
244 #ifdef DIAGNOSTIC
245 	if (chan & ~VALID_DMA_MASK)
246 		panic("isa_dmastart: channel out of range");
247 
248 	if ((chan < 4 && nbytes > (1<<16))
249 	    || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1)))
250 		panic("isa_dmastart: impossible request");
251 
252 	mtx_lock(&isa_dma_lock);
253 	if ((dma_inuse & (1 << chan)) == 0)
254 		printf("isa_dmastart: channel %d not acquired\n", chan);
255 #else
256 	mtx_lock(&isa_dma_lock);
257 #endif
258 
259 #if 0
260 	/*
261 	 * XXX This should be checked, but drivers like ad1848 only call
262 	 * isa_dmastart() once because they use Auto DMA mode.  If we
263 	 * leave this in, drivers that do this will print this continuously.
264 	 */
265 	if (dma_busy & (1 << chan))
266 		printf("isa_dmastart: channel %d busy\n", chan);
267 #endif
268 
269 	dma_busy |= (1 << chan);
270 
271 	if (dma_range_checked) {
272 		if (dma_bouncebuf[chan] == NULL
273 		    || dma_bouncebufsize[chan] < nbytes)
274 			panic("isa_dmastart: bad bounce buffer");
275 		dma_bounced |= (1 << chan);
276 		newaddr = dma_bouncebuf[chan];
277 
278 		/* copy bounce buffer on write */
279 		if (!(flags & ISADMA_READ))
280 			bcopy(addr, newaddr, nbytes);
281 		addr = newaddr;
282 	}
283 
284 	/* translate to physical */
285 	phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
286 
287 	if (flags & ISADMA_RAW) {
288 	    dma_auto_mode |= (1 << chan);
289 	} else {
290 	    dma_auto_mode &= ~(1 << chan);
291 	}
292 
293 	if ((chan & 4) == 0) {
294 		/*
295 		 * Program one of DMA channels 0..3.  These are
296 		 * byte mode channels.
297 		 */
298 		/* set dma channel mode, and reset address ff */
299 
300 		/* If ISADMA_RAW flag is set, then use autoinitialise mode */
301 		if (flags & ISADMA_RAW) {
302 		  if (flags & ISADMA_READ)
303 			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
304 		  else
305 			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
306 		}
307 		else
308 		if (flags & ISADMA_READ)
309 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
310 		else
311 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
312 		outb(DMA1_FFC, 0);
313 
314 		/* send start address */
315 		waport =  DMA1_CHN(chan);
316 		outb(waport, phys);
317 		outb(waport, phys>>8);
318 		outb(dmapageport[chan], phys>>16);
319 
320 		/* send count */
321 		outb(waport + 1, --nbytes);
322 		outb(waport + 1, nbytes>>8);
323 
324 		/* unmask channel */
325 		outb(DMA1_SMSK, chan);
326 	} else {
327 		/*
328 		 * Program one of DMA channels 4..7.  These are
329 		 * word mode channels.
330 		 */
331 		/* set dma channel mode, and reset address ff */
332 
333 		/* If ISADMA_RAW flag is set, then use autoinitialise mode */
334 		if (flags & ISADMA_RAW) {
335 		  if (flags & ISADMA_READ)
336 			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
337 		  else
338 			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
339 		}
340 		else
341 		if (flags & ISADMA_READ)
342 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
343 		else
344 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
345 		outb(DMA2_FFC, 0);
346 
347 		/* send start address */
348 		waport = DMA2_CHN(chan - 4);
349 		outb(waport, phys>>1);
350 		outb(waport, phys>>9);
351 		outb(dmapageport[chan], phys>>16);
352 
353 		/* send count */
354 		nbytes >>= 1;
355 		outb(waport + 2, --nbytes);
356 		outb(waport + 2, nbytes>>8);
357 
358 		/* unmask channel */
359 		outb(DMA2_SMSK, chan & 3);
360 	}
361 	mtx_unlock(&isa_dma_lock);
362 }
363 
364 void
365 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
366 {
367 #ifdef DIAGNOSTIC
368 	if (chan & ~VALID_DMA_MASK)
369 		panic("isa_dmadone: channel out of range");
370 
371 	if ((dma_inuse & (1 << chan)) == 0)
372 		printf("isa_dmadone: channel %d not acquired\n", chan);
373 #endif
374 
375 	mtx_lock(&isa_dma_lock);
376 	if (((dma_busy & (1 << chan)) == 0) &&
377 	    (dma_auto_mode & (1 << chan)) == 0 )
378 		printf("isa_dmadone: channel %d not busy\n", chan);
379 
380 	if ((dma_auto_mode & (1 << chan)) == 0)
381 		outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
382 
383 	if (dma_bounced & (1 << chan)) {
384 		/* copy bounce buffer on read */
385 		if (flags & ISADMA_READ)
386 			bcopy(dma_bouncebuf[chan], addr, nbytes);
387 
388 		dma_bounced &= ~(1 << chan);
389 	}
390 	dma_busy &= ~(1 << chan);
391 	mtx_unlock(&isa_dma_lock);
392 }
393 
394 /*
395  * Check for problems with the address range of a DMA transfer
396  * (non-contiguous physical pages, outside of bus address space,
397  * crossing DMA page boundaries).
398  * Return true if special handling needed.
399  */
400 
401 static int
402 isa_dmarangecheck(caddr_t va, u_int length, int chan)
403 {
404 	vm_paddr_t phys, priorpage = 0;
405 	vm_offset_t endva;
406 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
407 
408 	endva = (vm_offset_t)round_page((vm_offset_t)va + length);
409 	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
410 		phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
411 		if (phys == 0)
412 			panic("isa_dmacheck: no physical page present");
413 		if (phys >= ISARAM_END)
414 			return (1);
415 		if (priorpage) {
416 			if (priorpage + PAGE_SIZE != phys)
417 				return (1);
418 			/* check if crossing a DMA page boundary */
419 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
420 				return (1);
421 		}
422 		priorpage = phys;
423 	}
424 	return (0);
425 }
426 
427 /*
428  * Query the progress of a transfer on a DMA channel.
429  *
430  * To avoid having to interrupt a transfer in progress, we sample
431  * each of the high and low databytes twice, and apply the following
432  * logic to determine the correct count.
433  *
434  * Reads are performed with interrupts disabled, thus it is to be
435  * expected that the time between reads is very small.  At most
436  * one rollover in the low count byte can be expected within the
437  * four reads that are performed.
438  *
439  * There are three gaps in which a rollover can occur :
440  *
441  * - read low1
442  *              gap1
443  * - read high1
444  *              gap2
445  * - read low2
446  *              gap3
447  * - read high2
448  *
449  * If a rollover occurs in gap1 or gap2, the low2 value will be
450  * greater than the low1 value.  In this case, low2 and high2 are a
451  * corresponding pair.
452  *
453  * In any other case, low1 and high1 can be considered to be correct.
454  *
455  * The function returns the number of bytes remaining in the transfer,
456  * or -1 if the channel requested is not active.
457  *
458  */
459 static int
460 isa_dmastatus_locked(int chan)
461 {
462 	u_long	cnt = 0;
463 	int	ffport, waport;
464 	u_long	low1, high1, low2, high2;
465 
466 	mtx_assert(&isa_dma_lock, MA_OWNED);
467 
468 	/* channel active? */
469 	if ((dma_inuse & (1 << chan)) == 0) {
470 		printf("isa_dmastatus: channel %d not active\n", chan);
471 		return(-1);
472 	}
473 	/* channel busy? */
474 
475 	if (((dma_busy & (1 << chan)) == 0) &&
476 	    (dma_auto_mode & (1 << chan)) == 0 ) {
477 	    printf("chan %d not busy\n", chan);
478 	    return -2 ;
479 	}
480 	if (chan < 4) {			/* low DMA controller */
481 		ffport = DMA1_FFC;
482 		waport = DMA1_CHN(chan) + 1;
483 	} else {			/* high DMA controller */
484 		ffport = DMA2_FFC;
485 		waport = DMA2_CHN(chan - 4) + 2;
486 	}
487 
488 	disable_intr();			/* no interrupts Mr Jones! */
489 	outb(ffport, 0);		/* clear register LSB flipflop */
490 	low1 = inb(waport);
491 	high1 = inb(waport);
492 	outb(ffport, 0);		/* clear again */
493 	low2 = inb(waport);
494 	high2 = inb(waport);
495 	enable_intr();			/* enable interrupts again */
496 
497 	/*
498 	 * Now decide if a wrap has tried to skew our results.
499 	 * Note that after TC, the count will read 0xffff, while we want
500 	 * to return zero, so we add and then mask to compensate.
501 	 */
502 	if (low1 >= low2) {
503 		cnt = (low1 + (high1 << 8) + 1) & 0xffff;
504 	} else {
505 		cnt = (low2 + (high2 << 8) + 1) & 0xffff;
506 	}
507 
508 	if (chan >= 4)			/* high channels move words */
509 		cnt *= 2;
510 	return(cnt);
511 }
512 
513 int
514 isa_dmastatus(int chan)
515 {
516 	int status;
517 
518 	mtx_lock(&isa_dma_lock);
519 	status = isa_dmastatus_locked(chan);
520 	mtx_unlock(&isa_dma_lock);
521 
522 	return (status);
523 }
524 
525 /*
526  * Reached terminal count yet ?
527  */
528 int
529 isa_dmatc(int chan)
530 {
531 
532 	if (chan < 4)
533 		return(inb(DMA1_STATUS) & (1 << chan));
534 	else
535 		return(inb(DMA2_STATUS) & (1 << (chan & 3)));
536 }
537 
538 /*
539  * Stop a DMA transfer currently in progress.
540  */
541 int
542 isa_dmastop(int chan)
543 {
544 	int status;
545 
546 	mtx_lock(&isa_dma_lock);
547 	if ((dma_inuse & (1 << chan)) == 0)
548 		printf("isa_dmastop: channel %d not acquired\n", chan);
549 
550 	if (((dma_busy & (1 << chan)) == 0) &&
551 	    ((dma_auto_mode & (1 << chan)) == 0)) {
552 		printf("chan %d not busy\n", chan);
553 		mtx_unlock(&isa_dma_lock);
554 		return -2 ;
555 	}
556 
557 	if ((chan & 4) == 0) {
558 		outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
559 	} else {
560 		outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
561 	}
562 
563 	status = isa_dmastatus_locked(chan);
564 
565 	mtx_unlock(&isa_dma_lock);
566 
567 	return (status);
568 }
569 
570 /*
571  * Attach to the ISA PnP descriptor for the AT DMA controller
572  */
573 static struct isa_pnp_id atdma_ids[] = {
574 	{ 0x0002d041 /* PNP0200 */, "AT DMA controller" },
575 	{ 0 }
576 };
577 
578 static int
579 atdma_probe(device_t dev)
580 {
581 	int result;
582 
583 	if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
584 		device_quiet(dev);
585 	return(result);
586 }
587 
588 static int
589 atdma_attach(device_t dev)
590 {
591 	return(0);
592 }
593 
594 static device_method_t atdma_methods[] = {
595 	/* Device interface */
596 	DEVMETHOD(device_probe,		atdma_probe),
597 	DEVMETHOD(device_attach,	atdma_attach),
598 	DEVMETHOD(device_detach,	bus_generic_detach),
599 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
600 	DEVMETHOD(device_suspend,	bus_generic_suspend),
601 	DEVMETHOD(device_resume,	bus_generic_resume),
602 	{ 0, 0 }
603 };
604 
605 static driver_t atdma_driver = {
606 	"atdma",
607 	atdma_methods,
608 	1,		/* no softc */
609 };
610 
611 static devclass_t atdma_devclass;
612 
613 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
614 DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0);
615 ISA_PNP_INFO(atdma_ids);
616