2 * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called COPYING.
23 * This code implements the DMA subsystem. It provides a HW-neutral interface
24 * for other kernel code to use asynchronous memory copy capabilities,
25 * if present, and allows different HW DMA drivers to register as providing
28 * Due to the fact we are accelerating what is already a relatively fast
29 * operation, the code goes to great lengths to avoid additional overhead,
34 * The subsystem keeps two global lists, dma_device_list and dma_client_list.
35 * Both of these are protected by a mutex, dma_list_mutex.
37 * Each device has a channels list, which runs unlocked but is never modified
38 * once the device is registered, it's just setup by the driver.
40 * Each client has a channels list, it's only modified under the client->lock
41 * and in an RCU callback, so it's safe to read under rcu_read_lock().
43 * Each device has a kref, which is initialized to 1 when the device is
44 * registered. A kref_put is done for each class_device registered. When the
45 * class_device is released, the coresponding kref_put is done in the release
46 * method. Every time one of the device's channels is allocated to a client,
47 * a kref_get occurs. When the channel is freed, the coresponding kref_put
48 * happens. The device's release function does a completion, so
49 * unregister_device does a remove event, class_device_unregister, a kref_put
50 * for the first reference, then waits on the completion for all other
51 * references to finish.
53 * Each channel has an open-coded implementation of Rusty Russell's "bigref,"
54 * with a kref and a per_cpu local_t. A single reference is set when on an
55 * ADDED event, and removed with a REMOVE event. Net DMA client takes an
56 * extra reference per outstanding transaction. The relase function does a
57 * kref_put on the device. -ChrisL
60 #include <linux/init.h>
61 #include <linux/module.h>
62 #include <linux/device.h>
63 #include <linux/dmaengine.h>
64 #include <linux/hardirq.h>
65 #include <linux/spinlock.h>
66 #include <linux/percpu.h>
67 #include <linux/rcupdate.h>
68 #include <linux/mutex.h>
70 static DEFINE_MUTEX(dma_list_mutex);
71 static LIST_HEAD(dma_device_list);
72 static LIST_HEAD(dma_client_list);
74 /* --- sysfs implementation --- */
76 static ssize_t show_memcpy_count(struct class_device *cd, char *buf)
78 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
79 unsigned long count = 0;
82 for_each_possible_cpu(i)
83 count += per_cpu_ptr(chan->local, i)->memcpy_count;
85 return sprintf(buf, "%lu\n", count);
88 static ssize_t show_bytes_transferred(struct class_device *cd, char *buf)
90 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
91 unsigned long count = 0;
94 for_each_possible_cpu(i)
95 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
97 return sprintf(buf, "%lu\n", count);
100 static ssize_t show_in_use(struct class_device *cd, char *buf)
102 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
104 return sprintf(buf, "%d\n", (chan->client ? 1 : 0));
107 static struct class_device_attribute dma_class_attrs[] = {
108 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
109 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
110 __ATTR(in_use, S_IRUGO, show_in_use, NULL),
114 static void dma_async_device_cleanup(struct kref *kref);
116 static void dma_class_dev_release(struct class_device *cd)
118 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
119 kref_put(&chan->device->refcount, dma_async_device_cleanup);
122 static struct class dma_devclass = {
124 .class_dev_attrs = dma_class_attrs,
125 .release = dma_class_dev_release,
128 /* --- client and device registration --- */
131 * dma_client_chan_alloc - try to allocate a channel to a client
132 * @client: &dma_client
134 * Called with dma_list_mutex held.
136 static struct dma_chan *dma_client_chan_alloc(struct dma_client *client)
138 struct dma_device *device;
139 struct dma_chan *chan;
141 int desc; /* allocated descriptor count */
143 /* Find a channel, any DMA engine will do */
144 list_for_each_entry(device, &dma_device_list, global_node) {
145 list_for_each_entry(chan, &device->channels, device_node) {
149 desc = chan->device->device_alloc_chan_resources(chan);
151 kref_get(&device->refcount);
152 kref_init(&chan->refcount);
154 INIT_RCU_HEAD(&chan->rcu);
155 chan->client = client;
156 spin_lock_irqsave(&client->lock, flags);
157 list_add_tail_rcu(&chan->client_node,
159 spin_unlock_irqrestore(&client->lock, flags);
169 * dma_chan_cleanup - release a DMA channel's resources
170 * @kref: kernel reference structure that contains the DMA channel device
172 void dma_chan_cleanup(struct kref *kref)
174 struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
175 chan->device->device_free_chan_resources(chan);
177 kref_put(&chan->device->refcount, dma_async_device_cleanup);
179 EXPORT_SYMBOL(dma_chan_cleanup);
181 static void dma_chan_free_rcu(struct rcu_head *rcu)
183 struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
184 int bias = 0x7FFFFFFF;
186 for_each_possible_cpu(i)
187 bias -= local_read(&per_cpu_ptr(chan->local, i)->refcount);
188 atomic_sub(bias, &chan->refcount.refcount);
189 kref_put(&chan->refcount, dma_chan_cleanup);
192 static void dma_client_chan_free(struct dma_chan *chan)
194 atomic_add(0x7FFFFFFF, &chan->refcount.refcount);
196 call_rcu(&chan->rcu, dma_chan_free_rcu);
200 * dma_chans_rebalance - reallocate channels to clients
202 * When the number of DMA channel in the system changes,
203 * channels need to be rebalanced among clients.
205 static void dma_chans_rebalance(void)
207 struct dma_client *client;
208 struct dma_chan *chan;
211 mutex_lock(&dma_list_mutex);
213 list_for_each_entry(client, &dma_client_list, global_node) {
214 while (client->chans_desired > client->chan_count) {
215 chan = dma_client_chan_alloc(client);
218 client->chan_count++;
219 client->event_callback(client,
223 while (client->chans_desired < client->chan_count) {
224 spin_lock_irqsave(&client->lock, flags);
225 chan = list_entry(client->channels.next,
228 list_del_rcu(&chan->client_node);
229 spin_unlock_irqrestore(&client->lock, flags);
230 client->chan_count--;
231 client->event_callback(client,
233 DMA_RESOURCE_REMOVED);
234 dma_client_chan_free(chan);
238 mutex_unlock(&dma_list_mutex);
242 * dma_async_client_register - allocate and register a &dma_client
243 * @event_callback: callback for notification of channel addition/removal
245 struct dma_client *dma_async_client_register(dma_event_callback event_callback)
247 struct dma_client *client;
249 client = kzalloc(sizeof(*client), GFP_KERNEL);
253 INIT_LIST_HEAD(&client->channels);
254 spin_lock_init(&client->lock);
255 client->chans_desired = 0;
256 client->chan_count = 0;
257 client->event_callback = event_callback;
259 mutex_lock(&dma_list_mutex);
260 list_add_tail(&client->global_node, &dma_client_list);
261 mutex_unlock(&dma_list_mutex);
265 EXPORT_SYMBOL(dma_async_client_register);
268 * dma_async_client_unregister - unregister a client and free the &dma_client
269 * @client: &dma_client to free
271 * Force frees any allocated DMA channels, frees the &dma_client memory
273 void dma_async_client_unregister(struct dma_client *client)
275 struct dma_chan *chan;
281 list_for_each_entry_rcu(chan, &client->channels, client_node)
282 dma_client_chan_free(chan);
285 mutex_lock(&dma_list_mutex);
286 list_del(&client->global_node);
287 mutex_unlock(&dma_list_mutex);
290 dma_chans_rebalance();
292 EXPORT_SYMBOL(dma_async_client_unregister);
295 * dma_async_client_chan_request - request DMA channels
296 * @client: &dma_client
297 * @number: count of DMA channels requested
299 * Clients call dma_async_client_chan_request() to specify how many
300 * DMA channels they need, 0 to free all currently allocated.
301 * The resulting allocations/frees are indicated to the client via the
304 void dma_async_client_chan_request(struct dma_client *client,
307 client->chans_desired = number;
308 dma_chans_rebalance();
310 EXPORT_SYMBOL(dma_async_client_chan_request);
313 * dma_async_device_register - registers DMA devices found
314 * @device: &dma_device
316 int dma_async_device_register(struct dma_device *device)
320 struct dma_chan* chan;
325 init_completion(&device->done);
326 kref_init(&device->refcount);
327 device->dev_id = id++;
329 /* represent channels in sysfs. Probably want devs too */
330 list_for_each_entry(chan, &device->channels, device_node) {
331 chan->local = alloc_percpu(typeof(*chan->local));
332 if (chan->local == NULL)
335 chan->chan_id = chancnt++;
336 chan->class_dev.class = &dma_devclass;
337 chan->class_dev.dev = NULL;
338 snprintf(chan->class_dev.class_id, BUS_ID_SIZE, "dma%dchan%d",
339 device->dev_id, chan->chan_id);
341 kref_get(&device->refcount);
342 class_device_register(&chan->class_dev);
345 mutex_lock(&dma_list_mutex);
346 list_add_tail(&device->global_node, &dma_device_list);
347 mutex_unlock(&dma_list_mutex);
349 dma_chans_rebalance();
353 EXPORT_SYMBOL(dma_async_device_register);
356 * dma_async_device_cleanup - function called when all references are released
357 * @kref: kernel reference object
359 static void dma_async_device_cleanup(struct kref *kref)
361 struct dma_device *device;
363 device = container_of(kref, struct dma_device, refcount);
364 complete(&device->done);
368 * dma_async_device_unregister - unregisters DMA devices
369 * @device: &dma_device
371 void dma_async_device_unregister(struct dma_device *device)
373 struct dma_chan *chan;
376 mutex_lock(&dma_list_mutex);
377 list_del(&device->global_node);
378 mutex_unlock(&dma_list_mutex);
380 list_for_each_entry(chan, &device->channels, device_node) {
382 spin_lock_irqsave(&chan->client->lock, flags);
383 list_del(&chan->client_node);
384 chan->client->chan_count--;
385 spin_unlock_irqrestore(&chan->client->lock, flags);
386 chan->client->event_callback(chan->client,
388 DMA_RESOURCE_REMOVED);
389 dma_client_chan_free(chan);
391 class_device_unregister(&chan->class_dev);
393 dma_chans_rebalance();
395 kref_put(&device->refcount, dma_async_device_cleanup);
396 wait_for_completion(&device->done);
398 EXPORT_SYMBOL(dma_async_device_unregister);
400 static int __init dma_bus_init(void)
402 mutex_init(&dma_list_mutex);
403 return class_register(&dma_devclass);
405 subsys_initcall(dma_bus_init);