coh901318_lli.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * driver/dma/coh901318_lli.c
  3. *
  4. * Copyright (C) 2007-2009 ST-Ericsson
  5. * License terms: GNU General Public License (GPL) version 2
  6. * Support functions for handling lli for dma
  7. * Author: Per Friden <per.friden@stericsson.com>
  8. */
  9. #include <linux/dma-mapping.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/dmapool.h>
  12. #include <linux/memory.h>
  13. #include <mach/coh901318.h>
  14. #include "coh901318_lli.h"
  15. #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_U300_DEBUG))
  16. #define DEBUGFS_POOL_COUNTER_RESET(pool) (pool->debugfs_pool_counter = 0)
  17. #define DEBUGFS_POOL_COUNTER_ADD(pool, add) (pool->debugfs_pool_counter += add)
  18. #else
  19. #define DEBUGFS_POOL_COUNTER_RESET(pool)
  20. #define DEBUGFS_POOL_COUNTER_ADD(pool, add)
  21. #endif
  22. static struct coh901318_lli *
  23. coh901318_lli_next(struct coh901318_lli *data)
  24. {
  25. if (data == NULL || data->link_addr == 0)
  26. return NULL;
  27. return (struct coh901318_lli *) data->virt_link_addr;
  28. }
  29. int coh901318_pool_create(struct coh901318_pool *pool,
  30. struct device *dev,
  31. size_t size, size_t align)
  32. {
  33. spin_lock_init(&pool->lock);
  34. pool->dev = dev;
  35. pool->dmapool = dma_pool_create("lli_pool", dev, size, align, 0);
  36. DEBUGFS_POOL_COUNTER_RESET(pool);
  37. return 0;
  38. }
  39. int coh901318_pool_destroy(struct coh901318_pool *pool)
  40. {
  41. dma_pool_destroy(pool->dmapool);
  42. return 0;
  43. }
  44. struct coh901318_lli *
  45. coh901318_lli_alloc(struct coh901318_pool *pool, unsigned int len)
  46. {
  47. int i;
  48. struct coh901318_lli *head;
  49. struct coh901318_lli *lli;
  50. struct coh901318_lli *lli_prev;
  51. dma_addr_t phy;
  52. if (len == 0)
  53. goto err;
  54. spin_lock(&pool->lock);
  55. head = dma_pool_alloc(pool->dmapool, GFP_NOWAIT, &phy);
  56. if (head == NULL)
  57. goto err;
  58. DEBUGFS_POOL_COUNTER_ADD(pool, 1);
  59. lli = head;
  60. lli->phy_this = phy;
  61. for (i = 1; i < len; i++) {
  62. lli_prev = lli;
  63. lli = dma_pool_alloc(pool->dmapool, GFP_NOWAIT, &phy);
  64. if (lli == NULL)
  65. goto err_clean_up;
  66. DEBUGFS_POOL_COUNTER_ADD(pool, 1);
  67. lli->phy_this = phy;
  68. lli_prev->link_addr = phy;
  69. lli_prev->virt_link_addr = lli;
  70. }
  71. lli->link_addr = 0x00000000U;
  72. spin_unlock(&pool->lock);
  73. return head;
  74. err:
  75. spin_unlock(&pool->lock);
  76. return NULL;
  77. err_clean_up:
  78. lli_prev->link_addr = 0x00000000U;
  79. spin_unlock(&pool->lock);
  80. coh901318_lli_free(pool, &head);
  81. return NULL;
  82. }
  83. void coh901318_lli_free(struct coh901318_pool *pool,
  84. struct coh901318_lli **lli)
  85. {
  86. struct coh901318_lli *l;
  87. struct coh901318_lli *next;
  88. if (lli == NULL)
  89. return;
  90. l = *lli;
  91. if (l == NULL)
  92. return;
  93. spin_lock(&pool->lock);
  94. while (l->link_addr) {
  95. next = l->virt_link_addr;
  96. dma_pool_free(pool->dmapool, l, l->phy_this);
  97. DEBUGFS_POOL_COUNTER_ADD(pool, -1);
  98. l = next;
  99. }
  100. dma_pool_free(pool->dmapool, l, l->phy_this);
  101. DEBUGFS_POOL_COUNTER_ADD(pool, -1);
  102. spin_unlock(&pool->lock);
  103. *lli = NULL;
  104. }
  105. int
  106. coh901318_lli_fill_memcpy(struct coh901318_pool *pool,
  107. struct coh901318_lli *lli,
  108. dma_addr_t source, unsigned int size,
  109. dma_addr_t destination, u32 ctrl_chained,
  110. u32 ctrl_eom)
  111. {
  112. int s = size;
  113. dma_addr_t src = source;
  114. dma_addr_t dst = destination;
  115. lli->src_addr = src;
  116. lli->dst_addr = dst;
  117. while (lli->link_addr) {
  118. lli->control = ctrl_chained | MAX_DMA_PACKET_SIZE;
  119. lli->src_addr = src;
  120. lli->dst_addr = dst;
  121. s -= MAX_DMA_PACKET_SIZE;
  122. lli = coh901318_lli_next(lli);
  123. src += MAX_DMA_PACKET_SIZE;
  124. dst += MAX_DMA_PACKET_SIZE;
  125. }
  126. lli->control = ctrl_eom | s;
  127. lli->src_addr = src;
  128. lli->dst_addr = dst;
  129. /* One irq per single transfer */
  130. return 1;
  131. }
  132. int
  133. coh901318_lli_fill_single(struct coh901318_pool *pool,
  134. struct coh901318_lli *lli,
  135. dma_addr_t buf, unsigned int size,
  136. dma_addr_t dev_addr, u32 ctrl_chained, u32 ctrl_eom,
  137. enum dma_data_direction dir)
  138. {
  139. int s = size;
  140. dma_addr_t src;
  141. dma_addr_t dst;
  142. if (dir == DMA_TO_DEVICE) {
  143. src = buf;
  144. dst = dev_addr;
  145. } else if (dir == DMA_FROM_DEVICE) {
  146. src = dev_addr;
  147. dst = buf;
  148. } else {
  149. return -EINVAL;
  150. }
  151. while (lli->link_addr) {
  152. size_t block_size = MAX_DMA_PACKET_SIZE;
  153. lli->control = ctrl_chained | MAX_DMA_PACKET_SIZE;
  154. /* If we are on the next-to-final block and there will
  155. * be less than half a DMA packet left for the last
  156. * block, then we want to make this block a little
  157. * smaller to balance the sizes. This is meant to
  158. * avoid too small transfers if the buffer size is
  159. * (MAX_DMA_PACKET_SIZE*N + 1) */
  160. if (s < (MAX_DMA_PACKET_SIZE + MAX_DMA_PACKET_SIZE/2))
  161. block_size = MAX_DMA_PACKET_SIZE/2;
  162. s -= block_size;
  163. lli->src_addr = src;
  164. lli->dst_addr = dst;
  165. lli = coh901318_lli_next(lli);
  166. if (dir == DMA_TO_DEVICE)
  167. src += block_size;
  168. else if (dir == DMA_FROM_DEVICE)
  169. dst += block_size;
  170. }
  171. lli->control = ctrl_eom | s;
  172. lli->src_addr = src;
  173. lli->dst_addr = dst;
  174. /* One irq per single transfer */
  175. return 1;
  176. }
  177. int
  178. coh901318_lli_fill_sg(struct coh901318_pool *pool,
  179. struct coh901318_lli *lli,
  180. struct scatterlist *sgl, unsigned int nents,
  181. dma_addr_t dev_addr, u32 ctrl_chained, u32 ctrl,
  182. u32 ctrl_last,
  183. enum dma_data_direction dir, u32 ctrl_irq_mask)
  184. {
  185. int i;
  186. struct scatterlist *sg;
  187. u32 ctrl_sg;
  188. dma_addr_t src = 0;
  189. dma_addr_t dst = 0;
  190. int nbr_of_irq = 0;
  191. u32 bytes_to_transfer;
  192. u32 elem_size;
  193. if (lli == NULL)
  194. goto err;
  195. spin_lock(&pool->lock);
  196. if (dir == DMA_TO_DEVICE)
  197. dst = dev_addr;
  198. else if (dir == DMA_FROM_DEVICE)
  199. src = dev_addr;
  200. else
  201. goto err;
  202. for_each_sg(sgl, sg, nents, i) {
  203. if (sg_is_chain(sg)) {
  204. /* sg continues to the next sg-element don't
  205. * send ctrl_finish until the last
  206. * sg-element in the chain
  207. */
  208. ctrl_sg = ctrl_chained;
  209. } else if (i == nents - 1)
  210. ctrl_sg = ctrl_last;
  211. else
  212. ctrl_sg = ctrl ? ctrl : ctrl_last;
  213. if ((ctrl_sg & ctrl_irq_mask))
  214. nbr_of_irq++;
  215. if (dir == DMA_TO_DEVICE)
  216. /* increment source address */
  217. src = sg_dma_address(sg);
  218. else
  219. /* increment destination address */
  220. dst = sg_dma_address(sg);
  221. bytes_to_transfer = sg_dma_len(sg);
  222. while (bytes_to_transfer) {
  223. u32 val;
  224. if (bytes_to_transfer > MAX_DMA_PACKET_SIZE) {
  225. elem_size = MAX_DMA_PACKET_SIZE;
  226. val = ctrl_chained;
  227. } else {
  228. elem_size = bytes_to_transfer;
  229. val = ctrl_sg;
  230. }
  231. lli->control = val | elem_size;
  232. lli->src_addr = src;
  233. lli->dst_addr = dst;
  234. if (dir == DMA_FROM_DEVICE)
  235. dst += elem_size;
  236. else
  237. src += elem_size;
  238. BUG_ON(lli->link_addr & 3);
  239. bytes_to_transfer -= elem_size;
  240. lli = coh901318_lli_next(lli);
  241. }
  242. }
  243. spin_unlock(&pool->lock);
  244. /* There can be many IRQs per sg transfer */
  245. return nbr_of_irq;
  246. err:
  247. spin_unlock(&pool->lock);
  248. return -EINVAL;
  249. }