2 * DMA memory management for framework level HCD code (hc_driver)
4 * This implementation plugs in through generic "usb_bus" level methods,
5 * and should work with all USB controllers, regardles of bus type.
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
15 #include <asm/scatterlist.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dmapool.h>
20 #ifdef CONFIG_USB_DEBUG
26 #include <linux/usb.h>
31 * DMA-Coherent Buffers
34 /* FIXME tune these based on pool statistics ... */
35 static const size_t pool_max [HCD_BUFFER_POOLS] = {
36 /* platforms without dma-friendly caches might need to
37 * prevent cacheline sharing...
43 /* bigger --> allocate pages */
47 /* SETUP primitives */
50 * hcd_buffer_create - initialize buffer pools
51 * @hcd: the bus whose buffer pools are to be initialized
52 * Context: !in_interrupt()
54 * Call this as part of initializing a host controller that uses the dma
55 * memory allocators. It initializes some pools of dma-coherent memory that
56 * will be shared by all drivers using that controller, or returns a negative
57 * errno value on error.
59 * Call hcd_buffer_destroy() to clean up after using those pools.
61 int hcd_buffer_create (struct usb_hcd *hcd)
66 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
67 if (!(size = pool_max [i]))
69 snprintf (name, sizeof name, "buffer-%d", size);
70 hcd->pool [i] = dma_pool_create (name, hcd->self.controller,
73 hcd_buffer_destroy (hcd);
79 EXPORT_SYMBOL (hcd_buffer_create);
83 * hcd_buffer_destroy - deallocate buffer pools
84 * @hcd: the bus whose buffer pools are to be destroyed
85 * Context: !in_interrupt()
87 * This frees the buffer pools created by hcd_buffer_create().
89 void hcd_buffer_destroy (struct usb_hcd *hcd)
93 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
94 struct dma_pool *pool = hcd->pool [i];
96 dma_pool_destroy (pool);
101 EXPORT_SYMBOL (hcd_buffer_destroy);
104 /* sometimes alloc/free could use kmalloc with SLAB_DMA, for
105 * better sharing and to leverage mm/slab.c intelligence.
108 void *hcd_buffer_alloc (
115 struct usb_hcd *hcd = bus->hcpriv;
118 /* some USB hosts just use PIO */
119 if (!bus->controller->dma_mask) {
120 *dma = ~(dma_addr_t) 0;
121 return kmalloc (size, mem_flags);
124 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
125 if (size <= pool_max [i])
126 return dma_pool_alloc (hcd->pool [i], mem_flags, dma);
128 return dma_alloc_coherent (hcd->self.controller, size, dma, 0);
131 void hcd_buffer_free (
138 struct usb_hcd *hcd = bus->hcpriv;
144 if (!bus->controller->dma_mask) {
149 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
150 if (size <= pool_max [i]) {
151 dma_pool_free (hcd->pool [i], addr, dma);
155 dma_free_coherent (hcd->self.controller, size, addr, dma);