dma-api.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * arch/sh/drivers/dma/dma-api.c
  3. *
  4. * SuperH-specific DMA management API
  5. *
  6. * Copyright (C) 2003, 2004, 2005 Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/list.h>
  18. #include <linux/platform_device.h>
  19. #include <asm/dma.h>
  20. DEFINE_SPINLOCK(dma_spin_lock);
  21. static LIST_HEAD(registered_dmac_list);
  22. /*
  23. * A brief note about the reasons for this API as it stands.
  24. *
  25. * For starters, the old ISA DMA API didn't work for us for a number of
  26. * reasons, for one, the vast majority of channels on the SH DMAC are
  27. * dual-address mode only, and both the new and the old DMA APIs are after the
  28. * concept of managing a DMA buffer, which doesn't overly fit this model very
  29. * well. In addition to which, the new API is largely geared at IOMMUs and
  30. * GARTs, and doesn't even support the channel notion very well.
  31. *
  32. * The other thing that's a marginal issue, is the sheer number of random DMA
  33. * engines that are present (ie, in boards like the Dreamcast), some of which
  34. * cascade off of the SH DMAC, and others do not. As such, there was a real
  35. * need for a scalable subsystem that could deal with both single and
  36. * dual-address mode usage, in addition to interoperating with cascaded DMACs.
  37. *
  38. * There really isn't any reason why this needs to be SH specific, though I'm
  39. * not aware of too many other processors (with the exception of some MIPS)
  40. * that have the same concept of a dual address mode, or any real desire to
  41. * actually make use of the DMAC even if such a subsystem were exposed
  42. * elsewhere.
  43. *
  44. * The idea for this was derived from the ARM port, which acted as an excellent
  45. * reference when trying to address these issues.
  46. *
  47. * It should also be noted that the decision to add Yet Another DMA API(tm) to
  48. * the kernel wasn't made easily, and was only decided upon after conferring
  49. * with jejb with regards to the state of the old and new APIs as they applied
  50. * to these circumstances. Philip Blundell was also a great help in figuring
  51. * out some single-address mode DMA semantics that were otherwise rather
  52. * confusing.
  53. */
  54. struct dma_info *get_dma_info(unsigned int chan)
  55. {
  56. struct dma_info *info;
  57. unsigned int total = 0;
  58. /*
  59. * Look for each DMAC's range to determine who the owner of
  60. * the channel is.
  61. */
  62. list_for_each_entry(info, &registered_dmac_list, list) {
  63. total += info->nr_channels;
  64. if (chan > total)
  65. continue;
  66. return info;
  67. }
  68. return NULL;
  69. }
  70. static unsigned int get_nr_channels(void)
  71. {
  72. struct dma_info *info;
  73. unsigned int nr = 0;
  74. if (unlikely(list_empty(&registered_dmac_list)))
  75. return nr;
  76. list_for_each_entry(info, &registered_dmac_list, list)
  77. nr += info->nr_channels;
  78. return nr;
  79. }
  80. struct dma_channel *get_dma_channel(unsigned int chan)
  81. {
  82. struct dma_info *info = get_dma_info(chan);
  83. if (!info)
  84. return ERR_PTR(-EINVAL);
  85. return info->channels + chan;
  86. }
  87. int get_dma_residue(unsigned int chan)
  88. {
  89. struct dma_info *info = get_dma_info(chan);
  90. struct dma_channel *channel = &info->channels[chan];
  91. if (info->ops->get_residue)
  92. return info->ops->get_residue(channel);
  93. return 0;
  94. }
  95. int request_dma(unsigned int chan, const char *dev_id)
  96. {
  97. struct dma_info *info = get_dma_info(chan);
  98. struct dma_channel *channel = &info->channels[chan];
  99. down(&channel->sem);
  100. if (!info->ops || chan >= MAX_DMA_CHANNELS) {
  101. up(&channel->sem);
  102. return -EINVAL;
  103. }
  104. atomic_set(&channel->busy, 1);
  105. strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
  106. up(&channel->sem);
  107. if (info->ops->request)
  108. return info->ops->request(channel);
  109. return 0;
  110. }
  111. void free_dma(unsigned int chan)
  112. {
  113. struct dma_info *info = get_dma_info(chan);
  114. struct dma_channel *channel = &info->channels[chan];
  115. if (info->ops->free)
  116. info->ops->free(channel);
  117. atomic_set(&channel->busy, 0);
  118. }
  119. void dma_wait_for_completion(unsigned int chan)
  120. {
  121. struct dma_info *info = get_dma_info(chan);
  122. struct dma_channel *channel = &info->channels[chan];
  123. if (channel->flags & DMA_TEI_CAPABLE) {
  124. wait_event(channel->wait_queue,
  125. (info->ops->get_residue(channel) == 0));
  126. return;
  127. }
  128. while (info->ops->get_residue(channel))
  129. cpu_relax();
  130. }
  131. void dma_configure_channel(unsigned int chan, unsigned long flags)
  132. {
  133. struct dma_info *info = get_dma_info(chan);
  134. struct dma_channel *channel = &info->channels[chan];
  135. if (info->ops->configure)
  136. info->ops->configure(channel, flags);
  137. }
  138. int dma_xfer(unsigned int chan, unsigned long from,
  139. unsigned long to, size_t size, unsigned int mode)
  140. {
  141. struct dma_info *info = get_dma_info(chan);
  142. struct dma_channel *channel = &info->channels[chan];
  143. channel->sar = from;
  144. channel->dar = to;
  145. channel->count = size;
  146. channel->mode = mode;
  147. return info->ops->xfer(channel);
  148. }
  149. #ifdef CONFIG_PROC_FS
  150. static int dma_read_proc(char *buf, char **start, off_t off,
  151. int len, int *eof, void *data)
  152. {
  153. struct dma_info *info;
  154. char *p = buf;
  155. if (list_empty(&registered_dmac_list))
  156. return 0;
  157. /*
  158. * Iterate over each registered DMAC
  159. */
  160. list_for_each_entry(info, &registered_dmac_list, list) {
  161. int i;
  162. /*
  163. * Iterate over each channel
  164. */
  165. for (i = 0; i < info->nr_channels; i++) {
  166. struct dma_channel *channel = info->channels + i;
  167. if (!(channel->flags & DMA_CONFIGURED))
  168. continue;
  169. p += sprintf(p, "%2d: %14s %s\n", i,
  170. info->name, channel->dev_id);
  171. }
  172. }
  173. return p - buf;
  174. }
  175. #endif
  176. int register_dmac(struct dma_info *info)
  177. {
  178. unsigned int total_channels, i;
  179. INIT_LIST_HEAD(&info->list);
  180. printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
  181. info->name, info->nr_channels,
  182. info->nr_channels > 1 ? "s" : "");
  183. BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
  184. info->pdev = platform_device_register_simple((char *)info->name, -1,
  185. NULL, 0);
  186. if (IS_ERR(info->pdev))
  187. return PTR_ERR(info->pdev);
  188. /*
  189. * Don't touch pre-configured channels
  190. */
  191. if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
  192. unsigned int size;
  193. size = sizeof(struct dma_channel) * info->nr_channels;
  194. info->channels = kmalloc(size, GFP_KERNEL);
  195. if (!info->channels)
  196. return -ENOMEM;
  197. memset(info->channels, 0, size);
  198. }
  199. total_channels = get_nr_channels();
  200. for (i = 0; i < info->nr_channels; i++) {
  201. struct dma_channel *chan = info->channels + i;
  202. chan->chan = i;
  203. chan->vchan = i + total_channels;
  204. memcpy(chan->dev_id, "Unused", 7);
  205. if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
  206. chan->flags |= DMA_TEI_CAPABLE;
  207. init_MUTEX(&chan->sem);
  208. init_waitqueue_head(&chan->wait_queue);
  209. dma_create_sysfs_files(chan, info);
  210. }
  211. list_add(&info->list, &registered_dmac_list);
  212. return 0;
  213. }
  214. void unregister_dmac(struct dma_info *info)
  215. {
  216. unsigned int i;
  217. for (i = 0; i < info->nr_channels; i++)
  218. dma_remove_sysfs_files(info->channels + i, info);
  219. if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
  220. kfree(info->channels);
  221. list_del(&info->list);
  222. platform_device_unregister(info->pdev);
  223. }
  224. static int __init dma_api_init(void)
  225. {
  226. printk("DMA: Registering DMA API.\n");
  227. #ifdef CONFIG_PROC_FS
  228. create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
  229. #endif
  230. return 0;
  231. }
  232. subsys_initcall(dma_api_init);
  233. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  234. MODULE_DESCRIPTION("DMA API for SuperH");
  235. MODULE_LICENSE("GPL");
  236. EXPORT_SYMBOL(request_dma);
  237. EXPORT_SYMBOL(free_dma);
  238. EXPORT_SYMBOL(register_dmac);
  239. EXPORT_SYMBOL(get_dma_residue);
  240. EXPORT_SYMBOL(get_dma_info);
  241. EXPORT_SYMBOL(get_dma_channel);
  242. EXPORT_SYMBOL(dma_xfer);
  243. EXPORT_SYMBOL(dma_wait_for_completion);
  244. EXPORT_SYMBOL(dma_configure_channel);