dmaengine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. kref_get(&device->refcount);
  164. } else if (ack == DMA_NAK)
  165. return;
  166. }
  167. }
  168. }
  169. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  170. {
  171. enum dma_status status;
  172. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  173. dma_async_issue_pending(chan);
  174. do {
  175. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  176. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  177. printk(KERN_ERR "dma_sync_wait_timeout!\n");
  178. return DMA_ERROR;
  179. }
  180. } while (status == DMA_IN_PROGRESS);
  181. return status;
  182. }
  183. EXPORT_SYMBOL(dma_sync_wait);
  184. /**
  185. * dma_chan_cleanup - release a DMA channel's resources
  186. * @kref: kernel reference structure that contains the DMA channel device
  187. */
  188. void dma_chan_cleanup(struct kref *kref)
  189. {
  190. struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
  191. chan->device->device_free_chan_resources(chan);
  192. kref_put(&chan->device->refcount, dma_async_device_cleanup);
  193. }
  194. EXPORT_SYMBOL(dma_chan_cleanup);
  195. static void dma_chan_free_rcu(struct rcu_head *rcu)
  196. {
  197. struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
  198. int bias = 0x7FFFFFFF;
  199. int i;
  200. for_each_possible_cpu(i)
  201. bias -= local_read(&per_cpu_ptr(chan->local, i)->refcount);
  202. atomic_sub(bias, &chan->refcount.refcount);
  203. kref_put(&chan->refcount, dma_chan_cleanup);
  204. }
  205. static void dma_chan_release(struct dma_chan *chan)
  206. {
  207. atomic_add(0x7FFFFFFF, &chan->refcount.refcount);
  208. chan->slow_ref = 1;
  209. call_rcu(&chan->rcu, dma_chan_free_rcu);
  210. }
  211. /**
  212. * dma_chans_notify_available - broadcast available channels to the clients
  213. */
  214. static void dma_clients_notify_available(void)
  215. {
  216. struct dma_client *client;
  217. mutex_lock(&dma_list_mutex);
  218. list_for_each_entry(client, &dma_client_list, global_node)
  219. dma_client_chan_alloc(client);
  220. mutex_unlock(&dma_list_mutex);
  221. }
  222. /**
  223. * dma_chans_notify_available - tell the clients that a channel is going away
  224. * @chan: channel on its way out
  225. */
  226. static void dma_clients_notify_removed(struct dma_chan *chan)
  227. {
  228. struct dma_client *client;
  229. enum dma_state_client ack;
  230. mutex_lock(&dma_list_mutex);
  231. list_for_each_entry(client, &dma_client_list, global_node) {
  232. ack = client->event_callback(client, chan,
  233. DMA_RESOURCE_REMOVED);
  234. /* client was holding resources for this channel so
  235. * free it
  236. */
  237. if (ack == DMA_ACK) {
  238. dma_chan_put(chan);
  239. kref_put(&chan->device->refcount,
  240. dma_async_device_cleanup);
  241. }
  242. }
  243. mutex_unlock(&dma_list_mutex);
  244. }
  245. /**
  246. * dma_async_client_register - register a &dma_client
  247. * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
  248. */
  249. void dma_async_client_register(struct dma_client *client)
  250. {
  251. mutex_lock(&dma_list_mutex);
  252. list_add_tail(&client->global_node, &dma_client_list);
  253. mutex_unlock(&dma_list_mutex);
  254. }
  255. EXPORT_SYMBOL(dma_async_client_register);
  256. /**
  257. * dma_async_client_unregister - unregister a client and free the &dma_client
  258. * @client: &dma_client to free
  259. *
  260. * Force frees any allocated DMA channels, frees the &dma_client memory
  261. */
  262. void dma_async_client_unregister(struct dma_client *client)
  263. {
  264. struct dma_device *device;
  265. struct dma_chan *chan;
  266. enum dma_state_client ack;
  267. if (!client)
  268. return;
  269. mutex_lock(&dma_list_mutex);
  270. /* free all channels the client is holding */
  271. list_for_each_entry(device, &dma_device_list, global_node)
  272. list_for_each_entry(chan, &device->channels, device_node) {
  273. ack = client->event_callback(client, chan,
  274. DMA_RESOURCE_REMOVED);
  275. if (ack == DMA_ACK) {
  276. dma_chan_put(chan);
  277. kref_put(&chan->device->refcount,
  278. dma_async_device_cleanup);
  279. }
  280. }
  281. list_del(&client->global_node);
  282. mutex_unlock(&dma_list_mutex);
  283. }
  284. EXPORT_SYMBOL(dma_async_client_unregister);
  285. /**
  286. * dma_async_client_chan_request - send all available channels to the
  287. * client that satisfy the capability mask
  288. * @client - requester
  289. */
  290. void dma_async_client_chan_request(struct dma_client *client)
  291. {
  292. mutex_lock(&dma_list_mutex);
  293. dma_client_chan_alloc(client);
  294. mutex_unlock(&dma_list_mutex);
  295. }
  296. EXPORT_SYMBOL(dma_async_client_chan_request);
  297. /**
  298. * dma_async_device_register - registers DMA devices found
  299. * @device: &dma_device
  300. */
  301. int dma_async_device_register(struct dma_device *device)
  302. {
  303. static int id;
  304. int chancnt = 0, rc;
  305. struct dma_chan* chan;
  306. if (!device)
  307. return -ENODEV;
  308. /* validate device routines */
  309. BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
  310. !device->device_prep_dma_memcpy);
  311. BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
  312. !device->device_prep_dma_xor);
  313. BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
  314. !device->device_prep_dma_zero_sum);
  315. BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
  316. !device->device_prep_dma_memset);
  317. BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
  318. !device->device_prep_dma_interrupt);
  319. BUG_ON(!device->device_alloc_chan_resources);
  320. BUG_ON(!device->device_free_chan_resources);
  321. BUG_ON(!device->device_dependency_added);
  322. BUG_ON(!device->device_is_tx_complete);
  323. BUG_ON(!device->device_issue_pending);
  324. BUG_ON(!device->dev);
  325. init_completion(&device->done);
  326. kref_init(&device->refcount);
  327. device->dev_id = id++;
  328. /* represent channels in sysfs. Probably want devs too */
  329. list_for_each_entry(chan, &device->channels, device_node) {
  330. chan->local = alloc_percpu(typeof(*chan->local));
  331. if (chan->local == NULL)
  332. continue;
  333. chan->chan_id = chancnt++;
  334. chan->class_dev.class = &dma_devclass;
  335. chan->class_dev.dev = NULL;
  336. snprintf(chan->class_dev.class_id, BUS_ID_SIZE, "dma%dchan%d",
  337. device->dev_id, chan->chan_id);
  338. rc = class_device_register(&chan->class_dev);
  339. if (rc) {
  340. chancnt--;
  341. free_percpu(chan->local);
  342. chan->local = NULL;
  343. goto err_out;
  344. }
  345. kref_get(&device->refcount);
  346. kref_init(&chan->refcount);
  347. chan->slow_ref = 0;
  348. INIT_RCU_HEAD(&chan->rcu);
  349. }
  350. mutex_lock(&dma_list_mutex);
  351. list_add_tail(&device->global_node, &dma_device_list);
  352. mutex_unlock(&dma_list_mutex);
  353. dma_clients_notify_available();
  354. return 0;
  355. err_out:
  356. list_for_each_entry(chan, &device->channels, device_node) {
  357. if (chan->local == NULL)
  358. continue;
  359. kref_put(&device->refcount, dma_async_device_cleanup);
  360. class_device_unregister(&chan->class_dev);
  361. chancnt--;
  362. free_percpu(chan->local);
  363. }
  364. return rc;
  365. }
  366. EXPORT_SYMBOL(dma_async_device_register);
  367. /**
  368. * dma_async_device_cleanup - function called when all references are released
  369. * @kref: kernel reference object
  370. */
  371. static void dma_async_device_cleanup(struct kref *kref)
  372. {
  373. struct dma_device *device;
  374. device = container_of(kref, struct dma_device, refcount);
  375. complete(&device->done);
  376. }
  377. /**
  378. * dma_async_device_unregister - unregisters DMA devices
  379. * @device: &dma_device
  380. */
  381. void dma_async_device_unregister(struct dma_device *device)
  382. {
  383. struct dma_chan *chan;
  384. mutex_lock(&dma_list_mutex);
  385. list_del(&device->global_node);
  386. mutex_unlock(&dma_list_mutex);
  387. list_for_each_entry(chan, &device->channels, device_node) {
  388. dma_clients_notify_removed(chan);
  389. class_device_unregister(&chan->class_dev);
  390. dma_chan_release(chan);
  391. }
  392. kref_put(&device->refcount, dma_async_device_cleanup);
  393. wait_for_completion(&device->done);
  394. }
  395. EXPORT_SYMBOL(dma_async_device_unregister);
  396. /**
  397. * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
  398. * @chan: DMA channel to offload copy to
  399. * @dest: destination address (virtual)
  400. * @src: source address (virtual)
  401. * @len: length
  402. *
  403. * Both @dest and @src must be mappable to a bus address according to the
  404. * DMA mapping API rules for streaming mappings.
  405. * Both @dest and @src must stay memory resident (kernel memory or locked
  406. * user space pages).
  407. */
  408. dma_cookie_t
  409. dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
  410. void *src, size_t len)
  411. {
  412. struct dma_device *dev = chan->device;
  413. struct dma_async_tx_descriptor *tx;
  414. dma_addr_t addr;
  415. dma_cookie_t cookie;
  416. int cpu;
  417. tx = dev->device_prep_dma_memcpy(chan, len, 0);
  418. if (!tx)
  419. return -ENOMEM;
  420. tx->ack = 1;
  421. tx->callback = NULL;
  422. addr = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
  423. tx->tx_set_src(addr, tx, 0);
  424. addr = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
  425. tx->tx_set_dest(addr, tx, 0);
  426. cookie = tx->tx_submit(tx);
  427. cpu = get_cpu();
  428. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  429. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  430. put_cpu();
  431. return cookie;
  432. }
  433. EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
  434. /**
  435. * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
  436. * @chan: DMA channel to offload copy to
  437. * @page: destination page
  438. * @offset: offset in page to copy to
  439. * @kdata: source address (virtual)
  440. * @len: length
  441. *
  442. * Both @page/@offset and @kdata must be mappable to a bus address according
  443. * to the DMA mapping API rules for streaming mappings.
  444. * Both @page/@offset and @kdata must stay memory resident (kernel memory or
  445. * locked user space pages)
  446. */
  447. dma_cookie_t
  448. dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
  449. unsigned int offset, void *kdata, size_t len)
  450. {
  451. struct dma_device *dev = chan->device;
  452. struct dma_async_tx_descriptor *tx;
  453. dma_addr_t addr;
  454. dma_cookie_t cookie;
  455. int cpu;
  456. tx = dev->device_prep_dma_memcpy(chan, len, 0);
  457. if (!tx)
  458. return -ENOMEM;
  459. tx->ack = 1;
  460. tx->callback = NULL;
  461. addr = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
  462. tx->tx_set_src(addr, tx, 0);
  463. addr = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
  464. tx->tx_set_dest(addr, tx, 0);
  465. cookie = tx->tx_submit(tx);
  466. cpu = get_cpu();
  467. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  468. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  469. put_cpu();
  470. return cookie;
  471. }
  472. EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
  473. /**
  474. * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
  475. * @chan: DMA channel to offload copy to
  476. * @dest_pg: destination page
  477. * @dest_off: offset in page to copy to
  478. * @src_pg: source page
  479. * @src_off: offset in page to copy from
  480. * @len: length
  481. *
  482. * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
  483. * address according to the DMA mapping API rules for streaming mappings.
  484. * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
  485. * (kernel memory or locked user space pages).
  486. */
  487. dma_cookie_t
  488. dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
  489. unsigned int dest_off, struct page *src_pg, unsigned int src_off,
  490. size_t len)
  491. {
  492. struct dma_device *dev = chan->device;
  493. struct dma_async_tx_descriptor *tx;
  494. dma_addr_t addr;
  495. dma_cookie_t cookie;
  496. int cpu;
  497. tx = dev->device_prep_dma_memcpy(chan, len, 0);
  498. if (!tx)
  499. return -ENOMEM;
  500. tx->ack = 1;
  501. tx->callback = NULL;
  502. addr = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
  503. tx->tx_set_src(addr, tx, 0);
  504. addr = dma_map_page(dev->dev, dest_pg, dest_off, len, DMA_FROM_DEVICE);
  505. tx->tx_set_dest(addr, tx, 0);
  506. cookie = tx->tx_submit(tx);
  507. cpu = get_cpu();
  508. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  509. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  510. put_cpu();
  511. return cookie;
  512. }
  513. EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
  514. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  515. struct dma_chan *chan)
  516. {
  517. tx->chan = chan;
  518. spin_lock_init(&tx->lock);
  519. INIT_LIST_HEAD(&tx->depend_node);
  520. INIT_LIST_HEAD(&tx->depend_list);
  521. }
  522. EXPORT_SYMBOL(dma_async_tx_descriptor_init);
  523. static int __init dma_bus_init(void)
  524. {
  525. mutex_init(&dma_list_mutex);
  526. return class_register(&dma_devclass);
  527. }
  528. subsys_initcall(dma_bus_init);