dma-api.c 9.1 KB

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