Skip to content

Commit cb06ff1

Browse files
minchanhogregkh
authored andcommitted
ARM: PL011: Add support for Rx DMA buffer polling.
In DMA support, The received data is not pushed to tty until the DMA buffer is filled. But some megabyte rate chips such as BT expect fast response and data should be pushed immediately. In order to fix this issue, We suggest the use of the timer for polling DMA buffer. In our test, no data loss occurred at high-baudrate as compared with interrupt- driven (We tested with 3Mbps). We changes: - We add timer for polling. If we set poll_timer to 10, every 10ms, timer handler checks the residue in the dma buffer and transfer data to the tty. Also, last_residue is updated for the next polling. - poll_timeout is used to prevent the timer's system cost. If poll_timeout is set to 3000 and no data is received in 3 seconds, we inactivate poll timer and driver falls back to interrupt-driven. When data is received again in FIFO and UART irq is occurred, we switch back to DMA mode and start polling. - We use consistent DMA mappings to avoid from the frequent cache operation of the timer function for default. - pl011_dma_rx_chars is modified. the pending size is recalculated because data can be taken by polling. - the polling time is adjusted if dma rx poll is enabled but no rate is specified. Ideal polling interval to push 1 character at every interval is the reciprocal of 'baud rate / 10 line bits per character / 1000 ms per sec'. But It is very aggressive to system. Experimentally, '10000000 / baud' is suitable to receive dozens of characters. the poll rate can be specified statically by dma_rx_poll_rate of the platform data as well. Changes compared to v1: - Use of consistent DMA mappings. - Added dma_rx_poll_rate in platform data to specify the polling interval. - Added dma_rx_poll_timeout in platform data to specify the polling timeout. Changes compared to v2: - Use of consistent DMA mappings for default. - Added dma_rx_poll_enable in platform data to adjust the polling time according to the baud rate. - remove unnecessary lock from the polling function. Signed-off-by: Chanho Min <chanho.min@lge.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e302cd9 commit cb06ff1

2 files changed

Lines changed: 141 additions & 19 deletions

File tree

‎drivers/tty/serial/amba-pl011.c‎

Lines changed: 138 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* and hooked into this driver.
3030
*/
3131

32+
3233
#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
3334
#define SUPPORT_SYSRQ
3435
#endif
@@ -117,6 +118,12 @@ struct pl011_dmarx_data {
117118
struct pl011_sgbuf sgbuf_b;
118119
dma_cookie_t cookie;
119120
bool running;
121+
struct timer_list timer;
122+
unsigned int last_residue;
123+
unsigned long last_jiffies;
124+
bool auto_poll_rate;
125+
unsigned int poll_rate;
126+
unsigned int poll_timeout;
120127
};
121128

122129
struct pl011_dmatx_data {
@@ -223,25 +230,28 @@ static int pl011_fifo_to_tty(struct uart_amba_port *uap)
223230
static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
224231
enum dma_data_direction dir)
225232
{
226-
sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
233+
dma_addr_t dma_addr;
234+
235+
sg->buf = dma_alloc_coherent(chan->device->dev,
236+
PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
227237
if (!sg->buf)
228238
return -ENOMEM;
229239

230-
sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
240+
sg_init_table(&sg->sg, 1);
241+
sg_set_page(&sg->sg, phys_to_page(dma_addr),
242+
PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
243+
sg_dma_address(&sg->sg) = dma_addr;
231244

232-
if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
233-
kfree(sg->buf);
234-
return -EINVAL;
235-
}
236245
return 0;
237246
}
238247

239248
static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
240249
enum dma_data_direction dir)
241250
{
242251
if (sg->buf) {
243-
dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
244-
kfree(sg->buf);
252+
dma_free_coherent(chan->device->dev,
253+
PL011_DMA_BUFFER_SIZE, sg->buf,
254+
sg_dma_address(&sg->sg));
245255
}
246256
}
247257

@@ -300,6 +310,29 @@ static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
300310
dmaengine_slave_config(chan, &rx_conf);
301311
uap->dmarx.chan = chan;
302312

