dma.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* $Id: dma.c,v 1.7 1994/12/28 03:35:33 root Exp root $
  2. * 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. */
  12. #include <linux/module.h>
  13. #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>
  21. #include <asm/system.h>
  22. /* A note on resource allocation:
  23. *
  24. * All drivers needing DMA channels, should allocate and release them
  25. * through the public routines `request_dma()' and `free_dma()'.
  26. *
  27. * In order to avoid problems, all processes should allocate resources in
  28. * the same sequence and release them in the reverse order.
  29. *
  30. * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA.
  31. * When releasing them, first release the DMA, then release the IRQ.
  32. * If you don't, you may cause allocation requests to fail unnecessarily.
  33. * This doesn't really matter now, but it will once we get real semaphores
  34. * in the kernel.
  35. */
  36. DEFINE_SPINLOCK(dma_spin_lock);
  37. /*
  38. * If our port doesn't define this it has no PC like DMA
  39. */
  40. #ifdef MAX_DMA_CHANNELS
  41. /* Channel n is busy iff dma_chan_busy[n].lock != 0.
  42. * DMA0 used to be reserved for DRAM refresh, but apparently not any more...
  43. * DMA4 is reserved for cascading.
  44. */
  45. struct dma_chan {
  46. int lock;
  47. const char *device_id;
  48. };
  49. static struct dma_chan dma_chan_busy[MAX_DMA_CHANNELS] = {
  50. [4] = { 1, "cascade" },
  51. };
  52. int request_dma(unsigned int dmanr, const char * device_id)
  53. {
  54. if (dmanr >= MAX_DMA_CHANNELS)
  55. return -EINVAL;
  56. if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)
  57. return -EBUSY;
  58. dma_chan_busy[dmanr].device_id = device_id;
  59. /* old flag was 0, now contains 1 to indicate busy */
  60. return 0;
  61. } /* request_dma */
  62. void free_dma(unsigned int dmanr)
  63. {
  64. if (dmanr >= MAX_DMA_CHANNELS) {
  65. printk(KERN_WARNING "Trying to free DMA%d\n", dmanr);
  66. return;
  67. }
  68. if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {
  69. printk(KERN_WARNING "Trying to free free DMA%d\n", dmanr);
  70. return;
  71. }
  72. } /* free_dma */
  73. #else
  74. int request_dma(unsigned int dmanr, const char *device_id)
  75. {
  76. return -EINVAL;
  77. }
  78. void free_dma(unsigned int dmanr)
  79. {
  80. }
  81. #endif
  82. #ifdef CONFIG_PROC_FS
  83. #ifdef MAX_DMA_CHANNELS
  84. static int proc_dma_show(struct seq_file *m, void *v)
  85. {
  86. int i;
  87. for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
  88. if (dma_chan_busy[i].lock) {
  89. seq_printf(m, "%2d: %s\n", i,
  90. dma_chan_busy[i].device_id);
  91. }
  92. }
  93. return 0;
  94. }
  95. #else
  96. static int proc_dma_show(struct seq_file *m, void *v)
  97. {
  98. seq_puts(m, "No DMA\n");
  99. return 0;
  100. }
  101. #endif /* MAX_DMA_CHANNELS */
  102. static int proc_dma_open(struct inode *inode, struct file *file)
  103. {
  104. return single_open(file, proc_dma_show, NULL);
  105. }
  106. static struct file_operations proc_dma_operations = {
  107. .open = proc_dma_open,
  108. .read = seq_read,
  109. .llseek = seq_lseek,
  110. .release = single_release,
  111. };
  112. static int __init proc_dma_init(void)
  113. {
  114. struct proc_dir_entry *e;
  115. e = create_proc_entry("dma", 0, NULL);
  116. if (e)
  117. e->proc_fops = &proc_dma_operations;
  118. return 0;
  119. }
  120. __initcall(proc_dma_init);
  121. #endif
  122. EXPORT_SYMBOL(request_dma);
  123. EXPORT_SYMBOL(free_dma);
  124. EXPORT_SYMBOL(dma_spin_lock);