dma-api.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/spinlock.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/list.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mm.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <asm/dma.h>
  23. DEFINE_SPINLOCK(dma_spin_lock);
  24. static LIST_HEAD(registered_dmac_list);
  25. struct dma_info *get_dma_info(unsigned int chan)
  26. {
  27. struct dma_info *info;
  28. /*
  29. * Look for each DMAC's range to determine who the owner of
  30. * the channel is.
  31. */
  32. list_for_each_entry(info, &registered_dmac_list, list) {
  33. if ((chan < info->first_vchannel_nr) ||
  34. (chan >= info->first_vchannel_nr + info->nr_channels))
  35. continue;
  36. return info;
  37. }
  38. return NULL;
  39. }
  40. EXPORT_SYMBOL(get_dma_info);
  41. struct dma_info *get_dma_info_by_name(const char *dmac_name)
  42. {
  43. struct dma_info *info;
  44. list_for_each_entry(info, &registered_dmac_list, list) {
  45. if (dmac_name && (strcmp(dmac_name, info->name) != 0))
  46. continue;
  47. else
  48. return info;
  49. }
  50. return NULL;
  51. }
  52. EXPORT_SYMBOL(get_dma_info_by_name);
  53. static unsigned int get_nr_channels(void)
  54. {
  55. struct dma_info *info;
  56. unsigned int nr = 0;
  57. if (unlikely(list_empty(&registered_dmac_list)))
  58. return nr;
  59. list_for_each_entry(info, &registered_dmac_list, list)
  60. nr += info->nr_channels;
  61. return nr;
  62. }
  63. struct dma_channel *get_dma_channel(unsigned int chan)
  64. {
  65. struct dma_info *info = get_dma_info(chan);
  66. struct dma_channel *channel;
  67. int i;
  68. if (unlikely(!info))
  69. return ERR_PTR(-EINVAL);
  70. for (i = 0; i < info->nr_channels; i++) {
  71. channel = &info->channels[i];
  72. if (channel->vchan == chan)
  73. return channel;
  74. }
  75. return NULL;
  76. }
  77. EXPORT_SYMBOL(get_dma_channel);
  78. int get_dma_residue(unsigned int chan)
  79. {
  80. struct dma_info *info = get_dma_info(chan);
  81. struct dma_channel *channel = get_dma_channel(chan);
  82. if (info->ops->get_residue)
  83. return info->ops->get_residue(channel);
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(get_dma_residue);
  87. static int search_cap(const char **haystack, const char *needle)
  88. {
  89. const char **p;
  90. for (p = haystack; *p; p++)
  91. if (strcmp(*p, needle) == 0)
  92. return 1;
  93. return 0;
  94. }
  95. /**
  96. * request_dma_bycap - Allocate a DMA channel based on its capabilities
  97. * @dmac: List of DMA controllers to search
  98. * @caps: List of capabilities
  99. *
  100. * Search all channels of all DMA controllers to find a channel which
  101. * matches the requested capabilities. The result is the channel
  102. * number if a match is found, or %-ENODEV if no match is found.
  103. *
  104. * Note that not all DMA controllers export capabilities, in which
  105. * case they can never be allocated using this API, and so
  106. * request_dma() must be used specifying the channel number.
  107. */
  108. int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
  109. {
  110. unsigned int found = 0;
  111. struct dma_info *info;
  112. const char **p;
  113. int i;
  114. BUG_ON(!dmac || !caps);
  115. list_for_each_entry(info, &registered_dmac_list, list)
  116. if (strcmp(*dmac, info->name) == 0) {
  117. found = 1;
  118. break;
  119. }
  120. if (!found)
  121. return -ENODEV;
  122. for (i = 0; i < info->nr_channels; i++) {
  123. struct dma_channel *channel = &info->channels[i];
  124. if (unlikely(!channel->caps))
  125. continue;
  126. for (p = caps; *p; p++) {
  127. if (!search_cap(channel->caps, *p))
  128. break;
  129. if (request_dma(channel->chan, dev_id) == 0)
  130. return channel->chan;
  131. }
  132. }
  133. return -EINVAL;
  134. }
  135. EXPORT_SYMBOL(request_dma_bycap);
  136. int dmac_search_free_channel(const char *dev_id)
  137. {
  138. struct dma_channel *channel = { 0 };
  139. struct dma_info *info = get_dma_info(0);
  140. int i;
  141. for (i = 0; i < info->nr_channels; i++) {
  142. channel = &info->channels[i];
  143. if (unlikely(!channel))
  144. return -ENODEV;
  145. if (atomic_read(&channel->busy) == 0)
  146. break;
  147. }
  148. if (info->ops->request) {
  149. int result = info->ops->request(channel);
  150. if (result)
  151. return result;
  152. atomic_set(&channel->busy, 1);
  153. return channel->chan;
  154. }
  155. return -ENOSYS;
  156. }
  157. int request_dma(unsigned int chan, const char *dev_id)
  158. {
  159. struct dma_channel *channel = { 0 };
  160. struct dma_info *info = get_dma_info(chan);
  161. int result;
  162. channel = get_dma_channel(chan);
  163. if (atomic_xchg(&channel->busy, 1))
  164. return -EBUSY;
  165. strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
  166. if (info->ops->request) {
  167. result = info->ops->request(channel);
  168. if (result)
  169. atomic_set(&channel->busy, 0);
  170. return result;
  171. }
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(request_dma);
  175. void free_dma(unsigned int chan)
  176. {
  177. struct dma_info *info = get_dma_info(chan);
  178. struct dma_channel *channel = get_dma_channel(chan);
  179. if (info->ops->free)
  180. info->ops->free(channel);
  181. atomic_set(&channel->busy, 0);
  182. }
  183. EXPORT_SYMBOL(free_dma);
  184. void dma_wait_for_completion(unsigned int chan)
  185. {
  186. struct dma_info *info = get_dma_info(chan);
  187. struct dma_channel *channel = get_dma_channel(chan);
  188. if (channel->flags & DMA_TEI_CAPABLE) {
  189. wait_event(channel->wait_queue,
  190. (info->ops->get_residue(channel) == 0));
  191. return;
  192. }
  193. while (info->ops->get_residue(channel))
  194. cpu_relax();
  195. }
  196. EXPORT_SYMBOL(dma_wait_for_completion);
  197. int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
  198. {
  199. struct dma_info *info;
  200. unsigned int found = 0;
  201. int i;
  202. list_for_each_entry(info, &registered_dmac_list, list)
  203. if (strcmp(dmac, info->name) == 0) {
  204. found = 1;
  205. break;
  206. }
  207. if (unlikely(!found))
  208. return -ENODEV;
  209. for (i = 0; i < info->nr_channels; i++, caps++) {
  210. struct dma_channel *channel;
  211. if ((info->first_channel_nr + i) != caps->ch_num)
  212. return -EINVAL;
  213. channel = &info->channels[i];
  214. channel->caps = caps->caplist;
  215. }
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(register_chan_caps);
  219. void dma_configure_channel(unsigned int chan, unsigned long flags)
  220. {
  221. struct dma_info *info = get_dma_info(chan);
  222. struct dma_channel *channel = get_dma_channel(chan);
  223. if (info->ops->configure)
  224. info->ops->configure(channel, flags);
  225. }
  226. EXPORT_SYMBOL(dma_configure_channel);
  227. int dma_xfer(unsigned int chan, unsigned long from,
  228. unsigned long to, size_t size, unsigned int mode)
  229. {
  230. struct dma_info *info = get_dma_info(chan);
  231. struct dma_channel *channel = get_dma_channel(chan);
  232. channel->sar = from;
  233. channel->dar = to;
  234. channel->count = size;
  235. channel->mode = mode;
  236. return info->ops->xfer(channel);
  237. }
  238. EXPORT_SYMBOL(dma_xfer);
  239. int dma_extend(unsigned int chan, unsigned long op, void *param)
  240. {
  241. struct dma_info *info = get_dma_info(chan);
  242. struct dma_channel *channel = get_dma_channel(chan);
  243. if (info->ops->extend)
  244. return info->ops->extend(channel, op, param);
  245. return -ENOSYS;
  246. }
  247. EXPORT_SYMBOL(dma_extend);
  248. static int dma_proc_show(struct seq_file *m, void *v)
  249. {
  250. struct dma_info *info = v;
  251. if (list_empty(&registered_dmac_list))
  252. return 0;
  253. /*
  254. * Iterate over each registered DMAC
  255. */
  256. list_for_each_entry(info, &registered_dmac_list, list) {
  257. int i;
  258. /*
  259. * Iterate over each channel
  260. */
  261. for (i = 0; i < info->nr_channels; i++) {
  262. struct dma_channel *channel = info->channels + i;
  263. if (!(channel->flags & DMA_CONFIGURED))
  264. continue;
  265. seq_printf(m, "%2d: %14s %s\n", i,
  266. info->name, channel->dev_id);
  267. }
  268. }
  269. return 0;
  270. }
  271. static int dma_proc_open(struct inode *inode, struct file *file)
  272. {
  273. return single_open(file, dma_proc_show, NULL);
  274. }
  275. static const struct file_operations dma_proc_fops = {
  276. .open = dma_proc_open,
  277. .read = seq_read,
  278. .llseek = seq_lseek,
  279. .release = single_release,
  280. };
  281. int register_dmac(struct dma_info *info)
  282. {
  283. unsigned int total_channels, i;
  284. INIT_LIST_HEAD(&info->list);
  285. printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
  286. info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
  287. BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
  288. info->pdev = platform_device_register_simple(info->name, -1,
  289. NULL, 0);
  290. if (IS_ERR(info->pdev))
  291. return PTR_ERR(info->pdev);
  292. /*
  293. * Don't touch pre-configured channels
  294. */
  295. if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
  296. unsigned int size;
  297. size = sizeof(struct dma_channel) * info->nr_channels;
  298. info->channels = kzalloc(size, GFP_KERNEL);
  299. if (!info->channels)
  300. return -ENOMEM;
  301. }
  302. total_channels = get_nr_channels();
  303. info->first_vchannel_nr = total_channels;
  304. for (i = 0; i < info->nr_channels; i++) {
  305. struct dma_channel *chan = &info->channels[i];
  306. atomic_set(&chan->busy, 0);
  307. chan->chan = info->first_channel_nr + i;
  308. chan->vchan = info->first_channel_nr + i + total_channels;
  309. memcpy(chan->dev_id, "Unused", 7);
  310. if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
  311. chan->flags |= DMA_TEI_CAPABLE;
  312. init_waitqueue_head(&chan->wait_queue);
  313. dma_create_sysfs_files(chan, info);
  314. }
  315. list_add(&info->list, &registered_dmac_list);
  316. return 0;
  317. }
  318. EXPORT_SYMBOL(register_dmac);
  319. void unregister_dmac(struct dma_info *info)
  320. {
  321. unsigned int i;
  322. for (i = 0; i < info->nr_channels; i++)
  323. dma_remove_sysfs_files(info->channels + i, info);
  324. if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
  325. kfree(info->channels);
  326. list_del(&info->list);
  327. platform_device_unregister(info->pdev);
  328. }
  329. EXPORT_SYMBOL(unregister_dmac);
  330. static int __init dma_api_init(void)
  331. {
  332. printk(KERN_NOTICE "DMA: Registering DMA API.\n");
  333. return proc_create("dma", 0, NULL, &dma_proc_fops) ? 0 : -ENOMEM;
  334. }
  335. subsys_initcall(dma_api_init);
  336. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  337. MODULE_DESCRIPTION("DMA API for SuperH");
  338. MODULE_LICENSE("GPL");