blob: 89f2e7765093955b9bc37b1efcf5013d69f64482 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * DMA memory management for framework level HCD code (hc_driver)
3 *
4 * This implementation plugs in through generic "usb_bus" level methods,
Rahul Bedarkar025d4432014-01-04 11:24:41 +05305 * and should work with all USB controllers, regardless of bus type.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/device.h>
12#include <linux/mm.h>
Tobias Ollmann08283762010-12-25 11:17:01 +010013#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/dma-mapping.h>
15#include <linux/dmapool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020017#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19
20/*
21 * DMA-Coherent Buffers
22 */
23
24/* FIXME tune these based on pool statistics ... */
Sebastian Andrzej Siewior5efd2ea2014-12-05 15:13:54 +010025static size_t pool_max[HCD_BUFFER_POOLS] = {
26 32, 128, 512, 2048,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28
Sebastian Andrzej Siewior5efd2ea2014-12-05 15:13:54 +010029void __init usb_init_pool_max(void)
30{
31 /*
32 * The pool_max values must never be smaller than
33 * ARCH_KMALLOC_MINALIGN.
34 */
35 if (ARCH_KMALLOC_MINALIGN <= 32)
36 ; /* Original value is okay */
37 else if (ARCH_KMALLOC_MINALIGN <= 64)
38 pool_max[0] = 64;
39 else if (ARCH_KMALLOC_MINALIGN <= 128)
40 pool_max[0] = 0; /* Don't use this pool */
41 else
42 BUILD_BUG(); /* We don't allow this */
43}
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* SETUP primitives */
46
47/**
48 * hcd_buffer_create - initialize buffer pools
49 * @hcd: the bus whose buffer pools are to be initialized
50 * Context: !in_interrupt()
51 *
52 * Call this as part of initializing a host controller that uses the dma
53 * memory allocators. It initializes some pools of dma-coherent memory that
Yacine Belkadi626f0902013-08-02 20:10:04 +020054 * will be shared by all drivers using that controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
56 * Call hcd_buffer_destroy() to clean up after using those pools.
Yacine Belkadi626f0902013-08-02 20:10:04 +020057 *
58 * Return: 0 if successful. A negative errno value otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Oliver Neukum1a68f712007-01-25 11:17:41 +010060int hcd_buffer_create(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Oliver Neukum1a68f712007-01-25 11:17:41 +010062 char name[16];
Tobias Ollmann08283762010-12-25 11:17:01 +010063 int i, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Magnus Dammb3476672008-01-23 15:58:35 +090065 if (!hcd->self.controller->dma_mask &&
66 !(hcd->driver->flags & HCD_LOCAL_MEM))
Chris Humbertbd39b7f2005-11-28 09:29:23 -080067 return 0;
68
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -080069 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
70 size = pool_max[i];
71 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 continue;
Nizam Haider424dd7d2015-04-29 16:19:33 +053073 snprintf(name, sizeof(name), "buffer-%d", size);
Oliver Neukum1a68f712007-01-25 11:17:41 +010074 hcd->pool[i] = dma_pool_create(name, hcd->self.controller,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 size, size, 0);
Tobias Ollmann08283762010-12-25 11:17:01 +010076 if (!hcd->pool[i]) {
Oliver Neukum1a68f712007-01-25 11:17:41 +010077 hcd_buffer_destroy(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -ENOMEM;
79 }
80 }
81 return 0;
82}
83
84
85/**
86 * hcd_buffer_destroy - deallocate buffer pools
87 * @hcd: the bus whose buffer pools are to be destroyed
88 * Context: !in_interrupt()
89 *
90 * This frees the buffer pools created by hcd_buffer_create().
91 */
Oliver Neukum1a68f712007-01-25 11:17:41 +010092void hcd_buffer_destroy(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -080094 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -080096 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
97 struct dma_pool *pool = hcd->pool[i];
Nizam Haider424dd7d2015-04-29 16:19:33 +053098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (pool) {
Oliver Neukum1a68f712007-01-25 11:17:41 +0100100 dma_pool_destroy(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 hcd->pool[i] = NULL;
102 }
103 }
104}
105
106
Christoph Lameter441e1432006-12-06 20:33:19 -0800107/* sometimes alloc/free could use kmalloc with GFP_DMA, for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * better sharing and to leverage mm/slab.c intelligence.
109 */
110
Oliver Neukum1a68f712007-01-25 11:17:41 +0100111void *hcd_buffer_alloc(
Tobias Ollmann08283762010-12-25 11:17:01 +0100112 struct usb_bus *bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 size_t size,
Al Viro55016f12005-10-21 03:21:58 -0400114 gfp_t mem_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 dma_addr_t *dma
116)
117{
Alan Stern17200582006-08-30 11:32:52 -0400118 struct usb_hcd *hcd = bus_to_hcd(bus);
Tobias Ollmann08283762010-12-25 11:17:01 +0100119 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* some USB hosts just use PIO */
Magnus Dammb3476672008-01-23 15:58:35 +0900122 if (!bus->controller->dma_mask &&
123 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *dma = ~(dma_addr_t) 0;
Oliver Neukum1a68f712007-01-25 11:17:41 +0100125 return kmalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
Tobias Ollmann08283762010-12-25 11:17:01 +0100129 if (size <= pool_max[i])
130 return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Johannes Berga8aa4012009-04-18 11:00:39 +0200132 return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Oliver Neukum1a68f712007-01-25 11:17:41 +0100135void hcd_buffer_free(
Tobias Ollmann08283762010-12-25 11:17:01 +0100136 struct usb_bus *bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 size_t size,
Tobias Ollmann08283762010-12-25 11:17:01 +0100138 void *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dma_addr_t dma
140)
141{
Alan Stern17200582006-08-30 11:32:52 -0400142 struct usb_hcd *hcd = bus_to_hcd(bus);
Tobias Ollmann08283762010-12-25 11:17:01 +0100143 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if (!addr)
146 return;
147
Magnus Dammb3476672008-01-23 15:58:35 +0900148 if (!bus->controller->dma_mask &&
149 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
Oliver Neukum1a68f712007-01-25 11:17:41 +0100150 kfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return;
152 }
153
154 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
Tobias Ollmann08283762010-12-25 11:17:01 +0100155 if (size <= pool_max[i]) {
156 dma_pool_free(hcd->pool[i], addr, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return;
158 }
159 }
Oliver Neukum1a68f712007-01-25 11:17:41 +0100160 dma_free_coherent(hcd->self.controller, size, addr, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}