dma.c 3.6 KB

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