Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * DMA memory management for framework level HCD code (hc_driver)
0004  *
0005  * This implementation plugs in through generic "usb_bus" level methods,
0006  * and should work with all USB controllers, regardless of bus type.
0007  *
0008  * Released under the GPLv2 only.
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/device.h>
0015 #include <linux/mm.h>
0016 #include <linux/io.h>
0017 #include <linux/dma-mapping.h>
0018 #include <linux/dmapool.h>
0019 #include <linux/genalloc.h>
0020 #include <linux/usb.h>
0021 #include <linux/usb/hcd.h>
0022 
0023 
0024 /*
0025  * DMA-Coherent Buffers
0026  */
0027 
0028 /* FIXME tune these based on pool statistics ... */
0029 static size_t pool_max[HCD_BUFFER_POOLS] = {
0030     32, 128, 512, 2048,
0031 };
0032 
0033 void __init usb_init_pool_max(void)
0034 {
0035     /*
0036      * The pool_max values must never be smaller than
0037      * ARCH_KMALLOC_MINALIGN.
0038      */
0039     if (ARCH_KMALLOC_MINALIGN <= 32)
0040         ;           /* Original value is okay */
0041     else if (ARCH_KMALLOC_MINALIGN <= 64)
0042         pool_max[0] = 64;
0043     else if (ARCH_KMALLOC_MINALIGN <= 128)
0044         pool_max[0] = 0;    /* Don't use this pool */
0045     else
0046         BUILD_BUG();        /* We don't allow this */
0047 }
0048 
0049 /* SETUP primitives */
0050 
0051 /**
0052  * hcd_buffer_create - initialize buffer pools
0053  * @hcd: the bus whose buffer pools are to be initialized
0054  *
0055  * Context: task context, might sleep
0056  *
0057  * Call this as part of initializing a host controller that uses the dma
0058  * memory allocators.  It initializes some pools of dma-coherent memory that
0059  * will be shared by all drivers using that controller.
0060  *
0061  * Call hcd_buffer_destroy() to clean up after using those pools.
0062  *
0063  * Return: 0 if successful. A negative errno value otherwise.
0064  */
0065 int hcd_buffer_create(struct usb_hcd *hcd)
0066 {
0067     char        name[16];
0068     int     i, size;
0069 
0070     if (hcd->localmem_pool || !hcd_uses_dma(hcd))
0071         return 0;
0072 
0073     for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0074         size = pool_max[i];
0075         if (!size)
0076             continue;
0077         snprintf(name, sizeof(name), "buffer-%d", size);
0078         hcd->pool[i] = dma_pool_create(name, hcd->self.sysdev,
0079                 size, size, 0);
0080         if (!hcd->pool[i]) {
0081             hcd_buffer_destroy(hcd);
0082             return -ENOMEM;
0083         }
0084     }
0085     return 0;
0086 }
0087 
0088 
0089 /**
0090  * hcd_buffer_destroy - deallocate buffer pools
0091  * @hcd: the bus whose buffer pools are to be destroyed
0092  *
0093  * Context: task context, might sleep
0094  *
0095  * This frees the buffer pools created by hcd_buffer_create().
0096  */
0097 void hcd_buffer_destroy(struct usb_hcd *hcd)
0098 {
0099     int i;
0100 
0101     if (!IS_ENABLED(CONFIG_HAS_DMA))
0102         return;
0103 
0104     for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0105         dma_pool_destroy(hcd->pool[i]);
0106         hcd->pool[i] = NULL;
0107     }
0108 }
0109 
0110 
0111 /* sometimes alloc/free could use kmalloc with GFP_DMA, for
0112  * better sharing and to leverage mm/slab.c intelligence.
0113  */
0114 
0115 void *hcd_buffer_alloc(
0116     struct usb_bus      *bus,
0117     size_t          size,
0118     gfp_t           mem_flags,
0119     dma_addr_t      *dma
0120 )
0121 {
0122     struct usb_hcd      *hcd = bus_to_hcd(bus);
0123     int         i;
0124 
0125     if (size == 0)
0126         return NULL;
0127 
0128     if (hcd->localmem_pool)
0129         return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);
0130 
0131     /* some USB hosts just use PIO */
0132     if (!hcd_uses_dma(hcd)) {
0133         *dma = ~(dma_addr_t) 0;
0134         return kmalloc(size, mem_flags);
0135     }
0136 
0137     for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0138         if (size <= pool_max[i])
0139             return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
0140     }
0141     return dma_alloc_coherent(hcd->self.sysdev, size, dma, mem_flags);
0142 }
0143 
0144 void hcd_buffer_free(
0145     struct usb_bus      *bus,
0146     size_t          size,
0147     void            *addr,
0148     dma_addr_t      dma
0149 )
0150 {
0151     struct usb_hcd      *hcd = bus_to_hcd(bus);
0152     int         i;
0153 
0154     if (!addr)
0155         return;
0156 
0157     if (hcd->localmem_pool) {
0158         gen_pool_free(hcd->localmem_pool, (unsigned long)addr, size);
0159         return;
0160     }
0161 
0162     if (!hcd_uses_dma(hcd)) {
0163         kfree(addr);
0164         return;
0165     }
0166 
0167     for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0168         if (size <= pool_max[i]) {
0169             dma_pool_free(hcd->pool[i], addr, dma);
0170             return;
0171         }
0172     }
0173     dma_free_coherent(hcd->self.sysdev, size, addr, dma);
0174 }