0395-dmaengine-bcm2835-move-controlblock-chain-generation.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. From 79ef7f167c859ce2a11b1bb69fbfca786504d6d7 Mon Sep 17 00:00:00 2001
  2. From: Martin Sperl <kernel@martin.sperl.org>
  3. Date: Wed, 16 Mar 2016 12:24:59 -0700
  4. Subject: [PATCH] dmaengine: bcm2835: move controlblock chain generation into
  5. separate method
  6. In preparation of adding slave_sg functionality this patch moves the
  7. generation/allocation of bcm2835_desc and the building of
  8. the corresponding DMA-control-block chain from bcm2835_dma_prep_dma_cyclic
  9. into the newly created method bcm2835_dma_create_cb_chain.
  10. Signed-off-by: Martin Sperl <kernel@martin.sperl.org>
  11. Reviewed-by: Eric Anholt <eric@anholt.net>
  12. Signed-off-by: Eric Anholt <eric@anholt.net>
  13. Signed-off-by: Vinod Koul <vinod.koul@intel.com>
  14. ---
  15. drivers/dma/bcm2835-dma.c | 294 +++++++++++++++++++++++++++++++---------------
  16. 1 file changed, 198 insertions(+), 96 deletions(-)
  17. --- a/drivers/dma/bcm2835-dma.c
  18. +++ b/drivers/dma/bcm2835-dma.c
  19. @@ -88,12 +88,12 @@ struct bcm2835_desc {
  20. struct virt_dma_desc vd;
  21. enum dma_transfer_direction dir;
  22. - struct bcm2835_cb_entry *cb_list;
  23. -
  24. unsigned int frames;
  25. size_t size;
  26. bool cyclic;
  27. +
  28. + struct bcm2835_cb_entry cb_list[];
  29. };
  30. #define BCM2835_DMA_CS 0x00
  31. @@ -169,6 +169,13 @@ struct bcm2835_desc {
  32. #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
  33. #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
  34. +/* how many frames of max_len size do we need to transfer len bytes */
  35. +static inline size_t bcm2835_dma_frames_for_length(size_t len,
  36. + size_t max_len)
  37. +{
  38. + return DIV_ROUND_UP(len, max_len);
  39. +}
  40. +
  41. static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
  42. {
  43. return container_of(d, struct bcm2835_dmadev, ddev);
  44. @@ -185,19 +192,161 @@ static inline struct bcm2835_desc *to_bc
  45. return container_of(t, struct bcm2835_desc, vd.tx);
  46. }
  47. -static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
  48. +static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc)
  49. {
  50. - struct bcm2835_desc *desc = container_of(vd, struct bcm2835_desc, vd);
  51. - int i;
  52. + size_t i;
  53. for (i = 0; i < desc->frames; i++)
  54. dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
  55. desc->cb_list[i].paddr);
  56. - kfree(desc->cb_list);
  57. kfree(desc);
  58. }
  59. +static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
  60. +{
  61. + bcm2835_dma_free_cb_chain(
  62. + container_of(vd, struct bcm2835_desc, vd));
  63. +}
  64. +
  65. +static void bcm2835_dma_create_cb_set_length(
  66. + struct bcm2835_chan *chan,
  67. + struct bcm2835_dma_cb *control_block,
  68. + size_t len,
  69. + size_t period_len,
  70. + size_t *total_len,
  71. + u32 finalextrainfo)
  72. +{
  73. + /* set the length */
  74. + control_block->length = len;
  75. +
  76. + /* finished if we have no period_length */
  77. + if (!period_len)
  78. + return;
  79. +
  80. + /*
  81. + * period_len means: that we need to generate
  82. + * transfers that are terminating at every
  83. + * multiple of period_len - this is typically
  84. + * used to set the interrupt flag in info
  85. + * which is required during cyclic transfers
  86. + */
  87. +
  88. + /* have we filled in period_length yet? */
  89. + if (*total_len + control_block->length < period_len)
  90. + return;
  91. +
  92. + /* calculate the length that remains to reach period_length */
  93. + control_block->length = period_len - *total_len;
  94. +
  95. + /* reset total_length for next period */
  96. + *total_len = 0;
  97. +
  98. + /* add extrainfo bits in info */
  99. + control_block->info |= finalextrainfo;
  100. +}
  101. +
  102. +/**
  103. + * bcm2835_dma_create_cb_chain - create a control block and fills data in
  104. + *
  105. + * @chan: the @dma_chan for which we run this
  106. + * @direction: the direction in which we transfer
  107. + * @cyclic: it is a cyclic transfer
  108. + * @info: the default info bits to apply per controlblock
  109. + * @frames: number of controlblocks to allocate
  110. + * @src: the src address to assign (if the S_INC bit is set
  111. + * in @info, then it gets incremented)
  112. + * @dst: the dst address to assign (if the D_INC bit is set
  113. + * in @info, then it gets incremented)
  114. + * @buf_len: the full buffer length (may also be 0)
  115. + * @period_len: the period length when to apply @finalextrainfo
  116. + * in addition to the last transfer
  117. + * this will also break some control-blocks early
  118. + * @finalextrainfo: additional bits in last controlblock
  119. + * (or when period_len is reached in case of cyclic)
  120. + * @gfp: the GFP flag to use for allocation
  121. + */
  122. +static struct bcm2835_desc *bcm2835_dma_create_cb_chain(
  123. + struct dma_chan *chan, enum dma_transfer_direction direction,
  124. + bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
  125. + dma_addr_t src, dma_addr_t dst, size_t buf_len,
  126. + size_t period_len, gfp_t gfp)
  127. +{
  128. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  129. + size_t len = buf_len, total_len;
  130. + size_t frame;
  131. + struct bcm2835_desc *d;
  132. + struct bcm2835_cb_entry *cb_entry;
  133. + struct bcm2835_dma_cb *control_block;
  134. +
  135. + /* allocate and setup the descriptor. */
  136. + d = kzalloc(sizeof(*d) + frames * sizeof(struct bcm2835_cb_entry),
  137. + gfp);
  138. + if (!d)
  139. + return NULL;
  140. +
  141. + d->c = c;
  142. + d->dir = direction;
  143. + d->cyclic = cyclic;
  144. +
  145. + /*
  146. + * Iterate over all frames, create a control block
  147. + * for each frame and link them together.
  148. + */
  149. + for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) {
  150. + cb_entry = &d->cb_list[frame];
  151. + cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp,
  152. + &cb_entry->paddr);
  153. + if (!cb_entry->cb)
  154. + goto error_cb;
  155. +
  156. + /* fill in the control block */
  157. + control_block = cb_entry->cb;
  158. + control_block->info = info;
  159. + control_block->src = src;
  160. + control_block->dst = dst;
  161. + control_block->stride = 0;
  162. + control_block->next = 0;
  163. + /* set up length in control_block if requested */
  164. + if (buf_len) {
  165. + /* calculate length honoring period_length */
  166. + bcm2835_dma_create_cb_set_length(
  167. + c, control_block,
  168. + len, period_len, &total_len,
  169. + cyclic ? finalextrainfo : 0);
  170. +
  171. + /* calculate new remaining length */
  172. + len -= control_block->length;
  173. + }
  174. +
  175. + /* link this the last controlblock */
  176. + if (frame)
  177. + d->cb_list[frame - 1].cb->next = cb_entry->paddr;
  178. +
  179. + /* update src and dst and length */
  180. + if (src && (info & BCM2835_DMA_S_INC))
  181. + src += control_block->length;
  182. + if (dst && (info & BCM2835_DMA_D_INC))
  183. + dst += control_block->length;
  184. +
  185. + /* Length of total transfer */
  186. + d->size += control_block->length;
  187. + }
  188. +
  189. + /* the last frame requires extra flags */
  190. + d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
  191. +
  192. + /* detect a size missmatch */
  193. + if (buf_len && (d->size != buf_len))
  194. + goto error_cb;
  195. +
  196. + return d;
  197. +error_cb:
  198. + bcm2835_dma_free_cb_chain(d);
  199. +
  200. + return NULL;
  201. +}
  202. +
  203. static int bcm2835_dma_abort(void __iomem *chan_base)
  204. {
  205. unsigned long cs;
  206. @@ -391,12 +540,11 @@ static struct dma_async_tx_descriptor *b
  207. unsigned long flags)
  208. {
  209. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  210. - enum dma_slave_buswidth dev_width;
  211. struct bcm2835_desc *d;
  212. - dma_addr_t dev_addr;
  213. - unsigned int es, sync_type;
  214. - unsigned int frame;
  215. - int i;
  216. + dma_addr_t src, dst;
  217. + u32 info = BCM2835_DMA_WAIT_RESP;
  218. + u32 extra = BCM2835_DMA_INT_EN;
  219. + size_t frames;
  220. /* Grab configuration */
  221. if (!is_slave_direction(direction)) {
  222. @@ -404,104 +552,58 @@ static struct dma_async_tx_descriptor *b
  223. return NULL;
  224. }
  225. - if (direction == DMA_DEV_TO_MEM) {
  226. - dev_addr = c->cfg.src_addr;
  227. - dev_width = c->cfg.src_addr_width;
  228. - sync_type = BCM2835_DMA_S_DREQ;
  229. - } else {
  230. - dev_addr = c->cfg.dst_addr;
  231. - dev_width = c->cfg.dst_addr_width;
  232. - sync_type = BCM2835_DMA_D_DREQ;
  233. - }
  234. -
  235. - /* Bus width translates to the element size (ES) */
  236. - switch (dev_width) {
  237. - case DMA_SLAVE_BUSWIDTH_4_BYTES:
  238. - es = BCM2835_DMA_DATA_TYPE_S32;
  239. - break;
  240. - default:
  241. + if (!buf_len) {
  242. + dev_err(chan->device->dev,
  243. + "%s: bad buffer length (= 0)\n", __func__);
  244. return NULL;
  245. }
  246. - /* Now allocate and setup the descriptor. */
  247. - d = kzalloc(sizeof(*d), GFP_NOWAIT);
  248. - if (!d)
  249. - return NULL;
  250. -
  251. - d->c = c;
  252. - d->dir = direction;
  253. - d->frames = buf_len / period_len;
  254. - d->cyclic = true;
  255. + /*
  256. + * warn if buf_len is not a multiple of period_len - this may leed
  257. + * to unexpected latencies for interrupts and thus audiable clicks
  258. + */
  259. + if (buf_len % period_len)
  260. + dev_warn_once(chan->device->dev,
  261. + "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
  262. + __func__, buf_len, period_len);
  263. +
  264. + /* Setup DREQ channel */
  265. + if (c->dreq != 0)
  266. + info |= BCM2835_DMA_PER_MAP(c->dreq);
  267. - d->cb_list = kcalloc(d->frames, sizeof(*d->cb_list), GFP_KERNEL);
  268. - if (!d->cb_list) {
  269. - kfree(d);
  270. - return NULL;
  271. + if (direction == DMA_DEV_TO_MEM) {
  272. + if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  273. + return NULL;
  274. + src = c->cfg.src_addr;
  275. + dst = buf_addr;
  276. + info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
  277. + } else {
  278. + if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  279. + return NULL;
  280. + dst = c->cfg.dst_addr;
  281. + src = buf_addr;
  282. + info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
  283. }
  284. - /* Allocate memory for control blocks */
  285. - for (i = 0; i < d->frames; i++) {
  286. - struct bcm2835_cb_entry *cb_entry = &d->cb_list[i];
  287. - cb_entry->cb = dma_pool_zalloc(c->cb_pool, GFP_ATOMIC,
  288. - &cb_entry->paddr);
  289. - if (!cb_entry->cb)
  290. - goto error_cb;
  291. - }
  292. + /* calculate number of frames */
  293. + frames = DIV_ROUND_UP(buf_len, period_len);
  294. /*
  295. - * Iterate over all frames, create a control block
  296. - * for each frame and link them together.
  297. + * allocate the CB chain
  298. + * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
  299. + * implementation calls prep_dma_cyclic with interrupts disabled.
  300. */
  301. - for (frame = 0; frame < d->frames; frame++) {
  302. - struct bcm2835_dma_cb *control_block = d->cb_list[frame].cb;
  303. -
  304. - /* Setup adresses */
  305. - if (d->dir == DMA_DEV_TO_MEM) {
  306. - control_block->info = BCM2835_DMA_D_INC;
  307. - control_block->src = dev_addr;
  308. - control_block->dst = buf_addr + frame * period_len;
  309. - } else {
  310. - control_block->info = BCM2835_DMA_S_INC;
  311. - control_block->src = buf_addr + frame * period_len;
  312. - control_block->dst = dev_addr;
  313. - }
  314. -
  315. - /* Enable interrupt */
  316. - control_block->info |= BCM2835_DMA_INT_EN;
  317. -
  318. - /* Setup synchronization */
  319. - if (sync_type != 0)
  320. - control_block->info |= sync_type;
  321. -
  322. - /* Setup DREQ channel */
  323. - if (c->dreq != 0)
  324. - control_block->info |=
  325. - BCM2835_DMA_PER_MAP(c->dreq);
  326. -
  327. - /* Length of a frame */
  328. - control_block->length = period_len;
  329. - d->size += control_block->length;
  330. + d = bcm2835_dma_create_cb_chain(chan, direction, true,
  331. + info, extra,
  332. + frames, src, dst, buf_len,
  333. + period_len, GFP_NOWAIT);
  334. + if (!d)
  335. + return NULL;
  336. - /*
  337. - * Next block is the next frame.
  338. - * This DMA engine driver currently only supports cyclic DMA.
  339. - * Therefore, wrap around at number of frames.
  340. - */
  341. - control_block->next = d->cb_list[((frame + 1) % d->frames)].paddr;
  342. - }
  343. + /* wrap around into a loop */
  344. + d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
  345. return vchan_tx_prep(&c->vc, &d->vd, flags);
  346. -error_cb:
  347. - i--;
  348. - for (; i >= 0; i--) {
  349. - struct bcm2835_cb_entry *cb_entry = &d->cb_list[i];
  350. -
  351. - dma_pool_free(c->cb_pool, cb_entry->cb, cb_entry->paddr);
  352. - }
  353. -
  354. - kfree(d->cb_list);
  355. - kfree(d);
  356. - return NULL;
  357. }
  358. static int bcm2835_dma_slave_config(struct dma_chan *chan,