1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sh/drivers/dma/dma-api.c
5 * SuperH-specific DMA management API
7 * Copyright (C) 2003, 2004, 2005 Paul Mundt
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <linux/list.h>
15 #include <linux/platform_device.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
21 DEFINE_SPINLOCK(dma_spin_lock
);
22 static LIST_HEAD(registered_dmac_list
);
24 struct dma_info
*get_dma_info(unsigned int chan
)
26 struct dma_info
*info
;
29 * Look for each DMAC's range to determine who the owner of
32 list_for_each_entry(info
, ®istered_dmac_list
, list
) {
33 if ((chan
< info
->first_vchannel_nr
) ||
34 (chan
>= info
->first_vchannel_nr
+ info
->nr_channels
))
42 EXPORT_SYMBOL(get_dma_info
);
44 struct dma_info
*get_dma_info_by_name(const char *dmac_name
)
46 struct dma_info
*info
;
48 list_for_each_entry(info
, ®istered_dmac_list
, list
) {
49 if (dmac_name
&& (strcmp(dmac_name
, info
->name
) != 0))
57 EXPORT_SYMBOL(get_dma_info_by_name
);
59 static unsigned int get_nr_channels(void)
61 struct dma_info
*info
;
64 if (unlikely(list_empty(®istered_dmac_list
)))
67 list_for_each_entry(info
, ®istered_dmac_list
, list
)
68 nr
+= info
->nr_channels
;
73 struct dma_channel
*get_dma_channel(unsigned int chan
)
75 struct dma_info
*info
= get_dma_info(chan
);
76 struct dma_channel
*channel
;
80 return ERR_PTR(-EINVAL
);
82 for (i
= 0; i
< info
->nr_channels
; i
++) {
83 channel
= &info
->channels
[i
];
84 if (channel
->vchan
== chan
)
90 EXPORT_SYMBOL(get_dma_channel
);
92 int get_dma_residue(unsigned int chan
)
94 struct dma_info
*info
= get_dma_info(chan
);
95 struct dma_channel
*channel
= get_dma_channel(chan
);
97 if (info
->ops
->get_residue
)
98 return info
->ops
->get_residue(channel
);
102 EXPORT_SYMBOL(get_dma_residue
);
104 static int search_cap(const char **haystack
, const char *needle
)
108 for (p
= haystack
; *p
; p
++)
109 if (strcmp(*p
, needle
) == 0)
116 * request_dma_bycap - Allocate a DMA channel based on its capabilities
117 * @dmac: List of DMA controllers to search
118 * @caps: List of capabilities
120 * Search all channels of all DMA controllers to find a channel which
121 * matches the requested capabilities. The result is the channel
122 * number if a match is found, or %-ENODEV if no match is found.
124 * Note that not all DMA controllers export capabilities, in which
125 * case they can never be allocated using this API, and so
126 * request_dma() must be used specifying the channel number.
128 int request_dma_bycap(const char **dmac
, const char **caps
, const char *dev_id
)
130 unsigned int found
= 0;
131 struct dma_info
*info
;
135 BUG_ON(!dmac
|| !caps
);
137 list_for_each_entry(info
, ®istered_dmac_list
, list
)
138 if (strcmp(*dmac
, info
->name
) == 0) {
146 for (i
= 0; i
< info
->nr_channels
; i
++) {
147 struct dma_channel
*channel
= &info
->channels
[i
];
149 if (unlikely(!channel
->caps
))
152 for (p
= caps
; *p
; p
++) {
153 if (!search_cap(channel
->caps
, *p
))
155 if (request_dma(channel
->chan
, dev_id
) == 0)
156 return channel
->chan
;
162 EXPORT_SYMBOL(request_dma_bycap
);
164 int dmac_search_free_channel(const char *dev_id
)
166 struct dma_channel
*channel
= { 0 };
167 struct dma_info
*info
= get_dma_info(0);
170 for (i
= 0; i
< info
->nr_channels
; i
++) {
171 channel
= &info
->channels
[i
];
172 if (unlikely(!channel
))
175 if (atomic_read(&channel
->busy
) == 0)
179 if (info
->ops
->request
) {
180 int result
= info
->ops
->request(channel
);
184 atomic_set(&channel
->busy
, 1);
185 return channel
->chan
;
191 int request_dma(unsigned int chan
, const char *dev_id
)
193 struct dma_channel
*channel
= { 0 };
194 struct dma_info
*info
= get_dma_info(chan
);
197 channel
= get_dma_channel(chan
);
198 if (atomic_xchg(&channel
->busy
, 1))
201 strlcpy(channel
->dev_id
, dev_id
, sizeof(channel
->dev_id
));
203 if (info
->ops
->request
) {
204 result
= info
->ops
->request(channel
);
206 atomic_set(&channel
->busy
, 0);
213 EXPORT_SYMBOL(request_dma
);
215 void free_dma(unsigned int chan
)
217 struct dma_info
*info
= get_dma_info(chan
);
218 struct dma_channel
*channel
= get_dma_channel(chan
);
221 info
->ops
->free(channel
);
223 atomic_set(&channel
->busy
, 0);
225 EXPORT_SYMBOL(free_dma
);
227 void dma_wait_for_completion(unsigned int chan
)
229 struct dma_info
*info
= get_dma_info(chan
);
230 struct dma_channel
*channel
= get_dma_channel(chan
);
232 if (channel
->flags
& DMA_TEI_CAPABLE
) {
233 wait_event(channel
->wait_queue
,
234 (info
->ops
->get_residue(channel
) == 0));
238 while (info
->ops
->get_residue(channel
))
241 EXPORT_SYMBOL(dma_wait_for_completion
);
243 int register_chan_caps(const char *dmac
, struct dma_chan_caps
*caps
)
245 struct dma_info
*info
;
246 unsigned int found
= 0;
249 list_for_each_entry(info
, ®istered_dmac_list
, list
)
250 if (strcmp(dmac
, info
->name
) == 0) {
255 if (unlikely(!found
))
258 for (i
= 0; i
< info
->nr_channels
; i
++, caps
++) {
259 struct dma_channel
*channel
;
261 if ((info
->first_channel_nr
+ i
) != caps
->ch_num
)
264 channel
= &info
->channels
[i
];
265 channel
->caps
= caps
->caplist
;
270 EXPORT_SYMBOL(register_chan_caps
);
272 void dma_configure_channel(unsigned int chan
, unsigned long flags
)
274 struct dma_info
*info
= get_dma_info(chan
);
275 struct dma_channel
*channel
= get_dma_channel(chan
);
277 if (info
->ops
->configure
)
278 info
->ops
->configure(channel
, flags
);
280 EXPORT_SYMBOL(dma_configure_channel
);
282 int dma_xfer(unsigned int chan
, unsigned long from
,
283 unsigned long to
, size_t size
, unsigned int mode
)
285 struct dma_info
*info
= get_dma_info(chan
);
286 struct dma_channel
*channel
= get_dma_channel(chan
);
290 channel
->count
= size
;
291 channel
->mode
= mode
;
293 return info
->ops
->xfer(channel
);
295 EXPORT_SYMBOL(dma_xfer
);
297 int dma_extend(unsigned int chan
, unsigned long op
, void *param
)
299 struct dma_info
*info
= get_dma_info(chan
);
300 struct dma_channel
*channel
= get_dma_channel(chan
);
302 if (info
->ops
->extend
)
303 return info
->ops
->extend(channel
, op
, param
);
307 EXPORT_SYMBOL(dma_extend
);
309 static int dma_proc_show(struct seq_file
*m
, void *v
)
311 struct dma_info
*info
= v
;
313 if (list_empty(®istered_dmac_list
))
317 * Iterate over each registered DMAC
319 list_for_each_entry(info
, ®istered_dmac_list
, list
) {
323 * Iterate over each channel
325 for (i
= 0; i
< info
->nr_channels
; i
++) {
326 struct dma_channel
*channel
= info
->channels
+ i
;
328 if (!(channel
->flags
& DMA_CONFIGURED
))
331 seq_printf(m
, "%2d: %14s %s\n", i
,
332 info
->name
, channel
->dev_id
);
339 int register_dmac(struct dma_info
*info
)
341 unsigned int total_channels
, i
;
343 INIT_LIST_HEAD(&info
->list
);
345 printk(KERN_INFO
"DMA: Registering %s handler (%d channel%s).\n",
346 info
->name
, info
->nr_channels
, info
->nr_channels
> 1 ? "s" : "");
348 BUG_ON((info
->flags
& DMAC_CHANNELS_CONFIGURED
) && !info
->channels
);
350 info
->pdev
= platform_device_register_simple(info
->name
, -1,
352 if (IS_ERR(info
->pdev
))
353 return PTR_ERR(info
->pdev
);
356 * Don't touch pre-configured channels
358 if (!(info
->flags
& DMAC_CHANNELS_CONFIGURED
)) {
361 size
= sizeof(struct dma_channel
) * info
->nr_channels
;
363 info
->channels
= kzalloc(size
, GFP_KERNEL
);
368 total_channels
= get_nr_channels();
369 info
->first_vchannel_nr
= total_channels
;
370 for (i
= 0; i
< info
->nr_channels
; i
++) {
371 struct dma_channel
*chan
= &info
->channels
[i
];
373 atomic_set(&chan
->busy
, 0);
375 chan
->chan
= info
->first_channel_nr
+ i
;
376 chan
->vchan
= info
->first_channel_nr
+ i
+ total_channels
;
378 memcpy(chan
->dev_id
, "Unused", 7);
380 if (info
->flags
& DMAC_CHANNELS_TEI_CAPABLE
)
381 chan
->flags
|= DMA_TEI_CAPABLE
;
383 init_waitqueue_head(&chan
->wait_queue
);
384 dma_create_sysfs_files(chan
, info
);
387 list_add(&info
->list
, ®istered_dmac_list
);
391 EXPORT_SYMBOL(register_dmac
);
393 void unregister_dmac(struct dma_info
*info
)
397 for (i
= 0; i
< info
->nr_channels
; i
++)
398 dma_remove_sysfs_files(info
->channels
+ i
, info
);
400 if (!(info
->flags
& DMAC_CHANNELS_CONFIGURED
))
401 kfree(info
->channels
);
403 list_del(&info
->list
);
404 platform_device_unregister(info
->pdev
);
406 EXPORT_SYMBOL(unregister_dmac
);
408 static int __init
dma_api_init(void)
410 printk(KERN_NOTICE
"DMA: Registering DMA API.\n");
411 return proc_create_single("dma", 0, NULL
, dma_proc_show
) ? 0 : -ENOMEM
;
413 subsys_initcall(dma_api_init
);
415 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
416 MODULE_DESCRIPTION("DMA API for SuperH");
417 MODULE_LICENSE("GPL v2");