dmaengine.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59
  16. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called COPYING.
  20. */
  21. /*
  22. * This code implements the DMA subsystem. It provides a HW-neutral interface
  23. * for other kernel code to use asynchronous memory copy capabilities,
  24. * if present, and allows different HW DMA drivers to register as providing
  25. * this capability.
  26. *
  27. * Due to the fact we are accelerating what is already a relatively fast
  28. * operation, the code goes to great lengths to avoid additional overhead,
  29. * such as locking.
  30. *
  31. * LOCKING:
  32. *
  33. * The subsystem keeps two global lists, dma_device_list and dma_client_list.
  34. * Both of these are protected by a mutex, dma_list_mutex.
  35. *
  36. * Each device has a channels list, which runs unlocked but is never modified
  37. * once the device is registered, it's just setup by the driver.
  38. *
  39. * Each client has a channels list, it's only modified under the client->lock
  40. * and in an RCU callback, so it's safe to read under rcu_read_lock().
  41. *
  42. * Each device has a kref, which is initialized to 1 when the device is
  43. * registered. A kref_put is done for each class_device registered. When the
  44. * class_device is released, the coresponding kref_put is done in the release
  45. * method. Every time one of the device's channels is allocated to a client,
  46. * a kref_get occurs. When the channel is freed, the coresponding kref_put
  47. * happens. The device's release function does a completion, so
  48. * unregister_device does a remove event, class_device_unregister, a kref_put
  49. * for the first reference, then waits on the completion for all other
  50. * references to finish.
  51. *
  52. * Each channel has an open-coded implementation of Rusty Russell's "bigref,"
  53. * with a kref and a per_cpu local_t. A single reference is set when on an
  54. * ADDED event, and removed with a REMOVE event. Net DMA client takes an
  55. * extra reference per outstanding transaction. The relase function does a
  56. * kref_put on the device. -ChrisL
  57. */
  58. #include <linux/init.h>
  59. #include <linux/module.h>
  60. #include <linux/device.h>
  61. #include <linux/dmaengine.h>
  62. #include <linux/hardirq.h>
  63. #include <linux/spinlock.h>
  64. #include <linux/percpu.h>
  65. #include <linux/rcupdate.h>
  66. #include <linux/mutex.h>
  67. static DEFINE_MUTEX(dma_list_mutex);
  68. static LIST_HEAD(dma_device_list);
  69. static LIST_HEAD(dma_client_list);
  70. /* --- sysfs implementation --- */
  71. static ssize_t show_memcpy_count(struct class_device *cd, char *buf)
  72. {
  73. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  74. unsigned long count = 0;
  75. int i;
  76. for_each_possible_cpu(i)
  77. count += per_cpu_ptr(chan->local, i)->memcpy_count;
  78. return sprintf(buf, "%lu\n", count);
  79. }
  80. static ssize_t show_bytes_transferred(struct class_device *cd, char *buf)
  81. {
  82. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  83. unsigned long count = 0;
  84. int i;
  85. for_each_possible_cpu(i)
  86. count += per_cpu_ptr(chan->local, i)->bytes_transferred;
  87. return sprintf(buf, "%lu\n", count);
  88. }
  89. static ssize_t show_in_use(struct class_device *cd, char *buf)
  90. {
  91. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  92. return sprintf(buf, "%d\n", (chan->client ? 1 : 0));
  93. }
  94. static struct class_device_attribute dma_class_attrs[] = {
  95. __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
  96. __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
  97. __ATTR(in_use, S_IRUGO, show_in_use, NULL),
  98. __ATTR_NULL
  99. };
  100. static void dma_async_device_cleanup(struct kref *kref);
  101. static void dma_class_dev_release(struct class_device *cd)
  102. {
  103. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  104. kref_put(&chan->device->refcount, dma_async_device_cleanup);
  105. }
  106. static struct class dma_devclass = {
  107. .name = "dma",
  108. .class_dev_attrs = dma_class_attrs,
  109. .release = dma_class_dev_release,
  110. };
  111. /* --- client and device registration --- */
  112. /**
  113. * dma_client_chan_alloc - try to allocate a channel to a client
  114. * @client: &dma_client
  115. *
  116. * Called with dma_list_mutex held.
  117. */
  118. static struct dma_chan *dma_client_chan_alloc(struct dma_client *client)
  119. {
  120. struct dma_device *device;
  121. struct dma_chan *chan;
  122. unsigned long flags;
  123. int desc; /* allocated descriptor count */
  124. /* Find a channel, any DMA engine will do */
  125. list_for_each_entry(device, &dma_device_list, global_node) {
  126. list_for_each_entry(chan, &device->channels, device_node) {
  127. if (chan->client)
  128. continue;
  129. desc = chan->device->device_alloc_chan_resources(chan);
  130. if (desc >= 0) {
  131. kref_get(&device->refcount);
  132. kref_init(&chan->refcount);
  133. chan->slow_ref = 0;
  134. INIT_RCU_HEAD(&chan->rcu);
  135. chan->client = client;
  136. spin_lock_irqsave(&client->lock, flags);
  137. list_add_tail_rcu(&chan->client_node,
  138. &client->channels);
  139. spin_unlock_irqrestore(&client->lock, flags);
  140. return chan;
  141. }
  142. }
  143. }
  144. return NULL;
  145. }
  146. /**
  147. * dma_chan_cleanup - release a DMA channel's resources
  148. * @kref: kernel reference structure that contains the DMA channel device
  149. */
  150. void dma_chan_cleanup(struct kref *kref)
  151. {
  152. struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
  153. chan->device->device_free_chan_resources(chan);
  154. chan->client = NULL;
  155. kref_put(&chan->device->refcount, dma_async_device_cleanup);
  156. }
  157. static void dma_chan_free_rcu(struct rcu_head *rcu)
  158. {
  159. struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
  160. int bias = 0x7FFFFFFF;
  161. int i;
  162. for_each_possible_cpu(i)
  163. bias -= local_read(&per_cpu_ptr(chan->local, i)->refcount);
  164. atomic_sub(bias, &chan->refcount.refcount);
  165. kref_put(&chan->refcount, dma_chan_cleanup);
  166. }
  167. static void dma_client_chan_free(struct dma_chan *chan)
  168. {
  169. atomic_add(0x7FFFFFFF, &chan->refcount.refcount);
  170. chan->slow_ref = 1;
  171. call_rcu(&chan->rcu, dma_chan_free_rcu);
  172. }
  173. /**
  174. * dma_chans_rebalance - reallocate channels to clients
  175. *
  176. * When the number of DMA channel in the system changes,
  177. * channels need to be rebalanced among clients.
  178. */
  179. static void dma_chans_rebalance(void)
  180. {
  181. struct dma_client *client;
  182. struct dma_chan *chan;
  183. unsigned long flags;
  184. mutex_lock(&dma_list_mutex);
  185. list_for_each_entry(client, &dma_client_list, global_node) {
  186. while (client->chans_desired > client->chan_count) {
  187. chan = dma_client_chan_alloc(client);
  188. if (!chan)
  189. break;
  190. client->chan_count++;
  191. client->event_callback(client,
  192. chan,
  193. DMA_RESOURCE_ADDED);
  194. }
  195. while (client->chans_desired < client->chan_count) {
  196. spin_lock_irqsave(&client->lock, flags);
  197. chan = list_entry(client->channels.next,
  198. struct dma_chan,
  199. client_node);
  200. list_del_rcu(&chan->client_node);
  201. spin_unlock_irqrestore(&client->lock, flags);
  202. client->chan_count--;
  203. client->event_callback(client,
  204. chan,
  205. DMA_RESOURCE_REMOVED);
  206. dma_client_chan_free(chan);
  207. }
  208. }
  209. mutex_unlock(&dma_list_mutex);
  210. }
  211. /**
  212. * dma_async_client_register - allocate and register a &dma_client
  213. * @event_callback: callback for notification of channel addition/removal
  214. */
  215. struct dma_client *dma_async_client_register(dma_event_callback event_callback)
  216. {
  217. struct dma_client *client;
  218. client = kzalloc(sizeof(*client), GFP_KERNEL);
  219. if (!client)
  220. return NULL;
  221. INIT_LIST_HEAD(&client->channels);
  222. spin_lock_init(&client->lock);
  223. client->chans_desired = 0;
  224. client->chan_count = 0;
  225. client->event_callback = event_callback;
  226. mutex_lock(&dma_list_mutex);
  227. list_add_tail(&client->global_node, &dma_client_list);
  228. mutex_unlock(&dma_list_mutex);
  229. return client;
  230. }
  231. /**
  232. * dma_async_client_unregister - unregister a client and free the &dma_client
  233. * @client: &dma_client to free
  234. *
  235. * Force frees any allocated DMA channels, frees the &dma_client memory
  236. */
  237. void dma_async_client_unregister(struct dma_client *client)
  238. {
  239. struct dma_chan *chan;
  240. if (!client)
  241. return;
  242. rcu_read_lock();
  243. list_for_each_entry_rcu(chan, &client->channels, client_node)
  244. dma_client_chan_free(chan);
  245. rcu_read_unlock();
  246. mutex_lock(&dma_list_mutex);
  247. list_del(&client->global_node);
  248. mutex_unlock(&dma_list_mutex);
  249. kfree(client);
  250. dma_chans_rebalance();
  251. }
  252. /**
  253. * dma_async_client_chan_request - request DMA channels
  254. * @client: &dma_client
  255. * @number: count of DMA channels requested
  256. *
  257. * Clients call dma_async_client_chan_request() to specify how many
  258. * DMA channels they need, 0 to free all currently allocated.
  259. * The resulting allocations/frees are indicated to the client via the
  260. * event callback.
  261. */
  262. void dma_async_client_chan_request(struct dma_client *client,
  263. unsigned int number)
  264. {
  265. client->chans_desired = number;
  266. dma_chans_rebalance();
  267. }
  268. /**
  269. * dma_async_device_register - registers DMA devices found
  270. * @device: &dma_device
  271. */
  272. int dma_async_device_register(struct dma_device *device)
  273. {
  274. static int id;
  275. int chancnt = 0;
  276. struct dma_chan* chan;
  277. if (!device)
  278. return -ENODEV;
  279. init_completion(&device->done);
  280. kref_init(&device->refcount);
  281. device->dev_id = id++;
  282. /* represent channels in sysfs. Probably want devs too */
  283. list_for_each_entry(chan, &device->channels, device_node) {
  284. chan->local = alloc_percpu(typeof(*chan->local));
  285. if (chan->local == NULL)
  286. continue;
  287. chan->chan_id = chancnt++;
  288. chan->class_dev.class = &dma_devclass;
  289. chan->class_dev.dev = NULL;
  290. snprintf(chan->class_dev.class_id, BUS_ID_SIZE, "dma%dchan%d",
  291. device->dev_id, chan->chan_id);
  292. kref_get(&device->refcount);
  293. class_device_register(&chan->class_dev);
  294. }
  295. mutex_lock(&dma_list_mutex);
  296. list_add_tail(&device->global_node, &dma_device_list);
  297. mutex_unlock(&dma_list_mutex);
  298. dma_chans_rebalance();
  299. return 0;
  300. }
  301. /**
  302. * dma_async_device_cleanup - function called when all references are released
  303. * @kref: kernel reference object
  304. */
  305. static void dma_async_device_cleanup(struct kref *kref)
  306. {
  307. struct dma_device *device;
  308. device = container_of(kref, struct dma_device, refcount);
  309. complete(&device->done);
  310. }
  311. /**
  312. * dma_async_device_unregister - unregisters DMA devices
  313. * @device: &dma_device
  314. */
  315. void dma_async_device_unregister(struct dma_device *device)
  316. {
  317. struct dma_chan *chan;
  318. unsigned long flags;
  319. mutex_lock(&dma_list_mutex);
  320. list_del(&device->global_node);
  321. mutex_unlock(&dma_list_mutex);
  322. list_for_each_entry(chan, &device->channels, device_node) {
  323. if (chan->client) {
  324. spin_lock_irqsave(&chan->client->lock, flags);
  325. list_del(&chan->client_node);
  326. chan->client->chan_count--;
  327. spin_unlock_irqrestore(&chan->client->lock, flags);
  328. chan->client->event_callback(chan->client,
  329. chan,
  330. DMA_RESOURCE_REMOVED);
  331. dma_client_chan_free(chan);
  332. }
  333. class_device_unregister(&chan->class_dev);
  334. }
  335. dma_chans_rebalance();
  336. kref_put(&device->refcount, dma_async_device_cleanup);
  337. wait_for_completion(&device->done);
  338. }
  339. static int __init dma_bus_init(void)
  340. {
  341. mutex_init(&dma_list_mutex);
  342. return class_register(&dma_devclass);
  343. }
  344. subsys_initcall(dma_bus_init);
  345. EXPORT_SYMBOL(dma_async_client_register);
  346. EXPORT_SYMBOL(dma_async_client_unregister);
  347. EXPORT_SYMBOL(dma_async_client_chan_request);
  348. EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
  349. EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
  350. EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
  351. EXPORT_SYMBOL(dma_async_memcpy_complete);
  352. EXPORT_SYMBOL(dma_async_memcpy_issue_pending);
  353. EXPORT_SYMBOL(dma_async_device_register);
  354. EXPORT_SYMBOL(dma_async_device_unregister);
  355. EXPORT_SYMBOL(dma_chan_cleanup);