313+
if (plat->dma_rx_poll_enable) {
314+
/* Set poll rate if specified. */
315+
if (plat->dma_rx_poll_rate) {
316+
uap->dmarx.auto_poll_rate = false;
317+
uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
318+
} else {
319+
/*
320+
* 100 ms defaults to poll rate if not
321+
* specified. This will be adjusted with
322+
* the baud rate at set_termios.
323+
*/
324+
uap->dmarx.auto_poll_rate = true;
325+
uap->dmarx.poll_rate = 100;
326+
}
327+
/* 3 secs defaults poll_timeout if not specified. */
328+
if (plat->dma_rx_poll_timeout)
329+
uap->dmarx.poll_timeout =
330+
plat->dma_rx_poll_timeout;
331+
else
332+
uap->dmarx.poll_timeout = 3000;
333+
} else
334+
uap->dmarx.auto_poll_rate = false;
335+
303336
dev_info(uap->port.dev, "DMA channel RX %s\n",
304337
dma_chan_name(uap->dmarx.chan));
305338
}
@@ -701,31 +734,41 @@ static void pl011_dma_rx_chars(struct uart_amba_port *uap,
701734
struct tty_port *port = &uap->port.state->port;
702735
struct pl011_sgbuf *sgbuf = use_buf_b ?
703736
&uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
704-
struct device *dev = uap->dmarx.chan->device->dev;
705737
int dma_count = 0;
706738
u32 fifotaken = 0; /* only used for vdbg() */
707739

708-
/* Pick everything from the DMA first */
740+
struct pl011_dmarx_data *dmarx = &uap->dmarx;
741+
int dmataken = 0;
742+
743+
if (uap->dmarx.poll_rate) {
744+
/* The data can be taken by polling */
745+
dmataken = sgbuf->sg.length - dmarx->last_residue;
746+
/* Recalculate the pending size */
747+
if (pending >= dmataken)
748+
pending -= dmataken;
749+
}
750+
751+
/* Pick the remain data from the DMA */
709752
if (pending) {
710-
/* Sync in buffer */
711-
dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
712753

713754
/*
714755
* First take all chars in the DMA pipe, then look in the FIFO.
715756
* Note that tty_insert_flip_buf() tries to take as many chars
716757
* as it can.
717758
*/
718-
dma_count = tty_insert_flip_string(port, sgbuf->buf, pending);
719-
720-
/* Return buffer to device */
721-
dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
759+
dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
760+
pending);
722761

723762
uap->port.icount.rx += dma_count;
724763
if (dma_count < pending)
725764
dev_warn(uap->port.dev,
726765
"couldn't insert all characters (TTY is full?)\n");
727766
}
728767

768+
/* Reset the last_residue for Rx DMA poll */
769+
if (uap->dmarx.poll_rate)
770+
dmarx->last_residue = sgbuf->sg.length;
771+
729772
/*
730773
* Only continue with trying to read the FIFO if all DMA chars have
731774
* been taken first.
@@ -865,6 +908,57 @@ static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
865908
writew(uap->dmacr, uap->port.membase + UART011_DMACR);
866909
}
867910

911+
/*
912+
* Timer handler for Rx DMA polling.
913+
* Every polling, It checks the residue in the dma buffer and transfer
914+
* data to the tty. Also, last_residue is updated for the next polling.
915+
*/
916+
static void pl011_dma_rx_poll(unsigned long args)
917+
{
918+
struct uart_amba_port *uap = (struct uart_amba_port *)args;
919+
struct tty_port *port = &uap->port.state->port;
920+
struct pl011_dmarx_data *dmarx = &uap->dmarx;
921+
struct dma_chan *rxchan = uap->dmarx.chan;
922+
unsigned long flags = 0;
923+
unsigned int dmataken = 0;
924+
unsigned int size = 0;
925+
struct pl011_sgbuf *sgbuf;
926+
int dma_count;
927+
struct dma_tx_state state;
928+
929+
sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
930+
rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
931+
if (likely(state.residue < dmarx->last_residue)) {
932+
dmataken = sgbuf->sg.length - dmarx->last_residue;
933+
size = dmarx->last_residue - state.residue;
934+
dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
935+
size);
936+
if (dma_count == size)
937+
dmarx->last_residue = state.residue;
938+
dmarx->last_jiffies = jiffies;
939+
}
940+
tty_flip_buffer_push(port);
941+
942+
/*
943+
* If no data is received in poll_timeout, the driver will fall back
944+
* to interrupt mode. We will retrigger DMA at the first interrupt.
945+
*/
946+
if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
947+
> uap->dmarx.poll_timeout) {
948+
949+
spin_lock_irqsave(&uap->port.lock, flags);
950+
pl011_dma_rx_stop(uap);
951+
spin_unlock_irqrestore(&uap->port.lock, flags);
952+
953+
uap->dmarx.running = false;
954+
dmaengine_terminate_all(rxchan);
955+
del_timer(&uap->dmarx.timer);
956+
} else {
957+
mod_timer(&uap->dmarx.timer,
958+
jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
959+
}
960+
}
961+
868962
static void pl011_dma_startup(struct uart_amba_port *uap)
869963
{
870964
int ret;
@@ -927,6 +1021,16 @@ static void pl011_dma_startup(struct uart_amba_port *uap)
9271021
if (pl011_dma_rx_trigger_dma(uap))
9281022
dev_dbg(uap->port.dev, "could not trigger initial "
9291023
"RX DMA job, fall back to interrupt mode\n");
1024+
if (uap->dmarx.poll_rate) {
1025+
init_timer(&(uap->dmarx.timer));
1026+
uap->dmarx.timer.function = pl011_dma_rx_poll;
1027+
uap->dmarx.timer.data = (unsigned long)uap;
1028+
mod_timer(&uap->dmarx.timer,
1029+
jiffies +
1030+
msecs_to_jiffies(uap->dmarx.poll_rate));
1031+
uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1032+
uap->dmarx.last_jiffies = jiffies;
1033+
}
9301034
}
9311035
}
9321036

