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);
82 * hcd_buffer_destroy - deallocate buffer pools
83 * @hcd: the bus whose buffer pools are to be destroyed
84 * Context: !in_interrupt()
86 * This frees the buffer pools created by hcd_buffer_create().
88 void hcd_buffer_destroy (struct usb_hcd *hcd)
92 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
93 struct dma_pool *pool = hcd->pool [i];
95 dma_pool_destroy (pool);
102 /* sometimes alloc/free could use kmalloc with SLAB_DMA, for
103 * better sharing and to leverage mm/slab.c intelligence.
106 void *hcd_buffer_alloc (
113 struct usb_hcd *hcd = bus->hcpriv;
116 /* some USB hosts just use PIO */
117 if (!bus->controller->dma_mask) {
118 *dma = ~(dma_addr_t) 0;
119 return kmalloc (size, mem_flags);
122 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
123 if (size <= pool_max [i])
124 return dma_pool_alloc (hcd->pool [i], mem_flags, dma);
126 return dma_alloc_coherent (hcd->self.controller, size, dma, 0);
129 void hcd_buffer_free (
136 struct usb_hcd *hcd = bus->hcpriv;
142 if (!bus->controller->dma_mask) {
147 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
148 if (size <= pool_max [i]) {
149 dma_pool_free (hcd->pool [i], addr, dma);
153 dma_free_coherent (hcd->self.controller, size, addr, dma);