dma.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* $Id: dma.c,v 1.1.1.1 2007/06/12 07:27:11 eyryu Exp $
  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. /**
  53. * request_dma - request and reserve a system DMA channel
  54. * @dmanr: DMA channel number
  55. * @device_id: reserving device ID string, used in /proc/dma
  56. */
  57. int request_dma(unsigned int dmanr, const char * device_id)
  58. {
  59. if (dmanr >= MAX_DMA_CHANNELS)
  60. return -EINVAL;
  61. if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)
  62. return -EBUSY;
  63. dma_chan_busy[dmanr].device_id = device_id;
  64. /* old flag was 0, now contains 1 to indicate busy */
  65. return 0;
  66. } /* request_dma */
  67. /**
  68. * free_dma - free a reserved system DMA channel
  69. * @dmanr: DMA channel number
  70. */
  71. void free_dma(unsigned int dmanr)
  72. {
  73. if (dmanr >= MAX_DMA_CHANNELS) {
  74. printk(KERN_WARNING "Trying to free DMA%d\n", dmanr);
  75. return;
  76. }
  77. if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {
  78. printk(KERN_WARNING "Trying to free free DMA%d\n", dmanr);
  79. return;
  80. }
  81. } /* free_dma */
  82. #else
  83. int request_dma(unsigned int dmanr, const char *device_id)
  84. {
  85. return -EINVAL;
  86. }
  87. void free_dma(unsigned int dmanr)
  88. {
  89. }
  90. #endif
  91. #ifdef CONFIG_PROC_FS
  92. #ifdef MAX_DMA_CHANNELS
  93. static int proc_dma_show(struct seq_file *m, void *v)
  94. {
  95. int i;
  96. for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
  97. if (dma_chan_busy[i].lock) {
  98. seq_printf(m, "%2d: %s\n", i,
  99. dma_chan_busy[i].device_id);
  100. }
  101. }
  102. return 0;
  103. }
  104. #else
  105. static int proc_dma_show(struct seq_file *m, void *v)
  106. {
  107. seq_puts(m, "No DMA\n");
  108. return 0;
  109. }
  110. #endif /* MAX_DMA_CHANNELS */
  111. static int proc_dma_open(struct inode *inode, struct file *file)
  112. {
  113. return single_open(file, proc_dma_show, NULL);
  114. }
  115. static const struct file_operations proc_dma_operations = {
  116. .open = proc_dma_open,
  117. .read = seq_read,
  118. .llseek = seq_lseek,
  119. .release = single_release,
  120. };
  121. static int __init proc_dma_init(void)
  122. {
  123. struct proc_dir_entry *e;
  124. e = create_proc_entry("dma", 0, NULL);
  125. if (e)
  126. e->proc_fops = &proc_dma_operations;
  127. return 0;
  128. }
  129. __initcall(proc_dma_init);
  130. #endif
  131. EXPORT_SYMBOL(request_dma);
  132. EXPORT_SYMBOL(free_dma);
  133. EXPORT_SYMBOL(dma_spin_lock);