1// SPDX-License-Identifier: GPL-2.0
2/*
3 * DMA memory management for framework level HCD code (hc_driver)
4 *
5 * 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.
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/device.h>
15#include <linux/mm.h>
16#include <linux/io.h>
17#include <linux/dma-mapping.h>
18#include <linux/dmapool.h>
19#include <linux/genalloc.h>
20#include <linux/usb.h>
21#include <linux/usb/hcd.h>
22
23
24/*
25 * DMA-Coherent Buffers
26 */
27
28/* FIXME tune these based on pool statistics ... */
29static size_t pool_max[HCD_BUFFER_POOLS] = {
30	32, 128, 512, 2048,
31};
32
33void __init usb_init_pool_max(void)
34{
35	/*
36	 * The pool_max values must never be smaller than
37	 * ARCH_KMALLOC_MINALIGN.
38	 */
39	if (ARCH_KMALLOC_MINALIGN <= 32)
40		;			/* Original value is okay */
41	else if (ARCH_KMALLOC_MINALIGN <= 64)
42		pool_max[0] = 64;
43	else if (ARCH_KMALLOC_MINALIGN <= 128)
44		pool_max[0] = 0;	/* Don't use this pool */
45	else
46		BUILD_BUG();		/* We don't allow this */
47}
48
49/* SETUP primitives */
50
51/**
52 * hcd_buffer_create - initialize buffer pools
53 * @hcd: the bus whose buffer pools are to be initialized
54 * Context: !in_interrupt()
55 *
56 * Call this as part of initializing a host controller that uses the dma
57 * memory allocators.  It initializes some pools of dma-coherent memory that
58 * will be shared by all drivers using that controller.
59 *
60 * Call hcd_buffer_destroy() to clean up after using those pools.
61 *
62 * Return: 0 if successful. A negative errno value otherwise.
63 */
64int hcd_buffer_create(struct usb_hcd *hcd)
65{
66	char		name[16];
67	int		i, size;
68
69	if (hcd->localmem_pool || !hcd_uses_dma(hcd))
70		return 0;
71
72	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
73		size = pool_max[i];
74		if (!size)
75			continue;
76		snprintf(name, sizeof(name), "buffer-%d", size);
77		hcd->pool[i] = dma_pool_create(name, hcd->self.sysdev,
78				size, size, 0);
79		if (!hcd->pool[i]) {
80			hcd_buffer_destroy(hcd);
81			return -ENOMEM;
82		}
83	}
84	return 0;
85}
86
87
88/**
89 * hcd_buffer_destroy - deallocate buffer pools
90 * @hcd: the bus whose buffer pools are to be destroyed
91 * Context: !in_interrupt()
92 *
93 * This frees the buffer pools created by hcd_buffer_create().
94 */
95void hcd_buffer_destroy(struct usb_hcd *hcd)
96{
97	int i;
98
99	if (!IS_ENABLED(CONFIG_HAS_DMA))
100		return;
101
102	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
103		dma_pool_destroy(hcd->pool[i]);
104		hcd->pool[i] = NULL;
105	}
106}
107
108
109/* sometimes alloc/free could use kmalloc with GFP_DMA, for
110 * better sharing and to leverage mm/slab.c intelligence.
111 */
112
113void *hcd_buffer_alloc(
114	struct usb_bus		*bus,
115	size_t			size,
116	gfp_t			mem_flags,
117	dma_addr_t		*dma
118)
119{
120	struct usb_hcd		*hcd = bus_to_hcd(bus);
121	int			i;
122
123	if (size == 0)
124		return NULL;
125
126	if (hcd->localmem_pool)
127		return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);
128
129	/* some USB hosts just use PIO */
130	if (!hcd_uses_dma(hcd)) {
131		*dma = ~(dma_addr_t) 0;
132		return kmalloc(size, mem_flags);
133	}
134
135	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
136		if (size <= pool_max[i])
137			return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
138	}
139	return dma_alloc_coherent(hcd->self.sysdev, size, dma, mem_flags);
140}
141
142void hcd_buffer_free(
143	struct usb_bus		*bus,
144	size_t			size,
145	void			*addr,
146	dma_addr_t		dma
147)
148{
149	struct usb_hcd		*hcd = bus_to_hcd(bus);
150	int			i;
151
152	if (!addr)
153		return;
154
155	if (hcd->localmem_pool) {
156		gen_pool_free(hcd->localmem_pool, (unsigned long)addr, size);
157		return;
158	}
159
160	if (!hcd_uses_dma(hcd)) {
161		kfree(addr);
162		return;
163	}
164
165	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
166		if (size <= pool_max[i]) {
167			dma_pool_free(hcd->pool[i], addr, dma);
168			return;
169		}
170	}
171	dma_free_coherent(hcd->self.sysdev, size, addr, dma);
172}
173
174void *hcd_buffer_alloc_pages(struct usb_hcd *hcd,
175		size_t size, gfp_t mem_flags, dma_addr_t *dma)
176{
177	if (size == 0)
178		return NULL;
179
180	if (hcd->localmem_pool)
181		return gen_pool_dma_alloc_align(hcd->localmem_pool,
182				size, dma, PAGE_SIZE);
183
184	/* some USB hosts just use PIO */
185	if (!hcd_uses_dma(hcd)) {
186		*dma = DMA_MAPPING_ERROR;
187		return (void *)__get_free_pages(mem_flags,
188				get_order(size));
189	}
190
191	return dma_alloc_coherent(hcd->self.sysdev,
192			size, dma, mem_flags);
193}
194
195void hcd_buffer_free_pages(struct usb_hcd *hcd,
196		size_t size, void *addr, dma_addr_t dma)
197{
198	if (!addr)
199		return;
200
201	if (hcd->localmem_pool) {
202		gen_pool_free(hcd->localmem_pool,
203				(unsigned long)addr, size);
204		return;
205	}
206
207	if (!hcd_uses_dma(hcd)) {
208		free_pages((unsigned long)addr, get_order(size));
209		return;
210	}
211
212	dma_free_coherent(hcd->self.sysdev, size, addr, dma);
213}
214