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 27d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 28d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 29d2b99310SHans Petter Selasky #else 30ed6d949aSAndrew Thompson #include <sys/stdint.h> 31ed6d949aSAndrew Thompson #include <sys/stddef.h> 32ed6d949aSAndrew Thompson #include <sys/param.h> 33ed6d949aSAndrew Thompson #include <sys/queue.h> 34ed6d949aSAndrew Thompson #include <sys/types.h> 35ed6d949aSAndrew Thompson #include <sys/systm.h> 36ed6d949aSAndrew Thompson #include <sys/kernel.h> 37ed6d949aSAndrew Thompson #include <sys/bus.h> 38ed6d949aSAndrew Thompson #include <sys/module.h> 39ed6d949aSAndrew Thompson #include <sys/lock.h> 40ed6d949aSAndrew Thompson #include <sys/mutex.h> 41ed6d949aSAndrew Thompson #include <sys/condvar.h> 42ed6d949aSAndrew Thompson #include <sys/sysctl.h> 43ed6d949aSAndrew Thompson #include <sys/sx.h> 44ed6d949aSAndrew Thompson #include <sys/unistd.h> 45ed6d949aSAndrew Thompson #include <sys/callout.h> 46ed6d949aSAndrew Thompson #include <sys/malloc.h> 47ed6d949aSAndrew Thompson #include <sys/priv.h> 48ed6d949aSAndrew Thompson 4902ac6454SAndrew Thompson #include <dev/usb/usb.h> 50ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 51ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 5202ac6454SAndrew Thompson 53a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_debug 5402ac6454SAndrew Thompson 5502ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 5602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 5702ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 5802ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 5902ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 6002ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 6102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 6202ac6454SAndrew Thompson 6302ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 6402ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 65d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 6602ac6454SAndrew Thompson 67bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 68a593f6b8SAndrew Thompson static void usb_dma_tag_create(struct usb_dma_tag *, usb_size_t, usb_size_t); 69a593f6b8SAndrew Thompson static void usb_dma_tag_destroy(struct usb_dma_tag *); 70a593f6b8SAndrew Thompson static void usb_dma_lock_cb(void *, bus_dma_lock_op_t); 71a593f6b8SAndrew Thompson static void usb_pc_alloc_mem_cb(void *, bus_dma_segment_t *, int, int); 72a593f6b8SAndrew Thompson static void usb_pc_load_mem_cb(void *, bus_dma_segment_t *, int, int); 73a593f6b8SAndrew Thompson static void usb_pc_common_mem_cb(void *, bus_dma_segment_t *, int, int, 7402ac6454SAndrew Thompson uint8_t); 7502ac6454SAndrew Thompson #endif 7602ac6454SAndrew Thompson 7702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 78a593f6b8SAndrew Thompson * usbd_get_page - lookup DMA-able memory for the given offset 7902ac6454SAndrew Thompson * 8002ac6454SAndrew Thompson * NOTE: Only call this function when the "page_cache" structure has 8102ac6454SAndrew Thompson * been properly initialized ! 8202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 8302ac6454SAndrew Thompson void 84a593f6b8SAndrew Thompson usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset, 85760bc48eSAndrew Thompson struct usb_page_search *res) 8602ac6454SAndrew Thompson { 87271ae033SHans Petter Selasky #if USB_HAVE_BUSDMA 88760bc48eSAndrew Thompson struct usb_page *page; 8902ac6454SAndrew Thompson 9002ac6454SAndrew Thompson if (pc->page_start) { 9102ac6454SAndrew Thompson 9202ac6454SAndrew Thompson /* Case 1 - something has been loaded into DMA */ 9302ac6454SAndrew Thompson 9402ac6454SAndrew Thompson if (pc->buffer) { 9502ac6454SAndrew Thompson 9602ac6454SAndrew Thompson /* Case 1a - Kernel Virtual Address */ 9702ac6454SAndrew Thompson 9802ac6454SAndrew Thompson res->buffer = USB_ADD_BYTES(pc->buffer, offset); 9902ac6454SAndrew Thompson } 10002ac6454SAndrew Thompson offset += pc->page_offset_buf; 10102ac6454SAndrew Thompson 10202ac6454SAndrew Thompson /* compute destination page */ 10302ac6454SAndrew Thompson 10402ac6454SAndrew Thompson page = pc->page_start; 10502ac6454SAndrew Thompson 10602ac6454SAndrew Thompson if (pc->ismultiseg) { 10702ac6454SAndrew Thompson 10802ac6454SAndrew Thompson page += (offset / USB_PAGE_SIZE); 10902ac6454SAndrew Thompson 11002ac6454SAndrew Thompson offset %= USB_PAGE_SIZE; 11102ac6454SAndrew Thompson 11202ac6454SAndrew Thompson res->length = USB_PAGE_SIZE - offset; 11302ac6454SAndrew Thompson res->physaddr = page->physaddr + offset; 11402ac6454SAndrew Thompson } else { 1156d917491SHans Petter Selasky res->length = (usb_size_t)-1; 11602ac6454SAndrew Thompson res->physaddr = page->physaddr + offset; 11702ac6454SAndrew Thompson } 11802ac6454SAndrew Thompson if (!pc->buffer) { 11902ac6454SAndrew Thompson 12002ac6454SAndrew Thompson /* Case 1b - Non Kernel Virtual Address */ 12102ac6454SAndrew Thompson 12202ac6454SAndrew Thompson res->buffer = USB_ADD_BYTES(page->buffer, offset); 12302ac6454SAndrew Thompson } 124bdc081c6SAndrew Thompson return; 125bdc081c6SAndrew Thompson } 126bdc081c6SAndrew Thompson #endif 12702ac6454SAndrew Thompson /* Case 2 - Plain PIO */ 12802ac6454SAndrew Thompson 12902ac6454SAndrew Thompson res->buffer = USB_ADD_BYTES(pc->buffer, offset); 1306d917491SHans Petter Selasky res->length = (usb_size_t)-1; 131bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 13202ac6454SAndrew Thompson res->physaddr = 0; 133bdc081c6SAndrew Thompson #endif 13402ac6454SAndrew Thompson } 13502ac6454SAndrew Thompson 13602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 137c6b6f546SHans Petter Selasky * usb_pc_buffer_is_aligned - verify alignment 138c6b6f546SHans Petter Selasky * 139c6b6f546SHans Petter Selasky * This function is used to check if a page cache buffer is properly 140c6b6f546SHans Petter Selasky * aligned to reduce the use of bounce buffers in PIO mode. 141c6b6f546SHans Petter Selasky *------------------------------------------------------------------------*/ 142c6b6f546SHans Petter Selasky uint8_t 143c6b6f546SHans Petter Selasky usb_pc_buffer_is_aligned(struct usb_page_cache *pc, usb_frlength_t offset, 144c6b6f546SHans Petter Selasky usb_frlength_t len, usb_frlength_t mask) 145c6b6f546SHans Petter Selasky { 146c6b6f546SHans Petter Selasky struct usb_page_search buf_res; 147c6b6f546SHans Petter Selasky 148c6b6f546SHans Petter Selasky while (len != 0) { 149c6b6f546SHans Petter Selasky 150c6b6f546SHans Petter Selasky usbd_get_page(pc, offset, &buf_res); 151c6b6f546SHans Petter Selasky 152c6b6f546SHans Petter Selasky if (buf_res.length > len) 153c6b6f546SHans Petter Selasky buf_res.length = len; 154c6b6f546SHans Petter Selasky if (USB_P2U(buf_res.buffer) & mask) 155c6b6f546SHans Petter Selasky return (0); 156c6b6f546SHans Petter Selasky if (buf_res.length & mask) 157c6b6f546SHans Petter Selasky return (0); 158c6b6f546SHans Petter Selasky 159c6b6f546SHans Petter Selasky offset += buf_res.length; 160c6b6f546SHans Petter Selasky len -= buf_res.length; 161c6b6f546SHans Petter Selasky } 162c6b6f546SHans Petter Selasky return (1); 163c6b6f546SHans Petter Selasky } 164c6b6f546SHans Petter Selasky 165c6b6f546SHans Petter Selasky /*------------------------------------------------------------------------* 166a593f6b8SAndrew Thompson * usbd_copy_in - copy directly to DMA-able memory 16702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 16802ac6454SAndrew Thompson void 169a593f6b8SAndrew Thompson usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset, 170e0a69b51SAndrew Thompson const void *ptr, usb_frlength_t len) 17102ac6454SAndrew Thompson { 172760bc48eSAndrew Thompson struct usb_page_search buf_res; 17302ac6454SAndrew Thompson 17402ac6454SAndrew Thompson while (len != 0) { 17502ac6454SAndrew Thompson 176a593f6b8SAndrew Thompson usbd_get_page(cache, offset, &buf_res); 17702ac6454SAndrew Thompson 17802ac6454SAndrew Thompson if (buf_res.length > len) { 17902ac6454SAndrew Thompson buf_res.length = len; 18002ac6454SAndrew Thompson } 181271ae033SHans Petter Selasky memcpy(buf_res.buffer, ptr, buf_res.length); 18202ac6454SAndrew Thompson 18302ac6454SAndrew Thompson offset += buf_res.length; 18402ac6454SAndrew Thompson len -= buf_res.length; 18502ac6454SAndrew Thompson ptr = USB_ADD_BYTES(ptr, buf_res.length); 18602ac6454SAndrew Thompson } 18702ac6454SAndrew Thompson } 18802ac6454SAndrew Thompson 18902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 190a593f6b8SAndrew Thompson * usbd_copy_in_user - copy directly to DMA-able memory from userland 19102ac6454SAndrew Thompson * 19202ac6454SAndrew Thompson * Return values: 19302ac6454SAndrew Thompson * 0: Success 19402ac6454SAndrew Thompson * Else: Failure 19502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 196bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO 19702ac6454SAndrew Thompson int 198a593f6b8SAndrew Thompson usbd_copy_in_user(struct usb_page_cache *cache, usb_frlength_t offset, 199e0a69b51SAndrew Thompson const void *ptr, usb_frlength_t len) 20002ac6454SAndrew Thompson { 201760bc48eSAndrew Thompson struct usb_page_search buf_res; 20202ac6454SAndrew Thompson int error; 20302ac6454SAndrew Thompson 20402ac6454SAndrew Thompson while (len != 0) { 20502ac6454SAndrew Thompson 206a593f6b8SAndrew Thompson usbd_get_page(cache, offset, &buf_res); 20702ac6454SAndrew Thompson 20802ac6454SAndrew Thompson if (buf_res.length > len) { 20902ac6454SAndrew Thompson buf_res.length = len; 21002ac6454SAndrew Thompson } 21102ac6454SAndrew Thompson error = copyin(ptr, buf_res.buffer, buf_res.length); 21202ac6454SAndrew Thompson if (error) 21302ac6454SAndrew Thompson return (error); 21402ac6454SAndrew Thompson 21502ac6454SAndrew Thompson offset += buf_res.length; 21602ac6454SAndrew Thompson len -= buf_res.length; 21702ac6454SAndrew Thompson ptr = USB_ADD_BYTES(ptr, buf_res.length); 21802ac6454SAndrew Thompson } 21902ac6454SAndrew Thompson return (0); /* success */ 22002ac6454SAndrew Thompson } 221bdc081c6SAndrew Thompson #endif 22202ac6454SAndrew Thompson 22302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 224a593f6b8SAndrew Thompson * usbd_m_copy_in - copy a mbuf chain directly into DMA-able memory 22502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 226bdc081c6SAndrew Thompson #if USB_HAVE_MBUF 227a593f6b8SAndrew Thompson struct usb_m_copy_in_arg { 228760bc48eSAndrew Thompson struct usb_page_cache *cache; 229e0a69b51SAndrew Thompson usb_frlength_t dst_offset; 23002ac6454SAndrew Thompson }; 23102ac6454SAndrew Thompson 232578d0effSAndrew Thompson static int 233a593f6b8SAndrew Thompson usbd_m_copy_in_cb(void *arg, void *src, uint32_t count) 23402ac6454SAndrew Thompson { 235a593f6b8SAndrew Thompson register struct usb_m_copy_in_arg *ua = arg; 23602ac6454SAndrew Thompson 237a593f6b8SAndrew Thompson usbd_copy_in(ua->cache, ua->dst_offset, src, count); 23802ac6454SAndrew Thompson ua->dst_offset += count; 23902ac6454SAndrew Thompson return (0); 24002ac6454SAndrew Thompson } 24102ac6454SAndrew Thompson 24202ac6454SAndrew Thompson void 243a593f6b8SAndrew Thompson usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset, 244f9cb546cSAndrew Thompson struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len) 24502ac6454SAndrew Thompson { 246a593f6b8SAndrew Thompson struct usb_m_copy_in_arg arg = {cache, dst_offset}; 247bce421e9SHans Petter Selasky (void) m_apply(m, src_offset, src_len, &usbd_m_copy_in_cb, &arg); 24802ac6454SAndrew Thompson } 249bdc081c6SAndrew Thompson #endif 25002ac6454SAndrew Thompson 25102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 252a593f6b8SAndrew Thompson * usb_uiomove - factored out code 25302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 254bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO 25502ac6454SAndrew Thompson int 256a593f6b8SAndrew Thompson usb_uiomove(struct usb_page_cache *pc, struct uio *uio, 257e0a69b51SAndrew Thompson usb_frlength_t pc_offset, usb_frlength_t len) 25802ac6454SAndrew Thompson { 259760bc48eSAndrew Thompson struct usb_page_search res; 26002ac6454SAndrew Thompson int error = 0; 26102ac6454SAndrew Thompson 26202ac6454SAndrew Thompson while (len != 0) { 26302ac6454SAndrew Thompson 264a593f6b8SAndrew Thompson usbd_get_page(pc, pc_offset, &res); 26502ac6454SAndrew Thompson 26602ac6454SAndrew Thompson if (res.length > len) { 26702ac6454SAndrew Thompson res.length = len; 26802ac6454SAndrew Thompson } 26902ac6454SAndrew Thompson /* 27002ac6454SAndrew Thompson * "uiomove()" can sleep so one needs to make a wrapper, 27102ac6454SAndrew Thompson * exiting the mutex and checking things 27202ac6454SAndrew Thompson */ 27302ac6454SAndrew Thompson error = uiomove(res.buffer, res.length, uio); 27402ac6454SAndrew Thompson 27502ac6454SAndrew Thompson if (error) { 27602ac6454SAndrew Thompson break; 27702ac6454SAndrew Thompson } 27802ac6454SAndrew Thompson pc_offset += res.length; 27902ac6454SAndrew Thompson len -= res.length; 28002ac6454SAndrew Thompson } 28102ac6454SAndrew Thompson return (error); 28202ac6454SAndrew Thompson } 283bdc081c6SAndrew Thompson #endif 28402ac6454SAndrew Thompson 28502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 286a593f6b8SAndrew Thompson * usbd_copy_out - copy directly from DMA-able memory 28702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 28802ac6454SAndrew Thompson void 289a593f6b8SAndrew Thompson usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset, 290e0a69b51SAndrew Thompson void *ptr, usb_frlength_t len) 29102ac6454SAndrew Thompson { 292760bc48eSAndrew Thompson struct usb_page_search res; 29302ac6454SAndrew Thompson 29402ac6454SAndrew Thompson while (len != 0) { 29502ac6454SAndrew Thompson 296a593f6b8SAndrew Thompson usbd_get_page(cache, offset, &res); 29702ac6454SAndrew Thompson 29802ac6454SAndrew Thompson if (res.length > len) { 29902ac6454SAndrew Thompson res.length = len; 30002ac6454SAndrew Thompson } 301271ae033SHans Petter Selasky memcpy(ptr, res.buffer, res.length); 30202ac6454SAndrew Thompson 30302ac6454SAndrew Thompson offset += res.length; 30402ac6454SAndrew Thompson len -= res.length; 30502ac6454SAndrew Thompson ptr = USB_ADD_BYTES(ptr, res.length); 30602ac6454SAndrew Thompson } 30702ac6454SAndrew Thompson } 30802ac6454SAndrew Thompson 30902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 310a593f6b8SAndrew Thompson * usbd_copy_out_user - copy directly from DMA-able memory to userland 31102ac6454SAndrew Thompson * 31202ac6454SAndrew Thompson * Return values: 31302ac6454SAndrew Thompson * 0: Success 31402ac6454SAndrew Thompson * Else: Failure 31502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 316bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO 31702ac6454SAndrew Thompson int 318a593f6b8SAndrew Thompson usbd_copy_out_user(struct usb_page_cache *cache, usb_frlength_t offset, 319e0a69b51SAndrew Thompson void *ptr, usb_frlength_t len) 32002ac6454SAndrew Thompson { 321760bc48eSAndrew Thompson struct usb_page_search res; 32202ac6454SAndrew Thompson int error; 32302ac6454SAndrew Thompson 32402ac6454SAndrew Thompson while (len != 0) { 32502ac6454SAndrew Thompson 326a593f6b8SAndrew Thompson usbd_get_page(cache, offset, &res); 32702ac6454SAndrew Thompson 32802ac6454SAndrew Thompson if (res.length > len) { 32902ac6454SAndrew Thompson res.length = len; 33002ac6454SAndrew Thompson } 33102ac6454SAndrew Thompson error = copyout(res.buffer, ptr, res.length); 33202ac6454SAndrew Thompson if (error) 33302ac6454SAndrew Thompson return (error); 33402ac6454SAndrew Thompson 33502ac6454SAndrew Thompson offset += res.length; 33602ac6454SAndrew Thompson len -= res.length; 33702ac6454SAndrew Thompson ptr = USB_ADD_BYTES(ptr, res.length); 33802ac6454SAndrew Thompson } 33902ac6454SAndrew Thompson return (0); /* success */ 34002ac6454SAndrew Thompson } 341bdc081c6SAndrew Thompson #endif 34202ac6454SAndrew Thompson 34302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 344a593f6b8SAndrew Thompson * usbd_frame_zero - zero DMA-able memory 34502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 34602ac6454SAndrew Thompson void 347a593f6b8SAndrew Thompson usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset, 348e0a69b51SAndrew Thompson usb_frlength_t len) 34902ac6454SAndrew Thompson { 350760bc48eSAndrew Thompson struct usb_page_search res; 35102ac6454SAndrew Thompson 35202ac6454SAndrew Thompson while (len != 0) { 35302ac6454SAndrew Thompson 354a593f6b8SAndrew Thompson usbd_get_page(cache, offset, &res); 35502ac6454SAndrew Thompson 35602ac6454SAndrew Thompson if (res.length > len) { 35702ac6454SAndrew Thompson res.length = len; 35802ac6454SAndrew Thompson } 359271ae033SHans Petter Selasky memset(res.buffer, 0, res.length); 36002ac6454SAndrew Thompson 36102ac6454SAndrew Thompson offset += res.length; 36202ac6454SAndrew Thompson len -= res.length; 36302ac6454SAndrew Thompson } 36402ac6454SAndrew Thompson } 36502ac6454SAndrew Thompson 3663e873976SAndrew Thompson #if USB_HAVE_BUSDMA 36702ac6454SAndrew Thompson 36802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 369a593f6b8SAndrew Thompson * usb_dma_lock_cb - dummy callback 37002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 37102ac6454SAndrew Thompson static void 372a593f6b8SAndrew Thompson usb_dma_lock_cb(void *arg, bus_dma_lock_op_t op) 37302ac6454SAndrew Thompson { 37402ac6454SAndrew Thompson /* we use "mtx_owned()" instead of this function */ 37502ac6454SAndrew Thompson } 37602ac6454SAndrew Thompson 37702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 378a593f6b8SAndrew Thompson * usb_dma_tag_create - allocate a DMA tag 37902ac6454SAndrew Thompson * 38002ac6454SAndrew Thompson * NOTE: If the "align" parameter has a value of 1 the DMA-tag will 38102ac6454SAndrew Thompson * allow multi-segment mappings. Else all mappings are single-segment. 38202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 38302ac6454SAndrew Thompson static void 384a593f6b8SAndrew Thompson usb_dma_tag_create(struct usb_dma_tag *udt, 385f9cb546cSAndrew Thompson usb_size_t size, usb_size_t align) 38602ac6454SAndrew Thompson { 38702ac6454SAndrew Thompson bus_dma_tag_t tag; 38802ac6454SAndrew Thompson 38902ac6454SAndrew Thompson if (bus_dma_tag_create 39002ac6454SAndrew Thompson ( /* parent */ udt->tag_parent->tag, 39102ac6454SAndrew Thompson /* alignment */ align, 3921a7c3e90SHans Petter Selasky /* boundary */ 0, 39302ac6454SAndrew Thompson /* lowaddr */ (2ULL << (udt->tag_parent->dma_bits - 1)) - 1, 39402ac6454SAndrew Thompson /* highaddr */ BUS_SPACE_MAXADDR, 39502ac6454SAndrew Thompson /* filter */ NULL, 39602ac6454SAndrew Thompson /* filterarg */ NULL, 39702ac6454SAndrew Thompson /* maxsize */ size, 3986c3c4d71SMarius Strobl /* nsegments */ (align == 1 && size > 1) ? 39902ac6454SAndrew Thompson (2 + (size / USB_PAGE_SIZE)) : 1, 4006c3c4d71SMarius Strobl /* maxsegsz */ (align == 1 && size > USB_PAGE_SIZE) ? 40102ac6454SAndrew Thompson USB_PAGE_SIZE : size, 40202ac6454SAndrew Thompson /* flags */ BUS_DMA_KEEP_PG_OFFSET, 403a593f6b8SAndrew Thompson /* lockfn */ &usb_dma_lock_cb, 40402ac6454SAndrew Thompson /* lockarg */ NULL, 40502ac6454SAndrew Thompson &tag)) { 40602ac6454SAndrew Thompson tag = NULL; 40702ac6454SAndrew Thompson } 40802ac6454SAndrew Thompson udt->tag = tag; 40902ac6454SAndrew Thompson } 41002ac6454SAndrew Thompson 41102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 412a593f6b8SAndrew Thompson * usb_dma_tag_free - free a DMA tag 41302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 41402ac6454SAndrew Thompson static void 415a593f6b8SAndrew Thompson usb_dma_tag_destroy(struct usb_dma_tag *udt) 41602ac6454SAndrew Thompson { 41702ac6454SAndrew Thompson bus_dma_tag_destroy(udt->tag); 41802ac6454SAndrew Thompson } 41902ac6454SAndrew Thompson 42002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 421a593f6b8SAndrew Thompson * usb_pc_alloc_mem_cb - BUS-DMA callback function 42202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 42302ac6454SAndrew Thompson static void 424a593f6b8SAndrew Thompson usb_pc_alloc_mem_cb(void *arg, bus_dma_segment_t *segs, 42502ac6454SAndrew Thompson int nseg, int error) 42602ac6454SAndrew Thompson { 427a593f6b8SAndrew Thompson usb_pc_common_mem_cb(arg, segs, nseg, error, 0); 42802ac6454SAndrew Thompson } 42902ac6454SAndrew Thompson 43002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 431a593f6b8SAndrew Thompson * usb_pc_load_mem_cb - BUS-DMA callback function 43202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 43302ac6454SAndrew Thompson static void 434a593f6b8SAndrew Thompson usb_pc_load_mem_cb(void *arg, bus_dma_segment_t *segs, 43502ac6454SAndrew Thompson int nseg, int error) 43602ac6454SAndrew Thompson { 437a593f6b8SAndrew Thompson usb_pc_common_mem_cb(arg, segs, nseg, error, 1); 43802ac6454SAndrew Thompson } 43902ac6454SAndrew Thompson 44002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 441a593f6b8SAndrew Thompson * usb_pc_common_mem_cb - BUS-DMA callback function 44202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 44302ac6454SAndrew Thompson static void 444a593f6b8SAndrew Thompson usb_pc_common_mem_cb(void *arg, bus_dma_segment_t *segs, 44502ac6454SAndrew Thompson int nseg, int error, uint8_t isload) 44602ac6454SAndrew Thompson { 447760bc48eSAndrew Thompson struct usb_dma_parent_tag *uptag; 448760bc48eSAndrew Thompson struct usb_page_cache *pc; 449760bc48eSAndrew Thompson struct usb_page *pg; 450f9cb546cSAndrew Thompson usb_size_t rem; 4511a7c3e90SHans Petter Selasky bus_size_t off; 45202ac6454SAndrew Thompson uint8_t owned; 45302ac6454SAndrew Thompson 45402ac6454SAndrew Thompson pc = arg; 45502ac6454SAndrew Thompson uptag = pc->tag_parent; 45602ac6454SAndrew Thompson 45702ac6454SAndrew Thompson /* 45802ac6454SAndrew Thompson * XXX There is sometimes recursive locking here. 45902ac6454SAndrew Thompson * XXX We should try to find a better solution. 46002ac6454SAndrew Thompson * XXX Until further the "owned" variable does 46102ac6454SAndrew Thompson * XXX the trick. 46202ac6454SAndrew Thompson */ 46302ac6454SAndrew Thompson 46402ac6454SAndrew Thompson if (error) { 46502ac6454SAndrew Thompson goto done; 46602ac6454SAndrew Thompson } 4671a7c3e90SHans Petter Selasky 4681a7c3e90SHans Petter Selasky off = 0; 46902ac6454SAndrew Thompson pg = pc->page_start; 470d9c9c81cSPedro F. Giffuni pg->physaddr = rounddown2(segs->ds_addr, USB_PAGE_SIZE); 47102ac6454SAndrew Thompson rem = segs->ds_addr & (USB_PAGE_SIZE - 1); 47202ac6454SAndrew Thompson pc->page_offset_buf = rem; 47302ac6454SAndrew Thompson pc->page_offset_end += rem; 474ed6d949aSAndrew Thompson #ifdef USB_DEBUG 475288edd19SHans Petter Selasky if (nseg > 1) { 476288edd19SHans Petter Selasky int x; 477288edd19SHans Petter Selasky 478288edd19SHans Petter Selasky for (x = 0; x != nseg - 1; x++) { 479288edd19SHans Petter Selasky if (((segs[x].ds_addr + segs[x].ds_len) & (USB_PAGE_SIZE - 1)) == 480288edd19SHans Petter Selasky ((segs[x + 1].ds_addr & (USB_PAGE_SIZE - 1)))) 481288edd19SHans Petter Selasky continue; 48202ac6454SAndrew Thompson /* 483288edd19SHans Petter Selasky * This check verifies there is no page offset 484288edd19SHans Petter Selasky * hole between any of the segments. See the 485a2aa9bebSHans Petter Selasky * BUS_DMA_KEEP_PG_OFFSET flag. 48602ac6454SAndrew Thompson */ 487767cb2e2SAndrew Thompson DPRINTFN(0, "Page offset was not preserved\n"); 48802ac6454SAndrew Thompson error = 1; 48902ac6454SAndrew Thompson goto done; 49002ac6454SAndrew Thompson } 491288edd19SHans Petter Selasky } 49202ac6454SAndrew Thompson #endif 4935fb878a9SHans Petter Selasky while (pc->ismultiseg) { 4941a7c3e90SHans Petter Selasky off += USB_PAGE_SIZE; 4951a7c3e90SHans Petter Selasky if (off >= (segs->ds_len + rem)) { 4961a7c3e90SHans Petter Selasky /* page crossing */ 49702ac6454SAndrew Thompson nseg--; 49802ac6454SAndrew Thompson segs++; 4991a7c3e90SHans Petter Selasky off = 0; 5001a7c3e90SHans Petter Selasky rem = 0; 501b7b11d00SHans Petter Selasky if (nseg == 0) 502b7b11d00SHans Petter Selasky break; 5031a7c3e90SHans Petter Selasky } 50402ac6454SAndrew Thompson pg++; 505d9c9c81cSPedro F. Giffuni pg->physaddr = rounddown2(segs->ds_addr + off, USB_PAGE_SIZE); 50602ac6454SAndrew Thompson } 50702ac6454SAndrew Thompson 50802ac6454SAndrew Thompson done: 50902ac6454SAndrew Thompson owned = mtx_owned(uptag->mtx); 51002ac6454SAndrew Thompson if (!owned) 511*0eb8d462SHans Petter Selasky USB_MTX_LOCK(uptag->mtx); 51202ac6454SAndrew Thompson 51302ac6454SAndrew Thompson uptag->dma_error = (error ? 1 : 0); 51402ac6454SAndrew Thompson if (isload) { 51502ac6454SAndrew Thompson (uptag->func) (uptag); 51602ac6454SAndrew Thompson } else { 5178437751dSAndrew Thompson cv_broadcast(uptag->cv); 51802ac6454SAndrew Thompson } 51902ac6454SAndrew Thompson if (!owned) 520*0eb8d462SHans Petter Selasky USB_MTX_UNLOCK(uptag->mtx); 52102ac6454SAndrew Thompson } 52202ac6454SAndrew Thompson 52302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 524a593f6b8SAndrew Thompson * usb_pc_alloc_mem - allocate DMA'able memory 52502ac6454SAndrew Thompson * 52602ac6454SAndrew Thompson * Returns: 52702ac6454SAndrew Thompson * 0: Success 52802ac6454SAndrew Thompson * Else: Failure 52902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 53002ac6454SAndrew Thompson uint8_t 531a593f6b8SAndrew Thompson usb_pc_alloc_mem(struct usb_page_cache *pc, struct usb_page *pg, 532f9cb546cSAndrew Thompson usb_size_t size, usb_size_t align) 53302ac6454SAndrew Thompson { 534760bc48eSAndrew Thompson struct usb_dma_parent_tag *uptag; 535760bc48eSAndrew Thompson struct usb_dma_tag *utag; 53602ac6454SAndrew Thompson bus_dmamap_t map; 53702ac6454SAndrew Thompson void *ptr; 53802ac6454SAndrew Thompson int err; 53902ac6454SAndrew Thompson 54002ac6454SAndrew Thompson uptag = pc->tag_parent; 54102ac6454SAndrew Thompson 54202ac6454SAndrew Thompson if (align != 1) { 54302ac6454SAndrew Thompson /* 54402ac6454SAndrew Thompson * The alignment must be greater or equal to the 54502ac6454SAndrew Thompson * "size" else the object can be split between two 54602ac6454SAndrew Thompson * memory pages and we get a problem! 54702ac6454SAndrew Thompson */ 54802ac6454SAndrew Thompson while (align < size) { 54902ac6454SAndrew Thompson align *= 2; 55002ac6454SAndrew Thompson if (align == 0) { 55102ac6454SAndrew Thompson goto error; 55202ac6454SAndrew Thompson } 55302ac6454SAndrew Thompson } 55402ac6454SAndrew Thompson #if 1 55502ac6454SAndrew Thompson /* 55602ac6454SAndrew Thompson * XXX BUS-DMA workaround - FIXME later: 55702ac6454SAndrew Thompson * 55802ac6454SAndrew Thompson * We assume that that the aligment at this point of 55902ac6454SAndrew Thompson * the code is greater than or equal to the size and 56002ac6454SAndrew Thompson * less than two times the size, so that if we double 56102ac6454SAndrew Thompson * the size, the size will be greater than the 56202ac6454SAndrew Thompson * alignment. 56302ac6454SAndrew Thompson * 56402ac6454SAndrew Thompson * The bus-dma system has a check for "alignment" 56502ac6454SAndrew Thompson * being less than "size". If that check fails we end 56602ac6454SAndrew Thompson * up using contigmalloc which is page based even for 56702ac6454SAndrew Thompson * small allocations. Try to avoid that to save 56802ac6454SAndrew Thompson * memory, hence we sometimes to a large number of 56902ac6454SAndrew Thompson * small allocations! 57002ac6454SAndrew Thompson */ 57102ac6454SAndrew Thompson if (size <= (USB_PAGE_SIZE / 2)) { 57202ac6454SAndrew Thompson size *= 2; 57302ac6454SAndrew Thompson } 57402ac6454SAndrew Thompson #endif 57502ac6454SAndrew Thompson } 57602ac6454SAndrew Thompson /* get the correct DMA tag */ 577a593f6b8SAndrew Thompson utag = usb_dma_tag_find(uptag, size, align); 57802ac6454SAndrew Thompson if (utag == NULL) { 57902ac6454SAndrew Thompson goto error; 58002ac6454SAndrew Thompson } 58102ac6454SAndrew Thompson /* allocate memory */ 58202ac6454SAndrew Thompson if (bus_dmamem_alloc( 58302ac6454SAndrew Thompson utag->tag, &ptr, (BUS_DMA_WAITOK | BUS_DMA_COHERENT), &map)) { 58402ac6454SAndrew Thompson goto error; 58502ac6454SAndrew Thompson } 58602ac6454SAndrew Thompson /* setup page cache */ 58702ac6454SAndrew Thompson pc->buffer = ptr; 58802ac6454SAndrew Thompson pc->page_start = pg; 58902ac6454SAndrew Thompson pc->page_offset_buf = 0; 59002ac6454SAndrew Thompson pc->page_offset_end = size; 59102ac6454SAndrew Thompson pc->map = map; 59202ac6454SAndrew Thompson pc->tag = utag->tag; 59302ac6454SAndrew Thompson pc->ismultiseg = (align == 1); 59402ac6454SAndrew Thompson 595*0eb8d462SHans Petter Selasky USB_MTX_LOCK(uptag->mtx); 59602ac6454SAndrew Thompson 59702ac6454SAndrew Thompson /* load memory into DMA */ 59802ac6454SAndrew Thompson err = bus_dmamap_load( 599a593f6b8SAndrew Thompson utag->tag, map, ptr, size, &usb_pc_alloc_mem_cb, 60002ac6454SAndrew Thompson pc, (BUS_DMA_WAITOK | BUS_DMA_COHERENT)); 60102ac6454SAndrew Thompson 60202ac6454SAndrew Thompson if (err == EINPROGRESS) { 6038437751dSAndrew Thompson cv_wait(uptag->cv, uptag->mtx); 60402ac6454SAndrew Thompson err = 0; 60502ac6454SAndrew Thompson } 606*0eb8d462SHans Petter Selasky USB_MTX_UNLOCK(uptag->mtx); 60702ac6454SAndrew Thompson 60802ac6454SAndrew Thompson if (err || uptag->dma_error) { 60902ac6454SAndrew Thompson bus_dmamem_free(utag->tag, ptr, map); 61002ac6454SAndrew Thompson goto error; 61102ac6454SAndrew Thompson } 612271ae033SHans Petter Selasky memset(ptr, 0, size); 61302ac6454SAndrew Thompson 614a593f6b8SAndrew Thompson usb_pc_cpu_flush(pc); 61502ac6454SAndrew Thompson 61602ac6454SAndrew Thompson return (0); 61702ac6454SAndrew Thompson 61802ac6454SAndrew Thompson error: 61902ac6454SAndrew Thompson /* reset most of the page cache */ 62002ac6454SAndrew Thompson pc->buffer = NULL; 62102ac6454SAndrew Thompson pc->page_start = NULL; 62202ac6454SAndrew Thompson pc->page_offset_buf = 0; 62302ac6454SAndrew Thompson pc->page_offset_end = 0; 62402ac6454SAndrew Thompson pc->map = NULL; 62502ac6454SAndrew Thompson pc->tag = NULL; 62602ac6454SAndrew Thompson return (1); 62702ac6454SAndrew Thompson } 62802ac6454SAndrew Thompson 62902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 630a593f6b8SAndrew Thompson * usb_pc_free_mem - free DMA memory 63102ac6454SAndrew Thompson * 63202ac6454SAndrew Thompson * This function is NULL safe. 63302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 63402ac6454SAndrew Thompson void 635a593f6b8SAndrew Thompson usb_pc_free_mem(struct usb_page_cache *pc) 63602ac6454SAndrew Thompson { 63702ac6454SAndrew Thompson if (pc && pc->buffer) { 63802ac6454SAndrew Thompson 63902ac6454SAndrew Thompson bus_dmamap_unload(pc->tag, pc->map); 64002ac6454SAndrew Thompson 64102ac6454SAndrew Thompson bus_dmamem_free(pc->tag, pc->buffer, pc->map); 64202ac6454SAndrew Thompson 64302ac6454SAndrew Thompson pc->buffer = NULL; 64402ac6454SAndrew Thompson } 64502ac6454SAndrew Thompson } 64602ac6454SAndrew Thompson 64702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 648a593f6b8SAndrew Thompson * usb_pc_load_mem - load virtual memory into DMA 64902ac6454SAndrew Thompson * 65002ac6454SAndrew Thompson * Return values: 65102ac6454SAndrew Thompson * 0: Success 65202ac6454SAndrew Thompson * Else: Error 65302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 65402ac6454SAndrew Thompson uint8_t 655a593f6b8SAndrew Thompson usb_pc_load_mem(struct usb_page_cache *pc, usb_size_t size, uint8_t sync) 65602ac6454SAndrew Thompson { 65702ac6454SAndrew Thompson /* setup page cache */ 65802ac6454SAndrew Thompson pc->page_offset_buf = 0; 65902ac6454SAndrew Thompson pc->page_offset_end = size; 66002ac6454SAndrew Thompson pc->ismultiseg = 1; 66102ac6454SAndrew Thompson 662*0eb8d462SHans Petter Selasky USB_MTX_ASSERT(pc->tag_parent->mtx, MA_OWNED); 66302ac6454SAndrew Thompson 66402ac6454SAndrew Thompson if (size > 0) { 66502ac6454SAndrew Thompson if (sync) { 666760bc48eSAndrew Thompson struct usb_dma_parent_tag *uptag; 66702ac6454SAndrew Thompson int err; 66802ac6454SAndrew Thompson 66902ac6454SAndrew Thompson uptag = pc->tag_parent; 67002ac6454SAndrew Thompson 67102ac6454SAndrew Thompson /* 67202ac6454SAndrew Thompson * We have to unload the previous loaded DMA 67302ac6454SAndrew Thompson * pages before trying to load a new one! 67402ac6454SAndrew Thompson */ 67502ac6454SAndrew Thompson bus_dmamap_unload(pc->tag, pc->map); 67602ac6454SAndrew Thompson 67702ac6454SAndrew Thompson /* 67802ac6454SAndrew Thompson * Try to load memory into DMA. 67902ac6454SAndrew Thompson */ 68002ac6454SAndrew Thompson err = bus_dmamap_load( 68102ac6454SAndrew Thompson pc->tag, pc->map, pc->buffer, size, 682a593f6b8SAndrew Thompson &usb_pc_alloc_mem_cb, pc, BUS_DMA_WAITOK); 68302ac6454SAndrew Thompson if (err == EINPROGRESS) { 6848437751dSAndrew Thompson cv_wait(uptag->cv, uptag->mtx); 68502ac6454SAndrew Thompson err = 0; 68602ac6454SAndrew Thompson } 68702ac6454SAndrew Thompson if (err || uptag->dma_error) { 68802ac6454SAndrew Thompson return (1); 68902ac6454SAndrew Thompson } 69002ac6454SAndrew Thompson } else { 69102ac6454SAndrew Thompson 69202ac6454SAndrew Thompson /* 69302ac6454SAndrew Thompson * We have to unload the previous loaded DMA 69402ac6454SAndrew Thompson * pages before trying to load a new one! 69502ac6454SAndrew Thompson */ 69602ac6454SAndrew Thompson bus_dmamap_unload(pc->tag, pc->map); 69702ac6454SAndrew Thompson 69802ac6454SAndrew Thompson /* 69902ac6454SAndrew Thompson * Try to load memory into DMA. The callback 70002ac6454SAndrew Thompson * will be called in all cases: 70102ac6454SAndrew Thompson */ 70202ac6454SAndrew Thompson if (bus_dmamap_load( 70302ac6454SAndrew Thompson pc->tag, pc->map, pc->buffer, size, 704a593f6b8SAndrew Thompson &usb_pc_load_mem_cb, pc, BUS_DMA_WAITOK)) { 70502ac6454SAndrew Thompson } 70602ac6454SAndrew Thompson } 70702ac6454SAndrew Thompson } else { 70802ac6454SAndrew Thompson if (!sync) { 70902ac6454SAndrew Thompson /* 71002ac6454SAndrew Thompson * Call callback so that refcount is decremented 71102ac6454SAndrew Thompson * properly: 71202ac6454SAndrew Thompson */ 71302ac6454SAndrew Thompson pc->tag_parent->dma_error = 0; 71402ac6454SAndrew Thompson (pc->tag_parent->func) (pc->tag_parent); 71502ac6454SAndrew Thompson } 71602ac6454SAndrew Thompson } 71702ac6454SAndrew Thompson return (0); 71802ac6454SAndrew Thompson } 71902ac6454SAndrew Thompson 72002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 721a593f6b8SAndrew Thompson * usb_pc_cpu_invalidate - invalidate CPU cache 72202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 72302ac6454SAndrew Thompson void 724a593f6b8SAndrew Thompson usb_pc_cpu_invalidate(struct usb_page_cache *pc) 72502ac6454SAndrew Thompson { 72602ac6454SAndrew Thompson if (pc->page_offset_end == pc->page_offset_buf) { 72702ac6454SAndrew Thompson /* nothing has been loaded into this page cache! */ 72802ac6454SAndrew Thompson return; 72902ac6454SAndrew Thompson } 730f2db024fSAlfred Perlstein 731f2db024fSAlfred Perlstein /* 732f2db024fSAlfred Perlstein * TODO: We currently do XXX_POSTREAD and XXX_PREREAD at the 733f2db024fSAlfred Perlstein * same time, but in the future we should try to isolate the 734f2db024fSAlfred Perlstein * different cases to optimise the code. --HPS 735f2db024fSAlfred Perlstein */ 73635d01728SRafal Jaworowski bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_POSTREAD); 73735d01728SRafal Jaworowski bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREREAD); 73802ac6454SAndrew Thompson } 73902ac6454SAndrew Thompson 74002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 741a593f6b8SAndrew Thompson * usb_pc_cpu_flush - flush CPU cache 74202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 74302ac6454SAndrew Thompson void 744a593f6b8SAndrew Thompson usb_pc_cpu_flush(struct usb_page_cache *pc) 74502ac6454SAndrew Thompson { 74602ac6454SAndrew Thompson if (pc->page_offset_end == pc->page_offset_buf) { 74702ac6454SAndrew Thompson /* nothing has been loaded into this page cache! */ 74802ac6454SAndrew Thompson return; 74902ac6454SAndrew Thompson } 75035d01728SRafal Jaworowski bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREWRITE); 75102ac6454SAndrew Thompson } 75202ac6454SAndrew Thompson 75302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 754a593f6b8SAndrew Thompson * usb_pc_dmamap_create - create a DMA map 75502ac6454SAndrew Thompson * 75602ac6454SAndrew Thompson * Returns: 75702ac6454SAndrew Thompson * 0: Success 75802ac6454SAndrew Thompson * Else: Failure 75902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 76002ac6454SAndrew Thompson uint8_t 761a593f6b8SAndrew Thompson usb_pc_dmamap_create(struct usb_page_cache *pc, usb_size_t size) 76202ac6454SAndrew Thompson { 763760bc48eSAndrew Thompson struct usb_xfer_root *info; 764760bc48eSAndrew Thompson struct usb_dma_tag *utag; 76502ac6454SAndrew Thompson 76602ac6454SAndrew Thompson /* get info */ 767bdc081c6SAndrew Thompson info = USB_DMATAG_TO_XROOT(pc->tag_parent); 76802ac6454SAndrew Thompson 76902ac6454SAndrew Thompson /* sanity check */ 77002ac6454SAndrew Thompson if (info == NULL) { 77102ac6454SAndrew Thompson goto error; 77202ac6454SAndrew Thompson } 773a593f6b8SAndrew Thompson utag = usb_dma_tag_find(pc->tag_parent, size, 1); 77402ac6454SAndrew Thompson if (utag == NULL) { 77502ac6454SAndrew Thompson goto error; 77602ac6454SAndrew Thompson } 77702ac6454SAndrew Thompson /* create DMA map */ 77802ac6454SAndrew Thompson if (bus_dmamap_create(utag->tag, 0, &pc->map)) { 77902ac6454SAndrew Thompson goto error; 78002ac6454SAndrew Thompson } 78102ac6454SAndrew Thompson pc->tag = utag->tag; 78202ac6454SAndrew Thompson return 0; /* success */ 78302ac6454SAndrew Thompson 78402ac6454SAndrew Thompson error: 78502ac6454SAndrew Thompson pc->map = NULL; 78602ac6454SAndrew Thompson pc->tag = NULL; 78702ac6454SAndrew Thompson return 1; /* failure */ 78802ac6454SAndrew Thompson } 78902ac6454SAndrew Thompson 79002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 791a593f6b8SAndrew Thompson * usb_pc_dmamap_destroy 79202ac6454SAndrew Thompson * 79302ac6454SAndrew Thompson * This function is NULL safe. 79402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 79502ac6454SAndrew Thompson void 796a593f6b8SAndrew Thompson usb_pc_dmamap_destroy(struct usb_page_cache *pc) 79702ac6454SAndrew Thompson { 79802ac6454SAndrew Thompson if (pc && pc->tag) { 79902ac6454SAndrew Thompson bus_dmamap_destroy(pc->tag, pc->map); 80002ac6454SAndrew Thompson pc->tag = NULL; 80102ac6454SAndrew Thompson pc->map = NULL; 80202ac6454SAndrew Thompson } 80302ac6454SAndrew Thompson } 80402ac6454SAndrew Thompson 80502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 806a593f6b8SAndrew Thompson * usb_dma_tag_find - factored out code 80702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 808760bc48eSAndrew Thompson struct usb_dma_tag * 809a593f6b8SAndrew Thompson usb_dma_tag_find(struct usb_dma_parent_tag *udpt, 810f9cb546cSAndrew Thompson usb_size_t size, usb_size_t align) 81102ac6454SAndrew Thompson { 812760bc48eSAndrew Thompson struct usb_dma_tag *udt; 81302ac6454SAndrew Thompson uint8_t nudt; 81402ac6454SAndrew Thompson 815767cb2e2SAndrew Thompson USB_ASSERT(align > 0, ("Invalid parameter align = 0\n")); 816767cb2e2SAndrew Thompson USB_ASSERT(size > 0, ("Invalid parameter size = 0\n")); 81702ac6454SAndrew Thompson 81802ac6454SAndrew Thompson udt = udpt->utag_first; 81902ac6454SAndrew Thompson nudt = udpt->utag_max; 82002ac6454SAndrew Thompson 82102ac6454SAndrew Thompson while (nudt--) { 82202ac6454SAndrew Thompson 82302ac6454SAndrew Thompson if (udt->align == 0) { 824a593f6b8SAndrew Thompson usb_dma_tag_create(udt, size, align); 82502ac6454SAndrew Thompson if (udt->tag == NULL) { 82602ac6454SAndrew Thompson return (NULL); 82702ac6454SAndrew Thompson } 82802ac6454SAndrew Thompson udt->align = align; 82902ac6454SAndrew Thompson udt->size = size; 83002ac6454SAndrew Thompson return (udt); 83102ac6454SAndrew Thompson } 83202ac6454SAndrew Thompson if ((udt->align == align) && (udt->size == size)) { 83302ac6454SAndrew Thompson return (udt); 83402ac6454SAndrew Thompson } 83502ac6454SAndrew Thompson udt++; 83602ac6454SAndrew Thompson } 83702ac6454SAndrew Thompson return (NULL); 83802ac6454SAndrew Thompson } 83902ac6454SAndrew Thompson 84002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 841a593f6b8SAndrew Thompson * usb_dma_tag_setup - initialise USB DMA tags 84202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 84302ac6454SAndrew Thompson void 844a593f6b8SAndrew Thompson usb_dma_tag_setup(struct usb_dma_parent_tag *udpt, 845760bc48eSAndrew Thompson struct usb_dma_tag *udt, bus_dma_tag_t dmat, 846e0a69b51SAndrew Thompson struct mtx *mtx, usb_dma_callback_t *func, 847bdc081c6SAndrew Thompson uint8_t ndmabits, uint8_t nudt) 84802ac6454SAndrew Thompson { 849271ae033SHans Petter Selasky memset(udpt, 0, sizeof(*udpt)); 85002ac6454SAndrew Thompson 85102ac6454SAndrew Thompson /* sanity checking */ 85202ac6454SAndrew Thompson if ((nudt == 0) || 85302ac6454SAndrew Thompson (ndmabits == 0) || 85402ac6454SAndrew Thompson (mtx == NULL)) { 85502ac6454SAndrew Thompson /* something is corrupt */ 85602ac6454SAndrew Thompson return; 85702ac6454SAndrew Thompson } 85802ac6454SAndrew Thompson /* initialise condition variable */ 8598437751dSAndrew Thompson cv_init(udpt->cv, "USB DMA CV"); 86002ac6454SAndrew Thompson 86102ac6454SAndrew Thompson /* store some information */ 86202ac6454SAndrew Thompson udpt->mtx = mtx; 86302ac6454SAndrew Thompson udpt->func = func; 86402ac6454SAndrew Thompson udpt->tag = dmat; 86502ac6454SAndrew Thompson udpt->utag_first = udt; 86602ac6454SAndrew Thompson udpt->utag_max = nudt; 86702ac6454SAndrew Thompson udpt->dma_bits = ndmabits; 86802ac6454SAndrew Thompson 86902ac6454SAndrew Thompson while (nudt--) { 870271ae033SHans Petter Selasky memset(udt, 0, sizeof(*udt)); 87102ac6454SAndrew Thompson udt->tag_parent = udpt; 87202ac6454SAndrew Thompson udt++; 87302ac6454SAndrew Thompson } 87402ac6454SAndrew Thompson } 87502ac6454SAndrew Thompson 87602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 877a593f6b8SAndrew Thompson * usb_bus_tag_unsetup - factored out code 87802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 87902ac6454SAndrew Thompson void 880a593f6b8SAndrew Thompson usb_dma_tag_unsetup(struct usb_dma_parent_tag *udpt) 88102ac6454SAndrew Thompson { 882760bc48eSAndrew Thompson struct usb_dma_tag *udt; 88302ac6454SAndrew Thompson uint8_t nudt; 88402ac6454SAndrew Thompson 88502ac6454SAndrew Thompson udt = udpt->utag_first; 88602ac6454SAndrew Thompson nudt = udpt->utag_max; 88702ac6454SAndrew Thompson 88802ac6454SAndrew Thompson while (nudt--) { 88902ac6454SAndrew Thompson 89002ac6454SAndrew Thompson if (udt->align) { 89102ac6454SAndrew Thompson /* destroy the USB DMA tag */ 892a593f6b8SAndrew Thompson usb_dma_tag_destroy(udt); 89302ac6454SAndrew Thompson udt->align = 0; 89402ac6454SAndrew Thompson } 89502ac6454SAndrew Thompson udt++; 89602ac6454SAndrew Thompson } 89702ac6454SAndrew Thompson 89802ac6454SAndrew Thompson if (udpt->utag_max) { 89902ac6454SAndrew Thompson /* destroy the condition variable */ 9008437751dSAndrew Thompson cv_destroy(udpt->cv); 90102ac6454SAndrew Thompson } 90202ac6454SAndrew Thompson } 90302ac6454SAndrew Thompson 90402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 905a593f6b8SAndrew Thompson * usb_bdma_work_loop 90602ac6454SAndrew Thompson * 90702ac6454SAndrew Thompson * This function handles loading of virtual buffers into DMA and is 90802ac6454SAndrew Thompson * only called when "dma_refcount" is zero. 90902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 91002ac6454SAndrew Thompson void 911a593f6b8SAndrew Thompson usb_bdma_work_loop(struct usb_xfer_queue *pq) 91202ac6454SAndrew Thompson { 913760bc48eSAndrew Thompson struct usb_xfer_root *info; 914760bc48eSAndrew Thompson struct usb_xfer *xfer; 915e0a69b51SAndrew Thompson usb_frcount_t nframes; 91602ac6454SAndrew Thompson 91702ac6454SAndrew Thompson xfer = pq->curr; 91802ac6454SAndrew Thompson info = xfer->xroot; 91902ac6454SAndrew Thompson 920*0eb8d462SHans Petter Selasky USB_MTX_ASSERT(info->xfer_mtx, MA_OWNED); 92102ac6454SAndrew Thompson 92202ac6454SAndrew Thompson if (xfer->error) { 92302ac6454SAndrew Thompson /* some error happened */ 92402ac6454SAndrew Thompson USB_BUS_LOCK(info->bus); 925a593f6b8SAndrew Thompson usbd_transfer_done(xfer, 0); 92602ac6454SAndrew Thompson USB_BUS_UNLOCK(info->bus); 92702ac6454SAndrew Thompson return; 92802ac6454SAndrew Thompson } 92902ac6454SAndrew Thompson if (!xfer->flags_int.bdma_setup) { 930760bc48eSAndrew Thompson struct usb_page *pg; 931e0a69b51SAndrew Thompson usb_frlength_t frlength_0; 93202ac6454SAndrew Thompson uint8_t isread; 93302ac6454SAndrew Thompson 93402ac6454SAndrew Thompson xfer->flags_int.bdma_setup = 1; 93502ac6454SAndrew Thompson 93602ac6454SAndrew Thompson /* reset BUS-DMA load state */ 93702ac6454SAndrew Thompson 93802ac6454SAndrew Thompson info->dma_error = 0; 93902ac6454SAndrew Thompson 94002ac6454SAndrew Thompson if (xfer->flags_int.isochronous_xfr) { 94102ac6454SAndrew Thompson /* only one frame buffer */ 94202ac6454SAndrew Thompson nframes = 1; 94302ac6454SAndrew Thompson frlength_0 = xfer->sumlen; 94402ac6454SAndrew Thompson } else { 94502ac6454SAndrew Thompson /* can be multiple frame buffers */ 94602ac6454SAndrew Thompson nframes = xfer->nframes; 94702ac6454SAndrew Thompson frlength_0 = xfer->frlengths[0]; 94802ac6454SAndrew Thompson } 94902ac6454SAndrew Thompson 95002ac6454SAndrew Thompson /* 95102ac6454SAndrew Thompson * Set DMA direction first. This is needed to 95202ac6454SAndrew Thompson * select the correct cache invalidate and cache 95302ac6454SAndrew Thompson * flush operations. 95402ac6454SAndrew Thompson */ 95502ac6454SAndrew Thompson isread = USB_GET_DATA_ISREAD(xfer); 95602ac6454SAndrew Thompson pg = xfer->dma_page_ptr; 95702ac6454SAndrew Thompson 95802ac6454SAndrew Thompson if (xfer->flags_int.control_xfr && 95902ac6454SAndrew Thompson xfer->flags_int.control_hdr) { 96002ac6454SAndrew Thompson /* special case */ 961f29a0724SAndrew Thompson if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 96202ac6454SAndrew Thompson /* The device controller writes to memory */ 96302ac6454SAndrew Thompson xfer->frbuffers[0].isread = 1; 96402ac6454SAndrew Thompson } else { 96502ac6454SAndrew Thompson /* The host controller reads from memory */ 96602ac6454SAndrew Thompson xfer->frbuffers[0].isread = 0; 96702ac6454SAndrew Thompson } 96802ac6454SAndrew Thompson } else { 96902ac6454SAndrew Thompson /* default case */ 97002ac6454SAndrew Thompson xfer->frbuffers[0].isread = isread; 97102ac6454SAndrew Thompson } 97202ac6454SAndrew Thompson 97302ac6454SAndrew Thompson /* 97402ac6454SAndrew Thompson * Setup the "page_start" pointer which points to an array of 97502ac6454SAndrew Thompson * USB pages where information about the physical address of a 97602ac6454SAndrew Thompson * page will be stored. Also initialise the "isread" field of 97702ac6454SAndrew Thompson * the USB page caches. 97802ac6454SAndrew Thompson */ 97902ac6454SAndrew Thompson xfer->frbuffers[0].page_start = pg; 98002ac6454SAndrew Thompson 98102ac6454SAndrew Thompson info->dma_nframes = nframes; 98202ac6454SAndrew Thompson info->dma_currframe = 0; 98302ac6454SAndrew Thompson info->dma_frlength_0 = frlength_0; 98402ac6454SAndrew Thompson 98502ac6454SAndrew Thompson pg += (frlength_0 / USB_PAGE_SIZE); 98602ac6454SAndrew Thompson pg += 2; 98702ac6454SAndrew Thompson 98802ac6454SAndrew Thompson while (--nframes > 0) { 98902ac6454SAndrew Thompson xfer->frbuffers[nframes].isread = isread; 99002ac6454SAndrew Thompson xfer->frbuffers[nframes].page_start = pg; 99102ac6454SAndrew Thompson 99202ac6454SAndrew Thompson pg += (xfer->frlengths[nframes] / USB_PAGE_SIZE); 99302ac6454SAndrew Thompson pg += 2; 99402ac6454SAndrew Thompson } 99502ac6454SAndrew Thompson 99602ac6454SAndrew Thompson } 99702ac6454SAndrew Thompson if (info->dma_error) { 99802ac6454SAndrew Thompson USB_BUS_LOCK(info->bus); 999a593f6b8SAndrew Thompson usbd_transfer_done(xfer, USB_ERR_DMA_LOAD_FAILED); 100002ac6454SAndrew Thompson USB_BUS_UNLOCK(info->bus); 100102ac6454SAndrew Thompson return; 100202ac6454SAndrew Thompson } 100302ac6454SAndrew Thompson if (info->dma_currframe != info->dma_nframes) { 100402ac6454SAndrew Thompson 100502ac6454SAndrew Thompson if (info->dma_currframe == 0) { 100602ac6454SAndrew Thompson /* special case */ 1007a593f6b8SAndrew Thompson usb_pc_load_mem(xfer->frbuffers, 100802ac6454SAndrew Thompson info->dma_frlength_0, 0); 100902ac6454SAndrew Thompson } else { 101002ac6454SAndrew Thompson /* default case */ 101102ac6454SAndrew Thompson nframes = info->dma_currframe; 1012a593f6b8SAndrew Thompson usb_pc_load_mem(xfer->frbuffers + nframes, 101302ac6454SAndrew Thompson xfer->frlengths[nframes], 0); 101402ac6454SAndrew Thompson } 101502ac6454SAndrew Thompson 101602ac6454SAndrew Thompson /* advance frame index */ 101702ac6454SAndrew Thompson info->dma_currframe++; 101802ac6454SAndrew Thompson 101902ac6454SAndrew Thompson return; 102002ac6454SAndrew Thompson } 102102ac6454SAndrew Thompson /* go ahead */ 1022a593f6b8SAndrew Thompson usb_bdma_pre_sync(xfer); 102302ac6454SAndrew Thompson 102402ac6454SAndrew Thompson /* start loading next USB transfer, if any */ 1025a593f6b8SAndrew Thompson usb_command_wrapper(pq, NULL); 102602ac6454SAndrew Thompson 102702ac6454SAndrew Thompson /* finally start the hardware */ 1028a593f6b8SAndrew Thompson usbd_pipe_enter(xfer); 102902ac6454SAndrew Thompson } 103002ac6454SAndrew Thompson 103102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1032a593f6b8SAndrew Thompson * usb_bdma_done_event 103302ac6454SAndrew Thompson * 103402ac6454SAndrew Thompson * This function is called when the BUS-DMA has loaded virtual memory 103502ac6454SAndrew Thompson * into DMA, if any. 103602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 103702ac6454SAndrew Thompson void 1038a593f6b8SAndrew Thompson usb_bdma_done_event(struct usb_dma_parent_tag *udpt) 103902ac6454SAndrew Thompson { 1040760bc48eSAndrew Thompson struct usb_xfer_root *info; 104102ac6454SAndrew Thompson 1042bdc081c6SAndrew Thompson info = USB_DMATAG_TO_XROOT(udpt); 104302ac6454SAndrew Thompson 1044*0eb8d462SHans Petter Selasky USB_MTX_ASSERT(info->xfer_mtx, MA_OWNED); 104502ac6454SAndrew Thompson 104602ac6454SAndrew Thompson /* copy error */ 104702ac6454SAndrew Thompson info->dma_error = udpt->dma_error; 104802ac6454SAndrew Thompson 104902ac6454SAndrew Thompson /* enter workloop again */ 1050a593f6b8SAndrew Thompson usb_command_wrapper(&info->dma_q, 105102ac6454SAndrew Thompson info->dma_q.curr); 105202ac6454SAndrew Thompson } 105302ac6454SAndrew Thompson 105402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1055a593f6b8SAndrew Thompson * usb_bdma_pre_sync 105602ac6454SAndrew Thompson * 105702ac6454SAndrew Thompson * This function handles DMA synchronisation that must be done before 105802ac6454SAndrew Thompson * an USB transfer is started. 105902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 106002ac6454SAndrew Thompson void 1061a593f6b8SAndrew Thompson usb_bdma_pre_sync(struct usb_xfer *xfer) 106202ac6454SAndrew Thompson { 1063760bc48eSAndrew Thompson struct usb_page_cache *pc; 1064e0a69b51SAndrew Thompson usb_frcount_t nframes; 106502ac6454SAndrew Thompson 106602ac6454SAndrew Thompson if (xfer->flags_int.isochronous_xfr) { 106702ac6454SAndrew Thompson /* only one frame buffer */ 106802ac6454SAndrew Thompson nframes = 1; 106902ac6454SAndrew Thompson } else { 107002ac6454SAndrew Thompson /* can be multiple frame buffers */ 107102ac6454SAndrew Thompson nframes = xfer->nframes; 107202ac6454SAndrew Thompson } 107302ac6454SAndrew Thompson 107402ac6454SAndrew Thompson pc = xfer->frbuffers; 107502ac6454SAndrew Thompson 107602ac6454SAndrew Thompson while (nframes--) { 107702ac6454SAndrew Thompson 107802ac6454SAndrew Thompson if (pc->isread) { 1079a593f6b8SAndrew Thompson usb_pc_cpu_invalidate(pc); 108002ac6454SAndrew Thompson } else { 1081a593f6b8SAndrew Thompson usb_pc_cpu_flush(pc); 108202ac6454SAndrew Thompson } 108302ac6454SAndrew Thompson pc++; 108402ac6454SAndrew Thompson } 108502ac6454SAndrew Thompson } 108602ac6454SAndrew Thompson 108702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1088a593f6b8SAndrew Thompson * usb_bdma_post_sync 108902ac6454SAndrew Thompson * 109002ac6454SAndrew Thompson * This function handles DMA synchronisation that must be done after 109102ac6454SAndrew Thompson * an USB transfer is complete. 109202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 109302ac6454SAndrew Thompson void 1094a593f6b8SAndrew Thompson usb_bdma_post_sync(struct usb_xfer *xfer) 109502ac6454SAndrew Thompson { 1096760bc48eSAndrew Thompson struct usb_page_cache *pc; 1097e0a69b51SAndrew Thompson usb_frcount_t nframes; 109802ac6454SAndrew Thompson 109902ac6454SAndrew Thompson if (xfer->flags_int.isochronous_xfr) { 110002ac6454SAndrew Thompson /* only one frame buffer */ 110102ac6454SAndrew Thompson nframes = 1; 110202ac6454SAndrew Thompson } else { 110302ac6454SAndrew Thompson /* can be multiple frame buffers */ 110402ac6454SAndrew Thompson nframes = xfer->nframes; 110502ac6454SAndrew Thompson } 110602ac6454SAndrew Thompson 110702ac6454SAndrew Thompson pc = xfer->frbuffers; 110802ac6454SAndrew Thompson 110902ac6454SAndrew Thompson while (nframes--) { 111002ac6454SAndrew Thompson if (pc->isread) { 1111a593f6b8SAndrew Thompson usb_pc_cpu_invalidate(pc); 111202ac6454SAndrew Thompson } 111302ac6454SAndrew Thompson pc++; 111402ac6454SAndrew Thompson } 111502ac6454SAndrew Thompson } 1116bdc081c6SAndrew Thompson 1117bdc081c6SAndrew Thompson #endif 1118