diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/hub/usbh_hub.c b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/hub/usbh_hub.c index 7fb1a7ff8..78768e91d 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/hub/usbh_hub.c +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/hub/usbh_hub.c @@ -335,12 +335,13 @@ static int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf) hub->index = index; /* g_hub_buf allocate virtual address and physical address*/ - rc = naive_mmap(&vaddr, &paddr, 32, false); + vaddr = USB_MEM_VIRADDR_ALLOC; + hub->g_hub_buf = (uint8_t *)vaddr; + rc = naive_mmap(&vaddr, &paddr, 32, true); if(rc != 0){ USB_LOG_ERR("g_hub_buf allocate error, ret: %d\n", rc); return -ENOMEM; } - hub->g_hub_buf = (uint8_t *)vaddr; hub->g_hub_buf_phy = (uint8_t *)paddr; hport->config.intf[intf].priv = hub; diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/rndis_host.c b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/rndis_host.c index addf77d12..49dec828d 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/rndis_host.c +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/rndis_host.c @@ -148,20 +148,22 @@ void usbh_rndis_run(struct usbh_rndis *rndis_class) s_rndis_class_ptr = rndis_class; - ret = naive_mmap(&vaddr, &paddr, RNDIS_ETH_BUFFER_LEN, false); + vaddr = USB_MEM_VIRADDR_ALLOC; + tx_buffer = (uint8_t *)vaddr; + ret = naive_mmap(&vaddr, &paddr, RNDIS_ETH_BUFFER_LEN, true); if(ret != 0){ USB_LOG_ERR("tx_buf_ptr allocate error, ret: %d\n", ret); return; } - tx_buffer = (uint8_t *)vaddr; tx_buffer_phy = (uint8_t *)paddr; - ret = naive_mmap(&vaddr, &paddr, RNDIS_RXETH_BUFFER_LEN, false); + vaddr = USB_MEM_VIRADDR_ALLOC; + rx_buffer = (uint8_t *)vaddr; + ret = naive_mmap(&vaddr, &paddr, RNDIS_RXETH_BUFFER_LEN, true); if(ret != 0){ USB_LOG_ERR("rx_buf_ptr allocate error, ret: %d\n", ret); return; } - rx_buffer = (uint8_t *)vaddr; rx_buffer_phy = (uint8_t *)paddr; mutex_sem_handle = usb_osal_sem_create(0); diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/usbh_rndis.c b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/usbh_rndis.c index 2f3457ec2..73dbee68d 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/usbh_rndis.c +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/class/wireless/usbh_rndis.c @@ -291,20 +291,22 @@ static int usbh_rndis_connect(struct usbh_hubport *hport, uint8_t intf) rndis_class->data_intf = intf + 1; /* allocate virtual address and physical address*/ - ret = naive_mmap(&vaddr, &paddr, 4096, false); + vaddr = USB_MEM_VIRADDR_ALLOC; + g_rndis_buf = (uint8_t *)vaddr; + ret = naive_mmap(&vaddr, &paddr, 4096, true); if(ret != 0){ USB_LOG_ERR("g_rndis_buf allocate error, ret: %d\n", ret); return -ENOMEM; } - g_rndis_buf = (uint8_t *)vaddr; g_rndis_buf_phy = (uint8_t *)paddr; - ret = naive_mmap(&vaddr, &paddr, 4096, false); + vaddr = USB_MEM_VIRADDR_ALLOC; + g_rndis_keepalive_buf = (uint8_t *)vaddr; + ret = naive_mmap(&vaddr, &paddr, 4096, true); if(ret != 0){ USB_LOG_ERR("g_rndis_keepalive_buf allocate error, ret: %d\n", ret); return -ENOMEM; } - g_rndis_keepalive_buf = (uint8_t *)vaddr; g_rndis_keepalive_buf_phy = (uint8_t *)paddr; hport->config.intf[intf].priv = rndis_class; diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/core/usbh_core.c b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/core/usbh_core.c index f03bb21ad..051fc04dd 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/core/usbh_core.c +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/core/usbh_core.c @@ -850,12 +850,13 @@ int usbh_initialize(uint32_t id, struct usbh_bus *usb) } /* ep0_request_buffer allocate virtual address and physical address*/ - rc = naive_mmap(&vaddr, &paddr, CONFIG_USBHOST_REQUEST_BUFFER_LEN, false); + vaddr = USB_MEM_VIRADDR_ALLOC; + usb->ep0_request_buffer = (uint8_t *)vaddr; + rc = naive_mmap(&vaddr, &paddr, CONFIG_USBHOST_REQUEST_BUFFER_LEN, true); if(rc != 0){ USB_LOG_ERR("ep0_request_buffer allocate error, ret: %d\n", rc); return -ENOMEM; } - usb->ep0_request_buffer = (uint8_t *)vaddr; usb->ep0_request_buffer_phy = (uint8_t *)paddr; usb_slist_add_tail(&usb_buses, &(usb->list)); diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.c b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.c index 8ff4ac44d..a3e79d2c7 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.c +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.c @@ -10,7 +10,14 @@ History: 1. Date: 2024-06-12 Author: AIIT XUOS Lab Modification: re-develop usb_hc_malloc, usb_hc_malloc_align and usb_hc_free functions +2. Date: 2024-08-21 +Author: AIIT XUOS Lab +Modification: The usb memory virtual address base address uses 0x1000000000, refer to XiZi virtual memory distribution. *************************************************/ + +/* The usb memory virtual address base address uses 0x1000000000, refer to XiZi virtual memory distribution.*/ +uintptr_t usb_mem_base_viraddr = 0x1000000000; + struct usb_mem_pool usb_mem_data_pool; @@ -63,12 +70,14 @@ int usb_mem_data_pool_init(){ count = 0; while(count < MEM_POOL_SIZE){ + vir_addr = USB_MEM_VIRADDR_ALLOC; + usb_mem_data_pool.pages[count].vir_addr = vir_addr; + rc = naive_mmap(&vir_addr, &phy_addr, 4096, true); if(rc != 0) goto mmap_error; - usb_mem_data_pool.pages[count].vir_addr = vir_addr; usb_mem_data_pool.pages[count].phy_addr = phy_addr; count++; diff --git a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.h b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.h index 238b8f1ec..53aeccc6f 100644 --- a/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.h +++ b/Ubiquitous/XiZi_AIoT/services/drivers/usb/components/mem/usb_mem.h @@ -31,6 +31,10 @@ Modification: Use usb_malloc, usb_free and usb_align functions #define USB_MEM_ALIGNX __attribute__((aligned(CONFIG_USB_ALIGN_SIZE))) +/*The usb virtual address offset 0x1000 corresponds to the function naive_mmap. */ +extern uintptr_t usb_mem_base_viraddr; +#define USB_MEM_VIRADDR_ALLOC (usb_mem_base_viraddr += 0x1000) + #define MEM_POOL_SIZE 128 void *usb_hc_malloc(size_t size); 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 4786c6c83..9e91f0075 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 @@ -692,8 +692,9 @@ static int xhci_dcbaa_alloc ( struct xhci_host *xhci ) { xhci->dcbaa.context = NULL; - - rc = naive_mmap(&dcbaap_addr, &dcbaap_phy, len, false); + dcbaap_addr = USB_MEM_VIRADDR_ALLOC; + xhci->dcbaa_addr = dcbaap_addr; + rc = naive_mmap(&dcbaap_addr, &dcbaap_phy, len, true); if(rc != 0){ USB_LOG_ERR("XHCI %s could not allocate DCBAA\n", xhci->name ); @@ -701,9 +702,8 @@ static int xhci_dcbaa_alloc ( struct xhci_host *xhci ) { goto err_alloc; } - xhci->dcbaa_addr = dcbaap_addr; xhci->dcbaa_phy = dcbaap_phy; - xhci->dcbaa.context = (void *)dcbaap_addr; + xhci->dcbaa.context = (void *)xhci->dcbaa_addr; #if 0 if ( ! xhci->dcbaa.context ) { @@ -757,10 +757,12 @@ static int xhci_scratchpad_alloc ( struct xhci_host *xhci ) { return 0; } - /* Allocate buffer to store */ - array_len = scratch->count * sizeof ( scratch->array[0] ); + /* Allocate buffer to store */ + array_len = scratch->count * sizeof ( scratch->array[0] ); + vir_addr = USB_MEM_VIRADDR_ALLOC; + scratch->array_addr = (void *)vir_addr; - rc = naive_mmap(&vir_addr, &phy_addr, array_len, false); + rc = naive_mmap(&vir_addr, &phy_addr, array_len, true); if(rc != 0){ USB_LOG_ERR("XHCI %s could not allocate scratch buffer\n", xhci->name ); @@ -768,8 +770,7 @@ static int xhci_scratchpad_alloc ( struct xhci_host *xhci ) { goto err_alloc; } - scratch->array_addr = (void *)vir_addr; - memset(scratch->array_addr, 0, array_len); + memset(scratch->array_addr, 0, array_len); scratch->array_phy = phy_addr; @@ -789,14 +790,16 @@ static int xhci_scratchpad_alloc ( struct xhci_host *xhci ) { * 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); + vir_page_addr = USB_MEM_VIRADDR_ALLOC; + scratch->sp_buffers[i] = (void *)vir_page_addr; + + rc = naive_mmap(&vir_page_addr, &phy_page_addr, xhci->pagesize, true); 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; } @@ -884,7 +887,10 @@ static int xhci_command_alloc ( struct xhci_host *xhci ) { #endif len = sizeof(*xhci->cmds); - rc = naive_mmap(&cmds_addr, &cmds_phy, len, false); + cmds_addr = USB_MEM_VIRADDR_ALLOC; + xhci->cmds_addr = cmds_addr; + + rc = naive_mmap(&cmds_addr, &cmds_phy, len, true); if(rc != 0){ USB_LOG_ERR("XHCI %s could not allocate CMD RING\n", xhci->name ); @@ -892,10 +898,9 @@ static int xhci_command_alloc ( struct xhci_host *xhci ) { goto err_ring_alloc; } - xhci->cmds_addr = cmds_addr; xhci->cmds_phy = cmds_phy; - xhci->cmds = (struct xhci_ring *)cmds_addr; + xhci->cmds = (struct xhci_ring *)xhci->cmds_addr; memset(xhci->cmds, 0U, sizeof(*xhci->cmds)); xhci->cmds->lock = usb_osal_mutex_create(); @@ -950,7 +955,10 @@ static int xhci_event_alloc ( struct xhci_host *xhci ) { /* Allocate event ring */ len = sizeof(*xhci->evts); - rc = naive_mmap(&evts_addr, &evts_phy, len, false); + evts_addr = USB_MEM_VIRADDR_ALLOC; + xhci->evts_addr = evts_addr; + + rc = naive_mmap(&evts_addr, &evts_phy, len, true); if(rc != 0){ USB_LOG_ERR("XHCI %s could not allocate Event RING\n", xhci->name ); @@ -966,16 +974,17 @@ static int xhci_event_alloc ( struct xhci_host *xhci ) { goto err_alloc_trb; } #endif - xhci->evts_addr = evts_addr; xhci->evts_phy = evts_phy; - xhci->evts = (struct xhci_ring *)evts_addr; + xhci->evts = (struct xhci_ring *)xhci->evts_addr; memset(xhci->evts, 0U, sizeof(*xhci->evts)); /* Allocate event ring segment table */ len = sizeof(*xhci->eseg); - rc = naive_mmap(&eseg_addr, &eseg_phy, len, false); + eseg_addr = USB_MEM_VIRADDR_ALLOC; + xhci->eseg_addr = eseg_addr; + rc = naive_mmap(&eseg_addr, &eseg_phy, len, true); if(rc != 0){ USB_LOG_ERR("XHCI %s could not allocate Event Segment\n", xhci->name ); rc = -ENOMEM; @@ -990,10 +999,9 @@ static int xhci_event_alloc ( struct xhci_host *xhci ) { } #endif - xhci->eseg_addr = eseg_addr; xhci->eseg_phy = eseg_phy; - xhci->eseg = (struct xhci_er_seg *)eseg_addr; + xhci->eseg = (struct xhci_er_seg *)xhci->eseg_addr; memset(xhci->eseg, 0U, sizeof(*xhci->eseg)); xhci->eseg->base = CPU_TO_LE64 ( ( (uintptr_t)xhci->evts ) ); @@ -1809,14 +1817,15 @@ static int xhci_context ( struct xhci_host *xhci, struct xhci_slot *slot, goto err_alloc; } #endif - rc = naive_mmap(&input_vir, &input_phy, len, false); + input_vir = USB_MEM_VIRADDR_ALLOC; + input = (void *)input_vir; + memset ( input, 0, len ); + rc = naive_mmap(&input_vir, &input_phy, len, true); if(rc != 0){ USB_LOG_ERR("XHCI %s could not allocate xhci context\n", xhci->name ); rc = -ENOMEM; goto err_alloc; } - input = (void *)input_vir; - memset ( input, 0, len ); /* Populate input context */ populate ( xhci, slot, ep, input ); @@ -2073,15 +2082,16 @@ int xhci_device_open ( struct xhci_host *xhci, struct xhci_endpoint *ep, int *sl goto err_alloc_context; } #endif - rc = naive_mmap(&context_vir, &context_phy, len, false); + context_vir = USB_MEM_VIRADDR_ALLOC; + slot->context_vir = context_vir; + rc = naive_mmap(&context_vir, &context_phy, len, true); if(rc != 0){ USB_LOG_ERR("XHCI %s could not allocate xhci device context\n", xhci->name ); rc = -ENOMEM; goto err_alloc; } - slot->context_vir = context_vir; slot->context_phy = context_phy; - slot->context = (struct xhci_slot_context *)context_vir; + slot->context = (struct xhci_slot_context *)slot->context_vir; memset ( slot->context, 0, len ); /* Set device context base address */