blob: 19fa68a732f32b4fb0a13d6972e4319ced2a6e87 [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,
5 * and should work with all USB controllers, regardles of bus type.
6 */
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 Siewior8b1d57f2014-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 Siewior8b1d57f2014-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
54 * will be shared by all drivers using that controller, or returns a negative
55 * errno value on error.
56 *
57 * Call hcd_buffer_destroy() to clean up after using those pools.
58 */
Oliver Neukum1a68f712007-01-25 11:17:41 +010059int hcd_buffer_create(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Oliver Neukum1a68f712007-01-25 11:17:41 +010061 char name[16];
Tobias Ollmann08283762010-12-25 11:17:01 +010062 int i, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Magnus Dammb3476672008-01-23 15:58:35 +090064 if (!hcd->self.controller->dma_mask &&
65 !(hcd->driver->flags & HCD_LOCAL_MEM))
Chris Humbertbd39b7f2005-11-28 09:29:23 -080066 return 0;
67
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -080068 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
69 size = pool_max[i];
70 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 continue;
Oliver Neukum1a68f712007-01-25 11:17:41 +010072 snprintf(name, sizeof name, "buffer-%d", size);
73 hcd->pool[i] = dma_pool_create(name, hcd->self.controller,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 size, size, 0);
Tobias Ollmann08283762010-12-25 11:17:01 +010075 if (!hcd->pool[i]) {
Oliver Neukum1a68f712007-01-25 11:17:41 +010076 hcd_buffer_destroy(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -ENOMEM;
78 }
79 }
80 return 0;
81}
82
83
84/**
85 * hcd_buffer_destroy - deallocate buffer pools
86 * @hcd: the bus whose buffer pools are to be destroyed
87 * Context: !in_interrupt()
88 *
89 * This frees the buffer pools created by hcd_buffer_create().
90 */
Oliver Neukum1a68f712007-01-25 11:17:41 +010091void hcd_buffer_destroy(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -080093 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -080095 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
96 struct dma_pool *pool = hcd->pool[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (pool) {
Oliver Neukum1a68f712007-01-25 11:17:41 +010098 dma_pool_destroy(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 hcd->pool[i] = NULL;
100 }
101 }
102}
103
104
Christoph Lameter441e1432006-12-06 20:33:19 -0800105/* sometimes alloc/free could use kmalloc with GFP_DMA, for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * better sharing and to leverage mm/slab.c intelligence.
107 */
108
Oliver Neukum1a68f712007-01-25 11:17:41 +0100109void *hcd_buffer_alloc(
Tobias Ollmann08283762010-12-25 11:17:01 +0100110 struct usb_bus *bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 size_t size,
Al Viro55016f12005-10-21 03:21:58 -0400112 gfp_t mem_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 dma_addr_t *dma
114)
115{
Alan Stern17200582006-08-30 11:32:52 -0400116 struct usb_hcd *hcd = bus_to_hcd(bus);
Tobias Ollmann08283762010-12-25 11:17:01 +0100117 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* some USB hosts just use PIO */
Magnus Dammb3476672008-01-23 15:58:35 +0900120 if (!bus->controller->dma_mask &&
121 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *dma = ~(dma_addr_t) 0;
Oliver Neukum1a68f712007-01-25 11:17:41 +0100123 return kmalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
126 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
Tobias Ollmann08283762010-12-25 11:17:01 +0100127 if (size <= pool_max[i])
128 return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Johannes Berga8aa4012009-04-18 11:00:39 +0200130 return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Oliver Neukum1a68f712007-01-25 11:17:41 +0100133void hcd_buffer_free(
Tobias Ollmann08283762010-12-25 11:17:01 +0100134 struct usb_bus *bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 size_t size,
Tobias Ollmann08283762010-12-25 11:17:01 +0100136 void *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dma_addr_t dma
138)
139{
Alan Stern17200582006-08-30 11:32:52 -0400140 struct usb_hcd *hcd = bus_to_hcd(bus);
Tobias Ollmann08283762010-12-25 11:17:01 +0100141 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (!addr)
144 return;
145
Magnus Dammb3476672008-01-23 15:58:35 +0900146 if (!bus->controller->dma_mask &&
147 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
Oliver Neukum1a68f712007-01-25 11:17:41 +0100148 kfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return;
150 }
151
152 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
Tobias Ollmann08283762010-12-25 11:17:01 +0100153 if (size <= pool_max[i]) {
154 dma_pool_free(hcd->pool[i], addr, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return;
156 }
157 }
Oliver Neukum1a68f712007-01-25 11:17:41 +0100158 dma_free_coherent(hcd->self.controller, size, addr, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}