dmaengine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 is responsible for keeping track of the channels it uses. See
  40. * the definition of dma_event_callback in dmaengine.h.
  41. *
  42. * Each device has a kref, which is initialized to 1 when the device is
  43. * registered. A kref_get 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 dma_chan_get is called when a client
  54. * signals that it wants to use a channel, and dma_chan_put is called when
  55. * a channel is removed or a client using it is unregesitered. A client can
  56. * take extra references per outstanding transaction, as is the case with
  57. * the NET DMA client. The release function does a kref_put on the device.
  58. * -ChrisL, DanW
  59. */
  60. #include <linux/init.h>
  61. #include <linux/module.h>
  62. #include <linux/mm.h>
  63. #include <linux/device.h>
  64. #include <linux/dmaengine.h>
  65. #include <linux/hardirq.h>
  66. #include <linux/spinlock.h>
  67. #include <linux/percpu.h>
  68. #include <linux/rcupdate.h>
  69. #include <linux/mutex.h>
  70. #include <linux/jiffies.h>
  71. static DEFINE_MUTEX(dma_list_mutex);
  72. static LIST_HEAD(dma_device_list);
  73. static LIST_HEAD(dma_client_list);
  74. /* --- sysfs implementation --- */
  75. static ssize_t show_memcpy_count(struct class_device *cd, char *buf)
  76. {
  77. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  78. unsigned long count = 0;
  79. int i;
  80. for_each_possible_cpu(i)
  81. count += per_cpu_ptr(chan->local, i)->memcpy_count;
  82. return sprintf(buf, "%lu\n", count);
  83. }
  84. static ssize_t show_bytes_transferred(struct class_device *cd, char *buf)
  85. {
  86. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  87. unsigned long count = 0;
  88. int i;
  89. for_each_possible_cpu(i)
  90. count += per_cpu_ptr(chan->local, i)->bytes_transferred;
  91. return sprintf(buf, "%lu\n", count);
  92. }
  93. static ssize_t show_in_use(struct class_device *cd, char *buf)
  94. {
  95. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  96. int in_use = 0;
  97. if (unlikely(chan->slow_ref) &&
  98. atomic_read(&chan->refcount.refcount) > 1)
  99. in_use = 1;
  100. else {
  101. if (local_read(&(per_cpu_ptr(chan->local,
  102. get_cpu())->refcount)) > 0)
  103. in_use = 1;
  104. put_cpu();
  105. }
  106. return sprintf(buf, "%d\n", in_use);
  107. }
  108. static struct class_device_attribute dma_class_attrs[] = {
  109. __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
  110. __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
  111. __ATTR(in_use, S_IRUGO, show_in_use, NULL),
  112. __ATTR_NULL
  113. };
  114. static void dma_async_device_cleanup(struct kref *kref);
  115. static void dma_class_dev_release(struct class_device *cd)
  116. {
  117. struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
  118. kref_put(&chan->device->refcount, dma_async_device_cleanup);
  119. }
  120. static struct class dma_devclass = {
  121. .name = "dma",
  122. .class_dev_attrs = dma_class_attrs,
  123. .release = dma_class_dev_release,
  124. };
  125. /* --- client and device registration --- */
  126. #define dma_chan_satisfies_mask(chan, mask) \
  127. __dma_chan_satisfies_mask((chan), &(mask))
  128. static int
  129. __dma_chan_satisfies_mask(struct dma_chan *chan, dma_cap_mask_t *want)
  130. {
  131. dma_cap_mask_t has;
  132. bitmap_and(has.bits, want->bits, chan->device->cap_mask.bits,
  133. DMA_TX_TYPE_END);
  134. return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
  135. }
  136. /**
  137. * dma_client_chan_alloc - try to allocate channels to a client
  138. * @client: &dma_client
  139. *
  140. * Called with dma_list_mutex held.
  141. */
  142. static void dma_client_chan_alloc(struct dma_client *client)
  143. {
  144. struct dma_device *device;
  145. struct dma_chan *chan;
  146. int desc; /* allocated descriptor count */
  147. enum dma_state_client ack;
  148. /* Find a channel */
  149. list_for_each_entry(device, &dma_device_list, global_node)
  150. list_for_each_entry(chan, &device->channels, device_node) {
  151. if (!dma_chan_satisfies_mask(chan, client->cap_mask))
  152. continue;
  153. desc = chan->device->device_alloc_chan_resources(chan);
  154. if (desc >= 0) {
  155. ack = client->event_callback(client,
  156. chan,
  157. DMA_RESOURCE_AVAILABLE);
  158. /* we are done once this client rejects
  159. * an available resource
  160. */
  161. if (ack == DMA_ACK)
  162. dma_chan_get(chan);
  163. else if (ack == DMA_NAK)
  164. return;
  165. }
  166. }
  167. }
  168. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  169. {
  170. enum dma_status status;
  171. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  172. dma_async_issue_pending(chan);
  173. do {
  174. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  175. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  176. printk(KERN_ERR "dma_sync_wait_timeout!\n");
  177. return DMA_ERROR;
  178. }
  179. } while (status == DMA_IN_PROGRESS);
  180. return status;
  181. }
  182. EXPORT_SYMBOL(dma_sync_wait);
  183. /**
  184. * dma_chan_cleanup - release a DMA channel's resources
  185. * @kref: kernel reference structure that contains the DMA channel device
  186. */
  187. void dma_chan_cleanup(struct kref *kref)
  188. {
  189. struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
  190. chan->device->device_free_chan_resources(chan);
  191. kref_put(&chan->device->refcount, dma_async_device_cleanup);
  192. }
  193. EXPORT_SYMBOL(dma_chan_cleanup);
  194. static void dma_chan_free_rcu(struct rcu_head *rcu)
  195. {
  196. struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
  197. int bias = 0x7FFFFFFF;
  198. int i;
  199. for_each_possible_cpu(i)
  200. bias -= local_read(&per_cpu_ptr(chan->local, i)->refcount);
  201. atomic_sub(bias, &chan->refcount.refcount);
  202. kref_put(&chan->refcount, dma_chan_cleanup);
  203. }
  204. static void dma_chan_release(struct dma_chan *chan)
  205. {
  206. atomic_add(0x7FFFFFFF, &chan->refcount.refcount);
  207. chan->slow_ref = 1;
  208. call_rcu(&chan->rcu, dma_chan_free_rcu);
  209. }
  210. /**
  211. * dma_chans_notify_available - broadcast available channels to the clients
  212. */
  213. static void dma_clients_notify_available(void)
  214. {
  215. struct dma_client *client;
  216. mutex_lock(&dma_list_mutex);
  217. list_for_each_entry(client, &dma_client_list, global_node)
  218. dma_client_chan_alloc(client);
  219. mutex_unlock(&dma_list_mutex);
  220. }
  221. /**
  222. * dma_chans_notify_available - tell the clients that a channel is going away
  223. * @chan: channel on its way out
  224. */
  225. static void dma_clients_notify_removed(struct dma_chan *chan)
  226. {
  227. struct dma_client *client;
  228. enum dma_state_client ack;
  229. mutex_lock(&dma_list_mutex);
  230. list_for_each_entry(client, &dma_client_list, global_node) {
  231. ack = client->event_callback(client, chan,
  232. DMA_RESOURCE_REMOVED);
  233. /* client was holding resources for this channel so
  234. * free it
  235. */
  236. if (ack == DMA_ACK)
  237. dma_chan_put(chan);
  238. }
  239. mutex_unlock(&dma_list_mutex);
  240. }
  241. /**
  242. * dma_async_client_register - register a &dma_client
  243. * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
  244. */
  245. void dma_async_client_register(struct dma_client *client)
  246. {
  247. mutex_lock(&dma_list_mutex);
  248. list_add_tail(&client->global_node, &dma_client_list);
  249. mutex_unlock(&dma_list_mutex);
  250. }
  251. EXPORT_SYMBOL(dma_async_client_register);
  252. /**
  253. * dma_async_client_unregister - unregister a client and free the &dma_client
  254. * @client: &dma_client to free
  255. *
  256. * Force frees any allocated DMA channels, frees the &dma_client memory
  257. */
  258. void dma_async_client_unregister(struct dma_client *client)
  259. {
  260. struct dma_device *device;
  261. struct dma_chan *chan;
  262. enum dma_state_client ack;
  263. if (!client)
  264. return;
  265. mutex_lock(&dma_list_mutex);
  266. /* free all channels the client is holding */
  267. list_for_each_entry(device, &dma_device_list, global_node)
  268. list_for_each_entry(chan, &device->channels, device_node) {
  269. ack = client->event_callback(client, chan,
  270. DMA_RESOURCE_REMOVED);
  271. if (ack == DMA_ACK)
  272. dma_chan_put(chan);
  273. }
  274. list_del(&client->global_node);
  275. mutex_unlock(&dma_list_mutex);
  276. }
  277. EXPORT_SYMBOL(dma_async_client_unregister);
  278. /**
  279. * dma_async_client_chan_request - send all available channels to the
  280. * client that satisfy the capability mask
  281. * @client - requester
  282. */
  283. void dma_async_client_chan_request(struct dma_client *client)
  284. {
  285. mutex_lock(&dma_list_mutex);
  286. dma_client_chan_alloc(client);
  287. mutex_unlock(&dma_list_mutex);
  288. }
  289. EXPORT_SYMBOL(dma_async_client_chan_request);
  290. /**
  291. * dma_async_device_register - registers DMA devices found
  292. * @device: &dma_device
  293. */
  294. int dma_async_device_register(struct dma_device *device)
  295. {
  296. static int id;
  297. int chancnt = 0, rc;
  298. struct dma_chan* chan;
  299. if (!device)
  300. return -ENODEV;
  301. /* validate device routines */
  302. BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
  303. !device->device_prep_dma_memcpy);
  304. BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
  305. !device->device_prep_dma_xor);
  306. BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
  307. !device->device_prep_dma_zero_sum);
  308. BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
  309. !device->device_prep_dma_memset);
  310. BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
  311. !device->device_prep_dma_interrupt);
  312. BUG_ON(!device->device_alloc_chan_resources);
  313. BUG_ON(!device->device_free_chan_resources);
  314. BUG_ON(!device->device_dependency_added);
  315. BUG_ON(!device->device_is_tx_complete);
  316. BUG_ON(!device->device_issue_pending);
  317. BUG_ON(!device->dev);
  318. init_completion(&device->done);
  319. kref_init(&device->refcount);
  320. device->dev_id = id++;
  321. /* represent channels in sysfs. Probably want devs too */
  322. list_for_each_entry(chan, &device->channels, device_node) {
  323. chan->local = alloc_percpu(typeof(*chan->local));
  324. if (chan->local == NULL)
  325. continue;
  326. chan->chan_id = chancnt++;
  327. chan->class_dev.class = &dma_devclass;
  328. chan->class_dev.dev = NULL;
  329. snprintf(chan->class_dev.class_id, BUS_ID_SIZE, "dma%dchan%d",
  330. device->dev_id, chan->chan_id);
  331. rc = class_device_register(&chan->class_dev);
  332. if (rc) {
  333. chancnt--;
  334. free_percpu(chan->local);
  335. chan->local = NULL;
  336. goto err_out;
  337. }
  338. /* One for the channel, one of the class device */
  339. kref_get(&device->refcount);
  340. kref_get(&device->refcount);
  341. kref_init(&chan->refcount);
  342. chan->slow_ref = 0;
  343. INIT_RCU_HEAD(&chan->rcu);
  344. }
  345. mutex_lock(&dma_list_mutex);
  346. list_add_tail(&device->global_node, &dma_device_list);
  347. mutex_unlock(&dma_list_mutex);
  348. dma_clients_notify_available();
  349. return 0;
  350. err_out:
  351. list_for_each_entry(chan, &device->channels, device_node) {
  352. if (chan->local == NULL)
  353. continue;
  354. kref_put(&device->refcount, dma_async_device_cleanup);
  355. class_device_unregister(&chan->class_dev);
  356. chancnt--;
  357. free_percpu(chan->local);
  358. }
  359. return rc;
  360. }
  361. EXPORT_SYMBOL(dma_async_device_register);
  362. /**
  363. * dma_async_device_cleanup - function called when all references are released
  364. * @kref: kernel reference object
  365. */
  366. static void dma_async_device_cleanup(struct kref *kref)
  367. {
  368. struct dma_device *device;
  369. device = container_of(kref, struct dma_device, refcount);
  370. complete(&device->done);
  371. }
  372. /**
  373. * dma_async_device_unregister - unregisters DMA devices
  374. * @device: &dma_device
  375. */
  376. void dma_async_device_unregister(struct dma_device *device)
  377. {
  378. struct dma_chan *chan;
  379. mutex_lock(&dma_list_mutex);
  380. list_del(&device->global_node);
  381. mutex_unlock(&dma_list_mutex);
  382. list_for_each_entry(chan, &device->channels, device_node) {
  383. dma_clients_notify_removed(chan);
  384. class_device_unregister(&chan->class_dev);
  385. dma_chan_release(chan);
  386. }
  387. kref_put(&device->refcount, dma_async_device_cleanup);
  388. wait_for_completion(&device->done);
  389. }
  390. EXPORT_SYMBOL(dma_async_device_unregister);
  391. /**
  392. * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
  393. * @chan: DMA channel to offload copy to
  394. * @dest: destination address (virtual)
  395. * @src: source address (virtual)
  396. * @len: length
  397. *
  398. * Both @dest and @src must be mappable to a bus address according to the
  399. * DMA mapping API rules for streaming mappings.
  400. * Both @dest and @src must stay memory resident (kernel memory or locked
  401. * user space pages).
  402. */
  403. dma_cookie_t
  404. dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
  405. void *src, size_t len)
  406. {
  407. struct dma_device *dev = chan->device;
  408. struct dma_async_tx_descriptor *tx;
  409. dma_addr_t addr;
  410. dma_cookie_t cookie;
  411. int cpu;
  412. tx = dev->device_prep_dma_memcpy(chan, len, 0);
  413. if (!tx)
  414. return -ENOMEM;
  415. tx->ack = 1;
  416. tx->callback = NULL;
  417. addr = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
  418. tx->tx_set_src(addr, tx, 0);
  419. addr = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
  420. tx->tx_set_dest(addr, tx, 0);
  421. cookie = tx->tx_submit(tx);
  422. cpu = get_cpu();
  423. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  424. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  425. put_cpu();
  426. return cookie;
  427. }
  428. EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
  429. /**
  430. * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
  431. * @chan: DMA channel to offload copy to
  432. * @page: destination page
  433. * @offset: offset in page to copy to
  434. * @kdata: source address (virtual)
  435. * @len: length
  436. *
  437. * Both @page/@offset and @kdata must be mappable to a bus address according
  438. * to the DMA mapping API rules for streaming mappings.
  439. * Both @page/@offset and @kdata must stay memory resident (kernel memory or
  440. * locked user space pages)
  441. */
  442. dma_cookie_t
  443. dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
  444. unsigned int offset, void *kdata, size_t len)
  445. {
  446. struct dma_device *dev = chan->device;
  447. struct dma_async_tx_descriptor *tx;
  448. dma_addr_t addr;
  449. dma_cookie_t cookie;
  450. int cpu;
  451. tx = dev->device_prep_dma_memcpy(chan, len, 0);
  452. if (!tx)
  453. return -ENOMEM;
  454. tx->ack = 1;
  455. tx->callback = NULL;
  456. addr = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
  457. tx->tx_set_src(addr, tx, 0);
  458. addr = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
  459. tx->tx_set_dest(addr, tx, 0);
  460. cookie = tx->tx_submit(tx);
  461. cpu = get_cpu();
  462. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  463. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  464. put_cpu();
  465. return cookie;
  466. }
  467. EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
  468. /**
  469. * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
  470. * @chan: DMA channel to offload copy to
  471. * @dest_pg: destination page
  472. * @dest_off: offset in page to copy to
  473. * @src_pg: source page
  474. * @src_off: offset in page to copy from
  475. * @len: length
  476. *
  477. * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
  478. * address according to the DMA mapping API rules for streaming mappings.
  479. * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
  480. * (kernel memory or locked user space pages).
  481. */
  482. dma_cookie_t
  483. dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
  484. unsigned int dest_off, struct page *src_pg, unsigned int src_off,
  485. size_t len)
  486. {
  487. struct dma_device *dev = chan->device;
  488. struct dma_async_tx_descriptor *tx;
  489. dma_addr_t addr;
  490. dma_cookie_t cookie;
  491. int cpu;
  492. tx = dev->device_prep_dma_memcpy(chan, len, 0);
  493. if (!tx)
  494. return -ENOMEM;
  495. tx->ack = 1;
  496. tx->callback = NULL;
  497. addr = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
  498. tx->tx_set_src(addr, tx, 0);
  499. addr = dma_map_page(dev->dev, dest_pg, dest_off, len, DMA_FROM_DEVICE);
  500. tx->tx_set_dest(addr, tx, 0);
  501. cookie = tx->tx_submit(tx);
  502. cpu = get_cpu();
  503. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  504. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  505. put_cpu();
  506. return cookie;
  507. }
  508. EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
  509. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  510. struct dma_chan *chan)
  511. {
  512. tx->chan = chan;
  513. spin_lock_init(&tx->lock);
  514. INIT_LIST_HEAD(&tx->depend_node);
  515. INIT_LIST_HEAD(&tx->depend_list);
  516. }
  517. EXPORT_SYMBOL(dma_async_tx_descriptor_init);
  518. static int __init dma_bus_init(void)
  519. {
  520. mutex_init(&dma_list_mutex);
  521. return class_register(&dma_devclass);
  522. }
  523. subsys_initcall(dma_bus_init);