From 7cb39f992c17233f13a71f5c0c17631de8d90c99 Mon Sep 17 00:00:00 2001 From: xj Date: Fri, 5 Jul 2024 06:05:54 -0700 Subject: [PATCH] Implant scratchpad buffer functions --- .../drivers/usb/components/port/xhci/xhci.c | 72 +++++++++++++++++-- .../drivers/usb/components/port/xhci/xhci.h | 12 +++- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.c b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.c index abc84d67a..8484d7430 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.c +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.c @@ -698,7 +698,8 @@ static int xhci_dcbaa_alloc ( struct xhci_host *xhci ) { } xhci->dcbaa_addr = dcbaap_addr; - xhci->dcbaa.context = (void *)dcbaap_phy; + xhci->dcbaa_phy = dcbaap_phy; + xhci->dcbaa.context = (void *)dcbaap_addr; #if 0 if ( ! xhci->dcbaa.context ) { @@ -708,7 +709,9 @@ static int xhci_dcbaa_alloc ( struct xhci_host *xhci ) { } #endif - memset ( (void *)xhci->dcbaa_addr, 0, len ); +// memset ( (void *)xhci->dcbaa_addr, 0, len ); + memset ( xhci->dcbaa.context, 0, len ); + /* Program DCBAA pointer */ // dcbaap = (uintptr_t)( xhci->dcbaa.context ); @@ -739,6 +742,7 @@ static int xhci_scratchpad_alloc ( struct xhci_host *xhci ) { size_t buffer_len; size_t array_len; uintptr_t addr; + uintptr_t vir_addr, phy_addr, vir_page_addr, phy_page_addr; unsigned int i; int rc; @@ -749,6 +753,60 @@ static int xhci_scratchpad_alloc ( struct xhci_host *xhci ) { return 0; } + /* Allocate buffer to store */ + array_len = scratch->count * sizeof ( scratch->array[0] ); + + rc = naive_mmap(&vir_addr, &phy_addr, array_len, false); + + if(rc != 0){ + USB_LOG_ERR("XHCI %s could not allocate scratch buffer\n", xhci->name ); + rc = -ENOMEM; + goto err_alloc; + } + + scratch->array_addr = (void *)vir_addr; + memset(scratch->array_addr, 0, array_len); + scratch->array_phy = phy_addr; + + +/* + * Allocate memory for storing all virtual address of physical pages allocated to xHCI as scratchpad buffer. + */ + buffer_len = scratch->count * sizeof ( scratch->sp_buffers[0] ); + scratch->sp_buffers = usb_align(xhci_align (buffer_len), buffer_len); + + if(!scratch->sp_buffers){ + USB_LOG_ERR("XHCI %s could not allocate scratchpad buffers\n", + xhci->name ); + goto err_alloc_array; + } + +/* + * Allocate physical page one by one, and store its physical address in vir_addr. + */ + for(i = 0; i < scratch->count; i++){ + rc = naive_mmap(&vir_page_addr, &phy_page_addr, xhci->pagesize, false); + + if(rc != 0){ + USB_LOG_ERR("XHCI %s could not allocate scratchpad buffers\n", + xhci->name ); + goto err_alloc_array_vir; + } + scratch->sp_buffers[i] = (void *)vir_page_addr; + scratch->array_addr[i] = phy_page_addr; + } + +/* + * PLEASE NOTICE: + * The value that write to context[0] is the physical address of the memory region, which holds all physical address of memory pages + * that are allocated to xHCI as scratchpad buffers. The byte order of the physical address should be little end. + */ + USB_ASSERT ( xhci->dcbaa.context != NULL ); + xhci->dcbaa.context[0] = scratch->array_phy; + + return 0; + +#if 0 /* Allocate scratchpad buffers */ buffer_len = ( scratch->count * xhci->pagesize ); scratch->buffer = (uintptr_t)usb_align ( xhci->pagesize, buffer_len ); @@ -787,10 +845,16 @@ static int xhci_scratchpad_alloc ( struct xhci_host *xhci ) { ( scratch->array ), ( ( scratch->array ) + array_len ) ); return 0; +#endif + + + + +// usb_free ( scratch->array ); +err_alloc_array_vir: - usb_free ( scratch->array ); err_alloc_array: - usb_free ( scratch->buffer ); +// usb_free ( scratch->buffer ); err_alloc: return rc; } diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.h b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.h index 9c5a4b156..975bb2fec 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.h +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/port/xhci/xhci.h @@ -76,11 +76,16 @@ struct xhci_scratchpad { /** Scratchpad array */ uint64_t *array; /* - * vir_addr: Vitual address array. It holds all the virtual address of the pages allocated to xHCI scratchpad buffer. + * array_addr: address array. It holds all the virtual address of the memory allocated to xHCI for storing all + * physical address of scratchpad buffer. * array_phy: Physical address of array. */ - uint64_t *vir_addr; + uint64_t *array_addr; uintptr_t array_phy; + /* + * sp_buffers: scratchpad buffers. It holds all the virtual address of memory pages that are allocated to xHCI. + */ + void **sp_buffers; }; /** An input control context */ @@ -869,9 +874,10 @@ struct xhci_host { /* * Device context base address array. * - * dcbaa_addr holds the virtual address of the dcbaa, meanwhile acbaa keeps the value that should be writen to XHCI_OP_DCBAAP. + * dcbaa_addr holds the virtual address of the dcbaa, meanwhile dcbaa_phy keeps the value that should be writen to XHCI_OP_DCBAAP. */ uintptr_t dcbaa_addr; + uintptr_t dcbaa_phy; struct xhci_dcbaa dcbaa; /** Scratchpad buffer */