busdma_machdep-v6.c revision 243909
1239268Sgonzo/*-
2239268Sgonzo * Copyright (c) 2010 Mark Tinguely
3239268Sgonzo * Copyright (c) 2004 Olivier Houchard
4239268Sgonzo * Copyright (c) 2002 Peter Grehan
5239268Sgonzo * Copyright (c) 1997, 1998 Justin T. Gibbs.
6239268Sgonzo * All rights reserved.
7239268Sgonzo *
8239268Sgonzo * Redistribution and use in source and binary forms, with or without
9239268Sgonzo * modification, are permitted provided that the following conditions
10239268Sgonzo * are met:
11239268Sgonzo * 1. Redistributions of source code must retain the above copyright
12239268Sgonzo *    notice, this list of conditions, and the following disclaimer,
13239268Sgonzo *    without modification, immediately at the beginning of the file.
14239268Sgonzo * 2. The name of the author may not be used to endorse or promote products
15239268Sgonzo *    derived from this software without specific prior written permission.
16239268Sgonzo *
17239268Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18239268Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19239268Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20239268Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21239268Sgonzo * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22239268Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23239268Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24239268Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25239268Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26239268Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27239268Sgonzo * SUCH DAMAGE.
28239268Sgonzo *
29239268Sgonzo *  From i386/busdma_machdep.c 191438 2009-04-23 20:24:19Z jhb
30239268Sgonzo */
31239268Sgonzo
32239268Sgonzo#include <sys/cdefs.h>
33239268Sgonzo__FBSDID("$FreeBSD: head/sys/arm/arm/busdma_machdep-v6.c 243909 2012-12-05 21:07:27Z cognet $");
34239268Sgonzo
35239268Sgonzo#define _ARM32_BUS_DMA_PRIVATE
36239268Sgonzo#include <sys/param.h>
37239268Sgonzo#include <sys/kdb.h>
38239268Sgonzo#include <ddb/ddb.h>
39239268Sgonzo#include <ddb/db_output.h>
40239268Sgonzo#include <sys/systm.h>
41239268Sgonzo#include <sys/malloc.h>
42239268Sgonzo#include <sys/bus.h>
43239268Sgonzo#include <sys/interrupt.h>
44239268Sgonzo#include <sys/kernel.h>
45239268Sgonzo#include <sys/ktr.h>
46239268Sgonzo#include <sys/lock.h>
47239268Sgonzo#include <sys/proc.h>
48239268Sgonzo#include <sys/mutex.h>
49239268Sgonzo#include <sys/mbuf.h>
50239268Sgonzo#include <sys/uio.h>
51239268Sgonzo#include <sys/sysctl.h>
52239268Sgonzo
53239268Sgonzo#include <vm/vm.h>
54239268Sgonzo#include <vm/vm_page.h>
55239268Sgonzo#include <vm/vm_map.h>
56239268Sgonzo
57239268Sgonzo#include <machine/atomic.h>
58239268Sgonzo#include <machine/bus.h>
59239268Sgonzo#include <machine/cpufunc.h>
60239268Sgonzo#include <machine/md_var.h>
61239268Sgonzo
62239268Sgonzo#define MAX_BPAGES 64
63239268Sgonzo#define BUS_DMA_COULD_BOUNCE	BUS_DMA_BUS3
64239268Sgonzo#define BUS_DMA_MIN_ALLOC_COMP	BUS_DMA_BUS4
65239268Sgonzo
66239268Sgonzo#define FIX_DMAP_BUS_DMASYNC_POSTREAD
67239268Sgonzo
68239268Sgonzostruct bounce_zone;
69239268Sgonzo
70239268Sgonzostruct bus_dma_tag {
71239268Sgonzo	bus_dma_tag_t	  parent;
72239268Sgonzo	bus_size_t	  alignment;
73239268Sgonzo	bus_size_t	  boundary;
74239268Sgonzo	bus_addr_t	  lowaddr;
75239268Sgonzo	bus_addr_t	  highaddr;
76239268Sgonzo	bus_dma_filter_t *filter;
77239268Sgonzo	void		 *filterarg;
78239268Sgonzo	bus_size_t	  maxsize;
79239268Sgonzo	u_int		  nsegments;
80239268Sgonzo	bus_size_t	  maxsegsz;
81239268Sgonzo	int		  flags;
82239268Sgonzo	int		  ref_count;
83239268Sgonzo	int		  map_count;
84239268Sgonzo	bus_dma_lock_t	 *lockfunc;
85239268Sgonzo	void		 *lockfuncarg;
86239268Sgonzo	bus_dma_segment_t *segments;
87239268Sgonzo	struct bounce_zone *bounce_zone;
88239268Sgonzo	/*
89239268Sgonzo	 * DMA range for this tag.  If the page doesn't fall within
90239268Sgonzo	 * one of these ranges, an error is returned.  The caller
91239268Sgonzo	 * may then decide what to do with the transfer.  If the
92239268Sgonzo	 * range pointer is NULL, it is ignored.
93239268Sgonzo	 */
94239268Sgonzo	struct arm32_dma_range	*ranges;
95239268Sgonzo	int			_nranges;
96239268Sgonzo
97239268Sgonzo};
98239268Sgonzo
99239268Sgonzostruct bounce_page {
100239268Sgonzo	vm_offset_t	vaddr;		/* kva of bounce buffer */
101239268Sgonzo	bus_addr_t	busaddr;	/* Physical address */
102239268Sgonzo	vm_offset_t	datavaddr;	/* kva of client data */
103239268Sgonzo	bus_size_t	datacount;	/* client data count */
104239268Sgonzo	STAILQ_ENTRY(bounce_page) links;
105239268Sgonzo};
106239268Sgonzo
107239268Sgonzostruct sync_list {
108239268Sgonzo	vm_offset_t	vaddr;		/* kva of bounce buffer */
109239268Sgonzo	bus_addr_t	busaddr;	/* Physical address */
110239268Sgonzo	bus_size_t	datacount;	/* client data count */
111239268Sgonzo	STAILQ_ENTRY(sync_list) slinks;
112239268Sgonzo};
113239268Sgonzo
114239268Sgonzoint busdma_swi_pending;
115239268Sgonzo
116239268Sgonzostruct bounce_zone {
117239268Sgonzo	STAILQ_ENTRY(bounce_zone) links;
118239268Sgonzo	STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
119239268Sgonzo	int		total_bpages;
120239268Sgonzo	int		free_bpages;
121239268Sgonzo	int		reserved_bpages;
122239268Sgonzo	int		active_bpages;
123239268Sgonzo	int		total_bounced;
124239268Sgonzo	int		total_deferred;
125239268Sgonzo	int		map_count;
126239268Sgonzo	bus_size_t	alignment;
127239268Sgonzo	bus_addr_t	lowaddr;
128239268Sgonzo	char		zoneid[8];
129239268Sgonzo	char		lowaddrid[20];
130239268Sgonzo	struct sysctl_ctx_list sysctl_tree;
131239268Sgonzo	struct sysctl_oid *sysctl_tree_top;
132239268Sgonzo};
133239268Sgonzo
134239268Sgonzostatic struct mtx bounce_lock;
135239268Sgonzostatic int total_bpages;
136239268Sgonzostatic int busdma_zonecount;
137239268Sgonzostatic STAILQ_HEAD(, bounce_zone) bounce_zone_list;
138239268Sgonzo
139239268SgonzoSYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
140239268SgonzoSYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
141239268Sgonzo	   "Total bounce pages");
142239268Sgonzo
143239268Sgonzostruct bus_dmamap {
144239268Sgonzo	struct bp_list	       bpages;
145239268Sgonzo	int		       pagesneeded;
146239268Sgonzo	int		       pagesreserved;
147239268Sgonzo	bus_dma_tag_t	       dmat;
148239268Sgonzo	void		      *buf;		/* unmapped buffer pointer */
149239268Sgonzo	bus_size_t	       buflen;		/* unmapped buffer length */
150239268Sgonzo	pmap_t		       pmap;
151239268Sgonzo	bus_dmamap_callback_t *callback;
152239268Sgonzo	void		      *callback_arg;
153239268Sgonzo	STAILQ_ENTRY(bus_dmamap) links;
154239268Sgonzo	STAILQ_HEAD(,sync_list)	slist;
155239268Sgonzo};
156239268Sgonzo
157239268Sgonzostatic STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
158239268Sgonzostatic STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
159239268Sgonzo
160239268Sgonzostatic void init_bounce_pages(void *dummy);
161239268Sgonzostatic int alloc_bounce_zone(bus_dma_tag_t dmat);
162239268Sgonzostatic int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
163239268Sgonzostatic int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
164239268Sgonzo				int commit);
165239268Sgonzostatic bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
166239268Sgonzo				   vm_offset_t vaddr, bus_size_t size);
167239268Sgonzostatic void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
168239268Sgonzoint run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
169239268Sgonzostatic int _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
170239268Sgonzo    void *buf, bus_size_t buflen, int flags);
171239268Sgonzo
172239268Sgonzostatic __inline int
173239268Sgonzo_bus_dma_can_bounce(vm_offset_t lowaddr, vm_offset_t highaddr)
174239268Sgonzo{
175239268Sgonzo	int i;
176239268Sgonzo	for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) {
177239268Sgonzo		if ((lowaddr >= phys_avail[i] && lowaddr <= phys_avail[i + 1])
178239268Sgonzo		    || (lowaddr < phys_avail[i] &&
179239268Sgonzo		    highaddr > phys_avail[i]))
180239268Sgonzo			return (1);
181239268Sgonzo	}
182239268Sgonzo	return (0);
183239268Sgonzo}
184239268Sgonzo
185239268Sgonzostatic __inline struct arm32_dma_range *
186239268Sgonzo_bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
187239268Sgonzo    bus_addr_t curaddr)
188239268Sgonzo{
189239268Sgonzo	struct arm32_dma_range *dr;
190239268Sgonzo	int i;
191239268Sgonzo
192239268Sgonzo	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
193239268Sgonzo		if (curaddr >= dr->dr_sysbase &&
194239268Sgonzo		    round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
195239268Sgonzo			return (dr);
196239268Sgonzo	}
197239268Sgonzo
198239268Sgonzo	return (NULL);
199239268Sgonzo}
200239268Sgonzo
201239268Sgonzo/*
202239268Sgonzo * Return true if a match is made.
203239268Sgonzo *
204239268Sgonzo * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
205239268Sgonzo *
206239268Sgonzo * If paddr is within the bounds of the dma tag then call the filter callback
207239268Sgonzo * to check for a match, if there is no filter callback then assume a match.
208239268Sgonzo */
209239268Sgonzoint
210239268Sgonzorun_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
211239268Sgonzo{
212239268Sgonzo	int retval;
213239268Sgonzo
214239268Sgonzo	retval = 0;
215239268Sgonzo
216239268Sgonzo	do {
217239268Sgonzo		if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
218239268Sgonzo		 || ((paddr & (dmat->alignment - 1)) != 0))
219239268Sgonzo		 && (dmat->filter == NULL
220239268Sgonzo		  || (*dmat->filter)(dmat->filterarg, paddr) != 0))
221239268Sgonzo			retval = 1;
222239268Sgonzo
223239268Sgonzo		dmat = dmat->parent;
224239268Sgonzo	} while (retval == 0 && dmat != NULL);
225239268Sgonzo	return (retval);
226239268Sgonzo}
227239268Sgonzo
228239268Sgonzo/*
229239268Sgonzo * Convenience function for manipulating driver locks from busdma (during
230239268Sgonzo * busdma_swi, for example).  Drivers that don't provide their own locks
231239268Sgonzo * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
232239268Sgonzo * non-mutex locking scheme don't have to use this at all.
233239268Sgonzo */
234239268Sgonzovoid
235239268Sgonzobusdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
236239268Sgonzo{
237239268Sgonzo	struct mtx *dmtx;
238239268Sgonzo
239239268Sgonzo	dmtx = (struct mtx *)arg;
240239268Sgonzo	switch (op) {
241239268Sgonzo	case BUS_DMA_LOCK:
242239268Sgonzo		mtx_lock(dmtx);
243239268Sgonzo		break;
244239268Sgonzo	case BUS_DMA_UNLOCK:
245239268Sgonzo		mtx_unlock(dmtx);
246239268Sgonzo		break;
247239268Sgonzo	default:
248239268Sgonzo		panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
249239268Sgonzo	}
250239268Sgonzo}
251239268Sgonzo
252239268Sgonzo/*
253239268Sgonzo * dflt_lock should never get called.  It gets put into the dma tag when
254239268Sgonzo * lockfunc == NULL, which is only valid if the maps that are associated
255239268Sgonzo * with the tag are meant to never be defered.
256239268Sgonzo * XXX Should have a way to identify which driver is responsible here.
257239268Sgonzo */
258239268Sgonzostatic void
259239268Sgonzodflt_lock(void *arg, bus_dma_lock_op_t op)
260239268Sgonzo{
261239268Sgonzo	panic("driver error: busdma dflt_lock called");
262239268Sgonzo}
263239268Sgonzo
264239268Sgonzo/*
265239268Sgonzo * Allocate a device specific dma_tag.
266239268Sgonzo */
267239268Sgonzoint
268239268Sgonzobus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
269239268Sgonzo		   bus_size_t boundary, bus_addr_t lowaddr,
270239268Sgonzo		   bus_addr_t highaddr, bus_dma_filter_t *filter,
271239268Sgonzo		   void *filterarg, bus_size_t maxsize, int nsegments,
272239268Sgonzo		   bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
273239268Sgonzo		   void *lockfuncarg, bus_dma_tag_t *dmat)
274239268Sgonzo{
275239268Sgonzo	bus_dma_tag_t newtag;
276239268Sgonzo	int error = 0;
277239268Sgonzo
278239268Sgonzo#if 0
279239268Sgonzo	if (!parent)
280239268Sgonzo		parent = arm_root_dma_tag;
281239268Sgonzo#endif
282239268Sgonzo
283239268Sgonzo	/* Basic sanity checking */
284239268Sgonzo	if (boundary != 0 && boundary < maxsegsz)
285239268Sgonzo		maxsegsz = boundary;
286239268Sgonzo
287239268Sgonzo	/* Return a NULL tag on failure */
288239268Sgonzo	*dmat = NULL;
289239268Sgonzo
290239268Sgonzo	if (maxsegsz == 0) {
291239268Sgonzo		return (EINVAL);
292239268Sgonzo	}
293239268Sgonzo
294239268Sgonzo	newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
295239268Sgonzo	    M_ZERO | M_NOWAIT);
296239268Sgonzo	if (newtag == NULL) {
297239268Sgonzo		CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
298239268Sgonzo		    __func__, newtag, 0, error);
299239268Sgonzo		return (ENOMEM);
300239268Sgonzo	}
301239268Sgonzo
302239268Sgonzo	newtag->parent = parent;
303239268Sgonzo	newtag->alignment = alignment;
304239268Sgonzo	newtag->boundary = boundary;
305239268Sgonzo	newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
306239268Sgonzo	newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
307239268Sgonzo	    (PAGE_SIZE - 1);
308239268Sgonzo	newtag->filter = filter;
309239268Sgonzo	newtag->filterarg = filterarg;
310239268Sgonzo	newtag->maxsize = maxsize;
311239268Sgonzo	newtag->nsegments = nsegments;
312239268Sgonzo	newtag->maxsegsz = maxsegsz;
313239268Sgonzo	newtag->flags = flags;
314239268Sgonzo	newtag->ref_count = 1; /* Count ourself */
315239268Sgonzo	newtag->map_count = 0;
316239268Sgonzo	newtag->ranges = bus_dma_get_range();
317239268Sgonzo	newtag->_nranges = bus_dma_get_range_nb();
318239268Sgonzo	if (lockfunc != NULL) {
319239268Sgonzo		newtag->lockfunc = lockfunc;
320239268Sgonzo		newtag->lockfuncarg = lockfuncarg;
321239268Sgonzo	} else {
322239268Sgonzo		newtag->lockfunc = dflt_lock;
323239268Sgonzo		newtag->lockfuncarg = NULL;
324239268Sgonzo	}
325239268Sgonzo	newtag->segments = NULL;
326239268Sgonzo
327239268Sgonzo	/* Take into account any restrictions imposed by our parent tag */
328239268Sgonzo	if (parent != NULL) {
329239268Sgonzo		newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
330239268Sgonzo		newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
331239268Sgonzo		if (newtag->boundary == 0)
332239268Sgonzo			newtag->boundary = parent->boundary;
333239268Sgonzo		else if (parent->boundary != 0)
334239268Sgonzo			newtag->boundary = MIN(parent->boundary,
335239268Sgonzo					       newtag->boundary);
336239268Sgonzo		if ((newtag->filter != NULL) ||
337239268Sgonzo		    ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0))
338239268Sgonzo			newtag->flags |= BUS_DMA_COULD_BOUNCE;
339239268Sgonzo		if (newtag->filter == NULL) {
340239268Sgonzo			/*
341239268Sgonzo			 * Short circuit looking at our parent directly
342239268Sgonzo			 * since we have encapsulated all of its information
343239268Sgonzo			 */
344239268Sgonzo			newtag->filter = parent->filter;
345239268Sgonzo			newtag->filterarg = parent->filterarg;
346239268Sgonzo			newtag->parent = parent->parent;
347239268Sgonzo		}
348239268Sgonzo		if (newtag->parent != NULL)
349239268Sgonzo			atomic_add_int(&parent->ref_count, 1);
350239268Sgonzo	}
351239268Sgonzo
352239268Sgonzo	if (_bus_dma_can_bounce(newtag->lowaddr, newtag->highaddr)
353239268Sgonzo	 || newtag->alignment > 1)
354239268Sgonzo		newtag->flags |= BUS_DMA_COULD_BOUNCE;
355239268Sgonzo
356239268Sgonzo	if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
357239268Sgonzo	    (flags & BUS_DMA_ALLOCNOW) != 0) {
358239268Sgonzo		struct bounce_zone *bz;
359239268Sgonzo
360239268Sgonzo		/* Must bounce */
361239268Sgonzo
362239268Sgonzo		if ((error = alloc_bounce_zone(newtag)) != 0) {
363239268Sgonzo			free(newtag, M_DEVBUF);
364239268Sgonzo			return (error);
365239268Sgonzo		}
366239268Sgonzo		bz = newtag->bounce_zone;
367239268Sgonzo
368239268Sgonzo		if (ptoa(bz->total_bpages) < maxsize) {
369239268Sgonzo			int pages;
370239268Sgonzo
371239268Sgonzo			pages = atop(maxsize) - bz->total_bpages;
372239268Sgonzo
373239268Sgonzo			/* Add pages to our bounce pool */
374239268Sgonzo			if (alloc_bounce_pages(newtag, pages) < pages)
375239268Sgonzo				error = ENOMEM;
376239268Sgonzo		}
377239268Sgonzo		/* Performed initial allocation */
378239268Sgonzo		newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
379239268Sgonzo	} else
380239268Sgonzo		newtag->bounce_zone = NULL;
381239268Sgonzo
382239268Sgonzo	if (error != 0) {
383239268Sgonzo		free(newtag, M_DEVBUF);
384239268Sgonzo	} else {
385239268Sgonzo		*dmat = newtag;
386239268Sgonzo	}
387239268Sgonzo	CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
388239268Sgonzo	    __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
389239268Sgonzo	return (error);
390239268Sgonzo}
391239268Sgonzo
392239268Sgonzoint
393239268Sgonzobus_dma_tag_destroy(bus_dma_tag_t dmat)
394239268Sgonzo{
395239268Sgonzo	bus_dma_tag_t dmat_copy;
396239268Sgonzo	int error;
397239268Sgonzo
398239268Sgonzo	error = 0;
399239268Sgonzo	dmat_copy = dmat;
400239268Sgonzo
401239268Sgonzo	if (dmat != NULL) {
402239268Sgonzo
403239268Sgonzo		if (dmat->map_count != 0) {
404239268Sgonzo			error = EBUSY;
405239268Sgonzo			goto out;
406239268Sgonzo		}
407239268Sgonzo
408239268Sgonzo		while (dmat != NULL) {
409239268Sgonzo			bus_dma_tag_t parent;
410239268Sgonzo
411239268Sgonzo			parent = dmat->parent;
412239268Sgonzo			atomic_subtract_int(&dmat->ref_count, 1);
413239268Sgonzo			if (dmat->ref_count == 0) {
414239268Sgonzo				if (dmat->segments != NULL)
415239268Sgonzo					free(dmat->segments, M_DEVBUF);
416239268Sgonzo				free(dmat, M_DEVBUF);
417239268Sgonzo				/*
418239268Sgonzo				 * Last reference count, so
419239268Sgonzo				 * release our reference
420239268Sgonzo				 * count on our parent.
421239268Sgonzo				 */
422239268Sgonzo				dmat = parent;
423239268Sgonzo			} else
424239268Sgonzo				dmat = NULL;
425239268Sgonzo		}
426239268Sgonzo	}
427239268Sgonzoout:
428239268Sgonzo	CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
429239268Sgonzo	return (error);
430239268Sgonzo}
431239268Sgonzo
432239268Sgonzo/*
433239268Sgonzo * Allocate a handle for mapping from kva/uva/physical
434239268Sgonzo * address space into bus device space.
435239268Sgonzo */
436239268Sgonzoint
437239268Sgonzobus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
438239268Sgonzo{
439239268Sgonzo	int error;
440239268Sgonzo
441239268Sgonzo	error = 0;
442239268Sgonzo
443239268Sgonzo	*mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
444239268Sgonzo					     M_NOWAIT | M_ZERO);
445239268Sgonzo	if (*mapp == NULL) {
446239268Sgonzo		CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
447239268Sgonzo		return (ENOMEM);
448239268Sgonzo	}
449239268Sgonzo	STAILQ_INIT(&((*mapp)->slist));
450239268Sgonzo
451239268Sgonzo	if (dmat->segments == NULL) {
452239268Sgonzo		dmat->segments = (bus_dma_segment_t *)malloc(
453239268Sgonzo		    sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
454239268Sgonzo		    M_NOWAIT);
455239268Sgonzo		if (dmat->segments == NULL) {
456239268Sgonzo			CTR3(KTR_BUSDMA, "%s: tag %p error %d",
457239268Sgonzo			    __func__, dmat, ENOMEM);
458239268Sgonzo			free(*mapp, M_DEVBUF);
459239268Sgonzo			*mapp = NULL;
460239268Sgonzo			return (ENOMEM);
461239268Sgonzo		}
462239268Sgonzo	}
463239268Sgonzo	/*
464239268Sgonzo	 * Bouncing might be required if the driver asks for an active
465239268Sgonzo	 * exclusion region, a data alignment that is stricter than 1, and/or
466239268Sgonzo	 * an active address boundary.
467239268Sgonzo	 */
468239268Sgonzo	if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
469239268Sgonzo
470239268Sgonzo		/* Must bounce */
471239268Sgonzo		struct bounce_zone *bz;
472239268Sgonzo		int maxpages;
473239268Sgonzo
474239268Sgonzo		if (dmat->bounce_zone == NULL) {
475239268Sgonzo			if ((error = alloc_bounce_zone(dmat)) != 0) {
476239268Sgonzo				free(*mapp, M_DEVBUF);
477239268Sgonzo				*mapp = NULL;
478239268Sgonzo				return (error);
479239268Sgonzo			}
480239268Sgonzo		}
481239268Sgonzo		bz = dmat->bounce_zone;
482239268Sgonzo
483239268Sgonzo		/* Initialize the new map */
484239268Sgonzo		STAILQ_INIT(&((*mapp)->bpages));
485239268Sgonzo
486239268Sgonzo		/*
487239268Sgonzo		 * Attempt to add pages to our pool on a per-instance
488239268Sgonzo		 * basis up to a sane limit.
489239268Sgonzo		 */
490239268Sgonzo		maxpages = MAX_BPAGES;
491239268Sgonzo		if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
492239268Sgonzo		 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
493239268Sgonzo			int pages;
494239268Sgonzo
495239268Sgonzo			pages = MAX(atop(dmat->maxsize), 1);
496239268Sgonzo			pages = MIN(maxpages - bz->total_bpages, pages);
497239268Sgonzo			pages = MAX(pages, 1);
498239268Sgonzo			if (alloc_bounce_pages(dmat, pages) < pages)
499239268Sgonzo				error = ENOMEM;
500239268Sgonzo
501239268Sgonzo			if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
502239268Sgonzo				if (error == 0)
503239268Sgonzo					dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
504239268Sgonzo			} else {
505239268Sgonzo				error = 0;
506239268Sgonzo			}
507239268Sgonzo		}
508239268Sgonzo		bz->map_count++;
509239268Sgonzo	}
510239268Sgonzo	if (error == 0)
511239268Sgonzo		dmat->map_count++;
512239268Sgonzo	CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
513239268Sgonzo	    __func__, dmat, dmat->flags, error);
514239268Sgonzo	return (error);
515239268Sgonzo}
516239268Sgonzo
517239268Sgonzo/*
518239268Sgonzo * Destroy a handle for mapping from kva/uva/physical
519239268Sgonzo * address space into bus device space.
520239268Sgonzo */
521239268Sgonzoint
522239268Sgonzobus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
523239268Sgonzo{
524239268Sgonzo	if (STAILQ_FIRST(&map->bpages) != NULL ||
525239268Sgonzo	    STAILQ_FIRST(&map->slist) != NULL) {
526239268Sgonzo		CTR3(KTR_BUSDMA, "%s: tag %p error %d",
527239268Sgonzo		    __func__, dmat, EBUSY);
528239268Sgonzo		return (EBUSY);
529239268Sgonzo	}
530239268Sgonzo	if (dmat->bounce_zone)
531239268Sgonzo		dmat->bounce_zone->map_count--;
532239268Sgonzo	free(map, M_DEVBUF);
533239268Sgonzo	dmat->map_count--;
534239268Sgonzo	CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
535239268Sgonzo	return (0);
536239268Sgonzo}
537239268Sgonzo
538239268Sgonzo
539239268Sgonzo/*
540239268Sgonzo * Allocate a piece of memory that can be efficiently mapped into
541239268Sgonzo * bus device space based on the constraints lited in the dma tag.
542239268Sgonzo * A dmamap to for use with dmamap_load is also allocated.
543239268Sgonzo */
544239268Sgonzoint
545239268Sgonzobus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
546239268Sgonzo		 bus_dmamap_t *mapp)
547239268Sgonzo{
548239268Sgonzo	int mflags, len;
549239268Sgonzo
550239268Sgonzo	if (flags & BUS_DMA_NOWAIT)
551239268Sgonzo		mflags = M_NOWAIT;
552239268Sgonzo	else
553239268Sgonzo		mflags = M_WAITOK;
554239268Sgonzo
555239268Sgonzo	/* ARM non-snooping caches need a map for the VA cache sync structure */
556239268Sgonzo
557239268Sgonzo	*mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
558239268Sgonzo					     M_NOWAIT | M_ZERO);
559239268Sgonzo	if (*mapp == NULL) {
560239268Sgonzo		CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
561239268Sgonzo		    __func__, dmat, dmat->flags, ENOMEM);
562239268Sgonzo		return (ENOMEM);
563239268Sgonzo	}
564239268Sgonzo
565239268Sgonzo	STAILQ_INIT(&((*mapp)->slist));
566239268Sgonzo
567239268Sgonzo	if (dmat->segments == NULL) {
568239268Sgonzo		dmat->segments = (bus_dma_segment_t *)malloc(
569239268Sgonzo		    sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
570239268Sgonzo		    mflags);
571239268Sgonzo		if (dmat->segments == NULL) {
572239268Sgonzo			CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
573239268Sgonzo			    __func__, dmat, dmat->flags, ENOMEM);
574239268Sgonzo			free(*mapp, M_DEVBUF);
575239268Sgonzo			*mapp = NULL;
576239268Sgonzo			return (ENOMEM);
577239268Sgonzo		}
578239268Sgonzo	}
579239268Sgonzo
580239268Sgonzo	if (flags & BUS_DMA_ZERO)
581239268Sgonzo		mflags |= M_ZERO;
582239268Sgonzo
583239268Sgonzo	/*
584239268Sgonzo	 * XXX:
585239268Sgonzo	 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
586239268Sgonzo	 * alignment guarantees of malloc need to be nailed down, and the
587239268Sgonzo	 * code below should be rewritten to take that into account.
588239268Sgonzo	 *
589239268Sgonzo	 * In the meantime, we'll warn the user if malloc gets it wrong.
590239268Sgonzo	 *
591239268Sgonzo	 * allocate at least a cache line. This should help avoid cache
592239268Sgonzo	 * corruption.
593239268Sgonzo	 */
594239268Sgonzo	len = max(dmat->maxsize, arm_dcache_align);
595239268Sgonzo        if (len <= PAGE_SIZE &&
596239268Sgonzo	   (dmat->alignment < len) &&
597239268Sgonzo	   !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr)) {
598239268Sgonzo		*vaddr = malloc(len, M_DEVBUF, mflags);
599239268Sgonzo	} else {
600239268Sgonzo		/*
601239268Sgonzo		 * XXX Use Contigmalloc until it is merged into this facility
602239268Sgonzo		 *     and handles multi-seg allocations.  Nobody is doing
603239268Sgonzo		 *     multi-seg allocations yet though.
604239268Sgonzo		 * XXX Certain AGP hardware does.
605239268Sgonzo		 */
606239268Sgonzo		*vaddr = contigmalloc(len, M_DEVBUF, mflags,
607239268Sgonzo		    0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
608239268Sgonzo		    dmat->boundary);
609239268Sgonzo	}
610239268Sgonzo	if (*vaddr == NULL) {
611239268Sgonzo		CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
612239268Sgonzo		    __func__, dmat, dmat->flags, ENOMEM);
613239268Sgonzo		free(*mapp, M_DEVBUF);
614239268Sgonzo		*mapp = NULL;
615239268Sgonzo		return (ENOMEM);
616239268Sgonzo	} else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
617239268Sgonzo		printf("bus_dmamem_alloc failed to align memory properly.\n");
618239268Sgonzo	}
619239268Sgonzo	dmat->map_count++;
620239268Sgonzo
621239268Sgonzo	CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
622239268Sgonzo	    __func__, dmat, dmat->flags, 0);
623239268Sgonzo	return (0);
624239268Sgonzo}
625239268Sgonzo
626239268Sgonzo/*
627239268Sgonzo * Free a piece of memory and it's allociated dmamap, that was allocated
628239268Sgonzo * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
629239268Sgonzo */
630239268Sgonzovoid
631239268Sgonzobus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
632239268Sgonzo{
633239268Sgonzo	int len;
634239268Sgonzo
635239268Sgonzo#ifdef mftnotyet
636239268Sgonzo	pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, ARM_WRITE_BACK);
637239268Sgonzo#endif
638239268Sgonzo	len = max(dmat->maxsize, arm_dcache_align);
639239268Sgonzo        if (len <= PAGE_SIZE &&
640239268Sgonzo	   (dmat->alignment < len) &&
641239268Sgonzo	   !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr))
642239268Sgonzo		free(vaddr, M_DEVBUF);
643239268Sgonzo	else {
644239268Sgonzo		contigfree(vaddr, len, M_DEVBUF);
645239268Sgonzo	}
646239268Sgonzo	dmat->map_count--;
647239268Sgonzo	free(map, M_DEVBUF);
648239268Sgonzo	CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
649239268Sgonzo}
650239268Sgonzo
651239268Sgonzostatic int
652239268Sgonzo_bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
653239268Sgonzo    void *buf, bus_size_t buflen, int flags)
654239268Sgonzo{
655239268Sgonzo	vm_offset_t vaddr;
656239268Sgonzo	vm_offset_t vendaddr;
657239268Sgonzo	bus_addr_t paddr;
658239268Sgonzo
659239268Sgonzo	if (map->pagesneeded == 0) {
660239268Sgonzo		CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d"
661239268Sgonzo		    " map= %p, pagesneeded= %d",
662239268Sgonzo		    dmat->lowaddr, dmat->boundary, dmat->alignment,
663239268Sgonzo		    map, map->pagesneeded);
664239268Sgonzo		/*
665239268Sgonzo		 * Count the number of bounce pages
666239268Sgonzo		 * needed in order to complete this transfer
667239268Sgonzo		 */
668239268Sgonzo		vaddr = (vm_offset_t)buf;
669239268Sgonzo		vendaddr = (vm_offset_t)buf + buflen;
670239268Sgonzo
671239268Sgonzo		while (vaddr < vendaddr) {
672239268Sgonzo			if (__predict_true(map->pmap == pmap_kernel()))
673239268Sgonzo				paddr = pmap_kextract(vaddr);
674239268Sgonzo			else
675239268Sgonzo				paddr = pmap_extract(map->pmap, vaddr);
676239268Sgonzo			if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
677239268Sgonzo			    run_filter(dmat, paddr) != 0) {
678239268Sgonzo				map->pagesneeded++;
679239268Sgonzo			}
680239268Sgonzo			vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
681239268Sgonzo
682239268Sgonzo		}
683239268Sgonzo		CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded);
684239268Sgonzo	}
685239268Sgonzo
686239268Sgonzo	/* Reserve Necessary Bounce Pages */
687239268Sgonzo	if (map->pagesneeded != 0) {
688239268Sgonzo		mtx_lock(&bounce_lock);
689239268Sgonzo		if (flags & BUS_DMA_NOWAIT) {
690239268Sgonzo			if (reserve_bounce_pages(dmat, map, 0) != 0) {
691239268Sgonzo				map->pagesneeded = 0;
692239268Sgonzo				mtx_unlock(&bounce_lock);
693239268Sgonzo				return (ENOMEM);
694239268Sgonzo			}
695239268Sgonzo		} else {
696239268Sgonzo			if (reserve_bounce_pages(dmat, map, 1) != 0) {
697239268Sgonzo				/* Queue us for resources */
698239268Sgonzo				map->dmat = dmat;
699239268Sgonzo				map->buf = buf;
700239268Sgonzo				map->buflen = buflen;
701239268Sgonzo				STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
702239268Sgonzo				    map, links);
703239268Sgonzo				mtx_unlock(&bounce_lock);
704239268Sgonzo				return (EINPROGRESS);
705239268Sgonzo			}
706239268Sgonzo		}
707239268Sgonzo		mtx_unlock(&bounce_lock);
708239268Sgonzo	}
709239268Sgonzo
710239268Sgonzo	return (0);
711239268Sgonzo}
712239268Sgonzo
713239268Sgonzo/*
714239268Sgonzo * Utility function to load a linear buffer. lastaddrp holds state
715239268Sgonzo * between invocations (for multiple-buffer loads).  segp contains
716239268Sgonzo * the starting segment on entrace, and the ending segment on exit.
717239268Sgonzo * first indicates if this is the first invocation of this function.
718239268Sgonzo */
719239268Sgonzostatic __inline int
720239268Sgonzo_bus_dmamap_load_buffer(bus_dma_tag_t dmat,
721239268Sgonzo			bus_dmamap_t map,
722239268Sgonzo			void *buf, bus_size_t buflen,
723239268Sgonzo			int flags,
724239268Sgonzo			bus_addr_t *lastaddrp,
725239268Sgonzo			bus_dma_segment_t *segs,
726239268Sgonzo			int *segp,
727239268Sgonzo			int first)
728239268Sgonzo{
729239268Sgonzo	bus_size_t sgsize;
730239268Sgonzo	bus_addr_t curaddr, lastaddr, baddr, bmask;
731239268Sgonzo	vm_offset_t vaddr;
732239268Sgonzo	struct sync_list *sl;
733239268Sgonzo	int seg, error;
734239268Sgonzo
735239268Sgonzo	if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
736239268Sgonzo		error = _bus_dmamap_count_pages(dmat, map, buf, buflen, flags);
737239268Sgonzo		if (error)
738239268Sgonzo			return (error);
739239268Sgonzo	}
740239268Sgonzo
741239268Sgonzo	sl = NULL;
742239268Sgonzo	vaddr = (vm_offset_t)buf;
743239268Sgonzo	lastaddr = *lastaddrp;
744239268Sgonzo	bmask = ~(dmat->boundary - 1);
745239268Sgonzo
746239268Sgonzo	for (seg = *segp; buflen > 0 ; ) {
747239268Sgonzo		/*
748239268Sgonzo		 * Get the physical address for this segment.
749239268Sgonzo		 */
750239268Sgonzo		if (__predict_true(map->pmap == pmap_kernel()))
751239268Sgonzo			curaddr = pmap_kextract(vaddr);
752239268Sgonzo		else
753239268Sgonzo			curaddr = pmap_extract(map->pmap, vaddr);
754239268Sgonzo
755239268Sgonzo		/*
756239268Sgonzo		 * Compute the segment size, and adjust counts.
757239268Sgonzo		 */
758239268Sgonzo		sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
759239268Sgonzo		if (sgsize > dmat->maxsegsz)
760239268Sgonzo			sgsize = dmat->maxsegsz;
761239268Sgonzo		if (buflen < sgsize)
762239268Sgonzo			sgsize = buflen;
763239268Sgonzo
764239268Sgonzo		/*
765239268Sgonzo		 * Make sure we don't cross any boundaries.
766239268Sgonzo		 */
767239268Sgonzo		if (dmat->boundary > 0) {
768239268Sgonzo			baddr = (curaddr + dmat->boundary) & bmask;
769239268Sgonzo			if (sgsize > (baddr - curaddr))
770239268Sgonzo				sgsize = (baddr - curaddr);
771239268Sgonzo		}
772239268Sgonzo
773239268Sgonzo		if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
774239268Sgonzo		    map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
775239268Sgonzo			curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
776239268Sgonzo		} else {
777239268Sgonzo			/* add_sync_list(dmat, map, vaddr, sgsize, cflag); */
778239268Sgonzo			sl = (struct sync_list *)malloc(sizeof(struct sync_list),
779239268Sgonzo						M_DEVBUF, M_NOWAIT | M_ZERO);
780239268Sgonzo			if (sl == NULL)
781239268Sgonzo				goto cleanup;
782239268Sgonzo			STAILQ_INSERT_TAIL(&(map->slist), sl, slinks);
783239268Sgonzo			sl->vaddr = vaddr;
784239268Sgonzo			sl->datacount = sgsize;
785239268Sgonzo			sl->busaddr = curaddr;
786239268Sgonzo		}
787239268Sgonzo
788239268Sgonzo
789239268Sgonzo		if (dmat->ranges) {
790239268Sgonzo			struct arm32_dma_range *dr;
791239268Sgonzo
792239268Sgonzo			dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges,
793239268Sgonzo			    curaddr);
794239268Sgonzo			if (dr == NULL) {
795239268Sgonzo				_bus_dmamap_unload(dmat, map);
796239268Sgonzo				return (EINVAL);
797239268Sgonzo			}
798239268Sgonzo			/*
799239268Sgonzo			 * In a valid DMA range.  Translate the physical
800239268Sgonzo			 * memory address to an address in the DMA window.
801239268Sgonzo			 */
802239268Sgonzo			curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
803239268Sgonzo		}
804239268Sgonzo
805239268Sgonzo		/*
806239268Sgonzo		 * Insert chunk into a segment, coalescing with
807239268Sgonzo		 * previous segment if possible.
808239268Sgonzo		 */
809239268Sgonzo		if (first) {
810239268Sgonzo			segs[seg].ds_addr = curaddr;
811239268Sgonzo			segs[seg].ds_len = sgsize;
812239268Sgonzo			first = 0;
813239268Sgonzo		} else {
814239268Sgonzo			if (curaddr == lastaddr &&
815239268Sgonzo			    (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
816239268Sgonzo			    (dmat->boundary == 0 ||
817239268Sgonzo			     (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
818239268Sgonzo				segs[seg].ds_len += sgsize;
819239268Sgonzo			else {
820239268Sgonzo				if (++seg >= dmat->nsegments)
821239268Sgonzo					break;
822239268Sgonzo				segs[seg].ds_addr = curaddr;
823239268Sgonzo				segs[seg].ds_len = sgsize;
824239268Sgonzo			}
825239268Sgonzo		}
826239268Sgonzo
827239268Sgonzo		lastaddr = curaddr + sgsize;
828239268Sgonzo		vaddr += sgsize;
829239268Sgonzo		buflen -= sgsize;
830239268Sgonzo	}
831239268Sgonzo
832239268Sgonzo	*segp = seg;
833239268Sgonzo	*lastaddrp = lastaddr;
834239268Sgonzocleanup:
835239268Sgonzo	/*
836239268Sgonzo	 * Did we fit?
837239268Sgonzo	 */
838239268Sgonzo	if (buflen != 0) {
839239268Sgonzo		_bus_dmamap_unload(dmat, map);
840239268Sgonzo		return(EFBIG); /* XXX better return value here? */
841239268Sgonzo	}
842239268Sgonzo	return (0);
843239268Sgonzo}
844239268Sgonzo
845239268Sgonzo/*
846239268Sgonzo * Map the buffer buf into bus space using the dmamap map.
847239268Sgonzo */
848239268Sgonzoint
849239268Sgonzobus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
850239268Sgonzo		bus_size_t buflen, bus_dmamap_callback_t *callback,
851239268Sgonzo		void *callback_arg, int flags)
852239268Sgonzo{
853239268Sgonzo	bus_addr_t		lastaddr = 0;
854239268Sgonzo	int			error, nsegs = 0;
855239268Sgonzo
856239268Sgonzo	flags |= BUS_DMA_WAITOK;
857239268Sgonzo	map->callback = callback;
858239268Sgonzo	map->callback_arg = callback_arg;
859239268Sgonzo	map->pmap = kernel_pmap;
860239268Sgonzo
861239268Sgonzo	error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, flags,
862239268Sgonzo		     &lastaddr, dmat->segments, &nsegs, 1);
863239268Sgonzo
864239268Sgonzo	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
865239268Sgonzo	    __func__, dmat, dmat->flags, error, nsegs + 1);
866239268Sgonzo
867239268Sgonzo	if (error == EINPROGRESS) {
868239268Sgonzo		return (error);
869239268Sgonzo	}
870239268Sgonzo
871239268Sgonzo	if (error)
872239268Sgonzo		(*callback)(callback_arg, dmat->segments, 0, error);
873239268Sgonzo	else
874239268Sgonzo		(*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
875239268Sgonzo
876239268Sgonzo	/*
877239268Sgonzo	 * Return ENOMEM to the caller so that it can pass it up the stack.
878239268Sgonzo	 * This error only happens when NOWAIT is set, so deferal is disabled.
879239268Sgonzo	 */
880239268Sgonzo	if (error == ENOMEM)
881239268Sgonzo		return (error);
882239268Sgonzo
883239268Sgonzo	return (0);
884239268Sgonzo}
885239268Sgonzo
886239268Sgonzo
887239268Sgonzo/*
888239268Sgonzo * Like _bus_dmamap_load(), but for mbufs.
889239268Sgonzo */
890239268Sgonzostatic __inline int
891239268Sgonzo_bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
892239268Sgonzo			struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
893239268Sgonzo			int flags)
894239268Sgonzo{
895239268Sgonzo	int error;
896239268Sgonzo
897239268Sgonzo	M_ASSERTPKTHDR(m0);
898239268Sgonzo	map->pmap = kernel_pmap;
899239268Sgonzo
900239268Sgonzo	flags |= BUS_DMA_NOWAIT;
901239268Sgonzo	*nsegs = 0;
902239268Sgonzo	error = 0;
903239268Sgonzo	if (m0->m_pkthdr.len <= dmat->maxsize) {
904239268Sgonzo		int first = 1;
905239268Sgonzo		bus_addr_t lastaddr = 0;
906239268Sgonzo		struct mbuf *m;
907239268Sgonzo
908239268Sgonzo		for (m = m0; m != NULL && error == 0; m = m->m_next) {
909239268Sgonzo			if (m->m_len > 0) {
910239268Sgonzo				error = _bus_dmamap_load_buffer(dmat, map,
911239268Sgonzo						m->m_data, m->m_len,
912239268Sgonzo						flags, &lastaddr,
913239268Sgonzo						segs, nsegs, first);
914239268Sgonzo				first = 0;
915239268Sgonzo			}
916239268Sgonzo		}
917239268Sgonzo	} else {
918239268Sgonzo		error = EINVAL;
919239268Sgonzo	}
920239268Sgonzo
921239268Sgonzo	/* XXX FIXME: Having to increment nsegs is really annoying */
922239268Sgonzo	++*nsegs;
923239268Sgonzo	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
924239268Sgonzo	    __func__, dmat, dmat->flags, error, *nsegs);
925239268Sgonzo	return (error);
926239268Sgonzo}
927239268Sgonzo
928239268Sgonzoint
929239268Sgonzobus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
930239268Sgonzo		     struct mbuf *m0,
931239268Sgonzo		     bus_dmamap_callback2_t *callback, void *callback_arg,
932239268Sgonzo		     int flags)
933239268Sgonzo{
934239268Sgonzo	int nsegs, error;
935239268Sgonzo
936239268Sgonzo	error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, dmat->segments, &nsegs,
937239268Sgonzo		    flags);
938239268Sgonzo
939239268Sgonzo	if (error) {
940239268Sgonzo		/* force "no valid mappings" in callback */
941239268Sgonzo		(*callback)(callback_arg, dmat->segments, 0, 0, error);
942239268Sgonzo	} else {
943239268Sgonzo		(*callback)(callback_arg, dmat->segments,
944239268Sgonzo			    nsegs, m0->m_pkthdr.len, error);
945239268Sgonzo	}
946239268Sgonzo	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
947239268Sgonzo	    __func__, dmat, dmat->flags, error, nsegs);
948239268Sgonzo
949239268Sgonzo	return (error);
950239268Sgonzo}
951239268Sgonzo
952239268Sgonzoint
953239268Sgonzobus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
954239268Sgonzo			struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
955239268Sgonzo			int flags)
956239268Sgonzo{
957239268Sgonzo	return (_bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags));
958239268Sgonzo}
959239268Sgonzo
960239268Sgonzo/*
961239268Sgonzo * Like _bus_dmamap_load(), but for uios.
962239268Sgonzo */
963239268Sgonzoint
964239268Sgonzobus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
965239268Sgonzo		    struct uio *uio,
966239268Sgonzo		    bus_dmamap_callback2_t *callback, void *callback_arg,
967239268Sgonzo		    int flags)
968239268Sgonzo{
969239268Sgonzo	bus_addr_t lastaddr;
970239268Sgonzo	int nsegs, error, first, i;
971239268Sgonzo	bus_size_t resid;
972239268Sgonzo	struct iovec *iov;
973239268Sgonzo
974239268Sgonzo	flags |= BUS_DMA_NOWAIT;
975239268Sgonzo	resid = uio->uio_resid;
976239268Sgonzo	iov = uio->uio_iov;
977239268Sgonzo
978239268Sgonzo	if (uio->uio_segflg == UIO_USERSPACE) {
979239268Sgonzo		KASSERT(uio->uio_td != NULL,
980239268Sgonzo			("bus_dmamap_load_uio: USERSPACE but no proc"));
981239268Sgonzo		map->pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
982239268Sgonzo	} else
983239268Sgonzo		map->pmap = kernel_pmap;
984239268Sgonzo
985239268Sgonzo	nsegs = 0;
986239268Sgonzo	error = 0;
987239268Sgonzo	first = 1;
988239268Sgonzo	lastaddr = (bus_addr_t) 0;
989239268Sgonzo	for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
990239268Sgonzo		/*
991239268Sgonzo		 * Now at the first iovec to load.  Load each iovec
992239268Sgonzo		 * until we have exhausted the residual count.
993239268Sgonzo		 */
994239268Sgonzo		bus_size_t minlen =
995239268Sgonzo			resid < iov[i].iov_len ? resid : iov[i].iov_len;
996239268Sgonzo		caddr_t addr = (caddr_t) iov[i].iov_base;
997239268Sgonzo
998239268Sgonzo		if (minlen > 0) {
999239268Sgonzo			error = _bus_dmamap_load_buffer(dmat, map,
1000239268Sgonzo					addr, minlen, flags, &lastaddr,
1001239268Sgonzo					dmat->segments, &nsegs, first);
1002239268Sgonzo			first = 0;
1003239268Sgonzo			resid -= minlen;
1004239268Sgonzo		}
1005239268Sgonzo	}
1006239268Sgonzo
1007239268Sgonzo	if (error) {
1008239268Sgonzo		/* force "no valid mappings" in callback */
1009239268Sgonzo		(*callback)(callback_arg, dmat->segments, 0, 0, error);
1010239268Sgonzo	} else {
1011239268Sgonzo		(*callback)(callback_arg, dmat->segments,
1012239268Sgonzo			    nsegs+1, uio->uio_resid, error);
1013239268Sgonzo	}
1014239268Sgonzo	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
1015239268Sgonzo	    __func__, dmat, dmat->flags, error, nsegs + 1);
1016239268Sgonzo	return (error);
1017239268Sgonzo}
1018239268Sgonzo
1019239268Sgonzo/*
1020239268Sgonzo * Release the mapping held by map.
1021239268Sgonzo */
1022239268Sgonzovoid
1023239268Sgonzo_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
1024239268Sgonzo{
1025239268Sgonzo	struct bounce_page *bpage;
1026239268Sgonzo	struct bounce_zone *bz;
1027239268Sgonzo	struct sync_list *sl;
1028239268Sgonzo
1029239268Sgonzo        while ((sl = STAILQ_FIRST(&map->slist)) != NULL) {
1030239268Sgonzo                STAILQ_REMOVE_HEAD(&map->slist, slinks);
1031239268Sgonzo                free(sl, M_DEVBUF);
1032239268Sgonzo        }
1033239268Sgonzo
1034239268Sgonzo	if ((bz = dmat->bounce_zone) != NULL) {
1035239268Sgonzo		while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1036239268Sgonzo			STAILQ_REMOVE_HEAD(&map->bpages, links);
1037239268Sgonzo			free_bounce_page(dmat, bpage);
1038239268Sgonzo		}
1039239268Sgonzo
1040239268Sgonzo		bz = dmat->bounce_zone;
1041239268Sgonzo		bz->free_bpages += map->pagesreserved;
1042239268Sgonzo		bz->reserved_bpages -= map->pagesreserved;
1043239268Sgonzo		map->pagesreserved = 0;
1044239268Sgonzo		map->pagesneeded = 0;
1045239268Sgonzo	}
1046239268Sgonzo}
1047239268Sgonzo
1048239268Sgonzo#ifdef notyetbounceuser
1049239268Sgonzo	/* If busdma uses user pages, then the interrupt handler could
1050239268Sgonzo	 * be use the kernel vm mapping. Both bounce pages and sync list
1051239268Sgonzo	 * do not cross page boundaries.
1052239268Sgonzo	 * Below is a rough sequence that a person would do to fix the
1053239268Sgonzo	 * user page reference in the kernel vmspace. This would be
1054239268Sgonzo	 * done in the dma post routine.
1055239268Sgonzo	 */
1056239268Sgonzovoid
1057239268Sgonzo_bus_dmamap_fix_user(vm_offset_t buf, bus_size_t len,
1058239268Sgonzo			pmap_t pmap, int op)
1059239268Sgonzo{
1060239268Sgonzo	bus_size_t sgsize;
1061239268Sgonzo	bus_addr_t curaddr;
1062239268Sgonzo	vm_offset_t va;
1063239268Sgonzo
1064239268Sgonzo		/* each synclist entry is contained within a single page.
1065239268Sgonzo		 *
1066239268Sgonzo		 * this would be needed if BUS_DMASYNC_POSTxxxx was implemented
1067239268Sgonzo		*/
1068239268Sgonzo	curaddr = pmap_extract(pmap, buf);
1069239268Sgonzo	va = pmap_dma_map(curaddr);
1070239268Sgonzo	switch (op) {
1071239268Sgonzo	case SYNC_USER_INV:
1072239268Sgonzo		cpu_dcache_wb_range(va, sgsize);
1073239268Sgonzo		break;
1074239268Sgonzo
1075239268Sgonzo	case SYNC_USER_COPYTO:
1076239268Sgonzo		bcopy((void *)va, (void *)bounce, sgsize);
1077239268Sgonzo		break;
1078239268Sgonzo
1079239268Sgonzo	case SYNC_USER_COPYFROM:
1080239268Sgonzo		bcopy((void *) bounce, (void *)va, sgsize);
1081239268Sgonzo		break;
1082239268Sgonzo
1083239268Sgonzo	default:
1084239268Sgonzo		break;
1085239268Sgonzo	}
1086239268Sgonzo
1087239268Sgonzo	pmap_dma_unmap(va);
1088239268Sgonzo}
1089239268Sgonzo#endif
1090239268Sgonzo
1091239268Sgonzo#ifdef ARM_L2_PIPT
1092239268Sgonzo#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(pa, size)
1093239268Sgonzo#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(pa, size)
1094239268Sgonzo#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(pa, size)
1095239268Sgonzo#else
1096239268Sgonzo#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(va, size)
1097239268Sgonzo#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(va, size)
1098243909Scognet#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(va, size)
1099239268Sgonzo#endif
1100239268Sgonzo
1101239268Sgonzovoid
1102239268Sgonzo_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1103239268Sgonzo{
1104239268Sgonzo	struct bounce_page *bpage;
1105239268Sgonzo	struct sync_list *sl;
1106239268Sgonzo	bus_size_t len, unalign;
1107239268Sgonzo	vm_offset_t buf, ebuf;
1108239268Sgonzo#ifdef FIX_DMAP_BUS_DMASYNC_POSTREAD
1109239268Sgonzo	vm_offset_t bbuf;
1110239268Sgonzo	char _tmp_cl[arm_dcache_align], _tmp_clend[arm_dcache_align];
1111239268Sgonzo#endif
1112239268Sgonzo	int listcount = 0;
1113239268Sgonzo
1114239268Sgonzo		/* if buffer was from user space, it it possible that this
1115239268Sgonzo		 * is not the same vm map. The fix is to map each page in
1116239268Sgonzo		 * the buffer into the current address space (KVM) and then
1117239268Sgonzo		 * do the bounce copy or sync list cache operation.
1118239268Sgonzo		 *
1119239268Sgonzo		 * The sync list entries are already broken into
1120239268Sgonzo		 * their respective physical pages.
1121239268Sgonzo		 */
1122239268Sgonzo	if (!pmap_dmap_iscurrent(map->pmap))
1123239268Sgonzo		printf("_bus_dmamap_sync: wrong user map: %p %x\n", map->pmap, op);
1124239268Sgonzo
1125239268Sgonzo	if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1126239268Sgonzo
1127239268Sgonzo		/* Handle data bouncing. */
1128239268Sgonzo		CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
1129239268Sgonzo		    "performing bounce", __func__, dmat, dmat->flags, op);
1130239268Sgonzo
1131239268Sgonzo		if (op & BUS_DMASYNC_PREWRITE) {
1132239268Sgonzo			while (bpage != NULL) {
1133239268Sgonzo				bcopy((void *)bpage->datavaddr,
1134239268Sgonzo				      (void *)bpage->vaddr,
1135239268Sgonzo				      bpage->datacount);
1136239268Sgonzo				cpu_dcache_wb_range((vm_offset_t)bpage->vaddr,
1137239268Sgonzo					bpage->datacount);
1138239268Sgonzo				l2cache_wb_range((vm_offset_t)bpage->vaddr,
1139239268Sgonzo				    (vm_offset_t)bpage->busaddr,
1140239268Sgonzo				    bpage->datacount);
1141239268Sgonzo				bpage = STAILQ_NEXT(bpage, links);
1142239268Sgonzo			}
1143239268Sgonzo			dmat->bounce_zone->total_bounced++;
1144239268Sgonzo		}
1145239268Sgonzo
1146239268Sgonzo		if (op & BUS_DMASYNC_POSTREAD) {
1147239268Sgonzo			if (!pmap_dmap_iscurrent(map->pmap))
1148239268Sgonzo			    panic("_bus_dmamap_sync: wrong user map. apply fix");
1149239268Sgonzo
1150239268Sgonzo			cpu_dcache_inv_range((vm_offset_t)bpage->vaddr,
1151239268Sgonzo					bpage->datacount);
1152239268Sgonzo			l2cache_inv_range((vm_offset_t)bpage->vaddr,
1153239268Sgonzo			    (vm_offset_t)bpage->busaddr,
1154239268Sgonzo			    bpage->datacount);
1155239268Sgonzo			while (bpage != NULL) {
1156239268Sgonzo				vm_offset_t startv;
1157239268Sgonzo				vm_paddr_t startp;
1158239268Sgonzo				int len;
1159239268Sgonzo
1160239268Sgonzo				startv = bpage->vaddr &~ arm_dcache_align_mask;
1161239268Sgonzo				startp = bpage->busaddr &~ arm_dcache_align_mask;
1162239268Sgonzo				len = bpage->datacount;
1163239268Sgonzo
1164239268Sgonzo				if (startv != bpage->vaddr)
1165239268Sgonzo					len += bpage->vaddr & arm_dcache_align_mask;
1166239268Sgonzo				if (len & arm_dcache_align_mask)
1167239268Sgonzo					len = (len -
1168239268Sgonzo					    (len & arm_dcache_align_mask)) +
1169239268Sgonzo					    arm_dcache_align;
1170239268Sgonzo				cpu_dcache_inv_range(startv, len);
1171239268Sgonzo				l2cache_inv_range(startv, startp, len);
1172239268Sgonzo				bcopy((void *)bpage->vaddr,
1173239268Sgonzo				      (void *)bpage->datavaddr,
1174239268Sgonzo				      bpage->datacount);
1175239268Sgonzo				bpage = STAILQ_NEXT(bpage, links);
1176239268Sgonzo			}
1177239268Sgonzo			dmat->bounce_zone->total_bounced++;
1178239268Sgonzo		}
1179239268Sgonzo	}
1180239268Sgonzo
1181239268Sgonzo	sl = STAILQ_FIRST(&map->slist);
1182239268Sgonzo	while (sl) {
1183239268Sgonzo		listcount++;
1184239268Sgonzo		sl = STAILQ_NEXT(sl, slinks);
1185239268Sgonzo	}
1186239268Sgonzo	if ((sl = STAILQ_FIRST(&map->slist)) != NULL) {
1187239268Sgonzo		/* ARM caches are not self-snooping for dma */
1188239268Sgonzo
1189239268Sgonzo		CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
1190239268Sgonzo		    "performing sync", __func__, dmat, dmat->flags, op);
1191239268Sgonzo
1192239268Sgonzo		switch (op) {
1193239268Sgonzo		case BUS_DMASYNC_PREWRITE:
1194239268Sgonzo			while (sl != NULL) {
1195239268Sgonzo			    cpu_dcache_wb_range(sl->vaddr, sl->datacount);
1196239268Sgonzo			    l2cache_wb_range(sl->vaddr, sl->busaddr,
1197239268Sgonzo				sl->datacount);
1198239268Sgonzo			    sl = STAILQ_NEXT(sl, slinks);
1199239268Sgonzo			}
1200239268Sgonzo			break;
1201239268Sgonzo
1202239268Sgonzo		case BUS_DMASYNC_PREREAD:
1203239268Sgonzo			while (sl != NULL) {
1204239268Sgonzo					/* write back the unaligned portions */
1205239268Sgonzo				vm_paddr_t physaddr = sl->busaddr, ephysaddr;
1206239268Sgonzo				buf = sl->vaddr;
1207239268Sgonzo				len = sl->datacount;
1208239268Sgonzo				ebuf = buf + len;	/* end of buffer */
1209239268Sgonzo				ephysaddr = physaddr + len;
1210239268Sgonzo				unalign = buf & arm_dcache_align_mask;
1211239268Sgonzo				if (unalign) {
1212239268Sgonzo						/* wbinv leading fragment */
1213239268Sgonzo					buf &= ~arm_dcache_align_mask;
1214239268Sgonzo					physaddr &= ~arm_dcache_align_mask;
1215239268Sgonzo					cpu_dcache_wbinv_range(buf,
1216239268Sgonzo							arm_dcache_align);
1217239268Sgonzo					l2cache_wbinv_range(buf, physaddr,
1218239268Sgonzo					    arm_dcache_align);
1219239268Sgonzo					buf += arm_dcache_align;
1220239268Sgonzo					physaddr += arm_dcache_align;
1221239268Sgonzo					/* number byte in buffer wbinv */
1222239268Sgonzo					unalign = arm_dcache_align - unalign;
1223239268Sgonzo					if (len > unalign)
1224239268Sgonzo						len -= unalign;
1225239268Sgonzo					else
1226239268Sgonzo						len = 0;
1227239268Sgonzo				}
1228239268Sgonzo				unalign = ebuf & arm_dcache_align_mask;
1229239268Sgonzo				if (ebuf > buf && unalign) {
1230239268Sgonzo						/* wbinv trailing fragment */
1231239268Sgonzo					len -= unalign;
1232239268Sgonzo					ebuf -= unalign;
1233239268Sgonzo					ephysaddr -= unalign;
1234239268Sgonzo					cpu_dcache_wbinv_range(ebuf,
1235239268Sgonzo							arm_dcache_align);
1236239268Sgonzo					l2cache_wbinv_range(ebuf, ephysaddr,
1237239268Sgonzo					    arm_dcache_align);
1238239268Sgonzo				}
1239239268Sgonzo				if (ebuf > buf) {
1240239268Sgonzo					cpu_dcache_inv_range(buf, len);
1241239268Sgonzo					l2cache_inv_range(buf, physaddr, len);
1242239268Sgonzo				}
1243239268Sgonzo				sl = STAILQ_NEXT(sl, slinks);
1244239268Sgonzo			}
1245239268Sgonzo			break;
1246239268Sgonzo
1247239268Sgonzo		case BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD:
1248239268Sgonzo			while (sl != NULL) {
1249239268Sgonzo				cpu_dcache_wbinv_range(sl->vaddr, sl->datacount);
1250239268Sgonzo				l2cache_wbinv_range(sl->vaddr,
1251239268Sgonzo				    sl->busaddr, sl->datacount);
1252239268Sgonzo				sl = STAILQ_NEXT(sl, slinks);
1253239268Sgonzo			}
1254239268Sgonzo			break;
1255239268Sgonzo
1256239268Sgonzo#ifdef FIX_DMAP_BUS_DMASYNC_POSTREAD
1257239268Sgonzo		case BUS_DMASYNC_POSTREAD:
1258239268Sgonzo			if (!pmap_dmap_iscurrent(map->pmap))
1259239268Sgonzo			     panic("_bus_dmamap_sync: wrong user map. apply fix");
1260239268Sgonzo			while (sl != NULL) {
1261239268Sgonzo					/* write back the unaligned portions */
1262239268Sgonzo				vm_paddr_t physaddr;
1263239268Sgonzo				buf = sl->vaddr;
1264239268Sgonzo				len = sl->datacount;
1265239268Sgonzo				physaddr = sl->busaddr;
1266239268Sgonzo				bbuf = buf & ~arm_dcache_align_mask;
1267239268Sgonzo				ebuf = buf + len;
1268239268Sgonzo				physaddr = physaddr & ~arm_dcache_align_mask;
1269239268Sgonzo				unalign = buf & arm_dcache_align_mask;
1270239268Sgonzo				if (unalign) {
1271239268Sgonzo					memcpy(_tmp_cl, (void *)bbuf, unalign);
1272239268Sgonzo					len += unalign; /* inv entire cache line */
1273239268Sgonzo				}
1274239268Sgonzo				unalign = ebuf & arm_dcache_align_mask;
1275239268Sgonzo				if (unalign) {
1276239268Sgonzo					unalign = arm_dcache_align - unalign;
1277239268Sgonzo					memcpy(_tmp_clend, (void *)ebuf, unalign);
1278239268Sgonzo					len += unalign; /* inv entire cache line */
1279239268Sgonzo				}
1280239268Sgonzo					/* inv are cache length aligned */
1281239268Sgonzo				cpu_dcache_inv_range(bbuf, len);
1282239268Sgonzo				l2cache_inv_range(bbuf, physaddr, len);
1283239268Sgonzo
1284239268Sgonzo				unalign = (vm_offset_t)buf & arm_dcache_align_mask;
1285239268Sgonzo				if (unalign) {
1286239268Sgonzo					memcpy((void *)bbuf, _tmp_cl, unalign);
1287239268Sgonzo				}
1288239268Sgonzo				unalign = ebuf & arm_dcache_align_mask;
1289239268Sgonzo				if (unalign) {
1290239268Sgonzo					unalign = arm_dcache_align - unalign;
1291239268Sgonzo					memcpy((void *)ebuf, _tmp_clend, unalign);
1292239268Sgonzo				}
1293239268Sgonzo				sl = STAILQ_NEXT(sl, slinks);
1294239268Sgonzo			}
1295239268Sgonzo				break;
1296239268Sgonzo#endif /* FIX_DMAP_BUS_DMASYNC_POSTREAD */
1297239268Sgonzo
1298239268Sgonzo		default:
1299239268Sgonzo			break;
1300239268Sgonzo		}
1301239268Sgonzo	}
1302239268Sgonzo}
1303239268Sgonzo
1304239268Sgonzostatic void
1305239268Sgonzoinit_bounce_pages(void *dummy __unused)
1306239268Sgonzo{
1307239268Sgonzo
1308239268Sgonzo	total_bpages = 0;
1309239268Sgonzo	STAILQ_INIT(&bounce_zone_list);
1310239268Sgonzo	STAILQ_INIT(&bounce_map_waitinglist);
1311239268Sgonzo	STAILQ_INIT(&bounce_map_callbacklist);
1312239268Sgonzo	mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
1313239268Sgonzo}
1314239268SgonzoSYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
1315239268Sgonzo
1316239268Sgonzostatic struct sysctl_ctx_list *
1317239268Sgonzobusdma_sysctl_tree(struct bounce_zone *bz)
1318239268Sgonzo{
1319239268Sgonzo	return (&bz->sysctl_tree);
1320239268Sgonzo}
1321239268Sgonzo
1322239268Sgonzostatic struct sysctl_oid *
1323239268Sgonzobusdma_sysctl_tree_top(struct bounce_zone *bz)
1324239268Sgonzo{
1325239268Sgonzo	return (bz->sysctl_tree_top);
1326239268Sgonzo}
1327239268Sgonzo
1328239268Sgonzostatic int
1329239268Sgonzoalloc_bounce_zone(bus_dma_tag_t dmat)
1330239268Sgonzo{
1331239268Sgonzo	struct bounce_zone *bz;
1332239268Sgonzo
1333239268Sgonzo	/* Check to see if we already have a suitable zone */
1334239268Sgonzo	STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1335239268Sgonzo		if ((dmat->alignment <= bz->alignment)
1336239268Sgonzo		 && (dmat->lowaddr >= bz->lowaddr)) {
1337239268Sgonzo			dmat->bounce_zone = bz;
1338239268Sgonzo			return (0);
1339239268Sgonzo		}
1340239268Sgonzo	}
1341239268Sgonzo
1342239268Sgonzo	if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
1343239268Sgonzo	    M_NOWAIT | M_ZERO)) == NULL)
1344239268Sgonzo		return (ENOMEM);
1345239268Sgonzo
1346239268Sgonzo	STAILQ_INIT(&bz->bounce_page_list);
1347239268Sgonzo	bz->free_bpages = 0;
1348239268Sgonzo	bz->reserved_bpages = 0;
1349239268Sgonzo	bz->active_bpages = 0;
1350239268Sgonzo	bz->lowaddr = dmat->lowaddr;
1351239268Sgonzo	bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1352239268Sgonzo	bz->map_count = 0;
1353239268Sgonzo	snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1354239268Sgonzo	busdma_zonecount++;
1355239268Sgonzo	snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1356239268Sgonzo	STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1357239268Sgonzo	dmat->bounce_zone = bz;
1358239268Sgonzo
1359239268Sgonzo	sysctl_ctx_init(&bz->sysctl_tree);
1360239268Sgonzo	bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1361239268Sgonzo	    SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1362239268Sgonzo	    CTLFLAG_RD, 0, "");
1363239268Sgonzo	if (bz->sysctl_tree_top == NULL) {
1364239268Sgonzo		sysctl_ctx_free(&bz->sysctl_tree);
1365239268Sgonzo		return (0);	/* XXX error code? */
1366239268Sgonzo	}
1367239268Sgonzo
1368239268Sgonzo	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1369239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1370239268Sgonzo	    "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1371239268Sgonzo	    "Total bounce pages");
1372239268Sgonzo	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1373239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1374239268Sgonzo	    "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1375239268Sgonzo	    "Free bounce pages");
1376239268Sgonzo	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1377239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1378239268Sgonzo	    "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1379239268Sgonzo	    "Reserved bounce pages");
1380239268Sgonzo	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1381239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1382239268Sgonzo	    "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1383239268Sgonzo	    "Active bounce pages");
1384239268Sgonzo	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1385239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1386239268Sgonzo	    "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1387239268Sgonzo	    "Total bounce requests");
1388239268Sgonzo	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1389239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1390239268Sgonzo	    "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1391239268Sgonzo	    "Total bounce requests that were deferred");
1392239268Sgonzo	SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1393239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1394239268Sgonzo	    "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1395239268Sgonzo	SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1396239268Sgonzo	    SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1397239268Sgonzo	    "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1398239268Sgonzo
1399239268Sgonzo	return (0);
1400239268Sgonzo}
1401239268Sgonzo
1402239268Sgonzostatic int
1403239268Sgonzoalloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1404239268Sgonzo{
1405239268Sgonzo	struct bounce_zone *bz;
1406239268Sgonzo	int count;
1407239268Sgonzo
1408239268Sgonzo	bz = dmat->bounce_zone;
1409239268Sgonzo	count = 0;
1410239268Sgonzo	while (numpages > 0) {
1411239268Sgonzo		struct bounce_page *bpage;
1412239268Sgonzo
1413239268Sgonzo		bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1414239268Sgonzo						     M_NOWAIT | M_ZERO);
1415239268Sgonzo
1416239268Sgonzo		if (bpage == NULL)
1417239268Sgonzo			break;
1418239268Sgonzo		bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1419239268Sgonzo							 M_NOWAIT, 0ul,
1420239268Sgonzo							 bz->lowaddr,
1421239268Sgonzo							 PAGE_SIZE,
1422239268Sgonzo							 0);
1423239268Sgonzo		if (bpage->vaddr == 0) {
1424239268Sgonzo			free(bpage, M_DEVBUF);
1425239268Sgonzo			break;
1426239268Sgonzo		}
1427239268Sgonzo		bpage->busaddr = pmap_kextract(bpage->vaddr);
1428239268Sgonzo		mtx_lock(&bounce_lock);
1429239268Sgonzo		STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1430239268Sgonzo		total_bpages++;
1431239268Sgonzo		bz->total_bpages++;
1432239268Sgonzo		bz->free_bpages++;
1433239268Sgonzo		mtx_unlock(&bounce_lock);
1434239268Sgonzo		count++;
1435239268Sgonzo		numpages--;
1436239268Sgonzo	}
1437239268Sgonzo	return (count);
1438239268Sgonzo}
1439239268Sgonzo
1440239268Sgonzostatic int
1441239268Sgonzoreserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1442239268Sgonzo{
1443239268Sgonzo	struct bounce_zone *bz;
1444239268Sgonzo	int pages;
1445239268Sgonzo
1446239268Sgonzo	mtx_assert(&bounce_lock, MA_OWNED);
1447239268Sgonzo	bz = dmat->bounce_zone;
1448239268Sgonzo	pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1449239268Sgonzo	if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1450239268Sgonzo		return (map->pagesneeded - (map->pagesreserved + pages));
1451239268Sgonzo	bz->free_bpages -= pages;
1452239268Sgonzo	bz->reserved_bpages += pages;
1453239268Sgonzo	map->pagesreserved += pages;
1454239268Sgonzo	pages = map->pagesneeded - map->pagesreserved;
1455239268Sgonzo
1456239268Sgonzo	return (pages);
1457239268Sgonzo}
1458239268Sgonzo
1459239268Sgonzostatic bus_addr_t
1460239268Sgonzoadd_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1461239268Sgonzo		bus_size_t size)
1462239268Sgonzo{
1463239268Sgonzo	struct bounce_zone *bz;
1464239268Sgonzo	struct bounce_page *bpage;
1465239268Sgonzo
1466239268Sgonzo	KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1467239268Sgonzo	KASSERT(map != NULL,
1468239268Sgonzo	    ("add_bounce_page: bad map %p", map));
1469239268Sgonzo
1470239268Sgonzo	bz = dmat->bounce_zone;
1471239268Sgonzo	if (map->pagesneeded == 0)
1472239268Sgonzo		panic("add_bounce_page: map doesn't need any pages");
1473239268Sgonzo	map->pagesneeded--;
1474239268Sgonzo
1475239268Sgonzo	if (map->pagesreserved == 0)
1476239268Sgonzo		panic("add_bounce_page: map doesn't need any pages");
1477239268Sgonzo	map->pagesreserved--;
1478239268Sgonzo
1479239268Sgonzo	mtx_lock(&bounce_lock);
1480239268Sgonzo	bpage = STAILQ_FIRST(&bz->bounce_page_list);
1481239268Sgonzo	if (bpage == NULL)
1482239268Sgonzo		panic("add_bounce_page: free page list is empty");
1483239268Sgonzo
1484239268Sgonzo	STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1485239268Sgonzo	bz->reserved_bpages--;
1486239268Sgonzo	bz->active_bpages++;
1487239268Sgonzo	mtx_unlock(&bounce_lock);
1488239268Sgonzo
1489239268Sgonzo	if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1490239268Sgonzo		/* Page offset needs to be preserved. */
1491239268Sgonzo		bpage->vaddr |= vaddr & PAGE_MASK;
1492239268Sgonzo		bpage->busaddr |= vaddr & PAGE_MASK;
1493239268Sgonzo	}
1494239268Sgonzo	bpage->datavaddr = vaddr;
1495239268Sgonzo	bpage->datacount = size;
1496239268Sgonzo	STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1497239268Sgonzo	return (bpage->busaddr);
1498239268Sgonzo}
1499239268Sgonzo
1500239268Sgonzostatic void
1501239268Sgonzofree_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1502239268Sgonzo{
1503239268Sgonzo	struct bus_dmamap *map;
1504239268Sgonzo	struct bounce_zone *bz;
1505239268Sgonzo
1506239268Sgonzo	bz = dmat->bounce_zone;
1507239268Sgonzo	bpage->datavaddr = 0;
1508239268Sgonzo	bpage->datacount = 0;
1509239268Sgonzo	if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1510239268Sgonzo		/*
1511239268Sgonzo		 * Reset the bounce page to start at offset 0.  Other uses
1512239268Sgonzo		 * of this bounce page may need to store a full page of
1513239268Sgonzo		 * data and/or assume it starts on a page boundary.
1514239268Sgonzo		 */
1515239268Sgonzo		bpage->vaddr &= ~PAGE_MASK;
1516239268Sgonzo		bpage->busaddr &= ~PAGE_MASK;
1517239268Sgonzo	}
1518239268Sgonzo
1519239268Sgonzo	mtx_lock(&bounce_lock);
1520239268Sgonzo	STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1521239268Sgonzo	bz->free_bpages++;
1522239268Sgonzo	bz->active_bpages--;
1523239268Sgonzo	if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1524239268Sgonzo		if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1525239268Sgonzo			STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1526239268Sgonzo			STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1527239268Sgonzo					   map, links);
1528239268Sgonzo			busdma_swi_pending = 1;
1529239268Sgonzo			bz->total_deferred++;
1530239268Sgonzo			swi_sched(vm_ih, 0);
1531239268Sgonzo		}
1532239268Sgonzo	}
1533239268Sgonzo	mtx_unlock(&bounce_lock);
1534239268Sgonzo}
1535239268Sgonzo
1536239268Sgonzovoid
1537239268Sgonzobusdma_swi(void)
1538239268Sgonzo{
1539239268Sgonzo	bus_dma_tag_t dmat;
1540239268Sgonzo	struct bus_dmamap *map;
1541239268Sgonzo
1542239268Sgonzo	mtx_lock(&bounce_lock);
1543239268Sgonzo	while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1544239268Sgonzo		STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1545239268Sgonzo		mtx_unlock(&bounce_lock);
1546239268Sgonzo		dmat = map->dmat;
1547239268Sgonzo		(dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1548239268Sgonzo		bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1549239268Sgonzo				map->callback, map->callback_arg, /*flags*/0);
1550239268Sgonzo		(dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1551239268Sgonzo		mtx_lock(&bounce_lock);
1552239268Sgonzo	}
1553239268Sgonzo	mtx_unlock(&bounce_lock);
1554239268Sgonzo}
1555