mmc: tmio: fix a deadlock
[pandora-kernel.git] / drivers / mmc / host / tmio_mmc_dma.c
1 /*
2  * linux/drivers/mmc/tmio_mmc_dma.c
3  *
4  * Copyright (C) 2010-2011 Guennadi Liakhovetski
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * DMA function for TMIO MMC implementations
11  */
12
13 #include <linux/device.h>
14 #include <linux/dmaengine.h>
15 #include <linux/mfd/tmio.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/tmio.h>
18 #include <linux/pagemap.h>
19 #include <linux/scatterlist.h>
20
21 #include "tmio_mmc.h"
22
23 #define TMIO_MMC_MIN_DMA_LEN 8
24
25 void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
26 {
27         if (!host->chan_tx || !host->chan_rx)
28                 return;
29
30 #if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
31         /* Switch DMA mode on or off - SuperH specific? */
32         sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
33 #endif
34 }
35
36 static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
37 {
38         struct scatterlist *sg = host->sg_ptr, *sg_tmp;
39         struct dma_async_tx_descriptor *desc = NULL;
40         struct dma_chan *chan = host->chan_rx;
41         struct tmio_mmc_data *pdata = host->pdata;
42         dma_cookie_t cookie;
43         int ret, i;
44         bool aligned = true, multiple = true;
45         unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
46
47         for_each_sg(sg, sg_tmp, host->sg_len, i) {
48                 if (sg_tmp->offset & align)
49                         aligned = false;
50                 if (sg_tmp->length & align) {
51                         multiple = false;
52                         break;
53                 }
54         }
55
56         if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
57                           (align & PAGE_MASK))) || !multiple) {
58                 ret = -EINVAL;
59                 goto pio;
60         }
61
62         if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
63                 host->force_pio = true;
64                 return;
65         }
66
67         tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
68
69         /* The only sg element can be unaligned, use our bounce buffer then */
70         if (!aligned) {
71                 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
72                 host->sg_ptr = &host->bounce_sg;
73                 sg = host->sg_ptr;
74         }
75
76         ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
77         if (ret > 0)
78                 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
79                         DMA_FROM_DEVICE, DMA_CTRL_ACK);
80
81         if (desc) {
82                 cookie = dmaengine_submit(desc);
83                 if (cookie < 0) {
84                         desc = NULL;
85                         ret = cookie;
86                 }
87         }
88         dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
89                 __func__, host->sg_len, ret, cookie, host->mrq);
90
91 pio:
92         if (!desc) {
93                 /* DMA failed, fall back to PIO */
94                 if (ret >= 0)
95                         ret = -EIO;
96                 host->chan_rx = NULL;
97                 dma_release_channel(chan);
98                 /* Free the Tx channel too */
99                 chan = host->chan_tx;
100                 if (chan) {
101                         host->chan_tx = NULL;
102                         dma_release_channel(chan);
103                 }
104                 dev_warn(&host->pdev->dev,
105                          "DMA failed: %d, falling back to PIO\n", ret);
106                 tmio_mmc_enable_dma(host, false);
107         }
108
109         dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
110                 desc, cookie, host->sg_len);
111 }
112
113 static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
114 {
115         struct scatterlist *sg = host->sg_ptr, *sg_tmp;
116         struct dma_async_tx_descriptor *desc = NULL;
117         struct dma_chan *chan = host->chan_tx;
118         struct tmio_mmc_data *pdata = host->pdata;
119         dma_cookie_t cookie;
120         int ret, i;
121         bool aligned = true, multiple = true;
122         unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
123
124         for_each_sg(sg, sg_tmp, host->sg_len, i) {
125                 if (sg_tmp->offset & align)
126                         aligned = false;
127                 if (sg_tmp->length & align) {
128                         multiple = false;
129                         break;
130                 }
131         }
132
133         if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
134                           (align & PAGE_MASK))) || !multiple) {
135                 ret = -EINVAL;
136                 goto pio;
137         }
138
139         if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
140                 host->force_pio = true;
141                 return;
142         }
143
144         tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
145
146         /* The only sg element can be unaligned, use our bounce buffer then */
147         if (!aligned) {
148                 unsigned long flags;
149                 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
150                 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
151                 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
152                 tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
153                 host->sg_ptr = &host->bounce_sg;
154                 sg = host->sg_ptr;
155         }
156
157         ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
158         if (ret > 0)
159                 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
160                         DMA_TO_DEVICE, DMA_CTRL_ACK);
161
162         if (desc) {
163                 cookie = dmaengine_submit(desc);
164                 if (cookie < 0) {
165                         desc = NULL;
166                         ret = cookie;
167                 }
168         }
169         dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
170                 __func__, host->sg_len, ret, cookie, host->mrq);
171
172 pio:
173         if (!desc) {
174                 /* DMA failed, fall back to PIO */
175                 if (ret >= 0)
176                         ret = -EIO;
177                 host->chan_tx = NULL;
178                 dma_release_channel(chan);
179                 /* Free the Rx channel too */
180                 chan = host->chan_rx;
181                 if (chan) {
182                         host->chan_rx = NULL;
183                         dma_release_channel(chan);
184                 }
185                 dev_warn(&host->pdev->dev,
186                          "DMA failed: %d, falling back to PIO\n", ret);
187                 tmio_mmc_enable_dma(host, false);
188         }
189
190         dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
191                 desc, cookie);
192 }
193
194 void tmio_mmc_start_dma(struct tmio_mmc_host *host,
195                                struct mmc_data *data)
196 {
197         if (data->flags & MMC_DATA_READ) {
198                 if (host->chan_rx)
199                         tmio_mmc_start_dma_rx(host);
200         } else {
201                 if (host->chan_tx)
202                         tmio_mmc_start_dma_tx(host);
203         }
204 }
205
206 static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
207 {
208         struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
209         struct dma_chan *chan = NULL;
210
211         spin_lock_irq(&host->lock);
212
213         if (host && host->data) {
214                 if (host->data->flags & MMC_DATA_READ)
215                         chan = host->chan_rx;
216                 else
217                         chan = host->chan_tx;
218         }
219
220         spin_unlock_irq(&host->lock);
221
222         tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
223
224         if (chan)
225                 dma_async_issue_pending(chan);
226 }
227
228 static void tmio_mmc_tasklet_fn(unsigned long arg)
229 {
230         struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
231
232         spin_lock_irq(&host->lock);
233
234         if (!host->data)
235                 goto out;
236
237         if (host->data->flags & MMC_DATA_READ)
238                 dma_unmap_sg(host->chan_rx->device->dev,
239                              host->sg_ptr, host->sg_len,
240                              DMA_FROM_DEVICE);
241         else
242                 dma_unmap_sg(host->chan_tx->device->dev,
243                              host->sg_ptr, host->sg_len,
244                              DMA_TO_DEVICE);
245
246         tmio_mmc_do_data_irq(host);
247 out:
248         spin_unlock_irq(&host->lock);
249 }
250
251 /* It might be necessary to make filter MFD specific */
252 static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
253 {
254         dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
255         chan->private = arg;
256         return true;
257 }
258
259 void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
260 {
261         /* We can only either use DMA for both Tx and Rx or not use it at all */
262         if (!pdata->dma)
263                 return;
264
265         if (!host->chan_tx && !host->chan_rx) {
266                 dma_cap_mask_t mask;
267
268                 dma_cap_zero(mask);
269                 dma_cap_set(DMA_SLAVE, mask);
270
271                 host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
272                                                     pdata->dma->chan_priv_tx);
273                 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
274                         host->chan_tx);
275
276                 if (!host->chan_tx)
277                         return;
278
279                 host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
280                                                     pdata->dma->chan_priv_rx);
281                 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
282                         host->chan_rx);
283
284                 if (!host->chan_rx)
285                         goto ereqrx;
286
287                 host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
288                 if (!host->bounce_buf)
289                         goto ebouncebuf;
290
291                 tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
292                 tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
293         }
294
295         tmio_mmc_enable_dma(host, true);
296
297         return;
298
299 ebouncebuf:
300         dma_release_channel(host->chan_rx);
301         host->chan_rx = NULL;
302 ereqrx:
303         dma_release_channel(host->chan_tx);
304         host->chan_tx = NULL;
305 }
306
307 void tmio_mmc_release_dma(struct tmio_mmc_host *host)
308 {
309         if (host->chan_tx) {
310                 struct dma_chan *chan = host->chan_tx;
311                 host->chan_tx = NULL;
312                 dma_release_channel(chan);
313         }
314         if (host->chan_rx) {
315                 struct dma_chan *chan = host->chan_rx;
316                 host->chan_rx = NULL;
317                 dma_release_channel(chan);
318         }
319         if (host->bounce_buf) {
320                 free_pages((unsigned long)host->bounce_buf, 0);
321                 host->bounce_buf = NULL;
322         }
323 }