xref: /freebsd/sys/x86/isa/isa_dma.c (revision 42249ef2)
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(int chan)
150 {
151 #ifdef DIAGNOSTIC
152 	if (chan & ~VALID_DMA_MASK)
153 		panic("isa_dma_acquire: channel out of range");
154 #endif
155 
156 	mtx_lock(&isa_dma_lock);
157 	if (dma_inuse & (1 << chan)) {
158 		printf("isa_dma_acquire: channel %d already in use\n", chan);
159 		mtx_unlock(&isa_dma_lock);
160 		return (EBUSY);
161 	}
162 	dma_inuse |= (1 << chan);
163 	dma_auto_mode &= ~(1 << chan);
164 	mtx_unlock(&isa_dma_lock);
165 
166 	return (0);
167 }
168 
169 /*
170  * Unregister a DMA channel's usage.  Usually called from a device driver
171  * during close() or during its shutdown.
172  */
173 void
174 isa_dma_release(int chan)
175 {
176 #ifdef DIAGNOSTIC
177 	if (chan & ~VALID_DMA_MASK)
178 		panic("isa_dma_release: channel out of range");
179 
180 	mtx_lock(&isa_dma_lock);
181 	if ((dma_inuse & (1 << chan)) == 0)
182 		printf("isa_dma_release: channel %d not in use\n", chan);
183 #else
184 	mtx_lock(&isa_dma_lock);
185 #endif
186 
187 	if (dma_busy & (1 << chan)) {
188 		dma_busy &= ~(1 << chan);
189 		/*
190 		 * XXX We should also do "dma_bounced &= (1 << chan);"
191 		 * because we are acting on behalf of isa_dmadone() which
192 		 * was not called to end the last DMA operation.  This does
193 		 * not matter now, but it may in the future.
194 		 */
195 	}
196 
197 	dma_inuse &= ~(1 << chan);
198 	dma_auto_mode &= ~(1 << chan);
199 
200 	mtx_unlock(&isa_dma_lock);
201 }
202 
203 /*
204  * isa_dmacascade(): program 8237 DMA controller channel to accept
205  * external dma control by a board.
206  */
207 void
208 isa_dmacascade(int chan)
209 {
210 #ifdef DIAGNOSTIC
211 	if (chan & ~VALID_DMA_MASK)
212 		panic("isa_dmacascade: channel out of range");
213 #endif
214 
215 	mtx_lock(&isa_dma_lock);
216 	/* set dma channel mode, and set dma channel mode */
217 	if ((chan & 4) == 0) {
218 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
219 		outb(DMA1_SMSK, chan);
220 	} else {
221 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
222 		outb(DMA2_SMSK, chan & 3);
223 	}
224 	mtx_unlock(&isa_dma_lock);
225 }
226 
227 /*
228  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
229  * problems by using a bounce buffer.
230  */
231 void
232 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
233 {
234 	vm_paddr_t phys;
235 	int waport;
236 	caddr_t newaddr;
237 	int dma_range_checked;
238 
239 	dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
240 
241 #ifdef DIAGNOSTIC
242 	if (chan & ~VALID_DMA_MASK)
243 		panic("isa_dmastart: channel out of range");
244 
245 	if ((chan < 4 && nbytes > (1<<16))
246 	    || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1)))
247 		panic("isa_dmastart: impossible request");
248 
249 	mtx_lock(&isa_dma_lock);
250 	if ((dma_inuse & (1 << chan)) == 0)
251 		printf("isa_dmastart: channel %d not acquired\n", chan);
252 #else
253 	mtx_lock(&isa_dma_lock);
254 #endif
255 
256 #if 0
257 	/*
258 	 * XXX This should be checked, but drivers like ad1848 only call
259 	 * isa_dmastart() once because they use Auto DMA mode.  If we
260 	 * leave this in, drivers that do this will print this continuously.
261 	 */
262 	if (dma_busy & (1 << chan))
263 		printf("isa_dmastart: channel %d busy\n", chan);
264 #endif
265 
266 	dma_busy |= (1 << chan);
267 
268 	if (dma_range_checked) {
269 		if (dma_bouncebuf[chan] == NULL
270 		    || dma_bouncebufsize[chan] < nbytes)
271 			panic("isa_dmastart: bad bounce buffer");
272 		dma_bounced |= (1 << chan);
273 		newaddr = dma_bouncebuf[chan];
274 
275 		/* copy bounce buffer on write */
276 		if (!(flags & ISADMA_READ))
277 			bcopy(addr, newaddr, nbytes);
278 		addr = newaddr;
279 	}
280 
281 	/* translate to physical */
282 	phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
283 
284 	if (flags & ISADMA_RAW) {
285 	    dma_auto_mode |= (1 << chan);
286 	} else {
287 	    dma_auto_mode &= ~(1 << chan);
288 	}
289 
290 	if ((chan & 4) == 0) {
291 		/*
292 		 * Program one of DMA channels 0..3.  These are
293 		 * byte mode channels.
294 		 */
295 		/* set dma channel mode, and reset address ff */
296 
297 		/* If ISADMA_RAW flag is set, then use autoinitialise mode */
298 		if (flags & ISADMA_RAW) {
299 		  if (flags & ISADMA_READ)
300 			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
301 		  else
302 			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
303 		}
304 		else
305 		if (flags & ISADMA_READ)
306 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
307 		else
308 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
309 		outb(DMA1_FFC, 0);
310 
311 		/* send start address */
312 		waport =  DMA1_CHN(chan);
313 		outb(waport, phys);
314 		outb(waport, phys>>8);
315 		outb(dmapageport[chan], phys>>16);
316 
317 		/* send count */
318 		outb(waport + 1, --nbytes);
319 		outb(waport + 1, nbytes>>8);
320 
321 		/* unmask channel */
322 		outb(DMA1_SMSK, chan);
323 	} else {
324 		/*
325 		 * Program one of DMA channels 4..7.  These are
326 		 * word mode channels.
327 		 */
328 		/* set dma channel mode, and reset address ff */
329 
330 		/* If ISADMA_RAW flag is set, then use autoinitialise mode */
331 		if (flags & ISADMA_RAW) {
332 		  if (flags & ISADMA_READ)
333 			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
334 		  else
335 			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
336 		}
337 		else
338 		if (flags & ISADMA_READ)
339 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
340 		else
341 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
342 		outb(DMA2_FFC, 0);
343 
344 		/* send start address */
345 		waport = DMA2_CHN(chan - 4);
346 		outb(waport, phys>>1);
347 		outb(waport, phys>>9);
348 		outb(dmapageport[chan], phys>>16);
349 
350 		/* send count */
351 		nbytes >>= 1;
352 		outb(waport + 2, --nbytes);
353 		outb(waport + 2, nbytes>>8);
354 
355 		/* unmask channel */
356 		outb(DMA2_SMSK, chan & 3);
357 	}
358 	mtx_unlock(&isa_dma_lock);
359 }
360 
361 void
362 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
363 {
364 #ifdef DIAGNOSTIC
365 	if (chan & ~VALID_DMA_MASK)
366 		panic("isa_dmadone: channel out of range");
367 
368 	if ((dma_inuse & (1 << chan)) == 0)
369 		printf("isa_dmadone: channel %d not acquired\n", chan);
370 #endif
371 
372 	mtx_lock(&isa_dma_lock);
373 	if (((dma_busy & (1 << chan)) == 0) &&
374 	    (dma_auto_mode & (1 << chan)) == 0 )
375 		printf("isa_dmadone: channel %d not busy\n", chan);
376 
377 	if ((dma_auto_mode & (1 << chan)) == 0)
378 		outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
379 
380 	if (dma_bounced & (1 << chan)) {
381 		/* copy bounce buffer on read */
382 		if (flags & ISADMA_READ)
383 			bcopy(dma_bouncebuf[chan], addr, nbytes);
384 
385 		dma_bounced &= ~(1 << chan);
386 	}
387 	dma_busy &= ~(1 << chan);
388 	mtx_unlock(&isa_dma_lock);
389 }
390 
391 /*
392  * Check for problems with the address range of a DMA transfer
393  * (non-contiguous physical pages, outside of bus address space,
394  * crossing DMA page boundaries).
395  * Return true if special handling needed.
396  */
397 
398 static int
399 isa_dmarangecheck(caddr_t va, u_int length, int chan)
400 {
401 	vm_paddr_t phys, priorpage = 0;
402 	vm_offset_t endva;
403 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
404 
405 	endva = (vm_offset_t)round_page((vm_offset_t)va + length);
406 	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
407 		phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
408 		if (phys == 0)
409 			panic("isa_dmacheck: no physical page present");
410 		if (phys >= ISARAM_END)
411 			return (1);
412 		if (priorpage) {
413 			if (priorpage + PAGE_SIZE != phys)
414 				return (1);
415 			/* check if crossing a DMA page boundary */
416 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
417 				return (1);
418 		}
419 		priorpage = phys;
420 	}
421 	return (0);
422 }
423 
424 /*
425  * Query the progress of a transfer on a DMA channel.
426  *
427  * To avoid having to interrupt a transfer in progress, we sample
428  * each of the high and low databytes twice, and apply the following
429  * logic to determine the correct count.
430  *
431  * Reads are performed with interrupts disabled, thus it is to be
432  * expected that the time between reads is very small.  At most
433  * one rollover in the low count byte can be expected within the
434  * four reads that are performed.
435  *
436  * There are three gaps in which a rollover can occur :
437  *
438  * - read low1
439  *              gap1
440  * - read high1
441  *              gap2
442  * - read low2
443  *              gap3
444  * - read high2
445  *
446  * If a rollover occurs in gap1 or gap2, the low2 value will be
447  * greater than the low1 value.  In this case, low2 and high2 are a
448  * corresponding pair.
449  *
450  * In any other case, low1 and high1 can be considered to be correct.
451  *
452  * The function returns the number of bytes remaining in the transfer,
453  * or -1 if the channel requested is not active.
454  *
455  */
456 static int
457 isa_dmastatus_locked(int chan)
458 {
459 	u_long	cnt = 0;
460 	int	ffport, waport;
461 	u_long	low1, high1, low2, high2;
462 
463 	mtx_assert(&isa_dma_lock, MA_OWNED);
464 
465 	/* channel active? */
466 	if ((dma_inuse & (1 << chan)) == 0) {
467 		printf("isa_dmastatus: channel %d not active\n", chan);
468 		return(-1);
469 	}
470 	/* channel busy? */
471 
472 	if (((dma_busy & (1 << chan)) == 0) &&
473 	    (dma_auto_mode & (1 << chan)) == 0 ) {
474 	    printf("chan %d not busy\n", chan);
475 	    return -2 ;
476 	}
477 	if (chan < 4) {			/* low DMA controller */
478 		ffport = DMA1_FFC;
479 		waport = DMA1_CHN(chan) + 1;
480 	} else {			/* high DMA controller */
481 		ffport = DMA2_FFC;
482 		waport = DMA2_CHN(chan - 4) + 2;
483 	}
484 
485 	disable_intr();			/* no interrupts Mr Jones! */
486 	outb(ffport, 0);		/* clear register LSB flipflop */
487 	low1 = inb(waport);
488 	high1 = inb(waport);
489 	outb(ffport, 0);		/* clear again */
490 	low2 = inb(waport);
491 	high2 = inb(waport);
492 	enable_intr();			/* enable interrupts again */
493 
494 	/*
495 	 * Now decide if a wrap has tried to skew our results.
496 	 * Note that after TC, the count will read 0xffff, while we want
497 	 * to return zero, so we add and then mask to compensate.
498 	 */
499 	if (low1 >= low2) {
500 		cnt = (low1 + (high1 << 8) + 1) & 0xffff;
501 	} else {
502 		cnt = (low2 + (high2 << 8) + 1) & 0xffff;
503 	}
504 
505 	if (chan >= 4)			/* high channels move words */
506 		cnt *= 2;
507 	return(cnt);
508 }
509 
510 int
511 isa_dmastatus(int chan)
512 {
513 	int status;
514 
515 	mtx_lock(&isa_dma_lock);
516 	status = isa_dmastatus_locked(chan);
517 	mtx_unlock(&isa_dma_lock);
518 
519 	return (status);
520 }
521 
522 /*
523  * Reached terminal count yet ?
524  */
525 int
526 isa_dmatc(int chan)
527 {
528 
529 	if (chan < 4)
530 		return(inb(DMA1_STATUS) & (1 << chan));
531 	else
532 		return(inb(DMA2_STATUS) & (1 << (chan & 3)));
533 }
534 
535 /*
536  * Stop a DMA transfer currently in progress.
537  */
538 int
539 isa_dmastop(int chan)
540 {
541 	int status;
542 
543 	mtx_lock(&isa_dma_lock);
544 	if ((dma_inuse & (1 << chan)) == 0)
545 		printf("isa_dmastop: channel %d not acquired\n", chan);
546 
547 	if (((dma_busy & (1 << chan)) == 0) &&
548 	    ((dma_auto_mode & (1 << chan)) == 0)) {
549 		printf("chan %d not busy\n", chan);
550 		mtx_unlock(&isa_dma_lock);
551 		return -2 ;
552 	}
553 
554 	if ((chan & 4) == 0) {
555 		outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
556 	} else {
557 		outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
558 	}
559 
560 	status = isa_dmastatus_locked(chan);
561 
562 	mtx_unlock(&isa_dma_lock);
563 
564 	return (status);
565 }
566 
567 /*
568  * Attach to the ISA PnP descriptor for the AT DMA controller
569  */
570 static struct isa_pnp_id atdma_ids[] = {
571 	{ 0x0002d041 /* PNP0200 */, "AT DMA controller" },
572 	{ 0 }
573 };
574 
575 static int
576 atdma_probe(device_t dev)
577 {
578 	int result;
579 
580 	if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
581 		device_quiet(dev);
582 	return(result);
583 }
584 
585 static int
586 atdma_attach(device_t dev)
587 {
588 	return(0);
589 }
590 
591 static device_method_t atdma_methods[] = {
592 	/* Device interface */
593 	DEVMETHOD(device_probe,		atdma_probe),
594 	DEVMETHOD(device_attach,	atdma_attach),
595 	DEVMETHOD(device_detach,	bus_generic_detach),
596 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
597 	DEVMETHOD(device_suspend,	bus_generic_suspend),
598 	DEVMETHOD(device_resume,	bus_generic_resume),
599 	{ 0, 0 }
600 };
601 
602 static driver_t atdma_driver = {
603 	"atdma",
604 	atdma_methods,
605 	1,		/* no softc */
606 };
607 
608 static devclass_t atdma_devclass;
609 
610 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
611 DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0);
612 ISA_PNP_INFO(atdma_ids);
613