usb memory virtual address reference XiZi, and modify naive_mmap usage method.

This commit is contained in:
songyanguang 2024-08-21 10:45:34 +08:00
parent 883cd6e4d5
commit bdfdc65bf6
7 changed files with 68 additions and 39 deletions

View File

@ -335,12 +335,13 @@ static int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf)
hub->index = index; hub->index = index;
/* g_hub_buf allocate virtual address and physical address*/ /* 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){ if(rc != 0){
USB_LOG_ERR("g_hub_buf allocate error, ret: %d\n", rc); USB_LOG_ERR("g_hub_buf allocate error, ret: %d\n", rc);
return -ENOMEM; return -ENOMEM;
} }
hub->g_hub_buf = (uint8_t *)vaddr;
hub->g_hub_buf_phy = (uint8_t *)paddr; hub->g_hub_buf_phy = (uint8_t *)paddr;
hport->config.intf[intf].priv = hub; hport->config.intf[intf].priv = hub;

View File

@ -148,20 +148,22 @@ void usbh_rndis_run(struct usbh_rndis *rndis_class)
s_rndis_class_ptr = 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){ if(ret != 0){
USB_LOG_ERR("tx_buf_ptr allocate error, ret: %d\n", ret); USB_LOG_ERR("tx_buf_ptr allocate error, ret: %d\n", ret);
return; return;
} }
tx_buffer = (uint8_t *)vaddr;
tx_buffer_phy = (uint8_t *)paddr; 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){ if(ret != 0){
USB_LOG_ERR("rx_buf_ptr allocate error, ret: %d\n", ret); USB_LOG_ERR("rx_buf_ptr allocate error, ret: %d\n", ret);
return; return;
} }
rx_buffer = (uint8_t *)vaddr;
rx_buffer_phy = (uint8_t *)paddr; rx_buffer_phy = (uint8_t *)paddr;
mutex_sem_handle = usb_osal_sem_create(0); mutex_sem_handle = usb_osal_sem_create(0);

View File

@ -291,20 +291,22 @@ static int usbh_rndis_connect(struct usbh_hubport *hport, uint8_t intf)
rndis_class->data_intf = intf + 1; rndis_class->data_intf = intf + 1;
/* allocate virtual address and physical address*/ /* 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){ if(ret != 0){
USB_LOG_ERR("g_rndis_buf allocate error, ret: %d\n", ret); USB_LOG_ERR("g_rndis_buf allocate error, ret: %d\n", ret);
return -ENOMEM; return -ENOMEM;
} }
g_rndis_buf = (uint8_t *)vaddr;
g_rndis_buf_phy = (uint8_t *)paddr; 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){ if(ret != 0){
USB_LOG_ERR("g_rndis_keepalive_buf allocate error, ret: %d\n", ret); USB_LOG_ERR("g_rndis_keepalive_buf allocate error, ret: %d\n", ret);
return -ENOMEM; return -ENOMEM;
} }
g_rndis_keepalive_buf = (uint8_t *)vaddr;
g_rndis_keepalive_buf_phy = (uint8_t *)paddr; g_rndis_keepalive_buf_phy = (uint8_t *)paddr;
hport->config.intf[intf].priv = rndis_class; hport->config.intf[intf].priv = rndis_class;

View File

@ -850,12 +850,13 @@ int usbh_initialize(uint32_t id, struct usbh_bus *usb)
} }
/* ep0_request_buffer allocate virtual address and physical address*/ /* 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){ if(rc != 0){
USB_LOG_ERR("ep0_request_buffer allocate error, ret: %d\n", rc); USB_LOG_ERR("ep0_request_buffer allocate error, ret: %d\n", rc);
return -ENOMEM; return -ENOMEM;
} }
usb->ep0_request_buffer = (uint8_t *)vaddr;
usb->ep0_request_buffer_phy = (uint8_t *)paddr; usb->ep0_request_buffer_phy = (uint8_t *)paddr;
usb_slist_add_tail(&usb_buses, &(usb->list)); usb_slist_add_tail(&usb_buses, &(usb->list));

View File

@ -10,7 +10,14 @@ History:
1. Date: 2024-06-12 1. Date: 2024-06-12
Author: AIIT XUOS Lab Author: AIIT XUOS Lab
Modification: re-develop usb_hc_malloc, usb_hc_malloc_align and usb_hc_free functions 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; struct usb_mem_pool usb_mem_data_pool;
@ -63,12 +70,14 @@ int usb_mem_data_pool_init(){
count = 0; count = 0;
while(count < MEM_POOL_SIZE){ 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); rc = naive_mmap(&vir_addr, &phy_addr, 4096, true);
if(rc != 0) if(rc != 0)
goto mmap_error; goto mmap_error;
usb_mem_data_pool.pages[count].vir_addr = vir_addr;
usb_mem_data_pool.pages[count].phy_addr = phy_addr; usb_mem_data_pool.pages[count].phy_addr = phy_addr;
count++; count++;

View File

@ -31,6 +31,10 @@ Modification: Use usb_malloc, usb_free and usb_align functions
#define USB_MEM_ALIGNX __attribute__((aligned(CONFIG_USB_ALIGN_SIZE))) #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 #define MEM_POOL_SIZE 128
void *usb_hc_malloc(size_t size); void *usb_hc_malloc(size_t size);

View File

@ -692,8 +692,9 @@ static int xhci_dcbaa_alloc ( struct xhci_host *xhci ) {
xhci->dcbaa.context = NULL; xhci->dcbaa.context = NULL;
dcbaap_addr = USB_MEM_VIRADDR_ALLOC;
rc = naive_mmap(&dcbaap_addr, &dcbaap_phy, len, false); xhci->dcbaa_addr = dcbaap_addr;
rc = naive_mmap(&dcbaap_addr, &dcbaap_phy, len, true);
if(rc != 0){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate DCBAA\n", xhci->name ); 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; goto err_alloc;
} }
xhci->dcbaa_addr = dcbaap_addr;
xhci->dcbaa_phy = dcbaap_phy; xhci->dcbaa_phy = dcbaap_phy;
xhci->dcbaa.context = (void *)dcbaap_addr; xhci->dcbaa.context = (void *)xhci->dcbaa_addr;
#if 0 #if 0
if ( ! xhci->dcbaa.context ) { if ( ! xhci->dcbaa.context ) {
@ -757,10 +757,12 @@ static int xhci_scratchpad_alloc ( struct xhci_host *xhci ) {
return 0; return 0;
} }
/* Allocate buffer to store */ /* Allocate buffer to store */
array_len = scratch->count * sizeof ( scratch->array[0] ); 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){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate scratch buffer\n", xhci->name ); 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; 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; 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. * Allocate physical page one by one, and store its physical address in vir_addr.
*/ */
for(i = 0; i < scratch->count; i++){ 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){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate scratchpad buffers\n", USB_LOG_ERR("XHCI %s could not allocate scratchpad buffers\n",
xhci->name ); xhci->name );
goto err_alloc_array_vir; goto err_alloc_array_vir;
} }
scratch->sp_buffers[i] = (void *)vir_page_addr;
scratch->array_addr[i] = phy_page_addr; scratch->array_addr[i] = phy_page_addr;
} }
@ -884,7 +887,10 @@ static int xhci_command_alloc ( struct xhci_host *xhci ) {
#endif #endif
len = sizeof(*xhci->cmds); 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){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate CMD RING\n", xhci->name ); 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; goto err_ring_alloc;
} }
xhci->cmds_addr = cmds_addr;
xhci->cmds_phy = cmds_phy; 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)); memset(xhci->cmds, 0U, sizeof(*xhci->cmds));
xhci->cmds->lock = usb_osal_mutex_create(); xhci->cmds->lock = usb_osal_mutex_create();
@ -950,7 +955,10 @@ static int xhci_event_alloc ( struct xhci_host *xhci ) {
/* Allocate event ring */ /* Allocate event ring */
len = sizeof(*xhci->evts); 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){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate Event RING\n", xhci->name ); 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; goto err_alloc_trb;
} }
#endif #endif
xhci->evts_addr = evts_addr;
xhci->evts_phy = evts_phy; 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)); memset(xhci->evts, 0U, sizeof(*xhci->evts));
/* Allocate event ring segment table */ /* Allocate event ring segment table */
len = sizeof(*xhci->eseg); 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){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate Event Segment\n", xhci->name ); USB_LOG_ERR("XHCI %s could not allocate Event Segment\n", xhci->name );
rc = -ENOMEM; rc = -ENOMEM;
@ -990,10 +999,9 @@ static int xhci_event_alloc ( struct xhci_host *xhci ) {
} }
#endif #endif
xhci->eseg_addr = eseg_addr;
xhci->eseg_phy = eseg_phy; 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)); memset(xhci->eseg, 0U, sizeof(*xhci->eseg));
xhci->eseg->base = CPU_TO_LE64 ( ( (uintptr_t)xhci->evts ) ); 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; goto err_alloc;
} }
#endif #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){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate xhci context\n", xhci->name ); USB_LOG_ERR("XHCI %s could not allocate xhci context\n", xhci->name );
rc = -ENOMEM; rc = -ENOMEM;
goto err_alloc; goto err_alloc;
} }
input = (void *)input_vir;
memset ( input, 0, len );
/* Populate input context */ /* Populate input context */
populate ( xhci, slot, ep, input ); 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; goto err_alloc_context;
} }
#endif #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){ if(rc != 0){
USB_LOG_ERR("XHCI %s could not allocate xhci device context\n", xhci->name ); USB_LOG_ERR("XHCI %s could not allocate xhci device context\n", xhci->name );
rc = -ENOMEM; rc = -ENOMEM;
goto err_alloc; goto err_alloc;
} }
slot->context_vir = context_vir;
slot->context_phy = context_phy; 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 ); memset ( slot->context, 0, len );
/* Set device context base address */ /* Set device context base address */