1 // SPDX-License-Identifier: GPL-2.0 !! 1 /* $Id: dma.c,v 1.7 1994/12/28 03:35:33 root Exp root $ 2 /* << 3 * linux/kernel/dma.c: A DMA channel allocator 2 * linux/kernel/dma.c: A DMA channel allocator. Inspired by linux/kernel/irq.c. 4 * 3 * 5 * Written by Hennus Bergman, 1992. 4 * Written by Hennus Bergman, 1992. 6 * 5 * 7 * 1994/12/26: Changes by Alex Nash to fix a m 6 * 1994/12/26: Changes by Alex Nash to fix a minor bug in /proc/dma. 8 * In the previous version the reported devi 7 * In the previous version the reported device could end up being wrong, 9 * if a device requested a DMA channel that 8 * if a device requested a DMA channel that was already in use. 10 * [It also happened to remove the sizeof(ch 9 * [It also happened to remove the sizeof(char *) == sizeof(int) 11 * assumption introduced because of those /p 10 * assumption introduced because of those /proc/dma patches. -- Hennus] 12 */ 11 */ 13 #include <linux/export.h> !! 12 14 #include <linux/kernel.h> 13 #include <linux/kernel.h> 15 #include <linux/errno.h> 14 #include <linux/errno.h> 16 #include <linux/spinlock.h> 15 #include <linux/spinlock.h> 17 #include <linux/string.h> 16 #include <linux/string.h> 18 #include <linux/seq_file.h> << 19 #include <linux/proc_fs.h> << 20 #include <linux/init.h> << 21 #include <asm/dma.h> 17 #include <asm/dma.h> >> 18 #include <asm/system.h> 22 19 23 !! 20 24 21 25 /* A note on resource allocation: 22 /* A note on resource allocation: 26 * 23 * 27 * All drivers needing DMA channels, should al 24 * All drivers needing DMA channels, should allocate and release them 28 * through the public routines `request_dma()' 25 * through the public routines `request_dma()' and `free_dma()'. 29 * 26 * 30 * In order to avoid problems, all processes s 27 * In order to avoid problems, all processes should allocate resources in 31 * the same sequence and release them in the r 28 * the same sequence and release them in the reverse order. 32 * 29 * 33 * So, when allocating DMAs and IRQs, first al 30 * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA. 34 * When releasing them, first release the DMA, 31 * When releasing them, first release the DMA, then release the IRQ. 35 * If you don't, you may cause allocation requ 32 * If you don't, you may cause allocation requests to fail unnecessarily. 36 * This doesn't really matter now, but it will 33 * This doesn't really matter now, but it will once we get real semaphores 37 * in the kernel. 34 * in the kernel. 38 */ 35 */ 39 36 40 37 41 DEFINE_SPINLOCK(dma_spin_lock); !! 38 spinlock_t dma_spin_lock = SPIN_LOCK_UNLOCKED; 42 39 43 /* 40 /* 44 * If our port doesn't define this it has 41 * If our port doesn't define this it has no PC like DMA 45 */ 42 */ 46 43 47 #ifdef MAX_DMA_CHANNELS 44 #ifdef MAX_DMA_CHANNELS 48 45 49 46 50 /* Channel n is busy iff dma_chan_busy[n].lock 47 /* Channel n is busy iff dma_chan_busy[n].lock != 0. 51 * DMA0 used to be reserved for DRAM refresh, 48 * DMA0 used to be reserved for DRAM refresh, but apparently not any more... 52 * DMA4 is reserved for cascading. 49 * DMA4 is reserved for cascading. 53 */ 50 */ 54 51 55 struct dma_chan { 52 struct dma_chan { 56 int lock; 53 int lock; 57 const char *device_id; 54 const char *device_id; 58 }; 55 }; 59 56 60 static struct dma_chan dma_chan_busy[MAX_DMA_C 57 static struct dma_chan dma_chan_busy[MAX_DMA_CHANNELS] = { 61 [4] = { 1, "cascade" }, !! 58 { 0, 0 }, >> 59 { 0, 0 }, >> 60 { 0, 0 }, >> 61 { 0, 0 }, >> 62 { 1, "cascade" }, >> 63 { 0, 0 }, >> 64 { 0, 0 }, >> 65 { 0, 0 } 62 }; 66 }; 63 67 >> 68 int get_dma_list(char *buf) >> 69 { >> 70 int i, len = 0; >> 71 >> 72 for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) { >> 73 if (dma_chan_busy[i].lock) { >> 74 len += sprintf(buf+len, "%2d: %s\n", >> 75 i, >> 76 dma_chan_busy[i].device_id); >> 77 } >> 78 } >> 79 return len; >> 80 } /* get_dma_list */ >> 81 64 82 65 /** << 66 * request_dma - request and reserve a system << 67 * @dmanr: DMA channel number << 68 * @device_id: reserving device ID string, use << 69 */ << 70 int request_dma(unsigned int dmanr, const char 83 int request_dma(unsigned int dmanr, const char * device_id) 71 { 84 { 72 if (dmanr >= MAX_DMA_CHANNELS) 85 if (dmanr >= MAX_DMA_CHANNELS) 73 return -EINVAL; 86 return -EINVAL; 74 87 75 if (xchg(&dma_chan_busy[dmanr].lock, 1 88 if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0) 76 return -EBUSY; 89 return -EBUSY; 77 90 78 dma_chan_busy[dmanr].device_id = devic 91 dma_chan_busy[dmanr].device_id = device_id; 79 92 80 /* old flag was 0, now contains 1 to i 93 /* old flag was 0, now contains 1 to indicate busy */ 81 return 0; 94 return 0; 82 } /* request_dma */ 95 } /* request_dma */ 83 96 84 /** !! 97 85 * free_dma - free a reserved system DMA chann << 86 * @dmanr: DMA channel number << 87 */ << 88 void free_dma(unsigned int dmanr) 98 void free_dma(unsigned int dmanr) 89 { 99 { 90 if (dmanr >= MAX_DMA_CHANNELS) { 100 if (dmanr >= MAX_DMA_CHANNELS) { 91 printk(KERN_WARNING "Trying to !! 101 printk("Trying to free DMA%d\n", dmanr); 92 return; 102 return; 93 } 103 } 94 104 95 if (xchg(&dma_chan_busy[dmanr].lock, 0 105 if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) { 96 printk(KERN_WARNING "Trying to !! 106 printk("Trying to free free DMA%d\n", dmanr); 97 return; 107 return; 98 } !! 108 } 99 109 100 } /* free_dma */ 110 } /* free_dma */ 101 111 102 #else 112 #else 103 113 104 int request_dma(unsigned int dmanr, const char 114 int request_dma(unsigned int dmanr, const char *device_id) 105 { 115 { 106 return -EINVAL; 116 return -EINVAL; 107 } 117 } 108 118 109 void free_dma(unsigned int dmanr) 119 void free_dma(unsigned int dmanr) 110 { 120 { 111 } 121 } 112 122 113 #endif !! 123 int get_dma_list(char *buf) 114 !! 124 { 115 #ifdef CONFIG_PROC_FS !! 125 strcpy(buf, "No DMA\n"); 116 !! 126 return 7; 117 #ifdef MAX_DMA_CHANNELS << 118 static int proc_dma_show(struct seq_file *m, v << 119 { << 120 int i; << 121 << 122 for (i = 0 ; i < MAX_DMA_CHANNELS ; i+ << 123 if (dma_chan_busy[i].lock) { << 124 seq_printf(m, "%2d: %s << 125 dma_chan_bu << 126 } << 127 } << 128 return 0; << 129 } << 130 #else << 131 static int proc_dma_show(struct seq_file *m, v << 132 { << 133 seq_puts(m, "No DMA\n"); << 134 return 0; << 135 } 127 } 136 #endif /* MAX_DMA_CHANNELS */ << 137 << 138 static int __init proc_dma_init(void) << 139 { << 140 proc_create_single("dma", 0, NULL, pro << 141 return 0; << 142 } << 143 << 144 __initcall(proc_dma_init); << 145 #endif 128 #endif 146 << 147 EXPORT_SYMBOL(request_dma); << 148 EXPORT_SYMBOL(free_dma); << 149 EXPORT_SYMBOL(dma_spin_lock); << 150 129
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.