buffer.c 3.65 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2
3
4
5
/*
 * DMA memory management for framework level HCD code (hc_driver)
 *
 * This implementation plugs in through generic "usb_bus" level methods,
6
 * and should work with all USB controllers, regardless of bus type.
7
8
 *
 * Released under the GPLv2 only.
Linus Torvalds's avatar
Linus Torvalds committed
9
10
11
12
13
14
15
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/mm.h>
16
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
17
18
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
19
#include <linux/genalloc.h>
Linus Torvalds's avatar
Linus Torvalds committed
20
#include <linux/usb.h>
21
#include <linux/usb/hcd.h>
Linus Torvalds's avatar
Linus Torvalds committed
22
23
24
25
26
27
28


/*
 * DMA-Coherent Buffers
 */

/* FIXME tune these based on pool statistics ... */
29
30
static size_t pool_max[HCD_BUFFER_POOLS] = {
	32, 128, 512, 2048,
Linus Torvalds's avatar
Linus Torvalds committed
31
32
};

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void __init usb_init_pool_max(void)
{
	/*
	 * The pool_max values must never be smaller than
	 * ARCH_KMALLOC_MINALIGN.
	 */
	if (ARCH_KMALLOC_MINALIGN <= 32)
		;			/* Original value is okay */
	else if (ARCH_KMALLOC_MINALIGN <= 64)
		pool_max[0] = 64;
	else if (ARCH_KMALLOC_MINALIGN <= 128)
		pool_max[0] = 0;	/* Don't use this pool */
	else
		BUILD_BUG();		/* We don't allow this */
}
Linus Torvalds's avatar
Linus Torvalds committed
48
49
50
51
52
53
54
55
56
57

/* SETUP primitives */

/**
 * hcd_buffer_create - initialize buffer pools
 * @hcd: the bus whose buffer pools are to be initialized
 * Context: !in_interrupt()
 *
 * Call this as part of initializing a host controller that uses the dma
 * memory allocators.  It initializes some pools of dma-coherent memory that
58
 * will be shared by all drivers using that controller.
Linus Torvalds's avatar
Linus Torvalds committed
59
60
 *
 * Call hcd_buffer_destroy() to clean up after using those pools.
61
62
 *
 * Return: 0 if successful. A negative errno value otherwise.
Linus Torvalds's avatar
Linus Torvalds committed
63
 */
64
int hcd_buffer_create(struct usb_hcd *hcd)
Linus Torvalds's avatar
Linus Torvalds committed
65
{
66
	char		name[16];
67
	int		i, size;
Linus Torvalds's avatar
Linus Torvalds committed
68

69
	if (hcd->localmem_pool || !hcd_uses_dma(hcd))
70
71
		return 0;

72
73
74
	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
		size = pool_max[i];
		if (!size)
Linus Torvalds's avatar
Linus Torvalds committed
75
			continue;
76
		snprintf(name, sizeof(name), "buffer-%d", size);
77
		hcd->pool[i] = dma_pool_create(name, hcd->self.sysdev,
Linus Torvalds's avatar
Linus Torvalds committed
78
				size, size, 0);
79
		if (!hcd->pool[i]) {
80
			hcd_buffer_destroy(hcd);
Linus Torvalds's avatar
Linus Torvalds committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
			return -ENOMEM;
		}
	}
	return 0;
}


/**
 * hcd_buffer_destroy - deallocate buffer pools
 * @hcd: the bus whose buffer pools are to be destroyed
 * Context: !in_interrupt()
 *
 * This frees the buffer pools created by hcd_buffer_create().
 */
95
void hcd_buffer_destroy(struct usb_hcd *hcd)
Linus Torvalds's avatar
Linus Torvalds committed
96
{
97
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
98

99
100
101
	if (!IS_ENABLED(CONFIG_HAS_DMA))
		return;

102
	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
103
104
		dma_pool_destroy(hcd->pool[i]);
		hcd->pool[i] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
105
106
107
108
	}
}


109
/* sometimes alloc/free could use kmalloc with GFP_DMA, for
Linus Torvalds's avatar
Linus Torvalds committed
110
111
112
 * better sharing and to leverage mm/slab.c intelligence.
 */

113
void *hcd_buffer_alloc(
114
	struct usb_bus		*bus,
Linus Torvalds's avatar
Linus Torvalds committed
115
	size_t			size,
Al Viro's avatar
Al Viro committed
116
	gfp_t			mem_flags,
Linus Torvalds's avatar
Linus Torvalds committed
117
118
119
	dma_addr_t		*dma
)
{
120
	struct usb_hcd		*hcd = bus_to_hcd(bus);
121
	int			i;
Linus Torvalds's avatar
Linus Torvalds committed
122

123
124
125
	if (size == 0)
		return NULL;

126
127
128
	if (hcd->localmem_pool)
		return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);

Linus Torvalds's avatar
Linus Torvalds committed
129
	/* some USB hosts just use PIO */
130
	if (!hcd_uses_dma(hcd)) {
Linus Torvalds's avatar
Linus Torvalds committed
131
		*dma = ~(dma_addr_t) 0;
132
		return kmalloc(size, mem_flags);
Linus Torvalds's avatar
Linus Torvalds committed
133
134
135
	}

	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
136
137
		if (size <= pool_max[i])
			return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
Linus Torvalds's avatar
Linus Torvalds committed
138
	}
139
	return dma_alloc_coherent(hcd->self.sysdev, size, dma, mem_flags);
Linus Torvalds's avatar
Linus Torvalds committed
140
141
}

142
void hcd_buffer_free(
143
	struct usb_bus		*bus,
Linus Torvalds's avatar
Linus Torvalds committed
144
	size_t			size,
145
	void			*addr,
Linus Torvalds's avatar
Linus Torvalds committed
146
147
148
	dma_addr_t		dma
)
{
149
	struct usb_hcd		*hcd = bus_to_hcd(bus);
150
	int			i;
Linus Torvalds's avatar
Linus Torvalds committed
151
152
153
154

	if (!addr)
		return;

155
156
157
158
159
	if (hcd->localmem_pool) {
		gen_pool_free(hcd->localmem_pool, (unsigned long)addr, size);
		return;
	}

160
	if (!hcd_uses_dma(hcd)) {
161
		kfree(addr);
Linus Torvalds's avatar
Linus Torvalds committed
162
163
164
165
		return;
	}

	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
166
167
		if (size <= pool_max[i]) {
			dma_pool_free(hcd->pool[i], addr, dma);
Linus Torvalds's avatar
Linus Torvalds committed
168
169
170
			return;
		}
	}
171
	dma_free_coherent(hcd->self.sysdev, size, addr, dma);
Linus Torvalds's avatar
Linus Torvalds committed
172
}