blob: 6c6262f86c17785b53928fa1787467040c8d3938 [file] [log] [blame]
Adrian Bunkd9f32162008-10-15 22:01:26 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * linux/kernel/dma.c: A DMA channel allocator. Inspired by linux/kernel/irq.c.
3 *
4 * Written by Hennus Bergman, 1992.
5 *
6 * 1994/12/26: Changes by Alex Nash to fix a minor bug in /proc/dma.
7 * In the previous version the reported device could end up being wrong,
8 * if a device requested a DMA channel that was already in use.
9 * [It also happened to remove the sizeof(char *) == sizeof(int)
10 * assumption introduced because of those /proc/dma patches. -- Hennus]
11 */
Paul Gortmaker9984de12011-05-23 14:51:41 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/spinlock.h>
16#include <linux/string.h>
17#include <linux/seq_file.h>
18#include <linux/proc_fs.h>
19#include <linux/init.h>
20#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Daniel Walker902749c2007-10-18 03:06:06 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/* A note on resource allocation:
25 *
26 * All drivers needing DMA channels, should allocate and release them
27 * through the public routines `request_dma()' and `free_dma()'.
28 *
29 * In order to avoid problems, all processes should allocate resources in
30 * the same sequence and release them in the reverse order.
31 *
32 * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA.
33 * When releasing them, first release the DMA, then release the IRQ.
34 * If you don't, you may cause allocation requests to fail unnecessarily.
35 * This doesn't really matter now, but it will once we get real semaphores
36 * in the kernel.
37 */
38
39
40DEFINE_SPINLOCK(dma_spin_lock);
41
42/*
43 * If our port doesn't define this it has no PC like DMA
44 */
45
46#ifdef MAX_DMA_CHANNELS
47
48
49/* Channel n is busy iff dma_chan_busy[n].lock != 0.
50 * DMA0 used to be reserved for DRAM refresh, but apparently not any more...
51 * DMA4 is reserved for cascading.
52 */
53
54struct dma_chan {
55 int lock;
56 const char *device_id;
57};
58
59static struct dma_chan dma_chan_busy[MAX_DMA_CHANNELS] = {
60 [4] = { 1, "cascade" },
61};
62
63
Randy Dunlapeed34d02006-10-03 01:13:50 -070064/**
65 * request_dma - request and reserve a system DMA channel
66 * @dmanr: DMA channel number
67 * @device_id: reserving device ID string, used in /proc/dma
68 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069int request_dma(unsigned int dmanr, const char * device_id)
70{
71 if (dmanr >= MAX_DMA_CHANNELS)
72 return -EINVAL;
73
74 if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)
75 return -EBUSY;
76
77 dma_chan_busy[dmanr].device_id = device_id;
78
79 /* old flag was 0, now contains 1 to indicate busy */
80 return 0;
81} /* request_dma */
82
Randy Dunlapeed34d02006-10-03 01:13:50 -070083/**
84 * free_dma - free a reserved system DMA channel
85 * @dmanr: DMA channel number
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087void free_dma(unsigned int dmanr)
88{
89 if (dmanr >= MAX_DMA_CHANNELS) {
90 printk(KERN_WARNING "Trying to free DMA%d\n", dmanr);
91 return;
92 }
93
94 if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {
95 printk(KERN_WARNING "Trying to free free DMA%d\n", dmanr);
96 return;
Daniel Walker902749c2007-10-18 03:06:06 -070097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99} /* free_dma */
100
101#else
102
103int request_dma(unsigned int dmanr, const char *device_id)
104{
105 return -EINVAL;
106}
107
108void free_dma(unsigned int dmanr)
109{
110}
111
112#endif
113
114#ifdef CONFIG_PROC_FS
115
116#ifdef MAX_DMA_CHANNELS
117static int proc_dma_show(struct seq_file *m, void *v)
118{
119 int i;
120
121 for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
122 if (dma_chan_busy[i].lock) {
Daniel Walker902749c2007-10-18 03:06:06 -0700123 seq_printf(m, "%2d: %s\n", i,
124 dma_chan_busy[i].device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
127 return 0;
128}
129#else
130static int proc_dma_show(struct seq_file *m, void *v)
131{
132 seq_puts(m, "No DMA\n");
133 return 0;
134}
135#endif /* MAX_DMA_CHANNELS */
136
137static int proc_dma_open(struct inode *inode, struct file *file)
138{
139 return single_open(file, proc_dma_show, NULL);
140}
141
Helge Deller15ad7cd2006-12-06 20:40:36 -0800142static const struct file_operations proc_dma_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .open = proc_dma_open,
144 .read = seq_read,
145 .llseek = seq_lseek,
146 .release = single_release,
147};
148
149static int __init proc_dma_init(void)
150{
Denis V. Lunevc33fff02008-04-29 01:02:31 -0700151 proc_create("dma", 0, NULL, &proc_dma_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return 0;
153}
154
155__initcall(proc_dma_init);
156#endif
157
158EXPORT_SYMBOL(request_dma);
159EXPORT_SYMBOL(free_dma);
160EXPORT_SYMBOL(dma_spin_lock);