Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/kernel/dma.c: A DMA channel allocator. Inspired by linux/kernel/irq.c.
0004  *
0005  * Written by Hennus Bergman, 1992.
0006  *
0007  * 1994/12/26: Changes by Alex Nash to fix a minor bug in /proc/dma.
0008  *   In the previous version the reported device could end up being wrong,
0009  *   if a device requested a DMA channel that was already in use.
0010  *   [It also happened to remove the sizeof(char *) == sizeof(int)
0011  *   assumption introduced because of those /proc/dma patches. -- Hennus]
0012  */
0013 #include <linux/export.h>
0014 #include <linux/kernel.h>
0015 #include <linux/errno.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/string.h>
0018 #include <linux/seq_file.h>
0019 #include <linux/proc_fs.h>
0020 #include <linux/init.h>
0021 #include <asm/dma.h>
0022 
0023 
0024 
0025 /* A note on resource allocation:
0026  *
0027  * All drivers needing DMA channels, should allocate and release them
0028  * through the public routines `request_dma()' and `free_dma()'.
0029  *
0030  * In order to avoid problems, all processes should allocate resources in
0031  * the same sequence and release them in the reverse order.
0032  *
0033  * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA.
0034  * When releasing them, first release the DMA, then release the IRQ.
0035  * If you don't, you may cause allocation requests to fail unnecessarily.
0036  * This doesn't really matter now, but it will once we get real semaphores
0037  * in the kernel.
0038  */
0039 
0040 
0041 DEFINE_SPINLOCK(dma_spin_lock);
0042 
0043 /*
0044  *  If our port doesn't define this it has no PC like DMA
0045  */
0046 
0047 #ifdef MAX_DMA_CHANNELS
0048 
0049 
0050 /* Channel n is busy iff dma_chan_busy[n].lock != 0.
0051  * DMA0 used to be reserved for DRAM refresh, but apparently not any more...
0052  * DMA4 is reserved for cascading.
0053  */
0054 
0055 struct dma_chan {
0056     int  lock;
0057     const char *device_id;
0058 };
0059 
0060 static struct dma_chan dma_chan_busy[MAX_DMA_CHANNELS] = {
0061     [4] = { 1, "cascade" },
0062 };
0063 
0064 
0065 /**
0066  * request_dma - request and reserve a system DMA channel
0067  * @dmanr: DMA channel number
0068  * @device_id: reserving device ID string, used in /proc/dma
0069  */
0070 int request_dma(unsigned int dmanr, const char * device_id)
0071 {
0072     if (dmanr >= MAX_DMA_CHANNELS)
0073         return -EINVAL;
0074 
0075     if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)
0076         return -EBUSY;
0077 
0078     dma_chan_busy[dmanr].device_id = device_id;
0079 
0080     /* old flag was 0, now contains 1 to indicate busy */
0081     return 0;
0082 } /* request_dma */
0083 
0084 /**
0085  * free_dma - free a reserved system DMA channel
0086  * @dmanr: DMA channel number
0087  */
0088 void free_dma(unsigned int dmanr)
0089 {
0090     if (dmanr >= MAX_DMA_CHANNELS) {
0091         printk(KERN_WARNING "Trying to free DMA%d\n", dmanr);
0092         return;
0093     }
0094 
0095     if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {
0096         printk(KERN_WARNING "Trying to free free DMA%d\n", dmanr);
0097         return;
0098     }
0099 
0100 } /* free_dma */
0101 
0102 #else
0103 
0104 int request_dma(unsigned int dmanr, const char *device_id)
0105 {
0106     return -EINVAL;
0107 }
0108 
0109 void free_dma(unsigned int dmanr)
0110 {
0111 }
0112 
0113 #endif
0114 
0115 #ifdef CONFIG_PROC_FS
0116 
0117 #ifdef MAX_DMA_CHANNELS
0118 static int proc_dma_show(struct seq_file *m, void *v)
0119 {
0120     int i;
0121 
0122     for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
0123         if (dma_chan_busy[i].lock) {
0124             seq_printf(m, "%2d: %s\n", i,
0125                    dma_chan_busy[i].device_id);
0126         }
0127     }
0128     return 0;
0129 }
0130 #else
0131 static int proc_dma_show(struct seq_file *m, void *v)
0132 {
0133     seq_puts(m, "No DMA\n");
0134     return 0;
0135 }
0136 #endif /* MAX_DMA_CHANNELS */
0137 
0138 static int __init proc_dma_init(void)
0139 {
0140     proc_create_single("dma", 0, NULL, proc_dma_show);
0141     return 0;
0142 }
0143 
0144 __initcall(proc_dma_init);
0145 #endif
0146 
0147 EXPORT_SYMBOL(request_dma);
0148 EXPORT_SYMBOL(free_dma);
0149 EXPORT_SYMBOL(dma_spin_lock);