dma-api.c 9.1 KB

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