dmaengine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 device registered. When the
  44. * device is released, the corresponding 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 corresponding kref_put
  47. * happens. The device's release function does a completion, so
  48. * unregister_device does a remove event, 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 unregistered. 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 device *dev, struct device_attribute *attr, char *buf)
  76. {
  77. struct dma_chan *chan = to_dma_chan(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 device *dev, struct device_attribute *attr,
  85. char *buf)
  86. {
  87. struct dma_chan *chan = to_dma_chan(dev);
  88. unsigned long count = 0;
  89. int i;
  90. for_each_possible_cpu(i)
  91. count += per_cpu_ptr(chan->local, i)->bytes_transferred;
  92. return sprintf(buf, "%lu\n", count);
  93. }
  94. static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
  95. {
  96. struct dma_chan *chan = to_dma_chan(dev);
  97. int in_use = 0;
  98. if (unlikely(chan->slow_ref) &&
  99. atomic_read(&chan->refcount.refcount) > 1)
  100. in_use = 1;
  101. else {
  102. if (local_read(&(per_cpu_ptr(chan->local,
  103. get_cpu())->refcount)) > 0)
  104. in_use = 1;
  105. put_cpu();
  106. }
  107. return sprintf(buf, "%d\n", in_use);
  108. }
  109. static struct device_attribute dma_attrs[] = {
  110. __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
  111. __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
  112. __ATTR(in_use, S_IRUGO, show_in_use, NULL),
  113. __ATTR_NULL
  114. };
  115. static void dma_async_device_cleanup(struct kref *kref);
  116. static void dma_dev_release(struct device *dev)
  117. {
  118. struct dma_chan *chan = to_dma_chan(dev);
  119. kref_put(&chan->device->refcount, dma_async_device_cleanup);
  120. }
  121. static struct class dma_devclass = {
  122. .name = "dma",
  123. .dev_attrs = dma_attrs,
  124. .dev_release = dma_dev_release,
  125. };
  126. /* --- client and device registration --- */
  127. #define dma_chan_satisfies_mask(chan, mask) \
  128. __dma_chan_satisfies_mask((chan), &(mask))
  129. static int
  130. __dma_chan_satisfies_mask(struct dma_chan *chan, dma_cap_mask_t *want)
  131. {
  132. dma_cap_mask_t has;
  133. bitmap_and(has.bits, want->bits, chan->device->cap_mask.bits,
  134. DMA_TX_TYPE_END);
  135. return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
  136. }
  137. /**
  138. * dma_client_chan_alloc - try to allocate channels to a client
  139. * @client: &dma_client
  140. *
  141. * Called with dma_list_mutex held.
  142. */
  143. static void dma_client_chan_alloc(struct dma_client *client)
  144. {
  145. struct dma_device *device;
  146. struct dma_chan *chan;
  147. int desc; /* allocated descriptor count */
  148. enum dma_state_client ack;
  149. /* Find a channel */
  150. list_for_each_entry(device, &dma_device_list, global_node)
  151. list_for_each_entry(chan, &device->channels, device_node) {
  152. if (!dma_chan_satisfies_mask(chan, client->cap_mask))
  153. continue;
  154. desc = chan->device->device_alloc_chan_resources(
  155. chan, client);
  156. if (desc >= 0) {
  157. ack = client->event_callback(client,
  158. chan,
  159. DMA_RESOURCE_AVAILABLE);
  160. /* we are done once this client rejects
  161. * an available resource
  162. */
  163. if (ack == DMA_ACK) {
  164. dma_chan_get(chan);
  165. chan->client_count++;
  166. } else if (ack == DMA_NAK)
  167. return;
  168. }
  169. }
  170. }
  171. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  172. {
  173. enum dma_status status;
  174. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  175. dma_async_issue_pending(chan);
  176. do {
  177. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  178. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  179. printk(KERN_ERR "dma_sync_wait_timeout!\n");
  180. return DMA_ERROR;
  181. }
  182. } while (status == DMA_IN_PROGRESS);
  183. return status;
  184. }
  185. EXPORT_SYMBOL(dma_sync_wait);
  186. /**
  187. * dma_chan_cleanup - release a DMA channel's resources
  188. * @kref: kernel reference structure that contains the DMA channel device
  189. */
  190. void dma_chan_cleanup(struct kref *kref)
  191. {
  192. struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
  193. chan->device->device_free_chan_resources(chan);
  194. kref_put(&chan->device->refcount, dma_async_device_cleanup);
  195. }
  196. EXPORT_SYMBOL(dma_chan_cleanup);
  197. static void dma_chan_free_rcu(struct rcu_head *rcu)
  198. {
  199. struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
  200. int bias = 0x7FFFFFFF;
  201. int i;
  202. for_each_possible_cpu(i)
  203. bias -= local_read(&per_cpu_ptr(chan->local, i)->refcount);
  204. atomic_sub(bias, &chan->refcount.refcount);
  205. kref_put(&chan->refcount, dma_chan_cleanup);
  206. }
  207. static void dma_chan_release(struct dma_chan *chan)
  208. {
  209. atomic_add(0x7FFFFFFF, &chan->refcount.refcount);
  210. chan->slow_ref = 1;
  211. call_rcu(&chan->rcu, dma_chan_free_rcu);
  212. }
  213. /**
  214. * dma_chans_notify_available - broadcast available channels to the clients
  215. */
  216. static void dma_clients_notify_available(void)
  217. {
  218. struct dma_client *client;
  219. mutex_lock(&dma_list_mutex);
  220. list_for_each_entry(client, &dma_client_list, global_node)
  221. dma_client_chan_alloc(client);
  222. mutex_unlock(&dma_list_mutex);
  223. }
  224. /**
  225. * dma_chans_notify_available - tell the clients that a channel is going away
  226. * @chan: channel on its way out
  227. */
  228. static void dma_clients_notify_removed(struct dma_chan *chan)
  229. {
  230. struct dma_client *client;
  231. enum dma_state_client ack;
  232. mutex_lock(&dma_list_mutex);
  233. list_for_each_entry(client, &dma_client_list, global_node) {
  234. ack = client->event_callback(client, chan,
  235. DMA_RESOURCE_REMOVED);
  236. /* client was holding resources for this channel so
  237. * free it
  238. */
  239. if (ack == DMA_ACK) {
  240. dma_chan_put(chan);
  241. chan->client_count--;
  242. }
  243. }
  244. mutex_unlock(&dma_list_mutex);
  245. }
  246. /**
  247. * dma_async_client_register - register a &dma_client
  248. * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
  249. */
  250. void dma_async_client_register(struct dma_client *client)
  251. {
  252. mutex_lock(&dma_list_mutex);
  253. list_add_tail(&client->global_node, &dma_client_list);
  254. mutex_unlock(&dma_list_mutex);
  255. }
  256. EXPORT_SYMBOL(dma_async_client_register);
  257. /**
  258. * dma_async_client_unregister - unregister a client and free the &dma_client
  259. * @client: &dma_client to free
  260. *
  261. * Force frees any allocated DMA channels, frees the &dma_client memory
  262. */
  263. void dma_async_client_unregister(struct dma_client *client)
  264. {
  265. struct dma_device *device;
  266. struct dma_chan *chan;
  267. enum dma_state_client ack;
  268. if (!client)
  269. return;
  270. mutex_lock(&dma_list_mutex);
  271. /* free all channels the client is holding */
  272. list_for_each_entry(device, &dma_device_list, global_node)
  273. list_for_each_entry(chan, &device->channels, device_node) {
  274. ack = client->event_callback(client, chan,
  275. DMA_RESOURCE_REMOVED);
  276. if (ack == DMA_ACK) {
  277. dma_chan_put(chan);
  278. chan->client_count--;
  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_INTERRUPT, 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_is_tx_complete);
  322. BUG_ON(!device->device_issue_pending);
  323. BUG_ON(!device->dev);
  324. init_completion(&device->done);
  325. kref_init(&device->refcount);
  326. device->dev_id = id++;
  327. /* represent channels in sysfs. Probably want devs too */
  328. list_for_each_entry(chan, &device->channels, device_node) {
  329. chan->local = alloc_percpu(typeof(*chan->local));
  330. if (chan->local == NULL)
  331. continue;
  332. chan->chan_id = chancnt++;
  333. chan->dev.class = &dma_devclass;
  334. chan->dev.parent = device->dev;
  335. snprintf(chan->dev.bus_id, BUS_ID_SIZE, "dma%dchan%d",
  336. device->dev_id, chan->chan_id);
  337. rc = device_register(&chan->dev);
  338. if (rc) {
  339. chancnt--;
  340. free_percpu(chan->local);
  341. chan->local = NULL;
  342. goto err_out;
  343. }
  344. /* One for the channel, one of the class device */
  345. kref_get(&device->refcount);
  346. kref_get(&device->refcount);
  347. kref_init(&chan->refcount);
  348. chan->client_count = 0;
  349. chan->slow_ref = 0;
  350. INIT_RCU_HEAD(&chan->rcu);
  351. }
  352. mutex_lock(&dma_list_mutex);
  353. list_add_tail(&device->global_node, &dma_device_list);
  354. mutex_unlock(&dma_list_mutex);
  355. dma_clients_notify_available();
  356. return 0;
  357. err_out:
  358. list_for_each_entry(chan, &device->channels, device_node) {
  359. if (chan->local == NULL)
  360. continue;
  361. kref_put(&device->refcount, dma_async_device_cleanup);
  362. device_unregister(&chan->dev);
  363. chancnt--;
  364. free_percpu(chan->local);
  365. }
  366. return rc;
  367. }
  368. EXPORT_SYMBOL(dma_async_device_register);
  369. /**
  370. * dma_async_device_cleanup - function called when all references are released
  371. * @kref: kernel reference object
  372. */
  373. static void dma_async_device_cleanup(struct kref *kref)
  374. {
  375. struct dma_device *device;
  376. device = container_of(kref, struct dma_device, refcount);
  377. complete(&device->done);
  378. }
  379. /**
  380. * dma_async_device_unregister - unregisters DMA devices
  381. * @device: &dma_device
  382. */
  383. void dma_async_device_unregister(struct dma_device *device)
  384. {
  385. struct dma_chan *chan;
  386. mutex_lock(&dma_list_mutex);
  387. list_del(&device->global_node);
  388. mutex_unlock(&dma_list_mutex);
  389. list_for_each_entry(chan, &device->channels, device_node) {
  390. dma_clients_notify_removed(chan);
  391. device_unregister(&chan->dev);
  392. dma_chan_release(chan);
  393. }
  394. kref_put(&device->refcount, dma_async_device_cleanup);
  395. wait_for_completion(&device->done);
  396. }
  397. EXPORT_SYMBOL(dma_async_device_unregister);
  398. /**
  399. * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
  400. * @chan: DMA channel to offload copy to
  401. * @dest: destination address (virtual)
  402. * @src: source address (virtual)
  403. * @len: length
  404. *
  405. * Both @dest and @src must be mappable to a bus address according to the
  406. * DMA mapping API rules for streaming mappings.
  407. * Both @dest and @src must stay memory resident (kernel memory or locked
  408. * user space pages).
  409. */
  410. dma_cookie_t
  411. dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
  412. void *src, size_t len)
  413. {
  414. struct dma_device *dev = chan->device;
  415. struct dma_async_tx_descriptor *tx;
  416. dma_addr_t dma_dest, dma_src;
  417. dma_cookie_t cookie;
  418. int cpu;
  419. dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
  420. dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
  421. tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
  422. DMA_CTRL_ACK);
  423. if (!tx) {
  424. dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
  425. dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
  426. return -ENOMEM;
  427. }
  428. tx->callback = NULL;
  429. cookie = tx->tx_submit(tx);
  430. cpu = get_cpu();
  431. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  432. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  433. put_cpu();
  434. return cookie;
  435. }
  436. EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
  437. /**
  438. * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
  439. * @chan: DMA channel to offload copy to
  440. * @page: destination page
  441. * @offset: offset in page to copy to
  442. * @kdata: source address (virtual)
  443. * @len: length
  444. *
  445. * Both @page/@offset and @kdata must be mappable to a bus address according
  446. * to the DMA mapping API rules for streaming mappings.
  447. * Both @page/@offset and @kdata must stay memory resident (kernel memory or
  448. * locked user space pages)
  449. */
  450. dma_cookie_t
  451. dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
  452. unsigned int offset, void *kdata, size_t len)
  453. {
  454. struct dma_device *dev = chan->device;
  455. struct dma_async_tx_descriptor *tx;
  456. dma_addr_t dma_dest, dma_src;
  457. dma_cookie_t cookie;
  458. int cpu;
  459. dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
  460. dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
  461. tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
  462. DMA_CTRL_ACK);
  463. if (!tx) {
  464. dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
  465. dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
  466. return -ENOMEM;
  467. }
  468. tx->callback = NULL;
  469. cookie = tx->tx_submit(tx);
  470. cpu = get_cpu();
  471. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  472. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  473. put_cpu();
  474. return cookie;
  475. }
  476. EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
  477. /**
  478. * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
  479. * @chan: DMA channel to offload copy to
  480. * @dest_pg: destination page
  481. * @dest_off: offset in page to copy to
  482. * @src_pg: source page
  483. * @src_off: offset in page to copy from
  484. * @len: length
  485. *
  486. * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
  487. * address according to the DMA mapping API rules for streaming mappings.
  488. * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
  489. * (kernel memory or locked user space pages).
  490. */
  491. dma_cookie_t
  492. dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
  493. unsigned int dest_off, struct page *src_pg, unsigned int src_off,
  494. size_t len)
  495. {
  496. struct dma_device *dev = chan->device;
  497. struct dma_async_tx_descriptor *tx;
  498. dma_addr_t dma_dest, dma_src;
  499. dma_cookie_t cookie;
  500. int cpu;
  501. dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
  502. dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
  503. DMA_FROM_DEVICE);
  504. tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
  505. DMA_CTRL_ACK);
  506. if (!tx) {
  507. dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
  508. dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
  509. return -ENOMEM;
  510. }
  511. tx->callback = NULL;
  512. cookie = tx->tx_submit(tx);
  513. cpu = get_cpu();
  514. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  515. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  516. put_cpu();
  517. return cookie;
  518. }
  519. EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
  520. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  521. struct dma_chan *chan)
  522. {
  523. tx->chan = chan;
  524. spin_lock_init(&tx->lock);
  525. }
  526. EXPORT_SYMBOL(dma_async_tx_descriptor_init);
  527. static int __init dma_bus_init(void)
  528. {
  529. mutex_init(&dma_list_mutex);
  530. return class_register(&dma_devclass);
  531. }
  532. subsys_initcall(dma_bus_init);