busdma_machdep.c revision 95076
1/*
2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. The name of the author may not be used to endorse or promote products
12 *    derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/i386/i386/busdma_machdep.c 95076 2002-04-19 22:58:09Z alfred $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/malloc.h>
32#include <sys/bus.h>
33#include <sys/interrupt.h>
34#include <sys/lock.h>
35#include <sys/proc.h>
36#include <sys/mutex.h>
37
38#include <vm/vm.h>
39#include <vm/vm_page.h>
40
41#include <machine/bus.h>
42#include <machine/md_var.h>
43
44#define MAX(a,b) (((a) > (b)) ? (a) : (b))
45#define MIN(a,b) (((a) < (b)) ? (a) : (b))
46#define MAX_BPAGES 128
47
48struct bus_dma_tag {
49	bus_dma_tag_t	  parent;
50	bus_size_t	  alignment;
51	bus_size_t	  boundary;
52	bus_addr_t	  lowaddr;
53	bus_addr_t	  highaddr;
54	bus_dma_filter_t *filter;
55	void		 *filterarg;
56	bus_size_t	  maxsize;
57	u_int		  nsegments;
58	bus_size_t	  maxsegsz;
59	int		  flags;
60	int		  ref_count;
61	int		  map_count;
62};
63
64struct bounce_page {
65	vm_offset_t	vaddr;		/* kva of bounce buffer */
66	bus_addr_t	busaddr;	/* Physical address */
67	vm_offset_t	datavaddr;	/* kva of client data */
68	bus_size_t	datacount;	/* client data count */
69	STAILQ_ENTRY(bounce_page) links;
70};
71
72int busdma_swi_pending;
73
74static STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
75static int free_bpages;
76static int reserved_bpages;
77static int active_bpages;
78static int total_bpages;
79static bus_addr_t bounce_lowaddr = BUS_SPACE_MAXADDR;
80
81struct bus_dmamap {
82	struct bp_list	       bpages;
83	int		       pagesneeded;
84	int		       pagesreserved;
85	bus_dma_tag_t	       dmat;
86	void		      *buf;		/* unmapped buffer pointer */
87	bus_size_t	       buflen;		/* unmapped buffer length */
88	bus_dmamap_callback_t *callback;
89	void		      *callback_arg;
90	STAILQ_ENTRY(bus_dmamap) links;
91};
92
93static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
94static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
95static struct bus_dmamap nobounce_dmamap;
96
97static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
98static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map);
99static vm_offset_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
100				   vm_offset_t vaddr, bus_size_t size);
101static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
102static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
103
104/*
105 * Return true if a match is made.
106 *
107 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
108 *
109 * If paddr is within the bounds of the dma tag then call the filter callback
110 * to check for a match, if there is no filter callback then assume a match.
111 */
112static __inline int
113run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
114{
115	int retval;
116
117	retval = 0;
118	do {
119		if (paddr > dmat->lowaddr
120		 && paddr <= dmat->highaddr
121		 && (dmat->filter == NULL
122		  || (*dmat->filter)(dmat->filterarg, paddr) != 0))
123			retval = 1;
124
125		dmat = dmat->parent;
126	} while (retval == 0 && dmat != NULL);
127	return (retval);
128}
129
130#define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
131/*
132 * Allocate a device specific dma_tag.
133 */
134int
135bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
136		   bus_size_t boundary, bus_addr_t lowaddr,
137		   bus_addr_t highaddr, bus_dma_filter_t *filter,
138		   void *filterarg, bus_size_t maxsize, int nsegments,
139		   bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
140{
141	bus_dma_tag_t newtag;
142	int error = 0;
143
144	/* Return a NULL tag on failure */
145	*dmat = NULL;
146
147	newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
148	if (newtag == NULL)
149		return (ENOMEM);
150
151	newtag->parent = parent;
152	newtag->alignment = alignment;
153	newtag->boundary = boundary;
154	newtag->lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
155	newtag->highaddr = trunc_page((vm_offset_t)highaddr) + (PAGE_SIZE - 1);
156	newtag->filter = filter;
157	newtag->filterarg = filterarg;
158	newtag->maxsize = maxsize;
159	newtag->nsegments = nsegments;
160	newtag->maxsegsz = maxsegsz;
161	newtag->flags = flags;
162	newtag->ref_count = 1; /* Count ourself */
163	newtag->map_count = 0;
164
165	/* Take into account any restrictions imposed by our parent tag */
166	if (parent != NULL) {
167		newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
168		newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
169		/*
170		 * XXX Not really correct??? Probably need to honor boundary
171		 *     all the way up the inheritence chain.
172		 */
173		newtag->boundary = MAX(parent->boundary, newtag->boundary);
174		if (newtag->filter == NULL) {
175			/*
176			 * Short circuit looking at our parent directly
177			 * since we have encapsulated all of its information
178			 */
179			newtag->filter = parent->filter;
180			newtag->filterarg = parent->filterarg;
181			newtag->parent = parent->parent;
182		}
183		if (newtag->parent != NULL) {
184			parent->ref_count++;
185		}
186	}
187
188	if (newtag->lowaddr < ptoa(Maxmem) && (flags & BUS_DMA_ALLOCNOW) != 0) {
189		/* Must bounce */
190
191		if (lowaddr > bounce_lowaddr) {
192			/*
193			 * Go through the pool and kill any pages
194			 * that don't reside below lowaddr.
195			 */
196			panic("bus_dma_tag_create: page reallocation "
197			      "not implemented");
198		}
199		if (ptoa(total_bpages) < maxsize) {
200			int pages;
201
202			pages = atop(maxsize) - total_bpages;
203
204			/* Add pages to our bounce pool */
205			if (alloc_bounce_pages(newtag, pages) < pages)
206				error = ENOMEM;
207		}
208		/* Performed initial allocation */
209		newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
210	}
211
212	if (error != 0) {
213		free(newtag, M_DEVBUF);
214	} else {
215		*dmat = newtag;
216	}
217	return (error);
218}
219
220int
221bus_dma_tag_destroy(bus_dma_tag_t dmat)
222{
223	if (dmat != NULL) {
224
225		if (dmat->map_count != 0)
226			return (EBUSY);
227
228		while (dmat != NULL) {
229			bus_dma_tag_t parent;
230
231			parent = dmat->parent;
232			dmat->ref_count--;
233			if (dmat->ref_count == 0) {
234				free(dmat, M_DEVBUF);
235				/*
236				 * Last reference count, so
237				 * release our reference
238				 * count on our parent.
239				 */
240				dmat = parent;
241			} else
242				dmat = NULL;
243		}
244	}
245	return (0);
246}
247
248/*
249 * Allocate a handle for mapping from kva/uva/physical
250 * address space into bus device space.
251 */
252int
253bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
254{
255	int error;
256
257	error = 0;
258
259	if (dmat->lowaddr < ptoa(Maxmem)) {
260		/* Must bounce */
261		int maxpages;
262
263		*mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
264					     M_NOWAIT | M_ZERO);
265		if (*mapp == NULL)
266			return (ENOMEM);
267
268		/* Initialize the new map */
269		STAILQ_INIT(&((*mapp)->bpages));
270
271		/*
272		 * Attempt to add pages to our pool on a per-instance
273		 * basis up to a sane limit.
274		 */
275		maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
276		if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
277		 || (dmat->map_count > 0
278		  && total_bpages < maxpages)) {
279			int pages;
280
281			if (dmat->lowaddr > bounce_lowaddr) {
282				/*
283				 * Go through the pool and kill any pages
284				 * that don't reside below lowaddr.
285				 */
286				panic("bus_dmamap_create: page reallocation "
287				      "not implemented");
288			}
289			pages = atop(dmat->maxsize);
290			pages = MIN(maxpages - total_bpages, pages);
291			error = alloc_bounce_pages(dmat, pages);
292
293			if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
294				if (error == 0)
295					dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
296			} else {
297				error = 0;
298			}
299		}
300	} else {
301		*mapp = NULL;
302	}
303	if (error == 0)
304		dmat->map_count++;
305	return (error);
306}
307
308/*
309 * Destroy a handle for mapping from kva/uva/physical
310 * address space into bus device space.
311 */
312int
313bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
314{
315	if (map != NULL) {
316		if (STAILQ_FIRST(&map->bpages) != NULL)
317			return (EBUSY);
318		free(map, M_DEVBUF);
319	}
320	dmat->map_count--;
321	return (0);
322}
323
324
325/*
326 * Allocate a piece of memory that can be efficiently mapped into
327 * bus device space based on the constraints lited in the dma tag.
328 * A dmamap to for use with dmamap_load is also allocated.
329 */
330int
331bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
332		 bus_dmamap_t *mapp)
333{
334	/* If we succeed, no mapping/bouncing will be required */
335	*mapp = NULL;
336
337	if ((dmat->maxsize <= PAGE_SIZE) && dmat->lowaddr >= ptoa(Maxmem)) {
338		*vaddr = malloc(dmat->maxsize, M_DEVBUF,
339				(flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
340	} else {
341		/*
342		 * XXX Use Contigmalloc until it is merged into this facility
343		 *     and handles multi-seg allocations.  Nobody is doing
344		 *     multi-seg allocations yet though.
345		 */
346		*vaddr = contigmalloc(dmat->maxsize, M_DEVBUF,
347		    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK,
348		    0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
349		    dmat->boundary);
350	}
351	if (*vaddr == NULL)
352		return (ENOMEM);
353	return (0);
354}
355
356/*
357 * Free a piece of memory and it's allociated dmamap, that was allocated
358 * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
359 */
360void
361bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
362{
363	/*
364	 * dmamem does not need to be bounced, so the map should be
365	 * NULL
366	 */
367	if (map != NULL)
368		panic("bus_dmamem_free: Invalid map freed\n");
369	if ((dmat->maxsize <= PAGE_SIZE) && dmat->lowaddr >= ptoa(Maxmem))
370		free(vaddr, M_DEVBUF);
371	else
372		contigfree(vaddr, dmat->maxsize, M_DEVBUF);
373}
374
375#define BUS_DMAMAP_NSEGS ((BUS_SPACE_MAXSIZE / PAGE_SIZE) + 1)
376
377/*
378 * Map the buffer buf into bus space using the dmamap map.
379 */
380int
381bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
382		bus_size_t buflen, bus_dmamap_callback_t *callback,
383		void *callback_arg, int flags)
384{
385	vm_offset_t		vaddr;
386	vm_offset_t		paddr;
387#ifdef __GNUC__
388	bus_dma_segment_t	dm_segments[dmat->nsegments];
389#else
390	bus_dma_segment_t	dm_segments[BUS_DMAMAP_NSEGS];
391#endif
392	bus_dma_segment_t      *sg;
393	int			seg;
394	int			error;
395	vm_offset_t		nextpaddr;
396
397	if (map == NULL)
398		map = &nobounce_dmamap;
399
400	error = 0;
401	/*
402	 * If we are being called during a callback, pagesneeded will
403	 * be non-zero, so we can avoid doing the work twice.
404	 */
405	if (dmat->lowaddr < ptoa(Maxmem) && map->pagesneeded == 0) {
406		vm_offset_t	vendaddr;
407
408		/*
409		 * Count the number of bounce pages
410		 * needed in order to complete this transfer
411		 */
412		vaddr = trunc_page((vm_offset_t)buf);
413		vendaddr = (vm_offset_t)buf + buflen;
414
415		while (vaddr < vendaddr) {
416			paddr = pmap_kextract(vaddr);
417			if (run_filter(dmat, paddr) != 0) {
418
419				map->pagesneeded++;
420			}
421			vaddr += PAGE_SIZE;
422		}
423	}
424
425	/* Reserve Necessary Bounce Pages */
426	if (map->pagesneeded != 0) {
427		int s;
428
429		s = splhigh();
430	 	if (reserve_bounce_pages(dmat, map) != 0) {
431
432			/* Queue us for resources */
433			map->dmat = dmat;
434			map->buf = buf;
435			map->buflen = buflen;
436			map->callback = callback;
437			map->callback_arg = callback_arg;
438
439			STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
440			splx(s);
441
442			return (EINPROGRESS);
443		}
444		splx(s);
445	}
446
447	vaddr = (vm_offset_t)buf;
448	sg = &dm_segments[0];
449	seg = 1;
450	sg->ds_len = 0;
451
452	nextpaddr = 0;
453	do {
454		bus_size_t	size;
455
456		paddr = pmap_kextract(vaddr);
457		size = PAGE_SIZE - (paddr & PAGE_MASK);
458		if (size > buflen)
459			size = buflen;
460
461		if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
462			paddr = add_bounce_page(dmat, map, vaddr, size);
463		}
464
465		if (sg->ds_len == 0) {
466			sg->ds_addr = paddr;
467			sg->ds_len = size;
468		} else if (paddr == nextpaddr) {
469			sg->ds_len += size;
470		} else {
471			/* Go to the next segment */
472			sg++;
473			seg++;
474			if (seg > dmat->nsegments)
475				break;
476			sg->ds_addr = paddr;
477			sg->ds_len = size;
478		}
479		vaddr += size;
480		nextpaddr = paddr + size;
481		buflen -= size;
482
483	} while (buflen > 0);
484
485	if (buflen != 0) {
486		printf("bus_dmamap_load: Too many segs! buf_len = 0x%lx\n",
487		       (u_long)buflen);
488		error = EFBIG;
489	}
490
491	(*callback)(callback_arg, dm_segments, seg, error);
492
493	return (0);
494}
495
496/*
497 * Release the mapping held by map.
498 */
499void
500_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
501{
502	struct bounce_page *bpage;
503
504	while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
505		STAILQ_REMOVE_HEAD(&map->bpages, links);
506		free_bounce_page(dmat, bpage);
507	}
508}
509
510void
511_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
512{
513	struct bounce_page *bpage;
514
515	if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
516
517		/*
518		 * Handle data bouncing.  We might also
519		 * want to add support for invalidating
520		 * the caches on broken hardware
521		 */
522		switch (op) {
523		case BUS_DMASYNC_PREWRITE:
524			while (bpage != NULL) {
525				bcopy((void *)bpage->datavaddr,
526				      (void *)bpage->vaddr,
527				      bpage->datacount);
528				bpage = STAILQ_NEXT(bpage, links);
529			}
530			break;
531
532		case BUS_DMASYNC_POSTREAD:
533			while (bpage != NULL) {
534				bcopy((void *)bpage->vaddr,
535				      (void *)bpage->datavaddr,
536				      bpage->datacount);
537				bpage = STAILQ_NEXT(bpage, links);
538			}
539			break;
540		case BUS_DMASYNC_PREREAD:
541		case BUS_DMASYNC_POSTWRITE:
542			/* No-ops */
543			break;
544		}
545	}
546}
547
548static int
549alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
550{
551	int count;
552
553	count = 0;
554	if (total_bpages == 0) {
555		STAILQ_INIT(&bounce_page_list);
556		STAILQ_INIT(&bounce_map_waitinglist);
557		STAILQ_INIT(&bounce_map_callbacklist);
558	}
559
560	while (numpages > 0) {
561		struct bounce_page *bpage;
562		int s;
563
564		bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
565						     M_NOWAIT | M_ZERO);
566
567		if (bpage == NULL)
568			break;
569		bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
570							 M_NOWAIT, 0ul,
571							 dmat->lowaddr,
572							 PAGE_SIZE,
573							 0);
574		if (bpage->vaddr == NULL) {
575			free(bpage, M_DEVBUF);
576			break;
577		}
578		bpage->busaddr = pmap_kextract(bpage->vaddr);
579		s = splhigh();
580		STAILQ_INSERT_TAIL(&bounce_page_list, bpage, links);
581		total_bpages++;
582		free_bpages++;
583		splx(s);
584		count++;
585		numpages--;
586	}
587	return (count);
588}
589
590static int
591reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
592{
593	int pages;
594
595	pages = MIN(free_bpages, map->pagesneeded - map->pagesreserved);
596	free_bpages -= pages;
597	reserved_bpages += pages;
598	map->pagesreserved += pages;
599	pages = map->pagesneeded - map->pagesreserved;
600
601	return (pages);
602}
603
604static vm_offset_t
605add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
606		bus_size_t size)
607{
608	int s;
609	struct bounce_page *bpage;
610
611	if (map->pagesneeded == 0)
612		panic("add_bounce_page: map doesn't need any pages");
613	map->pagesneeded--;
614
615	if (map->pagesreserved == 0)
616		panic("add_bounce_page: map doesn't need any pages");
617	map->pagesreserved--;
618
619	s = splhigh();
620	bpage = STAILQ_FIRST(&bounce_page_list);
621	if (bpage == NULL)
622		panic("add_bounce_page: free page list is empty");
623
624	STAILQ_REMOVE_HEAD(&bounce_page_list, links);
625	reserved_bpages--;
626	active_bpages++;
627	splx(s);
628
629	bpage->datavaddr = vaddr;
630	bpage->datacount = size;
631	STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
632	return (bpage->busaddr);
633}
634
635static void
636free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
637{
638	int s;
639	struct bus_dmamap *map;
640
641	bpage->datavaddr = 0;
642	bpage->datacount = 0;
643
644	s = splhigh();
645	STAILQ_INSERT_HEAD(&bounce_page_list, bpage, links);
646	free_bpages++;
647	active_bpages--;
648	if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
649		if (reserve_bounce_pages(map->dmat, map) == 0) {
650			STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
651			STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
652					   map, links);
653			busdma_swi_pending = 1;
654			swi_sched(vm_ih, 0);
655		}
656	}
657	splx(s);
658}
659
660void
661busdma_swi(void)
662{
663	int s;
664	struct bus_dmamap *map;
665
666	s = splhigh();
667	while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
668		STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
669		splx(s);
670		bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
671				map->callback, map->callback_arg, /*flags*/0);
672		s = splhigh();
673	}
674	splx(s);
675}
676