xref: /freebsd/sys/dev/usb/usb_busdma.c (revision 271ae033e9335043e468f1c2d55006cd857e59a5)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
27ed6d949aSAndrew Thompson #include <sys/stdint.h>
28ed6d949aSAndrew Thompson #include <sys/stddef.h>
29ed6d949aSAndrew Thompson #include <sys/param.h>
30ed6d949aSAndrew Thompson #include <sys/queue.h>
31ed6d949aSAndrew Thompson #include <sys/types.h>
32ed6d949aSAndrew Thompson #include <sys/systm.h>
33ed6d949aSAndrew Thompson #include <sys/kernel.h>
34ed6d949aSAndrew Thompson #include <sys/bus.h>
35ed6d949aSAndrew Thompson #include <sys/module.h>
36ed6d949aSAndrew Thompson #include <sys/lock.h>
37ed6d949aSAndrew Thompson #include <sys/mutex.h>
38ed6d949aSAndrew Thompson #include <sys/condvar.h>
39ed6d949aSAndrew Thompson #include <sys/sysctl.h>
40ed6d949aSAndrew Thompson #include <sys/sx.h>
41ed6d949aSAndrew Thompson #include <sys/unistd.h>
42ed6d949aSAndrew Thompson #include <sys/callout.h>
43ed6d949aSAndrew Thompson #include <sys/malloc.h>
44ed6d949aSAndrew Thompson #include <sys/priv.h>
45ed6d949aSAndrew Thompson 
4602ac6454SAndrew Thompson #include <dev/usb/usb.h>
47ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
48ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
4902ac6454SAndrew Thompson 
50a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
5102ac6454SAndrew Thompson 
5202ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5302ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5502ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5902ac6454SAndrew Thompson 
6002ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
6202ac6454SAndrew Thompson 
63bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
64a593f6b8SAndrew Thompson static void	usb_dma_tag_create(struct usb_dma_tag *, usb_size_t, usb_size_t);
65a593f6b8SAndrew Thompson static void	usb_dma_tag_destroy(struct usb_dma_tag *);
66a593f6b8SAndrew Thompson static void	usb_dma_lock_cb(void *, bus_dma_lock_op_t);
67a593f6b8SAndrew Thompson static void	usb_pc_alloc_mem_cb(void *, bus_dma_segment_t *, int, int);
68a593f6b8SAndrew Thompson static void	usb_pc_load_mem_cb(void *, bus_dma_segment_t *, int, int);
69a593f6b8SAndrew Thompson static void	usb_pc_common_mem_cb(void *, bus_dma_segment_t *, int, int,
7002ac6454SAndrew Thompson 		    uint8_t);
7102ac6454SAndrew Thompson #endif
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
74a593f6b8SAndrew Thompson  *  usbd_get_page - lookup DMA-able memory for the given offset
7502ac6454SAndrew Thompson  *
7602ac6454SAndrew Thompson  * NOTE: Only call this function when the "page_cache" structure has
7702ac6454SAndrew Thompson  * been properly initialized !
7802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
7902ac6454SAndrew Thompson void
80a593f6b8SAndrew Thompson usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset,
81760bc48eSAndrew Thompson     struct usb_page_search *res)
8202ac6454SAndrew Thompson {
83*271ae033SHans Petter Selasky #if USB_HAVE_BUSDMA
84760bc48eSAndrew Thompson 	struct usb_page *page;
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson 	if (pc->page_start) {
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson 		/* Case 1 - something has been loaded into DMA */
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson 		if (pc->buffer) {
9102ac6454SAndrew Thompson 
9202ac6454SAndrew Thompson 			/* Case 1a - Kernel Virtual Address */
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson 			res->buffer = USB_ADD_BYTES(pc->buffer, offset);
9502ac6454SAndrew Thompson 		}
9602ac6454SAndrew Thompson 		offset += pc->page_offset_buf;
9702ac6454SAndrew Thompson 
9802ac6454SAndrew Thompson 		/* compute destination page */
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson 		page = pc->page_start;
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson 		if (pc->ismultiseg) {
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson 			page += (offset / USB_PAGE_SIZE);
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson 			offset %= USB_PAGE_SIZE;
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson 			res->length = USB_PAGE_SIZE - offset;
10902ac6454SAndrew Thompson 			res->physaddr = page->physaddr + offset;
11002ac6454SAndrew Thompson 		} else {
11102ac6454SAndrew Thompson 			res->length = 0 - 1;
11202ac6454SAndrew Thompson 			res->physaddr = page->physaddr + offset;
11302ac6454SAndrew Thompson 		}
11402ac6454SAndrew Thompson 		if (!pc->buffer) {
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson 			/* Case 1b - Non Kernel Virtual Address */
11702ac6454SAndrew Thompson 
11802ac6454SAndrew Thompson 			res->buffer = USB_ADD_BYTES(page->buffer, offset);
11902ac6454SAndrew Thompson 		}
120bdc081c6SAndrew Thompson 		return;
121bdc081c6SAndrew Thompson 	}
122bdc081c6SAndrew Thompson #endif
12302ac6454SAndrew Thompson 	/* Case 2 - Plain PIO */
12402ac6454SAndrew Thompson 
12502ac6454SAndrew Thompson 	res->buffer = USB_ADD_BYTES(pc->buffer, offset);
12602ac6454SAndrew Thompson 	res->length = 0 - 1;
127bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
12802ac6454SAndrew Thompson 	res->physaddr = 0;
129bdc081c6SAndrew Thompson #endif
13002ac6454SAndrew Thompson }
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
133a593f6b8SAndrew Thompson  *  usbd_copy_in - copy directly to DMA-able memory
13402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
13502ac6454SAndrew Thompson void
136a593f6b8SAndrew Thompson usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset,
137e0a69b51SAndrew Thompson     const void *ptr, usb_frlength_t len)
13802ac6454SAndrew Thompson {
139760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
14002ac6454SAndrew Thompson 
14102ac6454SAndrew Thompson 	while (len != 0) {
14202ac6454SAndrew Thompson 
143a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &buf_res);
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson 		if (buf_res.length > len) {
14602ac6454SAndrew Thompson 			buf_res.length = len;
14702ac6454SAndrew Thompson 		}
148*271ae033SHans Petter Selasky 		memcpy(buf_res.buffer, ptr, buf_res.length);
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson 		offset += buf_res.length;
15102ac6454SAndrew Thompson 		len -= buf_res.length;
15202ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, buf_res.length);
15302ac6454SAndrew Thompson 	}
15402ac6454SAndrew Thompson }
15502ac6454SAndrew Thompson 
15602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
157a593f6b8SAndrew Thompson  *  usbd_copy_in_user - copy directly to DMA-able memory from userland
15802ac6454SAndrew Thompson  *
15902ac6454SAndrew Thompson  * Return values:
16002ac6454SAndrew Thompson  *    0: Success
16102ac6454SAndrew Thompson  * Else: Failure
16202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
163bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
16402ac6454SAndrew Thompson int
165a593f6b8SAndrew Thompson usbd_copy_in_user(struct usb_page_cache *cache, usb_frlength_t offset,
166e0a69b51SAndrew Thompson     const void *ptr, usb_frlength_t len)
16702ac6454SAndrew Thompson {
168760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
16902ac6454SAndrew Thompson 	int error;
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson 	while (len != 0) {
17202ac6454SAndrew Thompson 
173a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &buf_res);
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson 		if (buf_res.length > len) {
17602ac6454SAndrew Thompson 			buf_res.length = len;
17702ac6454SAndrew Thompson 		}
17802ac6454SAndrew Thompson 		error = copyin(ptr, buf_res.buffer, buf_res.length);
17902ac6454SAndrew Thompson 		if (error)
18002ac6454SAndrew Thompson 			return (error);
18102ac6454SAndrew Thompson 
18202ac6454SAndrew Thompson 		offset += buf_res.length;
18302ac6454SAndrew Thompson 		len -= buf_res.length;
18402ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, buf_res.length);
18502ac6454SAndrew Thompson 	}
18602ac6454SAndrew Thompson 	return (0);			/* success */
18702ac6454SAndrew Thompson }
188bdc081c6SAndrew Thompson #endif
18902ac6454SAndrew Thompson 
19002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
191a593f6b8SAndrew Thompson  *  usbd_m_copy_in - copy a mbuf chain directly into DMA-able memory
19202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
193bdc081c6SAndrew Thompson #if USB_HAVE_MBUF
194a593f6b8SAndrew Thompson struct usb_m_copy_in_arg {
195760bc48eSAndrew Thompson 	struct usb_page_cache *cache;
196e0a69b51SAndrew Thompson 	usb_frlength_t dst_offset;
19702ac6454SAndrew Thompson };
19802ac6454SAndrew Thompson 
199578d0effSAndrew Thompson static int
200a593f6b8SAndrew Thompson usbd_m_copy_in_cb(void *arg, void *src, uint32_t count)
20102ac6454SAndrew Thompson {
202a593f6b8SAndrew Thompson 	register struct usb_m_copy_in_arg *ua = arg;
20302ac6454SAndrew Thompson 
204a593f6b8SAndrew Thompson 	usbd_copy_in(ua->cache, ua->dst_offset, src, count);
20502ac6454SAndrew Thompson 	ua->dst_offset += count;
20602ac6454SAndrew Thompson 	return (0);
20702ac6454SAndrew Thompson }
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson void
210a593f6b8SAndrew Thompson usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset,
211f9cb546cSAndrew Thompson     struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len)
21202ac6454SAndrew Thompson {
213a593f6b8SAndrew Thompson 	struct usb_m_copy_in_arg arg = {cache, dst_offset};
214578d0effSAndrew Thompson 	int error;
21502ac6454SAndrew Thompson 
216a593f6b8SAndrew Thompson 	error = m_apply(m, src_offset, src_len, &usbd_m_copy_in_cb, &arg);
21702ac6454SAndrew Thompson }
218bdc081c6SAndrew Thompson #endif
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
221a593f6b8SAndrew Thompson  *  usb_uiomove - factored out code
22202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
223bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
22402ac6454SAndrew Thompson int
225a593f6b8SAndrew Thompson usb_uiomove(struct usb_page_cache *pc, struct uio *uio,
226e0a69b51SAndrew Thompson     usb_frlength_t pc_offset, usb_frlength_t len)
22702ac6454SAndrew Thompson {
228760bc48eSAndrew Thompson 	struct usb_page_search res;
22902ac6454SAndrew Thompson 	int error = 0;
23002ac6454SAndrew Thompson 
23102ac6454SAndrew Thompson 	while (len != 0) {
23202ac6454SAndrew Thompson 
233a593f6b8SAndrew Thompson 		usbd_get_page(pc, pc_offset, &res);
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson 		if (res.length > len) {
23602ac6454SAndrew Thompson 			res.length = len;
23702ac6454SAndrew Thompson 		}
23802ac6454SAndrew Thompson 		/*
23902ac6454SAndrew Thompson 		 * "uiomove()" can sleep so one needs to make a wrapper,
24002ac6454SAndrew Thompson 		 * exiting the mutex and checking things
24102ac6454SAndrew Thompson 		 */
24202ac6454SAndrew Thompson 		error = uiomove(res.buffer, res.length, uio);
24302ac6454SAndrew Thompson 
24402ac6454SAndrew Thompson 		if (error) {
24502ac6454SAndrew Thompson 			break;
24602ac6454SAndrew Thompson 		}
24702ac6454SAndrew Thompson 		pc_offset += res.length;
24802ac6454SAndrew Thompson 		len -= res.length;
24902ac6454SAndrew Thompson 	}
25002ac6454SAndrew Thompson 	return (error);
25102ac6454SAndrew Thompson }
252bdc081c6SAndrew Thompson #endif
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
255a593f6b8SAndrew Thompson  *  usbd_copy_out - copy directly from DMA-able memory
25602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
25702ac6454SAndrew Thompson void
258a593f6b8SAndrew Thompson usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset,
259e0a69b51SAndrew Thompson     void *ptr, usb_frlength_t len)
26002ac6454SAndrew Thompson {
261760bc48eSAndrew Thompson 	struct usb_page_search res;
26202ac6454SAndrew Thompson 
26302ac6454SAndrew Thompson 	while (len != 0) {
26402ac6454SAndrew Thompson 
265a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &res);
26602ac6454SAndrew Thompson 
26702ac6454SAndrew Thompson 		if (res.length > len) {
26802ac6454SAndrew Thompson 			res.length = len;
26902ac6454SAndrew Thompson 		}
270*271ae033SHans Petter Selasky 		memcpy(ptr, res.buffer, res.length);
27102ac6454SAndrew Thompson 
27202ac6454SAndrew Thompson 		offset += res.length;
27302ac6454SAndrew Thompson 		len -= res.length;
27402ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, res.length);
27502ac6454SAndrew Thompson 	}
27602ac6454SAndrew Thompson }
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
279a593f6b8SAndrew Thompson  *  usbd_copy_out_user - copy directly from DMA-able memory to userland
28002ac6454SAndrew Thompson  *
28102ac6454SAndrew Thompson  * Return values:
28202ac6454SAndrew Thompson  *    0: Success
28302ac6454SAndrew Thompson  * Else: Failure
28402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
285bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
28602ac6454SAndrew Thompson int
287a593f6b8SAndrew Thompson usbd_copy_out_user(struct usb_page_cache *cache, usb_frlength_t offset,
288e0a69b51SAndrew Thompson     void *ptr, usb_frlength_t len)
28902ac6454SAndrew Thompson {
290760bc48eSAndrew Thompson 	struct usb_page_search res;
29102ac6454SAndrew Thompson 	int error;
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 	while (len != 0) {
29402ac6454SAndrew Thompson 
295a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &res);
29602ac6454SAndrew Thompson 
29702ac6454SAndrew Thompson 		if (res.length > len) {
29802ac6454SAndrew Thompson 			res.length = len;
29902ac6454SAndrew Thompson 		}
30002ac6454SAndrew Thompson 		error = copyout(res.buffer, ptr, res.length);
30102ac6454SAndrew Thompson 		if (error)
30202ac6454SAndrew Thompson 			return (error);
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson 		offset += res.length;
30502ac6454SAndrew Thompson 		len -= res.length;
30602ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, res.length);
30702ac6454SAndrew Thompson 	}
30802ac6454SAndrew Thompson 	return (0);			/* success */
30902ac6454SAndrew Thompson }
310bdc081c6SAndrew Thompson #endif
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
313a593f6b8SAndrew Thompson  *  usbd_frame_zero - zero DMA-able memory
31402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
31502ac6454SAndrew Thompson void
316a593f6b8SAndrew Thompson usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset,
317e0a69b51SAndrew Thompson     usb_frlength_t len)
31802ac6454SAndrew Thompson {
319760bc48eSAndrew Thompson 	struct usb_page_search res;
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	while (len != 0) {
32202ac6454SAndrew Thompson 
323a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &res);
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 		if (res.length > len) {
32602ac6454SAndrew Thompson 			res.length = len;
32702ac6454SAndrew Thompson 		}
328*271ae033SHans Petter Selasky 		memset(res.buffer, 0, res.length);
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 		offset += res.length;
33102ac6454SAndrew Thompson 		len -= res.length;
33202ac6454SAndrew Thompson 	}
33302ac6454SAndrew Thompson }
33402ac6454SAndrew Thompson 
3353e873976SAndrew Thompson #if USB_HAVE_BUSDMA
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
338a593f6b8SAndrew Thompson  *	usb_dma_lock_cb - dummy callback
33902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
34002ac6454SAndrew Thompson static void
341a593f6b8SAndrew Thompson usb_dma_lock_cb(void *arg, bus_dma_lock_op_t op)
34202ac6454SAndrew Thompson {
34302ac6454SAndrew Thompson 	/* we use "mtx_owned()" instead of this function */
34402ac6454SAndrew Thompson }
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
347a593f6b8SAndrew Thompson  *	usb_dma_tag_create - allocate a DMA tag
34802ac6454SAndrew Thompson  *
34902ac6454SAndrew Thompson  * NOTE: If the "align" parameter has a value of 1 the DMA-tag will
35002ac6454SAndrew Thompson  * allow multi-segment mappings. Else all mappings are single-segment.
35102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
35202ac6454SAndrew Thompson static void
353a593f6b8SAndrew Thompson usb_dma_tag_create(struct usb_dma_tag *udt,
354f9cb546cSAndrew Thompson     usb_size_t size, usb_size_t align)
35502ac6454SAndrew Thompson {
35602ac6454SAndrew Thompson 	bus_dma_tag_t tag;
35702ac6454SAndrew Thompson 
35802ac6454SAndrew Thompson 	if (bus_dma_tag_create
35902ac6454SAndrew Thompson 	    ( /* parent    */ udt->tag_parent->tag,
36002ac6454SAndrew Thompson 	     /* alignment */ align,
36158f6d273SMarcel Moolenaar 	     /* boundary  */ (align == 1) ?
36258f6d273SMarcel Moolenaar 	    USB_PAGE_SIZE : 0,
36302ac6454SAndrew Thompson 	     /* lowaddr   */ (2ULL << (udt->tag_parent->dma_bits - 1)) - 1,
36402ac6454SAndrew Thompson 	     /* highaddr  */ BUS_SPACE_MAXADDR,
36502ac6454SAndrew Thompson 	     /* filter    */ NULL,
36602ac6454SAndrew Thompson 	     /* filterarg */ NULL,
36702ac6454SAndrew Thompson 	     /* maxsize   */ size,
3686c3c4d71SMarius Strobl 	     /* nsegments */ (align == 1 && size > 1) ?
36902ac6454SAndrew Thompson 	    (2 + (size / USB_PAGE_SIZE)) : 1,
3706c3c4d71SMarius Strobl 	     /* maxsegsz  */ (align == 1 && size > USB_PAGE_SIZE) ?
37102ac6454SAndrew Thompson 	    USB_PAGE_SIZE : size,
37202ac6454SAndrew Thompson 	     /* flags     */ BUS_DMA_KEEP_PG_OFFSET,
373a593f6b8SAndrew Thompson 	     /* lockfn    */ &usb_dma_lock_cb,
37402ac6454SAndrew Thompson 	     /* lockarg   */ NULL,
37502ac6454SAndrew Thompson 	    &tag)) {
37602ac6454SAndrew Thompson 		tag = NULL;
37702ac6454SAndrew Thompson 	}
37802ac6454SAndrew Thompson 	udt->tag = tag;
37902ac6454SAndrew Thompson }
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
382a593f6b8SAndrew Thompson  *	usb_dma_tag_free - free a DMA tag
38302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
38402ac6454SAndrew Thompson static void
385a593f6b8SAndrew Thompson usb_dma_tag_destroy(struct usb_dma_tag *udt)
38602ac6454SAndrew Thompson {
38702ac6454SAndrew Thompson 	bus_dma_tag_destroy(udt->tag);
38802ac6454SAndrew Thompson }
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
391a593f6b8SAndrew Thompson  *	usb_pc_alloc_mem_cb - BUS-DMA callback function
39202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
39302ac6454SAndrew Thompson static void
394a593f6b8SAndrew Thompson usb_pc_alloc_mem_cb(void *arg, bus_dma_segment_t *segs,
39502ac6454SAndrew Thompson     int nseg, int error)
39602ac6454SAndrew Thompson {
397a593f6b8SAndrew Thompson 	usb_pc_common_mem_cb(arg, segs, nseg, error, 0);
39802ac6454SAndrew Thompson }
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
401a593f6b8SAndrew Thompson  *	usb_pc_load_mem_cb - BUS-DMA callback function
40202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
40302ac6454SAndrew Thompson static void
404a593f6b8SAndrew Thompson usb_pc_load_mem_cb(void *arg, bus_dma_segment_t *segs,
40502ac6454SAndrew Thompson     int nseg, int error)
40602ac6454SAndrew Thompson {
407a593f6b8SAndrew Thompson 	usb_pc_common_mem_cb(arg, segs, nseg, error, 1);
40802ac6454SAndrew Thompson }
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
411a593f6b8SAndrew Thompson  *	usb_pc_common_mem_cb - BUS-DMA callback function
41202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
41302ac6454SAndrew Thompson static void
414a593f6b8SAndrew Thompson usb_pc_common_mem_cb(void *arg, bus_dma_segment_t *segs,
41502ac6454SAndrew Thompson     int nseg, int error, uint8_t isload)
41602ac6454SAndrew Thompson {
417760bc48eSAndrew Thompson 	struct usb_dma_parent_tag *uptag;
418760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
419760bc48eSAndrew Thompson 	struct usb_page *pg;
420f9cb546cSAndrew Thompson 	usb_size_t rem;
42102ac6454SAndrew Thompson 	uint8_t owned;
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 	pc = arg;
42402ac6454SAndrew Thompson 	uptag = pc->tag_parent;
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson 	/*
42702ac6454SAndrew Thompson 	 * XXX There is sometimes recursive locking here.
42802ac6454SAndrew Thompson 	 * XXX We should try to find a better solution.
42902ac6454SAndrew Thompson 	 * XXX Until further the "owned" variable does
43002ac6454SAndrew Thompson 	 * XXX the trick.
43102ac6454SAndrew Thompson 	 */
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 	if (error) {
43402ac6454SAndrew Thompson 		goto done;
43502ac6454SAndrew Thompson 	}
43602ac6454SAndrew Thompson 	pg = pc->page_start;
43702ac6454SAndrew Thompson 	pg->physaddr = segs->ds_addr & ~(USB_PAGE_SIZE - 1);
43802ac6454SAndrew Thompson 	rem = segs->ds_addr & (USB_PAGE_SIZE - 1);
43902ac6454SAndrew Thompson 	pc->page_offset_buf = rem;
44002ac6454SAndrew Thompson 	pc->page_offset_end += rem;
44102ac6454SAndrew Thompson 	nseg--;
442ed6d949aSAndrew Thompson #ifdef USB_DEBUG
44302ac6454SAndrew Thompson 	if (rem != (USB_P2U(pc->buffer) & (USB_PAGE_SIZE - 1))) {
44402ac6454SAndrew Thompson 		/*
44502ac6454SAndrew Thompson 		 * This check verifies that the physical address is correct:
44602ac6454SAndrew Thompson 		 */
447767cb2e2SAndrew Thompson 		DPRINTFN(0, "Page offset was not preserved\n");
44802ac6454SAndrew Thompson 		error = 1;
44902ac6454SAndrew Thompson 		goto done;
45002ac6454SAndrew Thompson 	}
45102ac6454SAndrew Thompson #endif
45202ac6454SAndrew Thompson 	while (nseg > 0) {
45302ac6454SAndrew Thompson 		nseg--;
45402ac6454SAndrew Thompson 		segs++;
45502ac6454SAndrew Thompson 		pg++;
45602ac6454SAndrew Thompson 		pg->physaddr = segs->ds_addr & ~(USB_PAGE_SIZE - 1);
45702ac6454SAndrew Thompson 	}
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson done:
46002ac6454SAndrew Thompson 	owned = mtx_owned(uptag->mtx);
46102ac6454SAndrew Thompson 	if (!owned)
46202ac6454SAndrew Thompson 		mtx_lock(uptag->mtx);
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	uptag->dma_error = (error ? 1 : 0);
46502ac6454SAndrew Thompson 	if (isload) {
46602ac6454SAndrew Thompson 		(uptag->func) (uptag);
46702ac6454SAndrew Thompson 	} else {
4688437751dSAndrew Thompson 		cv_broadcast(uptag->cv);
46902ac6454SAndrew Thompson 	}
47002ac6454SAndrew Thompson 	if (!owned)
47102ac6454SAndrew Thompson 		mtx_unlock(uptag->mtx);
47202ac6454SAndrew Thompson }
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
475a593f6b8SAndrew Thompson  *	usb_pc_alloc_mem - allocate DMA'able memory
47602ac6454SAndrew Thompson  *
47702ac6454SAndrew Thompson  * Returns:
47802ac6454SAndrew Thompson  *    0: Success
47902ac6454SAndrew Thompson  * Else: Failure
48002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
48102ac6454SAndrew Thompson uint8_t
482a593f6b8SAndrew Thompson usb_pc_alloc_mem(struct usb_page_cache *pc, struct usb_page *pg,
483f9cb546cSAndrew Thompson     usb_size_t size, usb_size_t align)
48402ac6454SAndrew Thompson {
485760bc48eSAndrew Thompson 	struct usb_dma_parent_tag *uptag;
486760bc48eSAndrew Thompson 	struct usb_dma_tag *utag;
48702ac6454SAndrew Thompson 	bus_dmamap_t map;
48802ac6454SAndrew Thompson 	void *ptr;
48902ac6454SAndrew Thompson 	int err;
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson 	uptag = pc->tag_parent;
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	if (align != 1) {
49402ac6454SAndrew Thompson 		/*
49502ac6454SAndrew Thompson 	         * The alignment must be greater or equal to the
49602ac6454SAndrew Thompson 	         * "size" else the object can be split between two
49702ac6454SAndrew Thompson 	         * memory pages and we get a problem!
49802ac6454SAndrew Thompson 	         */
49902ac6454SAndrew Thompson 		while (align < size) {
50002ac6454SAndrew Thompson 			align *= 2;
50102ac6454SAndrew Thompson 			if (align == 0) {
50202ac6454SAndrew Thompson 				goto error;
50302ac6454SAndrew Thompson 			}
50402ac6454SAndrew Thompson 		}
50502ac6454SAndrew Thompson #if 1
50602ac6454SAndrew Thompson 		/*
50702ac6454SAndrew Thompson 		 * XXX BUS-DMA workaround - FIXME later:
50802ac6454SAndrew Thompson 		 *
50902ac6454SAndrew Thompson 		 * We assume that that the aligment at this point of
51002ac6454SAndrew Thompson 		 * the code is greater than or equal to the size and
51102ac6454SAndrew Thompson 		 * less than two times the size, so that if we double
51202ac6454SAndrew Thompson 		 * the size, the size will be greater than the
51302ac6454SAndrew Thompson 		 * alignment.
51402ac6454SAndrew Thompson 		 *
51502ac6454SAndrew Thompson 		 * The bus-dma system has a check for "alignment"
51602ac6454SAndrew Thompson 		 * being less than "size". If that check fails we end
51702ac6454SAndrew Thompson 		 * up using contigmalloc which is page based even for
51802ac6454SAndrew Thompson 		 * small allocations. Try to avoid that to save
51902ac6454SAndrew Thompson 		 * memory, hence we sometimes to a large number of
52002ac6454SAndrew Thompson 		 * small allocations!
52102ac6454SAndrew Thompson 		 */
52202ac6454SAndrew Thompson 		if (size <= (USB_PAGE_SIZE / 2)) {
52302ac6454SAndrew Thompson 			size *= 2;
52402ac6454SAndrew Thompson 		}
52502ac6454SAndrew Thompson #endif
52602ac6454SAndrew Thompson 	}
52702ac6454SAndrew Thompson 	/* get the correct DMA tag */
528a593f6b8SAndrew Thompson 	utag = usb_dma_tag_find(uptag, size, align);
52902ac6454SAndrew Thompson 	if (utag == NULL) {
53002ac6454SAndrew Thompson 		goto error;
53102ac6454SAndrew Thompson 	}
53202ac6454SAndrew Thompson 	/* allocate memory */
53302ac6454SAndrew Thompson 	if (bus_dmamem_alloc(
53402ac6454SAndrew Thompson 	    utag->tag, &ptr, (BUS_DMA_WAITOK | BUS_DMA_COHERENT), &map)) {
53502ac6454SAndrew Thompson 		goto error;
53602ac6454SAndrew Thompson 	}
53702ac6454SAndrew Thompson 	/* setup page cache */
53802ac6454SAndrew Thompson 	pc->buffer = ptr;
53902ac6454SAndrew Thompson 	pc->page_start = pg;
54002ac6454SAndrew Thompson 	pc->page_offset_buf = 0;
54102ac6454SAndrew Thompson 	pc->page_offset_end = size;
54202ac6454SAndrew Thompson 	pc->map = map;
54302ac6454SAndrew Thompson 	pc->tag = utag->tag;
54402ac6454SAndrew Thompson 	pc->ismultiseg = (align == 1);
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson 	mtx_lock(uptag->mtx);
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	/* load memory into DMA */
54902ac6454SAndrew Thompson 	err = bus_dmamap_load(
550a593f6b8SAndrew Thompson 	    utag->tag, map, ptr, size, &usb_pc_alloc_mem_cb,
55102ac6454SAndrew Thompson 	    pc, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson 	if (err == EINPROGRESS) {
5548437751dSAndrew Thompson 		cv_wait(uptag->cv, uptag->mtx);
55502ac6454SAndrew Thompson 		err = 0;
55602ac6454SAndrew Thompson 	}
55702ac6454SAndrew Thompson 	mtx_unlock(uptag->mtx);
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson 	if (err || uptag->dma_error) {
56002ac6454SAndrew Thompson 		bus_dmamem_free(utag->tag, ptr, map);
56102ac6454SAndrew Thompson 		goto error;
56202ac6454SAndrew Thompson 	}
563*271ae033SHans Petter Selasky 	memset(ptr, 0, size);
56402ac6454SAndrew Thompson 
565a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(pc);
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson 	return (0);
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson error:
57002ac6454SAndrew Thompson 	/* reset most of the page cache */
57102ac6454SAndrew Thompson 	pc->buffer = NULL;
57202ac6454SAndrew Thompson 	pc->page_start = NULL;
57302ac6454SAndrew Thompson 	pc->page_offset_buf = 0;
57402ac6454SAndrew Thompson 	pc->page_offset_end = 0;
57502ac6454SAndrew Thompson 	pc->map = NULL;
57602ac6454SAndrew Thompson 	pc->tag = NULL;
57702ac6454SAndrew Thompson 	return (1);
57802ac6454SAndrew Thompson }
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
581a593f6b8SAndrew Thompson  *	usb_pc_free_mem - free DMA memory
58202ac6454SAndrew Thompson  *
58302ac6454SAndrew Thompson  * This function is NULL safe.
58402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
58502ac6454SAndrew Thompson void
586a593f6b8SAndrew Thompson usb_pc_free_mem(struct usb_page_cache *pc)
58702ac6454SAndrew Thompson {
58802ac6454SAndrew Thompson 	if (pc && pc->buffer) {
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson 		bus_dmamap_unload(pc->tag, pc->map);
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 		bus_dmamem_free(pc->tag, pc->buffer, pc->map);
59302ac6454SAndrew Thompson 
59402ac6454SAndrew Thompson 		pc->buffer = NULL;
59502ac6454SAndrew Thompson 	}
59602ac6454SAndrew Thompson }
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
599a593f6b8SAndrew Thompson  *	usb_pc_load_mem - load virtual memory into DMA
60002ac6454SAndrew Thompson  *
60102ac6454SAndrew Thompson  * Return values:
60202ac6454SAndrew Thompson  * 0: Success
60302ac6454SAndrew Thompson  * Else: Error
60402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
60502ac6454SAndrew Thompson uint8_t
606a593f6b8SAndrew Thompson usb_pc_load_mem(struct usb_page_cache *pc, usb_size_t size, uint8_t sync)
60702ac6454SAndrew Thompson {
60802ac6454SAndrew Thompson 	/* setup page cache */
60902ac6454SAndrew Thompson 	pc->page_offset_buf = 0;
61002ac6454SAndrew Thompson 	pc->page_offset_end = size;
61102ac6454SAndrew Thompson 	pc->ismultiseg = 1;
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 	mtx_assert(pc->tag_parent->mtx, MA_OWNED);
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 	if (size > 0) {
61602ac6454SAndrew Thompson 		if (sync) {
617760bc48eSAndrew Thompson 			struct usb_dma_parent_tag *uptag;
61802ac6454SAndrew Thompson 			int err;
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 			uptag = pc->tag_parent;
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 			/*
62302ac6454SAndrew Thompson 			 * We have to unload the previous loaded DMA
62402ac6454SAndrew Thompson 			 * pages before trying to load a new one!
62502ac6454SAndrew Thompson 			 */
62602ac6454SAndrew Thompson 			bus_dmamap_unload(pc->tag, pc->map);
62702ac6454SAndrew Thompson 
62802ac6454SAndrew Thompson 			/*
62902ac6454SAndrew Thompson 			 * Try to load memory into DMA.
63002ac6454SAndrew Thompson 			 */
63102ac6454SAndrew Thompson 			err = bus_dmamap_load(
63202ac6454SAndrew Thompson 			    pc->tag, pc->map, pc->buffer, size,
633a593f6b8SAndrew Thompson 			    &usb_pc_alloc_mem_cb, pc, BUS_DMA_WAITOK);
63402ac6454SAndrew Thompson 			if (err == EINPROGRESS) {
6358437751dSAndrew Thompson 				cv_wait(uptag->cv, uptag->mtx);
63602ac6454SAndrew Thompson 				err = 0;
63702ac6454SAndrew Thompson 			}
63802ac6454SAndrew Thompson 			if (err || uptag->dma_error) {
63902ac6454SAndrew Thompson 				return (1);
64002ac6454SAndrew Thompson 			}
64102ac6454SAndrew Thompson 		} else {
64202ac6454SAndrew Thompson 
64302ac6454SAndrew Thompson 			/*
64402ac6454SAndrew Thompson 			 * We have to unload the previous loaded DMA
64502ac6454SAndrew Thompson 			 * pages before trying to load a new one!
64602ac6454SAndrew Thompson 			 */
64702ac6454SAndrew Thompson 			bus_dmamap_unload(pc->tag, pc->map);
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 			/*
65002ac6454SAndrew Thompson 			 * Try to load memory into DMA. The callback
65102ac6454SAndrew Thompson 			 * will be called in all cases:
65202ac6454SAndrew Thompson 			 */
65302ac6454SAndrew Thompson 			if (bus_dmamap_load(
65402ac6454SAndrew Thompson 			    pc->tag, pc->map, pc->buffer, size,
655a593f6b8SAndrew Thompson 			    &usb_pc_load_mem_cb, pc, BUS_DMA_WAITOK)) {
65602ac6454SAndrew Thompson 			}
65702ac6454SAndrew Thompson 		}
65802ac6454SAndrew Thompson 	} else {
65902ac6454SAndrew Thompson 		if (!sync) {
66002ac6454SAndrew Thompson 			/*
66102ac6454SAndrew Thompson 			 * Call callback so that refcount is decremented
66202ac6454SAndrew Thompson 			 * properly:
66302ac6454SAndrew Thompson 			 */
66402ac6454SAndrew Thompson 			pc->tag_parent->dma_error = 0;
66502ac6454SAndrew Thompson 			(pc->tag_parent->func) (pc->tag_parent);
66602ac6454SAndrew Thompson 		}
66702ac6454SAndrew Thompson 	}
66802ac6454SAndrew Thompson 	return (0);
66902ac6454SAndrew Thompson }
67002ac6454SAndrew Thompson 
67102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
672a593f6b8SAndrew Thompson  *	usb_pc_cpu_invalidate - invalidate CPU cache
67302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
67402ac6454SAndrew Thompson void
675a593f6b8SAndrew Thompson usb_pc_cpu_invalidate(struct usb_page_cache *pc)
67602ac6454SAndrew Thompson {
67702ac6454SAndrew Thompson 	if (pc->page_offset_end == pc->page_offset_buf) {
67802ac6454SAndrew Thompson 		/* nothing has been loaded into this page cache! */
67902ac6454SAndrew Thompson 		return;
68002ac6454SAndrew Thompson 	}
681f2db024fSAlfred Perlstein 
682f2db024fSAlfred Perlstein 	/*
683f2db024fSAlfred Perlstein 	 * TODO: We currently do XXX_POSTREAD and XXX_PREREAD at the
684f2db024fSAlfred Perlstein 	 * same time, but in the future we should try to isolate the
685f2db024fSAlfred Perlstein 	 * different cases to optimise the code. --HPS
686f2db024fSAlfred Perlstein 	 */
68735d01728SRafal Jaworowski 	bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_POSTREAD);
68835d01728SRafal Jaworowski 	bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREREAD);
68902ac6454SAndrew Thompson }
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
692a593f6b8SAndrew Thompson  *	usb_pc_cpu_flush - flush CPU cache
69302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
69402ac6454SAndrew Thompson void
695a593f6b8SAndrew Thompson usb_pc_cpu_flush(struct usb_page_cache *pc)
69602ac6454SAndrew Thompson {
69702ac6454SAndrew Thompson 	if (pc->page_offset_end == pc->page_offset_buf) {
69802ac6454SAndrew Thompson 		/* nothing has been loaded into this page cache! */
69902ac6454SAndrew Thompson 		return;
70002ac6454SAndrew Thompson 	}
70135d01728SRafal Jaworowski 	bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREWRITE);
70202ac6454SAndrew Thompson }
70302ac6454SAndrew Thompson 
70402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
705a593f6b8SAndrew Thompson  *	usb_pc_dmamap_create - create a DMA map
70602ac6454SAndrew Thompson  *
70702ac6454SAndrew Thompson  * Returns:
70802ac6454SAndrew Thompson  *    0: Success
70902ac6454SAndrew Thompson  * Else: Failure
71002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
71102ac6454SAndrew Thompson uint8_t
712a593f6b8SAndrew Thompson usb_pc_dmamap_create(struct usb_page_cache *pc, usb_size_t size)
71302ac6454SAndrew Thompson {
714760bc48eSAndrew Thompson 	struct usb_xfer_root *info;
715760bc48eSAndrew Thompson 	struct usb_dma_tag *utag;
71602ac6454SAndrew Thompson 
71702ac6454SAndrew Thompson 	/* get info */
718bdc081c6SAndrew Thompson 	info = USB_DMATAG_TO_XROOT(pc->tag_parent);
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson 	/* sanity check */
72102ac6454SAndrew Thompson 	if (info == NULL) {
72202ac6454SAndrew Thompson 		goto error;
72302ac6454SAndrew Thompson 	}
724a593f6b8SAndrew Thompson 	utag = usb_dma_tag_find(pc->tag_parent, size, 1);
72502ac6454SAndrew Thompson 	if (utag == NULL) {
72602ac6454SAndrew Thompson 		goto error;
72702ac6454SAndrew Thompson 	}
72802ac6454SAndrew Thompson 	/* create DMA map */
72902ac6454SAndrew Thompson 	if (bus_dmamap_create(utag->tag, 0, &pc->map)) {
73002ac6454SAndrew Thompson 		goto error;
73102ac6454SAndrew Thompson 	}
73202ac6454SAndrew Thompson 	pc->tag = utag->tag;
73302ac6454SAndrew Thompson 	return 0;			/* success */
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson error:
73602ac6454SAndrew Thompson 	pc->map = NULL;
73702ac6454SAndrew Thompson 	pc->tag = NULL;
73802ac6454SAndrew Thompson 	return 1;			/* failure */
73902ac6454SAndrew Thompson }
74002ac6454SAndrew Thompson 
74102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
742a593f6b8SAndrew Thompson  *	usb_pc_dmamap_destroy
74302ac6454SAndrew Thompson  *
74402ac6454SAndrew Thompson  * This function is NULL safe.
74502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
74602ac6454SAndrew Thompson void
747a593f6b8SAndrew Thompson usb_pc_dmamap_destroy(struct usb_page_cache *pc)
74802ac6454SAndrew Thompson {
74902ac6454SAndrew Thompson 	if (pc && pc->tag) {
75002ac6454SAndrew Thompson 		bus_dmamap_destroy(pc->tag, pc->map);
75102ac6454SAndrew Thompson 		pc->tag = NULL;
75202ac6454SAndrew Thompson 		pc->map = NULL;
75302ac6454SAndrew Thompson 	}
75402ac6454SAndrew Thompson }
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
757a593f6b8SAndrew Thompson  *	usb_dma_tag_find - factored out code
75802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
759760bc48eSAndrew Thompson struct usb_dma_tag *
760a593f6b8SAndrew Thompson usb_dma_tag_find(struct usb_dma_parent_tag *udpt,
761f9cb546cSAndrew Thompson     usb_size_t size, usb_size_t align)
76202ac6454SAndrew Thompson {
763760bc48eSAndrew Thompson 	struct usb_dma_tag *udt;
76402ac6454SAndrew Thompson 	uint8_t nudt;
76502ac6454SAndrew Thompson 
766767cb2e2SAndrew Thompson 	USB_ASSERT(align > 0, ("Invalid parameter align = 0\n"));
767767cb2e2SAndrew Thompson 	USB_ASSERT(size > 0, ("Invalid parameter size = 0\n"));
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson 	udt = udpt->utag_first;
77002ac6454SAndrew Thompson 	nudt = udpt->utag_max;
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson 	while (nudt--) {
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson 		if (udt->align == 0) {
775a593f6b8SAndrew Thompson 			usb_dma_tag_create(udt, size, align);
77602ac6454SAndrew Thompson 			if (udt->tag == NULL) {
77702ac6454SAndrew Thompson 				return (NULL);
77802ac6454SAndrew Thompson 			}
77902ac6454SAndrew Thompson 			udt->align = align;
78002ac6454SAndrew Thompson 			udt->size = size;
78102ac6454SAndrew Thompson 			return (udt);
78202ac6454SAndrew Thompson 		}
78302ac6454SAndrew Thompson 		if ((udt->align == align) && (udt->size == size)) {
78402ac6454SAndrew Thompson 			return (udt);
78502ac6454SAndrew Thompson 		}
78602ac6454SAndrew Thompson 		udt++;
78702ac6454SAndrew Thompson 	}
78802ac6454SAndrew Thompson 	return (NULL);
78902ac6454SAndrew Thompson }
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
792a593f6b8SAndrew Thompson  *	usb_dma_tag_setup - initialise USB DMA tags
79302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
79402ac6454SAndrew Thompson void
795a593f6b8SAndrew Thompson usb_dma_tag_setup(struct usb_dma_parent_tag *udpt,
796760bc48eSAndrew Thompson     struct usb_dma_tag *udt, bus_dma_tag_t dmat,
797e0a69b51SAndrew Thompson     struct mtx *mtx, usb_dma_callback_t *func,
798bdc081c6SAndrew Thompson     uint8_t ndmabits, uint8_t nudt)
79902ac6454SAndrew Thompson {
800*271ae033SHans Petter Selasky 	memset(udpt, 0, sizeof(*udpt));
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 	/* sanity checking */
80302ac6454SAndrew Thompson 	if ((nudt == 0) ||
80402ac6454SAndrew Thompson 	    (ndmabits == 0) ||
80502ac6454SAndrew Thompson 	    (mtx == NULL)) {
80602ac6454SAndrew Thompson 		/* something is corrupt */
80702ac6454SAndrew Thompson 		return;
80802ac6454SAndrew Thompson 	}
80902ac6454SAndrew Thompson 	/* initialise condition variable */
8108437751dSAndrew Thompson 	cv_init(udpt->cv, "USB DMA CV");
81102ac6454SAndrew Thompson 
81202ac6454SAndrew Thompson 	/* store some information */
81302ac6454SAndrew Thompson 	udpt->mtx = mtx;
81402ac6454SAndrew Thompson 	udpt->func = func;
81502ac6454SAndrew Thompson 	udpt->tag = dmat;
81602ac6454SAndrew Thompson 	udpt->utag_first = udt;
81702ac6454SAndrew Thompson 	udpt->utag_max = nudt;
81802ac6454SAndrew Thompson 	udpt->dma_bits = ndmabits;
81902ac6454SAndrew Thompson 
82002ac6454SAndrew Thompson 	while (nudt--) {
821*271ae033SHans Petter Selasky 		memset(udt, 0, sizeof(*udt));
82202ac6454SAndrew Thompson 		udt->tag_parent = udpt;
82302ac6454SAndrew Thompson 		udt++;
82402ac6454SAndrew Thompson 	}
82502ac6454SAndrew Thompson }
82602ac6454SAndrew Thompson 
82702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
828a593f6b8SAndrew Thompson  *	usb_bus_tag_unsetup - factored out code
82902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
83002ac6454SAndrew Thompson void
831a593f6b8SAndrew Thompson usb_dma_tag_unsetup(struct usb_dma_parent_tag *udpt)
83202ac6454SAndrew Thompson {
833760bc48eSAndrew Thompson 	struct usb_dma_tag *udt;
83402ac6454SAndrew Thompson 	uint8_t nudt;
83502ac6454SAndrew Thompson 
83602ac6454SAndrew Thompson 	udt = udpt->utag_first;
83702ac6454SAndrew Thompson 	nudt = udpt->utag_max;
83802ac6454SAndrew Thompson 
83902ac6454SAndrew Thompson 	while (nudt--) {
84002ac6454SAndrew Thompson 
84102ac6454SAndrew Thompson 		if (udt->align) {
84202ac6454SAndrew Thompson 			/* destroy the USB DMA tag */
843a593f6b8SAndrew Thompson 			usb_dma_tag_destroy(udt);
84402ac6454SAndrew Thompson 			udt->align = 0;
84502ac6454SAndrew Thompson 		}
84602ac6454SAndrew Thompson 		udt++;
84702ac6454SAndrew Thompson 	}
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson 	if (udpt->utag_max) {
85002ac6454SAndrew Thompson 		/* destroy the condition variable */
8518437751dSAndrew Thompson 		cv_destroy(udpt->cv);
85202ac6454SAndrew Thompson 	}
85302ac6454SAndrew Thompson }
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
856a593f6b8SAndrew Thompson  *	usb_bdma_work_loop
85702ac6454SAndrew Thompson  *
85802ac6454SAndrew Thompson  * This function handles loading of virtual buffers into DMA and is
85902ac6454SAndrew Thompson  * only called when "dma_refcount" is zero.
86002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
86102ac6454SAndrew Thompson void
862a593f6b8SAndrew Thompson usb_bdma_work_loop(struct usb_xfer_queue *pq)
86302ac6454SAndrew Thompson {
864760bc48eSAndrew Thompson 	struct usb_xfer_root *info;
865760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
866e0a69b51SAndrew Thompson 	usb_frcount_t nframes;
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson 	xfer = pq->curr;
86902ac6454SAndrew Thompson 	info = xfer->xroot;
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 	mtx_assert(info->xfer_mtx, MA_OWNED);
87202ac6454SAndrew Thompson 
87302ac6454SAndrew Thompson 	if (xfer->error) {
87402ac6454SAndrew Thompson 		/* some error happened */
87502ac6454SAndrew Thompson 		USB_BUS_LOCK(info->bus);
876a593f6b8SAndrew Thompson 		usbd_transfer_done(xfer, 0);
87702ac6454SAndrew Thompson 		USB_BUS_UNLOCK(info->bus);
87802ac6454SAndrew Thompson 		return;
87902ac6454SAndrew Thompson 	}
88002ac6454SAndrew Thompson 	if (!xfer->flags_int.bdma_setup) {
881760bc48eSAndrew Thompson 		struct usb_page *pg;
882e0a69b51SAndrew Thompson 		usb_frlength_t frlength_0;
88302ac6454SAndrew Thompson 		uint8_t isread;
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson 		xfer->flags_int.bdma_setup = 1;
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson 		/* reset BUS-DMA load state */
88802ac6454SAndrew Thompson 
88902ac6454SAndrew Thompson 		info->dma_error = 0;
89002ac6454SAndrew Thompson 
89102ac6454SAndrew Thompson 		if (xfer->flags_int.isochronous_xfr) {
89202ac6454SAndrew Thompson 			/* only one frame buffer */
89302ac6454SAndrew Thompson 			nframes = 1;
89402ac6454SAndrew Thompson 			frlength_0 = xfer->sumlen;
89502ac6454SAndrew Thompson 		} else {
89602ac6454SAndrew Thompson 			/* can be multiple frame buffers */
89702ac6454SAndrew Thompson 			nframes = xfer->nframes;
89802ac6454SAndrew Thompson 			frlength_0 = xfer->frlengths[0];
89902ac6454SAndrew Thompson 		}
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson 		/*
90202ac6454SAndrew Thompson 		 * Set DMA direction first. This is needed to
90302ac6454SAndrew Thompson 		 * select the correct cache invalidate and cache
90402ac6454SAndrew Thompson 		 * flush operations.
90502ac6454SAndrew Thompson 		 */
90602ac6454SAndrew Thompson 		isread = USB_GET_DATA_ISREAD(xfer);
90702ac6454SAndrew Thompson 		pg = xfer->dma_page_ptr;
90802ac6454SAndrew Thompson 
90902ac6454SAndrew Thompson 		if (xfer->flags_int.control_xfr &&
91002ac6454SAndrew Thompson 		    xfer->flags_int.control_hdr) {
91102ac6454SAndrew Thompson 			/* special case */
912f29a0724SAndrew Thompson 			if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
91302ac6454SAndrew Thompson 				/* The device controller writes to memory */
91402ac6454SAndrew Thompson 				xfer->frbuffers[0].isread = 1;
91502ac6454SAndrew Thompson 			} else {
91602ac6454SAndrew Thompson 				/* The host controller reads from memory */
91702ac6454SAndrew Thompson 				xfer->frbuffers[0].isread = 0;
91802ac6454SAndrew Thompson 			}
91902ac6454SAndrew Thompson 		} else {
92002ac6454SAndrew Thompson 			/* default case */
92102ac6454SAndrew Thompson 			xfer->frbuffers[0].isread = isread;
92202ac6454SAndrew Thompson 		}
92302ac6454SAndrew Thompson 
92402ac6454SAndrew Thompson 		/*
92502ac6454SAndrew Thompson 		 * Setup the "page_start" pointer which points to an array of
92602ac6454SAndrew Thompson 		 * USB pages where information about the physical address of a
92702ac6454SAndrew Thompson 		 * page will be stored. Also initialise the "isread" field of
92802ac6454SAndrew Thompson 		 * the USB page caches.
92902ac6454SAndrew Thompson 		 */
93002ac6454SAndrew Thompson 		xfer->frbuffers[0].page_start = pg;
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 		info->dma_nframes = nframes;
93302ac6454SAndrew Thompson 		info->dma_currframe = 0;
93402ac6454SAndrew Thompson 		info->dma_frlength_0 = frlength_0;
93502ac6454SAndrew Thompson 
93602ac6454SAndrew Thompson 		pg += (frlength_0 / USB_PAGE_SIZE);
93702ac6454SAndrew Thompson 		pg += 2;
93802ac6454SAndrew Thompson 
93902ac6454SAndrew Thompson 		while (--nframes > 0) {
94002ac6454SAndrew Thompson 			xfer->frbuffers[nframes].isread = isread;
94102ac6454SAndrew Thompson 			xfer->frbuffers[nframes].page_start = pg;
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 			pg += (xfer->frlengths[nframes] / USB_PAGE_SIZE);
94402ac6454SAndrew Thompson 			pg += 2;
94502ac6454SAndrew Thompson 		}
94602ac6454SAndrew Thompson 
94702ac6454SAndrew Thompson 	}
94802ac6454SAndrew Thompson 	if (info->dma_error) {
94902ac6454SAndrew Thompson 		USB_BUS_LOCK(info->bus);
950a593f6b8SAndrew Thompson 		usbd_transfer_done(xfer, USB_ERR_DMA_LOAD_FAILED);
95102ac6454SAndrew Thompson 		USB_BUS_UNLOCK(info->bus);
95202ac6454SAndrew Thompson 		return;
95302ac6454SAndrew Thompson 	}
95402ac6454SAndrew Thompson 	if (info->dma_currframe != info->dma_nframes) {
95502ac6454SAndrew Thompson 
95602ac6454SAndrew Thompson 		if (info->dma_currframe == 0) {
95702ac6454SAndrew Thompson 			/* special case */
958a593f6b8SAndrew Thompson 			usb_pc_load_mem(xfer->frbuffers,
95902ac6454SAndrew Thompson 			    info->dma_frlength_0, 0);
96002ac6454SAndrew Thompson 		} else {
96102ac6454SAndrew Thompson 			/* default case */
96202ac6454SAndrew Thompson 			nframes = info->dma_currframe;
963a593f6b8SAndrew Thompson 			usb_pc_load_mem(xfer->frbuffers + nframes,
96402ac6454SAndrew Thompson 			    xfer->frlengths[nframes], 0);
96502ac6454SAndrew Thompson 		}
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson 		/* advance frame index */
96802ac6454SAndrew Thompson 		info->dma_currframe++;
96902ac6454SAndrew Thompson 
97002ac6454SAndrew Thompson 		return;
97102ac6454SAndrew Thompson 	}
97202ac6454SAndrew Thompson 	/* go ahead */
973a593f6b8SAndrew Thompson 	usb_bdma_pre_sync(xfer);
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson 	/* start loading next USB transfer, if any */
976a593f6b8SAndrew Thompson 	usb_command_wrapper(pq, NULL);
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 	/* finally start the hardware */
979a593f6b8SAndrew Thompson 	usbd_pipe_enter(xfer);
98002ac6454SAndrew Thompson }
98102ac6454SAndrew Thompson 
98202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
983a593f6b8SAndrew Thompson  *	usb_bdma_done_event
98402ac6454SAndrew Thompson  *
98502ac6454SAndrew Thompson  * This function is called when the BUS-DMA has loaded virtual memory
98602ac6454SAndrew Thompson  * into DMA, if any.
98702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
98802ac6454SAndrew Thompson void
989a593f6b8SAndrew Thompson usb_bdma_done_event(struct usb_dma_parent_tag *udpt)
99002ac6454SAndrew Thompson {
991760bc48eSAndrew Thompson 	struct usb_xfer_root *info;
99202ac6454SAndrew Thompson 
993bdc081c6SAndrew Thompson 	info = USB_DMATAG_TO_XROOT(udpt);
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 	mtx_assert(info->xfer_mtx, MA_OWNED);
99602ac6454SAndrew Thompson 
99702ac6454SAndrew Thompson 	/* copy error */
99802ac6454SAndrew Thompson 	info->dma_error = udpt->dma_error;
99902ac6454SAndrew Thompson 
100002ac6454SAndrew Thompson 	/* enter workloop again */
1001a593f6b8SAndrew Thompson 	usb_command_wrapper(&info->dma_q,
100202ac6454SAndrew Thompson 	    info->dma_q.curr);
100302ac6454SAndrew Thompson }
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1006a593f6b8SAndrew Thompson  *	usb_bdma_pre_sync
100702ac6454SAndrew Thompson  *
100802ac6454SAndrew Thompson  * This function handles DMA synchronisation that must be done before
100902ac6454SAndrew Thompson  * an USB transfer is started.
101002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
101102ac6454SAndrew Thompson void
1012a593f6b8SAndrew Thompson usb_bdma_pre_sync(struct usb_xfer *xfer)
101302ac6454SAndrew Thompson {
1014760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
1015e0a69b51SAndrew Thompson 	usb_frcount_t nframes;
101602ac6454SAndrew Thompson 
101702ac6454SAndrew Thompson 	if (xfer->flags_int.isochronous_xfr) {
101802ac6454SAndrew Thompson 		/* only one frame buffer */
101902ac6454SAndrew Thompson 		nframes = 1;
102002ac6454SAndrew Thompson 	} else {
102102ac6454SAndrew Thompson 		/* can be multiple frame buffers */
102202ac6454SAndrew Thompson 		nframes = xfer->nframes;
102302ac6454SAndrew Thompson 	}
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 	pc = xfer->frbuffers;
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	while (nframes--) {
102802ac6454SAndrew Thompson 
102902ac6454SAndrew Thompson 		if (pc->isread) {
1030a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(pc);
103102ac6454SAndrew Thompson 		} else {
1032a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc);
103302ac6454SAndrew Thompson 		}
103402ac6454SAndrew Thompson 		pc++;
103502ac6454SAndrew Thompson 	}
103602ac6454SAndrew Thompson }
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1039a593f6b8SAndrew Thompson  *	usb_bdma_post_sync
104002ac6454SAndrew Thompson  *
104102ac6454SAndrew Thompson  * This function handles DMA synchronisation that must be done after
104202ac6454SAndrew Thompson  * an USB transfer is complete.
104302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
104402ac6454SAndrew Thompson void
1045a593f6b8SAndrew Thompson usb_bdma_post_sync(struct usb_xfer *xfer)
104602ac6454SAndrew Thompson {
1047760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
1048e0a69b51SAndrew Thompson 	usb_frcount_t nframes;
104902ac6454SAndrew Thompson 
105002ac6454SAndrew Thompson 	if (xfer->flags_int.isochronous_xfr) {
105102ac6454SAndrew Thompson 		/* only one frame buffer */
105202ac6454SAndrew Thompson 		nframes = 1;
105302ac6454SAndrew Thompson 	} else {
105402ac6454SAndrew Thompson 		/* can be multiple frame buffers */
105502ac6454SAndrew Thompson 		nframes = xfer->nframes;
105602ac6454SAndrew Thompson 	}
105702ac6454SAndrew Thompson 
105802ac6454SAndrew Thompson 	pc = xfer->frbuffers;
105902ac6454SAndrew Thompson 
106002ac6454SAndrew Thompson 	while (nframes--) {
106102ac6454SAndrew Thompson 		if (pc->isread) {
1062a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(pc);
106302ac6454SAndrew Thompson 		}
106402ac6454SAndrew Thompson 		pc++;
106502ac6454SAndrew Thompson 	}
106602ac6454SAndrew Thompson }
1067bdc081c6SAndrew Thompson 
1068bdc081c6SAndrew Thompson #endif
1069