@@ -962,6 +1066,8 @@ static void pl011_dma_shutdown(struct uart_amba_port *uap)
9621066
/* Clean up the RX DMA */
9631067
pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
9641068
pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
1069+
if (uap->dmarx.poll_rate)
1070+
del_timer_sync(&uap->dmarx.timer);
9651071
uap->using_rx_dma = false;
9661072
}
9671073
}
@@ -976,7 +1082,6 @@ static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
9761082
return uap->using_rx_dma && uap->dmarx.running;
9771083
}
9781084

979-
9801085
#else
9811086
/* Blank functions if the DMA engine is not available */
9821087
static inline void pl011_dma_probe(struct uart_amba_port *uap)
@@ -1088,8 +1193,18 @@ static void pl011_rx_chars(struct uart_amba_port *uap)
10881193
dev_dbg(uap->port.dev, "could not trigger RX DMA job "
10891194
"fall back to interrupt mode again\n");
10901195
uap->im |= UART011_RXIM;
1091-
} else
1196+
} else {
10921197
uap->im &= ~UART011_RXIM;
1198+
/* Start Rx DMA poll */
1199+
if (uap->dmarx.poll_rate) {
1200+
uap->dmarx.last_jiffies = jiffies;
1201+
uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1202+
mod_timer(&uap->dmarx.timer,
1203+
jiffies +
1204+
msecs_to_jiffies(uap->dmarx.poll_rate));
1205+
}
1206+
}
1207+
10931208
writew(uap->im, uap->port.membase + UART011_IMSC);
10941209
}
10951210
spin_lock(&uap->port.lock);
@@ -1164,7 +1279,6 @@ static irqreturn_t pl011_int(int irq, void *dev_id)
11641279
unsigned int dummy_read;
11651280

11661281
spin_lock_irqsave(&uap->port.lock, flags);
1167-
11681282
status = readw(uap->port.membase + UART011_MIS);
11691283
if (status) {
11701284
do {
@@ -1551,6 +1665,11 @@ pl011_set_termios(struct uart_port *port, struct ktermios *termios,
15511665
*/
15521666
baud = uart_get_baud_rate(port, termios, old, 0,
15531667
port->uartclk / clkdiv);
1668+
/*
1669+
* Adjust RX DMA polling rate with baud rate if not specified.
1670+
*/
1671+
if (uap->dmarx.auto_poll_rate)
1672+
uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
15541673

15551674
if (baud > port->uartclk/16)
15561675
quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);

‎include/linux/amba/serial.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ struct amba_pl011_data {
203203
bool (*dma_filter)(struct dma_chan *chan, void *filter_param);
204204
void *dma_rx_param;
205205
void *dma_tx_param;
206+
bool dma_rx_poll_enable;
207+
unsigned int dma_rx_poll_rate;
208+
unsigned int dma_rx_poll_timeout;
206209
void (*init) (void);
207210
void (*exit) (void);
208211
};

0 commit comments

Comments
 (0)