busdma_machdep.c revision 162607
1139724Simp/*-
240029Sgibbs * Copyright (c) 1997, 1998 Justin T. Gibbs.
332516Sgibbs * All rights reserved.
432516Sgibbs *
532516Sgibbs * Redistribution and use in source and binary forms, with or without
632516Sgibbs * modification, are permitted provided that the following conditions
732516Sgibbs * are met:
832516Sgibbs * 1. Redistributions of source code must retain the above copyright
932516Sgibbs *    notice, this list of conditions, and the following disclaimer,
1032516Sgibbs *    without modification, immediately at the beginning of the file.
1132516Sgibbs * 2. The name of the author may not be used to endorse or promote products
1232516Sgibbs *    derived from this software without specific prior written permission.
1332516Sgibbs *
1432516Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1532516Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1632516Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1732516Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
1832516Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1932516Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2032516Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2132516Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2232516Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2332516Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2432516Sgibbs * SUCH DAMAGE.
2532516Sgibbs */
2632516Sgibbs
27115683Sobrien#include <sys/cdefs.h>
28115683Sobrien__FBSDID("$FreeBSD: head/sys/i386/i386/busdma_machdep.c 162607 2006-09-24 19:24:26Z imp $");
29115683Sobrien
3032516Sgibbs#include <sys/param.h>
31154367Sscottl#include <sys/kdb.h>
32154367Sscottl#include <ddb/ddb.h>
33154367Sscottl#include <ddb/db_output.h>
3432516Sgibbs#include <sys/systm.h>
3532516Sgibbs#include <sys/malloc.h>
3667551Sjhb#include <sys/bus.h>
3767551Sjhb#include <sys/interrupt.h>
38112346Smux#include <sys/kernel.h>
39136805Srwatson#include <sys/ktr.h>
4076827Salfred#include <sys/lock.h>
4179224Sdillon#include <sys/proc.h>
4276827Salfred#include <sys/mutex.h>
43104486Ssam#include <sys/mbuf.h>
44104486Ssam#include <sys/uio.h>
45131529Sscottl#include <sys/sysctl.h>
4632516Sgibbs
4732516Sgibbs#include <vm/vm.h>
4832516Sgibbs#include <vm/vm_page.h>
49104486Ssam#include <vm/vm_map.h>
5032516Sgibbs
51112436Smux#include <machine/atomic.h>
5232516Sgibbs#include <machine/bus.h>
5332516Sgibbs#include <machine/md_var.h>
5432516Sgibbs
55113228Sjake#define MAX_BPAGES 512
56162211Sscottl#define BUS_DMA_USE_FILTER	BUS_DMA_BUS2
57162211Sscottl#define BUS_DMA_COULD_BOUNCE	BUS_DMA_BUS3
58162211Sscottl#define BUS_DMA_MIN_ALLOC_COMP	BUS_DMA_BUS4
5932516Sgibbs
60137445Sscottlstruct bounce_zone;
61137445Sscottl
6232516Sgibbsstruct bus_dma_tag {
6332516Sgibbs	bus_dma_tag_t	  parent;
6435767Sgibbs	bus_size_t	  alignment;
6532516Sgibbs	bus_size_t	  boundary;
6632516Sgibbs	bus_addr_t	  lowaddr;
6732516Sgibbs	bus_addr_t	  highaddr;
6832516Sgibbs	bus_dma_filter_t *filter;
6932516Sgibbs	void		 *filterarg;
7032516Sgibbs	bus_size_t	  maxsize;
7135767Sgibbs	u_int		  nsegments;
7232516Sgibbs	bus_size_t	  maxsegsz;
7332516Sgibbs	int		  flags;
7432516Sgibbs	int		  ref_count;
7532516Sgibbs	int		  map_count;
76117126Sscottl	bus_dma_lock_t	 *lockfunc;
77117126Sscottl	void		 *lockfuncarg;
78118246Sscottl	bus_dma_segment_t *segments;
79137445Sscottl	struct bounce_zone *bounce_zone;
8032516Sgibbs};
8132516Sgibbs
82132545Sscottlstruct bounce_page {
83132545Sscottl	vm_offset_t	vaddr;		/* kva of bounce buffer */
84132545Sscottl	bus_addr_t	busaddr;	/* Physical address */
85132545Sscottl	vm_offset_t	datavaddr;	/* kva of client data */
86132545Sscottl	bus_size_t	datacount;	/* client data count */
87132545Sscottl	STAILQ_ENTRY(bounce_page) links;
88132545Sscottl};
89132545Sscottl
9032516Sgibbsint busdma_swi_pending;
9132516Sgibbs
92137445Sscottlstruct bounce_zone {
93137445Sscottl	STAILQ_ENTRY(bounce_zone) links;
94137445Sscottl	STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
95137965Sscottl	int		total_bpages;
96137445Sscottl	int		free_bpages;
97137445Sscottl	int		reserved_bpages;
98137445Sscottl	int		active_bpages;
99137445Sscottl	int		total_bounced;
100137445Sscottl	int		total_deferred;
101137445Sscottl	bus_size_t	alignment;
102137445Sscottl	bus_size_t	boundary;
103137445Sscottl	bus_addr_t	lowaddr;
104137445Sscottl	char		zoneid[8];
105137445Sscottl	char		lowaddrid[20];
106137445Sscottl	struct sysctl_ctx_list sysctl_tree;
107137445Sscottl	struct sysctl_oid *sysctl_tree_top;
108137445Sscottl};
109137445Sscottl
110117136Smuxstatic struct mtx bounce_lock;
11132516Sgibbsstatic int total_bpages;
112137445Sscottlstatic int busdma_zonecount;
113137445Sscottlstatic STAILQ_HEAD(, bounce_zone) bounce_zone_list;
11432516Sgibbs
115131529SscottlSYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
116131529SscottlSYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
117131529Sscottl	   "Total bounce pages");
118131529Sscottl
11932516Sgibbsstruct bus_dmamap {
12032516Sgibbs	struct bp_list	       bpages;
12132516Sgibbs	int		       pagesneeded;
12232516Sgibbs	int		       pagesreserved;
12332516Sgibbs	bus_dma_tag_t	       dmat;
12432516Sgibbs	void		      *buf;		/* unmapped buffer pointer */
12532516Sgibbs	bus_size_t	       buflen;		/* unmapped buffer length */
12632516Sgibbs	bus_dmamap_callback_t *callback;
12732516Sgibbs	void		      *callback_arg;
12860938Sjake	STAILQ_ENTRY(bus_dmamap) links;
12932516Sgibbs};
13032516Sgibbs
13160938Sjakestatic STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
13260938Sjakestatic STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
13332516Sgibbsstatic struct bus_dmamap nobounce_dmamap;
13432516Sgibbs
135112346Smuxstatic void init_bounce_pages(void *dummy);
136137965Sscottlstatic int alloc_bounce_zone(bus_dma_tag_t dmat);
13732516Sgibbsstatic int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
138113228Sjakestatic int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
139117136Smux				int commit);
140112569Sjakestatic bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
14132516Sgibbs				   vm_offset_t vaddr, bus_size_t size);
14232516Sgibbsstatic void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
143162275Sscottlint run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
144162275Sscottlint _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
145162275Sscottl			    bus_size_t buflen, int flags, int *nb);
14632516Sgibbs
14795076Salfred/*
14895076Salfred * Return true if a match is made.
149117136Smux *
15095076Salfred * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
151117136Smux *
15295076Salfred * If paddr is within the bounds of the dma tag then call the filter callback
15395076Salfred * to check for a match, if there is no filter callback then assume a match.
15495076Salfred */
155162275Sscottlint
156137894Sscottlrun_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
15732516Sgibbs{
15832516Sgibbs	int retval;
15932516Sgibbs
16032516Sgibbs	retval = 0;
161131529Sscottl
16232516Sgibbs	do {
163131529Sscottl		if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
164137894Sscottl		 || ((paddr & (dmat->alignment - 1)) != 0))
16532516Sgibbs		 && (dmat->filter == NULL
166132545Sscottl		  || (*dmat->filter)(dmat->filterarg, paddr) != 0))
16732516Sgibbs			retval = 1;
16832516Sgibbs
16932516Sgibbs		dmat = dmat->parent;
17032516Sgibbs	} while (retval == 0 && dmat != NULL);
17132516Sgibbs	return (retval);
17232516Sgibbs}
17332516Sgibbs
174117126Sscottl/*
175117126Sscottl * Convenience function for manipulating driver locks from busdma (during
176117126Sscottl * busdma_swi, for example).  Drivers that don't provide their own locks
177117126Sscottl * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
178117126Sscottl * non-mutex locking scheme don't have to use this at all.
179117126Sscottl */
180117126Sscottlvoid
181117126Sscottlbusdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
182117126Sscottl{
183117126Sscottl	struct mtx *dmtx;
184117126Sscottl
185117126Sscottl	dmtx = (struct mtx *)arg;
186117126Sscottl	switch (op) {
187117126Sscottl	case BUS_DMA_LOCK:
188117126Sscottl		mtx_lock(dmtx);
189117126Sscottl		break;
190117126Sscottl	case BUS_DMA_UNLOCK:
191117126Sscottl		mtx_unlock(dmtx);
192117126Sscottl		break;
193117126Sscottl	default:
194117126Sscottl		panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
195117126Sscottl	}
196117126Sscottl}
197117126Sscottl
198117126Sscottl/*
199117126Sscottl * dflt_lock should never get called.  It gets put into the dma tag when
200117126Sscottl * lockfunc == NULL, which is only valid if the maps that are associated
201117126Sscottl * with the tag are meant to never be defered.
202117126Sscottl * XXX Should have a way to identify which driver is responsible here.
203117126Sscottl */
204117126Sscottlstatic void
205117126Sscottldflt_lock(void *arg, bus_dma_lock_op_t op)
206117126Sscottl{
207117126Sscottl	panic("driver error: busdma dflt_lock called");
208117126Sscottl}
209117126Sscottl
21032516Sgibbs/*
21132516Sgibbs * Allocate a device specific dma_tag.
21232516Sgibbs */
21332516Sgibbsint
21435767Sgibbsbus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
21535767Sgibbs		   bus_size_t boundary, bus_addr_t lowaddr,
21635767Sgibbs		   bus_addr_t highaddr, bus_dma_filter_t *filter,
21735767Sgibbs		   void *filterarg, bus_size_t maxsize, int nsegments,
218117126Sscottl		   bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
219117126Sscottl		   void *lockfuncarg, bus_dma_tag_t *dmat)
22032516Sgibbs{
22132516Sgibbs	bus_dma_tag_t newtag;
22232516Sgibbs	int error = 0;
22332516Sgibbs
224131529Sscottl	/* Basic sanity checking */
225131529Sscottl	if (boundary != 0 && boundary < maxsegsz)
226131529Sscottl		maxsegsz = boundary;
227131529Sscottl
22832516Sgibbs	/* Return a NULL tag on failure */
22932516Sgibbs	*dmat = NULL;
23032516Sgibbs
231137460Sscottl	newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
232137460Sscottl	    M_ZERO | M_NOWAIT);
233136805Srwatson	if (newtag == NULL) {
234143293Smux		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
235143284Smux		    __func__, newtag, 0, error);
23632516Sgibbs		return (ENOMEM);
237136805Srwatson	}
23832516Sgibbs
23932516Sgibbs	newtag->parent = parent;
24048449Smjacob	newtag->alignment = alignment;
24132516Sgibbs	newtag->boundary = boundary;
242112569Sjake	newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
243112569Sjake	newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
244112569Sjake	    (PAGE_SIZE - 1);
24532516Sgibbs	newtag->filter = filter;
24632516Sgibbs	newtag->filterarg = filterarg;
24732516Sgibbs	newtag->maxsize = maxsize;
24832516Sgibbs	newtag->nsegments = nsegments;
24932516Sgibbs	newtag->maxsegsz = maxsegsz;
25032516Sgibbs	newtag->flags = flags;
25132516Sgibbs	newtag->ref_count = 1; /* Count ourself */
25232516Sgibbs	newtag->map_count = 0;
253117126Sscottl	if (lockfunc != NULL) {
254117126Sscottl		newtag->lockfunc = lockfunc;
255117126Sscottl		newtag->lockfuncarg = lockfuncarg;
256117126Sscottl	} else {
257117126Sscottl		newtag->lockfunc = dflt_lock;
258117126Sscottl		newtag->lockfuncarg = NULL;
259117126Sscottl	}
260118246Sscottl	newtag->segments = NULL;
261118246Sscottl
26232516Sgibbs	/* Take into account any restrictions imposed by our parent tag */
26332516Sgibbs	if (parent != NULL) {
26432516Sgibbs		newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
26532516Sgibbs		newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
266134934Sscottl		if (newtag->boundary == 0)
267134934Sscottl			newtag->boundary = parent->boundary;
268134934Sscottl		else if (parent->boundary != 0)
269134934Sscottl			newtag->boundary = MIN(parent->boundary,
270134934Sscottl					       newtag->boundary);
271162211Sscottl		if ((newtag->filter != NULL) ||
272162211Sscottl		    ((parent->flags & BUS_DMA_USE_FILTER) != 0))
273162211Sscottl			newtag->flags |= BUS_DMA_USE_FILTER;
27432516Sgibbs		if (newtag->filter == NULL) {
27532516Sgibbs			/*
27632516Sgibbs			 * Short circuit looking at our parent directly
27735256Sdes			 * since we have encapsulated all of its information
27832516Sgibbs			 */
27932516Sgibbs			newtag->filter = parent->filter;
28032516Sgibbs			newtag->filterarg = parent->filterarg;
28132516Sgibbs			newtag->parent = parent->parent;
28232516Sgibbs		}
283112436Smux		if (newtag->parent != NULL)
284112436Smux			atomic_add_int(&parent->ref_count, 1);
28532516Sgibbs	}
286137965Sscottl
287137965Sscottl	if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
288138194Sscottl	 || newtag->alignment > 1)
289137965Sscottl		newtag->flags |= BUS_DMA_COULD_BOUNCE;
290137965Sscottl
291137965Sscottl	if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
292112569Sjake	    (flags & BUS_DMA_ALLOCNOW) != 0) {
293137965Sscottl		struct bounce_zone *bz;
294137965Sscottl
29532516Sgibbs		/* Must bounce */
29632516Sgibbs
297154367Sscottl		if ((error = alloc_bounce_zone(newtag)) != 0) {
298154367Sscottl			free(newtag, M_DEVBUF);
299137965Sscottl			return (error);
300154367Sscottl		}
301137965Sscottl		bz = newtag->bounce_zone;
302137965Sscottl
303137965Sscottl		if (ptoa(bz->total_bpages) < maxsize) {
30432516Sgibbs			int pages;
30532516Sgibbs
306137965Sscottl			pages = atop(maxsize) - bz->total_bpages;
30732516Sgibbs
30832516Sgibbs			/* Add pages to our bounce pool */
30932516Sgibbs			if (alloc_bounce_pages(newtag, pages) < pages)
31032516Sgibbs				error = ENOMEM;
31132516Sgibbs		}
31235767Sgibbs		/* Performed initial allocation */
31335767Sgibbs		newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
31432516Sgibbs	}
31532516Sgibbs
31632516Sgibbs	if (error != 0) {
31732516Sgibbs		free(newtag, M_DEVBUF);
31832516Sgibbs	} else {
31932516Sgibbs		*dmat = newtag;
32032516Sgibbs	}
321143293Smux	CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
322143284Smux	    __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
32332516Sgibbs	return (error);
32432516Sgibbs}
32532516Sgibbs
32632516Sgibbsint
32732516Sgibbsbus_dma_tag_destroy(bus_dma_tag_t dmat)
32832516Sgibbs{
329136805Srwatson	bus_dma_tag_t dmat_copy;
330136805Srwatson	int error;
331136805Srwatson
332136805Srwatson	error = 0;
333136805Srwatson	dmat_copy = dmat;
334136805Srwatson
33532516Sgibbs	if (dmat != NULL) {
33632516Sgibbs
337136805Srwatson		if (dmat->map_count != 0) {
338136805Srwatson			error = EBUSY;
339136805Srwatson			goto out;
340136805Srwatson		}
34132516Sgibbs
34232516Sgibbs		while (dmat != NULL) {
34332516Sgibbs			bus_dma_tag_t parent;
34432516Sgibbs
34532516Sgibbs			parent = dmat->parent;
346112436Smux			atomic_subtract_int(&dmat->ref_count, 1);
34732516Sgibbs			if (dmat->ref_count == 0) {
348118246Sscottl				if (dmat->segments != NULL)
349118246Sscottl					free(dmat->segments, M_DEVBUF);
35032516Sgibbs				free(dmat, M_DEVBUF);
35140029Sgibbs				/*
35240029Sgibbs				 * Last reference count, so
35340029Sgibbs				 * release our reference
35440029Sgibbs				 * count on our parent.
35540029Sgibbs				 */
35640029Sgibbs				dmat = parent;
35740029Sgibbs			} else
35840029Sgibbs				dmat = NULL;
35932516Sgibbs		}
36032516Sgibbs	}
361136805Srwatsonout:
362143293Smux	CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
363136805Srwatson	return (error);
36432516Sgibbs}
36532516Sgibbs
36632516Sgibbs/*
36732516Sgibbs * Allocate a handle for mapping from kva/uva/physical
36832516Sgibbs * address space into bus device space.
36932516Sgibbs */
37032516Sgibbsint
37132516Sgibbsbus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
37232516Sgibbs{
37332516Sgibbs	int error;
37432516Sgibbs
37532516Sgibbs	error = 0;
37632516Sgibbs
377118246Sscottl	if (dmat->segments == NULL) {
378118246Sscottl		dmat->segments = (bus_dma_segment_t *)malloc(
379118246Sscottl		    sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
380118246Sscottl		    M_NOWAIT);
381136805Srwatson		if (dmat->segments == NULL) {
382143293Smux			CTR3(KTR_BUSDMA, "%s: tag %p error %d",
383143284Smux			    __func__, dmat, ENOMEM);
384118246Sscottl			return (ENOMEM);
385136805Srwatson		}
386118246Sscottl	}
387118246Sscottl
388131529Sscottl	/*
389131529Sscottl	 * Bouncing might be required if the driver asks for an active
390131529Sscottl	 * exclusion region, a data alignment that is stricter than 1, and/or
391131529Sscottl	 * an active address boundary.
392131529Sscottl	 */
393137965Sscottl	if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
394137445Sscottl
39532516Sgibbs		/* Must bounce */
396143449Sscottl		struct bounce_zone *bz;
39732516Sgibbs		int maxpages;
39832516Sgibbs
399137965Sscottl		if (dmat->bounce_zone == NULL) {
400137965Sscottl			if ((error = alloc_bounce_zone(dmat)) != 0)
401137965Sscottl				return (error);
402137965Sscottl		}
403143449Sscottl		bz = dmat->bounce_zone;
404137965Sscottl
40532516Sgibbs		*mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
40669781Sdwmalone					     M_NOWAIT | M_ZERO);
407136805Srwatson		if (*mapp == NULL) {
408143293Smux			CTR3(KTR_BUSDMA, "%s: tag %p error %d",
409143284Smux			    __func__, dmat, ENOMEM);
41035767Sgibbs			return (ENOMEM);
411136805Srwatson		}
41269781Sdwmalone
41369781Sdwmalone		/* Initialize the new map */
41469781Sdwmalone		STAILQ_INIT(&((*mapp)->bpages));
41569781Sdwmalone
41632516Sgibbs		/*
41732516Sgibbs		 * Attempt to add pages to our pool on a per-instance
41832516Sgibbs		 * basis up to a sane limit.
41932516Sgibbs		 */
420143449Sscottl		if (dmat->alignment > 1)
421143449Sscottl			maxpages = MAX_BPAGES;
422143449Sscottl		else
423143449Sscottl			maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
42435767Sgibbs		if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
425143449Sscottl		 || (dmat->map_count > 0 && bz->total_bpages < maxpages)) {
42632516Sgibbs			int pages;
42732516Sgibbs
428113228Sjake			pages = MAX(atop(dmat->maxsize), 1);
429143449Sscottl			pages = MIN(maxpages - bz->total_bpages, pages);
430143449Sscottl			pages = MAX(pages, 1);
431113228Sjake			if (alloc_bounce_pages(dmat, pages) < pages)
432113228Sjake				error = ENOMEM;
43335767Sgibbs
43435767Sgibbs			if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
43535767Sgibbs				if (error == 0)
43635767Sgibbs					dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
43735767Sgibbs			} else {
43835767Sgibbs				error = 0;
43935767Sgibbs			}
44032516Sgibbs		}
44132516Sgibbs	} else {
44240029Sgibbs		*mapp = NULL;
44332516Sgibbs	}
44432516Sgibbs	if (error == 0)
44532516Sgibbs		dmat->map_count++;
446143293Smux	CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
447143284Smux	    __func__, dmat, dmat->flags, error);
44832516Sgibbs	return (error);
44932516Sgibbs}
45032516Sgibbs
45132516Sgibbs/*
45232516Sgibbs * Destroy a handle for mapping from kva/uva/physical
45332516Sgibbs * address space into bus device space.
45432516Sgibbs */
45532516Sgibbsint
45632516Sgibbsbus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
45732516Sgibbs{
458117136Smux	if (map != NULL && map != &nobounce_dmamap) {
459136805Srwatson		if (STAILQ_FIRST(&map->bpages) != NULL) {
460143293Smux			CTR3(KTR_BUSDMA, "%s: tag %p error %d",
461143284Smux			    __func__, dmat, EBUSY);
46232516Sgibbs			return (EBUSY);
463136805Srwatson		}
46432516Sgibbs		free(map, M_DEVBUF);
46532516Sgibbs	}
46632516Sgibbs	dmat->map_count--;
467143293Smux	CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
46832516Sgibbs	return (0);
46932516Sgibbs}
47032516Sgibbs
47135767Sgibbs
47235767Sgibbs/*
47335767Sgibbs * Allocate a piece of memory that can be efficiently mapped into
47435767Sgibbs * bus device space based on the constraints lited in the dma tag.
47535767Sgibbs * A dmamap to for use with dmamap_load is also allocated.
47635767Sgibbs */
47735767Sgibbsint
478115316Sscottlbus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
479115316Sscottl		 bus_dmamap_t *mapp)
48035767Sgibbs{
481159130Ssilby	int mflags;
482118081Smux
483118081Smux	if (flags & BUS_DMA_NOWAIT)
484118081Smux		mflags = M_NOWAIT;
485118081Smux	else
486118081Smux		mflags = M_WAITOK;
487118081Smux	if (flags & BUS_DMA_ZERO)
488118081Smux		mflags |= M_ZERO;
489118081Smux
49035767Sgibbs	/* If we succeed, no mapping/bouncing will be required */
49140029Sgibbs	*mapp = NULL;
49235767Sgibbs
493118246Sscottl	if (dmat->segments == NULL) {
494118246Sscottl		dmat->segments = (bus_dma_segment_t *)malloc(
495118246Sscottl		    sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
496118246Sscottl		    M_NOWAIT);
497136805Srwatson		if (dmat->segments == NULL) {
498143293Smux			CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
499143284Smux			    __func__, dmat, dmat->flags, ENOMEM);
500118246Sscottl			return (ENOMEM);
501136805Srwatson		}
502118246Sscottl	}
503118246Sscottl
504159011Ssilby	/*
505159011Ssilby	 * XXX:
506159011Ssilby	 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
507159011Ssilby	 * alignment guarantees of malloc need to be nailed down, and the
508159011Ssilby	 * code below should be rewritten to take that into account.
509159011Ssilby	 *
510159130Ssilby	 * In the meantime, we'll warn the user if malloc gets it wrong.
511159011Ssilby	 */
512115316Sscottl	if ((dmat->maxsize <= PAGE_SIZE) &&
513159011Ssilby	   (dmat->alignment < dmat->maxsize) &&
514112569Sjake	    dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
515118081Smux		*vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
51635767Sgibbs	} else {
51735767Sgibbs		/*
51835767Sgibbs		 * XXX Use Contigmalloc until it is merged into this facility
51935767Sgibbs		 *     and handles multi-seg allocations.  Nobody is doing
52035767Sgibbs		 *     multi-seg allocations yet though.
521131529Sscottl		 * XXX Certain AGP hardware does.
52235767Sgibbs		 */
523118081Smux		*vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
52448449Smjacob		    0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
52548449Smjacob		    dmat->boundary);
52635767Sgibbs	}
527136805Srwatson	if (*vaddr == NULL) {
528143293Smux		CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
529143284Smux		    __func__, dmat, dmat->flags, ENOMEM);
53035767Sgibbs		return (ENOMEM);
531159130Ssilby	} else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
532162607Simp		printf("bus_dmamem_alloc failed to align memory properly.\n");
533159092Smjacob	}
534143293Smux	CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
535143284Smux	    __func__, dmat, dmat->flags, ENOMEM);
53635767Sgibbs	return (0);
53735767Sgibbs}
53835767Sgibbs
53935767Sgibbs/*
54035767Sgibbs * Free a piece of memory and it's allociated dmamap, that was allocated
54195076Salfred * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
54235767Sgibbs */
54335767Sgibbsvoid
544115316Sscottlbus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
54535767Sgibbs{
54635767Sgibbs	/*
54735767Sgibbs	 * dmamem does not need to be bounced, so the map should be
54835767Sgibbs	 * NULL
54935767Sgibbs	 */
55049859Sgibbs	if (map != NULL)
55135767Sgibbs		panic("bus_dmamem_free: Invalid map freed\n");
552159011Ssilby	if ((dmat->maxsize <= PAGE_SIZE) &&
553159011Ssilby	   (dmat->alignment < dmat->maxsize) &&
554159011Ssilby	    dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
55540029Sgibbs		free(vaddr, M_DEVBUF);
556112196Smux	else {
557115316Sscottl		contigfree(vaddr, dmat->maxsize, M_DEVBUF);
558112196Smux	}
559143293Smux	CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
56035767Sgibbs}
56135767Sgibbs
562162275Sscottlint
563162211Sscottl_bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
564162211Sscottl			bus_size_t buflen, int flags, int *nb)
565104486Ssam{
566113228Sjake	vm_offset_t vaddr;
567162211Sscottl	vm_offset_t vendaddr;
568113228Sjake	bus_addr_t paddr;
569162211Sscottl	int needbounce = *nb;
570104486Ssam
571162211Sscottl	if ((map != &nobounce_dmamap && map->pagesneeded == 0)) {
572137142Sscottl		CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
573137142Sscottl		    "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
574137142Sscottl		    dmat->boundary, dmat->alignment);
575137142Sscottl		CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
576137142Sscottl		    map, &nobounce_dmamap, map->pagesneeded);
577113228Sjake		/*
578113228Sjake		 * Count the number of bounce pages
579113228Sjake		 * needed in order to complete this transfer
580113228Sjake		 */
581113228Sjake		vaddr = trunc_page((vm_offset_t)buf);
582113228Sjake		vendaddr = (vm_offset_t)buf + buflen;
583113228Sjake
584113228Sjake		while (vaddr < vendaddr) {
585113228Sjake			paddr = pmap_kextract(vaddr);
586162211Sscottl			if (((dmat->flags & BUS_DMA_USE_FILTER) != 0) &&
587162211Sscottl			    run_filter(dmat, paddr) != 0) {
588113228Sjake				needbounce = 1;
589113228Sjake				map->pagesneeded++;
590113228Sjake			}
591113228Sjake			vaddr += PAGE_SIZE;
592113228Sjake		}
593137142Sscottl		CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
594113228Sjake	}
595113228Sjake
596113228Sjake	/* Reserve Necessary Bounce Pages */
597113228Sjake	if (map->pagesneeded != 0) {
598113228Sjake		mtx_lock(&bounce_lock);
599113472Ssimokawa		if (flags & BUS_DMA_NOWAIT) {
600113472Ssimokawa			if (reserve_bounce_pages(dmat, map, 0) != 0) {
601113472Ssimokawa				mtx_unlock(&bounce_lock);
602113472Ssimokawa				return (ENOMEM);
603113472Ssimokawa			}
604113472Ssimokawa		} else {
605113472Ssimokawa			if (reserve_bounce_pages(dmat, map, 1) != 0) {
606132545Sscottl				/* Queue us for resources */
607113472Ssimokawa				map->dmat = dmat;
608113472Ssimokawa				map->buf = buf;
609113472Ssimokawa				map->buflen = buflen;
610113472Ssimokawa				STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
611117136Smux				    map, links);
612113472Ssimokawa				mtx_unlock(&bounce_lock);
613113472Ssimokawa				return (EINPROGRESS);
614113472Ssimokawa			}
615113228Sjake		}
616113228Sjake		mtx_unlock(&bounce_lock);
617113228Sjake	}
618113228Sjake
619162211Sscottl	*nb = needbounce;
620162211Sscottl	return (0);
621162211Sscottl}
622162211Sscottl
623162211Sscottl/*
624162211Sscottl * Utility function to load a linear buffer.  lastaddrp holds state
625162211Sscottl * between invocations (for multiple-buffer loads).  segp contains
626162211Sscottl * the starting segment on entrace, and the ending segment on exit.
627162211Sscottl * first indicates if this is the first invocation of this function.
628162211Sscottl */
629162211Sscottlstatic __inline int
630162211Sscottl_bus_dmamap_load_buffer(bus_dma_tag_t dmat,
631162211Sscottl    			bus_dmamap_t map,
632162211Sscottl			void *buf, bus_size_t buflen,
633162211Sscottl			pmap_t pmap,
634162211Sscottl			int flags,
635162211Sscottl			bus_addr_t *lastaddrp,
636162211Sscottl			bus_dma_segment_t *segs,
637162211Sscottl			int *segp,
638162211Sscottl			int first)
639162211Sscottl{
640162211Sscottl	bus_size_t sgsize;
641162211Sscottl	bus_addr_t curaddr, lastaddr, baddr, bmask;
642162211Sscottl	vm_offset_t vaddr;
643162211Sscottl	int needbounce = 0;
644162211Sscottl	int seg, error;
645162211Sscottl
646162211Sscottl	if (map == NULL)
647162211Sscottl		map = &nobounce_dmamap;
648162211Sscottl
649162211Sscottl	if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
650162211Sscottl		error = _bus_dmamap_count_pages(dmat, map, buf, buflen, flags,
651162211Sscottl		    &needbounce);
652162211Sscottl		if (error)
653162211Sscottl			return (error);
654162211Sscottl	}
655162211Sscottl
656137142Sscottl	vaddr = (vm_offset_t)buf;
657104486Ssam	lastaddr = *lastaddrp;
658113228Sjake	bmask = ~(dmat->boundary - 1);
659104486Ssam
660104486Ssam	for (seg = *segp; buflen > 0 ; ) {
661104486Ssam		/*
662104486Ssam		 * Get the physical address for this segment.
663104486Ssam		 */
664104486Ssam		if (pmap)
665104486Ssam			curaddr = pmap_extract(pmap, vaddr);
666104486Ssam		else
667104486Ssam			curaddr = pmap_kextract(vaddr);
668104486Ssam
669104486Ssam		/*
670104486Ssam		 * Compute the segment size, and adjust counts.
671104486Ssam		 */
672104486Ssam		sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
673104486Ssam		if (buflen < sgsize)
674104486Ssam			sgsize = buflen;
675104486Ssam
676104486Ssam		/*
677104486Ssam		 * Make sure we don't cross any boundaries.
678104486Ssam		 */
679104486Ssam		if (dmat->boundary > 0) {
680104486Ssam			baddr = (curaddr + dmat->boundary) & bmask;
681104486Ssam			if (sgsize > (baddr - curaddr))
682104486Ssam				sgsize = (baddr - curaddr);
683104486Ssam		}
684104486Ssam
685162211Sscottl		if (((dmat->flags & BUS_DMA_USE_FILTER) != 0) &&
686162211Sscottl		    map->pagesneeded != 0 && run_filter(dmat, curaddr))
687113228Sjake			curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
688113228Sjake
689104486Ssam		/*
690104486Ssam		 * Insert chunk into a segment, coalescing with
691104486Ssam		 * previous segment if possible.
692104486Ssam		 */
693104486Ssam		if (first) {
694104486Ssam			segs[seg].ds_addr = curaddr;
695104486Ssam			segs[seg].ds_len = sgsize;
696104486Ssam			first = 0;
697104486Ssam		} else {
698113228Sjake			if (needbounce == 0 && curaddr == lastaddr &&
699104486Ssam			    (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
700104486Ssam			    (dmat->boundary == 0 ||
701104486Ssam			     (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
702104486Ssam				segs[seg].ds_len += sgsize;
703104486Ssam			else {
704104486Ssam				if (++seg >= dmat->nsegments)
705104486Ssam					break;
706104486Ssam				segs[seg].ds_addr = curaddr;
707104486Ssam				segs[seg].ds_len = sgsize;
708104486Ssam			}
709104486Ssam		}
710104486Ssam
711104486Ssam		lastaddr = curaddr + sgsize;
712104486Ssam		vaddr += sgsize;
713104486Ssam		buflen -= sgsize;
714104486Ssam	}
715104486Ssam
716104486Ssam	*segp = seg;
717104486Ssam	*lastaddrp = lastaddr;
718104486Ssam
719104486Ssam	/*
720104486Ssam	 * Did we fit?
721104486Ssam	 */
722104486Ssam	return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
723104486Ssam}
724104486Ssam
725104486Ssam/*
726113459Ssimokawa * Map the buffer buf into bus space using the dmamap map.
727113459Ssimokawa */
728113459Ssimokawaint
729113459Ssimokawabus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
730113459Ssimokawa		bus_size_t buflen, bus_dmamap_callback_t *callback,
731113459Ssimokawa		void *callback_arg, int flags)
732113459Ssimokawa{
733113492Smux	bus_addr_t		lastaddr = 0;
734113459Ssimokawa	int			error, nsegs = 0;
735113459Ssimokawa
736113472Ssimokawa	if (map != NULL) {
737113472Ssimokawa		flags |= BUS_DMA_WAITOK;
738113472Ssimokawa		map->callback = callback;
739113472Ssimokawa		map->callback_arg = callback_arg;
740113472Ssimokawa	}
741113472Ssimokawa
742118246Sscottl	error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
743139840Sscottl	     &lastaddr, dmat->segments, &nsegs, 1);
744113459Ssimokawa
745158264Sscottl	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
746158264Sscottl	    __func__, dmat, dmat->flags, error, nsegs + 1);
747158264Sscottl
748136805Srwatson	if (error == EINPROGRESS) {
749113492Smux		return (error);
750136805Srwatson	}
751113472Ssimokawa
752113459Ssimokawa	if (error)
753118246Sscottl		(*callback)(callback_arg, dmat->segments, 0, error);
754113459Ssimokawa	else
755118246Sscottl		(*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
756113459Ssimokawa
757158264Sscottl	/*
758158264Sscottl	 * Return ENOMEM to the caller so that it can pass it up the stack.
759158264Sscottl	 * This error only happens when NOWAIT is set, so deferal is disabled.
760158264Sscottl	 */
761158264Sscottl	if (error == ENOMEM)
762158264Sscottl		return (error);
763158264Sscottl
764113459Ssimokawa	return (0);
765113459Ssimokawa}
766113459Ssimokawa
767113459Ssimokawa
768113459Ssimokawa/*
769104486Ssam * Like _bus_dmamap_load(), but for mbufs.
770104486Ssam */
771162275Sscottlstatic __inline int
772162275Sscottl_bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
773162275Sscottl			struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
774162275Sscottl			int flags)
775104486Ssam{
776162275Sscottl	int error;
777104486Ssam
778117136Smux	M_ASSERTPKTHDR(m0);
779104486Ssam
780113472Ssimokawa	flags |= BUS_DMA_NOWAIT;
781162275Sscottl	*nsegs = 0;
782104486Ssam	error = 0;
783104486Ssam	if (m0->m_pkthdr.len <= dmat->maxsize) {
784104486Ssam		int first = 1;
785113228Sjake		bus_addr_t lastaddr = 0;
786104486Ssam		struct mbuf *m;
787104486Ssam
788104486Ssam		for (m = m0; m != NULL && error == 0; m = m->m_next) {
789110335Sharti			if (m->m_len > 0) {
790113228Sjake				error = _bus_dmamap_load_buffer(dmat, map,
791110335Sharti						m->m_data, m->m_len,
792110335Sharti						NULL, flags, &lastaddr,
793162275Sscottl						segs, nsegs, first);
794110335Sharti				first = 0;
795110335Sharti			}
796104486Ssam		}
797104486Ssam	} else {
798104486Ssam		error = EINVAL;
799104486Ssam	}
800104486Ssam
801162275Sscottl	/* XXX FIXME: Having to increment nsegs is really annoying */
802162275Sscottl	++*nsegs;
803162275Sscottl	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
804162275Sscottl	    __func__, dmat, dmat->flags, error, *nsegs);
805162275Sscottl	return (error);
806162275Sscottl}
807162275Sscottl
808162275Sscottlint
809162275Sscottlbus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
810162275Sscottl		     struct mbuf *m0,
811162275Sscottl		     bus_dmamap_callback2_t *callback, void *callback_arg,
812162275Sscottl		     int flags)
813162275Sscottl{
814162275Sscottl	int nsegs, error;
815162275Sscottl
816162275Sscottl	error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, dmat->segments, &nsegs,
817162275Sscottl	    flags);
818162275Sscottl
819104486Ssam	if (error) {
820104486Ssam		/* force "no valid mappings" in callback */
821118246Sscottl		(*callback)(callback_arg, dmat->segments, 0, 0, error);
822104486Ssam	} else {
823118246Sscottl		(*callback)(callback_arg, dmat->segments,
824162275Sscottl			    nsegs, m0->m_pkthdr.len, error);
825104486Ssam	}
826143293Smux	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
827162275Sscottl	    __func__, dmat, dmat->flags, error, nsegs);
828104486Ssam	return (error);
829104486Ssam}
830104486Ssam
831139840Sscottlint
832139840Sscottlbus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
833139840Sscottl			struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
834139840Sscottl			int flags)
835139840Sscottl{
836162275Sscottl	return (_bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags));
837139840Sscottl}
838139840Sscottl
839104486Ssam/*
840104486Ssam * Like _bus_dmamap_load(), but for uios.
841104486Ssam */
842104486Ssamint
843104486Ssambus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
844104486Ssam		    struct uio *uio,
845104486Ssam		    bus_dmamap_callback2_t *callback, void *callback_arg,
846104486Ssam		    int flags)
847104486Ssam{
848113228Sjake	bus_addr_t lastaddr;
849104486Ssam	int nsegs, error, first, i;
850104486Ssam	bus_size_t resid;
851104486Ssam	struct iovec *iov;
852137142Sscottl	pmap_t pmap;
853104486Ssam
854113472Ssimokawa	flags |= BUS_DMA_NOWAIT;
855104486Ssam	resid = uio->uio_resid;
856104486Ssam	iov = uio->uio_iov;
857104486Ssam
858104486Ssam	if (uio->uio_segflg == UIO_USERSPACE) {
859137142Sscottl		KASSERT(uio->uio_td != NULL,
860104486Ssam			("bus_dmamap_load_uio: USERSPACE but no proc"));
861137142Sscottl		pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
862137142Sscottl	} else
863137142Sscottl		pmap = NULL;
864104486Ssam
865104486Ssam	nsegs = 0;
866104486Ssam	error = 0;
867104486Ssam	first = 1;
868104486Ssam	for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
869104486Ssam		/*
870104486Ssam		 * Now at the first iovec to load.  Load each iovec
871104486Ssam		 * until we have exhausted the residual count.
872104486Ssam		 */
873104486Ssam		bus_size_t minlen =
874104486Ssam			resid < iov[i].iov_len ? resid : iov[i].iov_len;
875104486Ssam		caddr_t addr = (caddr_t) iov[i].iov_base;
876104486Ssam
877110335Sharti		if (minlen > 0) {
878113228Sjake			error = _bus_dmamap_load_buffer(dmat, map,
879139840Sscottl					addr, minlen, pmap, flags, &lastaddr,
880139840Sscottl					dmat->segments, &nsegs, first);
881110335Sharti			first = 0;
882104486Ssam
883110335Sharti			resid -= minlen;
884110335Sharti		}
885104486Ssam	}
886104486Ssam
887104486Ssam	if (error) {
888104486Ssam		/* force "no valid mappings" in callback */
889118246Sscottl		(*callback)(callback_arg, dmat->segments, 0, 0, error);
890104486Ssam	} else {
891118246Sscottl		(*callback)(callback_arg, dmat->segments,
892104486Ssam			    nsegs+1, uio->uio_resid, error);
893104486Ssam	}
894143293Smux	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
895143284Smux	    __func__, dmat, dmat->flags, error, nsegs + 1);
896104486Ssam	return (error);
897104486Ssam}
898104486Ssam
899104486Ssam/*
90032516Sgibbs * Release the mapping held by map.
90132516Sgibbs */
90232516Sgibbsvoid
90332516Sgibbs_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
90432516Sgibbs{
90532516Sgibbs	struct bounce_page *bpage;
90632516Sgibbs
90732516Sgibbs	while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
90832516Sgibbs		STAILQ_REMOVE_HEAD(&map->bpages, links);
90932516Sgibbs		free_bounce_page(dmat, bpage);
91032516Sgibbs	}
91132516Sgibbs}
91232516Sgibbs
91332516Sgibbsvoid
914115343Sscottl_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
91532516Sgibbs{
91632516Sgibbs	struct bounce_page *bpage;
91732516Sgibbs
91832516Sgibbs	if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
91932516Sgibbs		/*
92032516Sgibbs		 * Handle data bouncing.  We might also
92132516Sgibbs		 * want to add support for invalidating
92232516Sgibbs		 * the caches on broken hardware
92332516Sgibbs		 */
924137445Sscottl		dmat->bounce_zone->total_bounced++;
925143293Smux		CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
926143284Smux		    "performing bounce", __func__, op, dmat, dmat->flags);
927131529Sscottl
928113347Smux		if (op & BUS_DMASYNC_PREWRITE) {
92932516Sgibbs			while (bpage != NULL) {
93032516Sgibbs				bcopy((void *)bpage->datavaddr,
93132516Sgibbs				      (void *)bpage->vaddr,
93232516Sgibbs				      bpage->datacount);
93332516Sgibbs				bpage = STAILQ_NEXT(bpage, links);
93432516Sgibbs			}
935113347Smux		}
93632516Sgibbs
937113347Smux		if (op & BUS_DMASYNC_POSTREAD) {
93832516Sgibbs			while (bpage != NULL) {
93932516Sgibbs				bcopy((void *)bpage->vaddr,
94032516Sgibbs				      (void *)bpage->datavaddr,
94132516Sgibbs				      bpage->datacount);
94232516Sgibbs				bpage = STAILQ_NEXT(bpage, links);
94332516Sgibbs			}
94432516Sgibbs		}
94532516Sgibbs	}
94632516Sgibbs}
94732516Sgibbs
948112346Smuxstatic void
949112346Smuxinit_bounce_pages(void *dummy __unused)
950112346Smux{
951112346Smux
952112346Smux	total_bpages = 0;
953137445Sscottl	STAILQ_INIT(&bounce_zone_list);
954112346Smux	STAILQ_INIT(&bounce_map_waitinglist);
955112346Smux	STAILQ_INIT(&bounce_map_callbacklist);
956112346Smux	mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
957112346Smux}
958112346SmuxSYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
959112346Smux
960137445Sscottlstatic struct sysctl_ctx_list *
961137445Sscottlbusdma_sysctl_tree(struct bounce_zone *bz)
962137445Sscottl{
963137445Sscottl	return (&bz->sysctl_tree);
964137445Sscottl}
965137445Sscottl
966137445Sscottlstatic struct sysctl_oid *
967137445Sscottlbusdma_sysctl_tree_top(struct bounce_zone *bz)
968137445Sscottl{
969137445Sscottl	return (bz->sysctl_tree_top);
970137445Sscottl}
971137445Sscottl
972137965Sscottlstatic int
973137445Sscottlalloc_bounce_zone(bus_dma_tag_t dmat)
974137445Sscottl{
975137445Sscottl	struct bounce_zone *bz;
976137445Sscottl
977137965Sscottl	/* Check to see if we already have a suitable zone */
978137965Sscottl	STAILQ_FOREACH(bz, &bounce_zone_list, links) {
979137965Sscottl		if ((dmat->alignment <= bz->alignment)
980137965Sscottl		 && (dmat->boundary <= bz->boundary)
981137965Sscottl		 && (dmat->lowaddr >= bz->lowaddr)) {
982137965Sscottl			dmat->bounce_zone = bz;
983137965Sscottl			return (0);
984137965Sscottl		}
985137965Sscottl	}
986137965Sscottl
987137445Sscottl	if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
988137445Sscottl	    M_NOWAIT | M_ZERO)) == NULL)
989137965Sscottl		return (ENOMEM);
990137445Sscottl
991137445Sscottl	STAILQ_INIT(&bz->bounce_page_list);
992137445Sscottl	bz->free_bpages = 0;
993137445Sscottl	bz->reserved_bpages = 0;
994137445Sscottl	bz->active_bpages = 0;
995137445Sscottl	bz->lowaddr = dmat->lowaddr;
996137445Sscottl	bz->alignment = dmat->alignment;
997137445Sscottl	bz->boundary = dmat->boundary;
998137445Sscottl	snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
999137445Sscottl	busdma_zonecount++;
1000137460Sscottl	snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1001137445Sscottl	STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1002137965Sscottl	dmat->bounce_zone = bz;
1003137445Sscottl
1004137445Sscottl	sysctl_ctx_init(&bz->sysctl_tree);
1005137445Sscottl	bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1006137445Sscottl	    SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1007137445Sscottl	    CTLFLAG_RD, 0, "");
1008137445Sscottl	if (bz->sysctl_tree_top == NULL) {
1009137445Sscottl		sysctl_ctx_free(&bz->sysctl_tree);
1010137965Sscottl		return (0);	/* XXX error code? */
1011137445Sscottl	}
1012137445Sscottl
1013137445Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1014137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1015137965Sscottl	    "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1016152775Sle	    "Total bounce pages");
1017137965Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1018137965Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1019137445Sscottl	    "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1020137445Sscottl	    "Free bounce pages");
1021137445Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1022137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1023137445Sscottl	    "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1024137445Sscottl	    "Reserved bounce pages");
1025137445Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1026137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1027137445Sscottl	    "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1028137445Sscottl	    "Active bounce pages");
1029137445Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1030137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1031137445Sscottl	    "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1032137445Sscottl	    "Total bounce requests");
1033137445Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1034137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1035137445Sscottl	    "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1036137445Sscottl	    "Total bounce requests that were deferred");
1037137445Sscottl	SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1038137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1039137445Sscottl	    "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1040137445Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1041137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1042137445Sscottl	    "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1043137445Sscottl	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1044137445Sscottl	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1045137445Sscottl	    "boundary", CTLFLAG_RD, &bz->boundary, 0, "");
1046137445Sscottl
1047137965Sscottl	return (0);
1048137445Sscottl}
1049137445Sscottl
105032516Sgibbsstatic int
105132516Sgibbsalloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
105232516Sgibbs{
1053137445Sscottl	struct bounce_zone *bz;
105432516Sgibbs	int count;
105532516Sgibbs
1056137445Sscottl	bz = dmat->bounce_zone;
105732516Sgibbs	count = 0;
105832516Sgibbs	while (numpages > 0) {
105932516Sgibbs		struct bounce_page *bpage;
106032516Sgibbs
106132516Sgibbs		bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
106269781Sdwmalone						     M_NOWAIT | M_ZERO);
106332516Sgibbs
106432516Sgibbs		if (bpage == NULL)
106532516Sgibbs			break;
106632516Sgibbs		bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
106732516Sgibbs							 M_NOWAIT, 0ul,
1068137445Sscottl							 bz->lowaddr,
1069132545Sscottl							 PAGE_SIZE,
1070137445Sscottl							 bz->boundary);
1071102241Sarchie		if (bpage->vaddr == 0) {
107232516Sgibbs			free(bpage, M_DEVBUF);
107332516Sgibbs			break;
107432516Sgibbs		}
107532516Sgibbs		bpage->busaddr = pmap_kextract(bpage->vaddr);
1076112346Smux		mtx_lock(&bounce_lock);
1077137445Sscottl		STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
107832516Sgibbs		total_bpages++;
1079137965Sscottl		bz->total_bpages++;
1080137445Sscottl		bz->free_bpages++;
1081112346Smux		mtx_unlock(&bounce_lock);
108232516Sgibbs		count++;
108332516Sgibbs		numpages--;
108432516Sgibbs	}
108532516Sgibbs	return (count);
108632516Sgibbs}
108732516Sgibbs
108832516Sgibbsstatic int
1089113228Sjakereserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
109032516Sgibbs{
1091137445Sscottl	struct bounce_zone *bz;
109232516Sgibbs	int pages;
109332516Sgibbs
1094112346Smux	mtx_assert(&bounce_lock, MA_OWNED);
1095137445Sscottl	bz = dmat->bounce_zone;
1096137445Sscottl	pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1097113228Sjake	if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1098113228Sjake		return (map->pagesneeded - (map->pagesreserved + pages));
1099137445Sscottl	bz->free_bpages -= pages;
1100137445Sscottl	bz->reserved_bpages += pages;
110132516Sgibbs	map->pagesreserved += pages;
110232516Sgibbs	pages = map->pagesneeded - map->pagesreserved;
110332516Sgibbs
110432516Sgibbs	return (pages);
110532516Sgibbs}
110632516Sgibbs
1107112569Sjakestatic bus_addr_t
110832516Sgibbsadd_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
110932516Sgibbs		bus_size_t size)
111032516Sgibbs{
1111137445Sscottl	struct bounce_zone *bz;
111232516Sgibbs	struct bounce_page *bpage;
111332516Sgibbs
1114137445Sscottl	KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1115113228Sjake	KASSERT(map != NULL && map != &nobounce_dmamap,
1116113228Sjake	    ("add_bounce_page: bad map %p", map));
1117113228Sjake
1118137445Sscottl	bz = dmat->bounce_zone;
111932516Sgibbs	if (map->pagesneeded == 0)
112032516Sgibbs		panic("add_bounce_page: map doesn't need any pages");
112132516Sgibbs	map->pagesneeded--;
112232516Sgibbs
112332516Sgibbs	if (map->pagesreserved == 0)
112432516Sgibbs		panic("add_bounce_page: map doesn't need any pages");
112532516Sgibbs	map->pagesreserved--;
112632516Sgibbs
1127112346Smux	mtx_lock(&bounce_lock);
1128137445Sscottl	bpage = STAILQ_FIRST(&bz->bounce_page_list);
112932516Sgibbs	if (bpage == NULL)
113032516Sgibbs		panic("add_bounce_page: free page list is empty");
113132516Sgibbs
1132137445Sscottl	STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1133137445Sscottl	bz->reserved_bpages--;
1134137445Sscottl	bz->active_bpages++;
1135112346Smux	mtx_unlock(&bounce_lock);
113632516Sgibbs
113732516Sgibbs	bpage->datavaddr = vaddr;
113832516Sgibbs	bpage->datacount = size;
113932516Sgibbs	STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
114032516Sgibbs	return (bpage->busaddr);
114132516Sgibbs}
114232516Sgibbs
114332516Sgibbsstatic void
114432516Sgibbsfree_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
114532516Sgibbs{
114632516Sgibbs	struct bus_dmamap *map;
1147137445Sscottl	struct bounce_zone *bz;
114832516Sgibbs
1149137445Sscottl	bz = dmat->bounce_zone;
115032516Sgibbs	bpage->datavaddr = 0;
115132516Sgibbs	bpage->datacount = 0;
115232516Sgibbs
1153112346Smux	mtx_lock(&bounce_lock);
1154137445Sscottl	STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1155137445Sscottl	bz->free_bpages++;
1156137445Sscottl	bz->active_bpages--;
115732516Sgibbs	if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1158113228Sjake		if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
115932516Sgibbs			STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
116032516Sgibbs			STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
116132516Sgibbs					   map, links);
116232516Sgibbs			busdma_swi_pending = 1;
1163137445Sscottl			bz->total_deferred++;
116488900Sjhb			swi_sched(vm_ih, 0);
116532516Sgibbs		}
116632516Sgibbs	}
1167112346Smux	mtx_unlock(&bounce_lock);
116832516Sgibbs}
116932516Sgibbs
117032516Sgibbsvoid
117195076Salfredbusdma_swi(void)
117232516Sgibbs{
1173117126Sscottl	bus_dma_tag_t dmat;
117432516Sgibbs	struct bus_dmamap *map;
117532516Sgibbs
1176112346Smux	mtx_lock(&bounce_lock);
117732516Sgibbs	while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
117832516Sgibbs		STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1179112346Smux		mtx_unlock(&bounce_lock);
1180117136Smux		dmat = map->dmat;
1181117126Sscottl		(dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
118232516Sgibbs		bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
118332516Sgibbs				map->callback, map->callback_arg, /*flags*/0);
1184117126Sscottl		(dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1185112346Smux		mtx_lock(&bounce_lock);
118632516Sgibbs	}
1187112346Smux	mtx_unlock(&bounce_lock);
118832516Sgibbs}
1189