From c5a0664fa68a8541c1400bf7330a072581b12c38 Mon Sep 17 00:00:00 2001
From: Joel A Fernandes <joelagnel@ti.com>
Date: Sat, 12 Nov 2011 23:42:26 -0600
Subject: [PATCH v2] usb::musb::cppi41_dma: Check if scheduling is required after DMA

Even after TX DMA, data can still be in the FIFO and we would keep
rescheduling the worker thread in a polling fashion taking up a lot
of CPU if the controller was slow to respond. We check if rescheduling
is required and sleep if so.

This fixes high CPU load issue seen with g_mass_storage on a BeagleBone
and could potentially fix similar issues seen with other gadget drivers.

v2 changes:
Avoid rescheduling a "worker", this process involves a lot of workqueue
related crunching that is unnecessary. If we're polling, we might as well
do it with minimum overhead, and giving other processes CPU time at the
same time.

Signed-off-by: Joel A Fernandes <joelagnel@ti.com>
---
 drivers/usb/musb/cppi41_dma.c |   30 ++++++++++++++++++------------
 1 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/drivers/usb/musb/cppi41_dma.c b/drivers/usb/musb/cppi41_dma.c
index a24707c..3ac58b1 100644
--- a/drivers/usb/musb/cppi41_dma.c
+++ b/drivers/usb/musb/cppi41_dma.c
@@ -1283,6 +1283,7 @@ void cppi41_check_fifo_empty(struct cppi41 *cppi)
 	for (index = 0; index < USB_CPPI41_NUM_CH; index++) {
 		void __iomem *epio;
 		u16 csr;
+		unsigned long flags;
 
 		tx_ch = &cppi->tx_cppi_ch[index];
 		if (tx_ch->tx_complete) {
@@ -1299,29 +1300,34 @@ void cppi41_check_fifo_empty(struct cppi41 *cppi)
 			epio = tx_ch->end_pt->regs;
 			csr = musb_readw(epio, MUSB_TXCSR);
 
-			if (csr & (MUSB_TXCSR_TXPKTRDY |
-				MUSB_TXCSR_FIFONOTEMPTY))
-				resched = 1;
-			else {
-				tx_ch->tx_complete = 0;
-				musb_dma_completion(musb, index+1, 1);
+			while (1) {
+				csr = musb_readw(epio, MUSB_TXCSR);
+				if(!(csr & (MUSB_TXCSR_TXPKTRDY |
+					MUSB_TXCSR_FIFONOTEMPTY)))
+				break;
+				cond_resched();
 			}
+			tx_ch->tx_complete = 0;
+			spin_lock_irqsave(&musb->lock, flags);
+			musb_dma_completion(musb, index+1, 1);
+			spin_unlock_irqrestore(&musb->lock, flags);
 		}
 	}
-
-	if (resched)
-		schedule_work(&cppi->txdma_work);
 }
 
 void txdma_completion_work(struct work_struct *data)
 {
 	struct cppi41 *cppi = container_of(data, struct cppi41, txdma_work);
 	struct musb *musb = cppi->musb;
-	unsigned long flags;
 
-	spin_lock_irqsave(&musb->lock, flags);
+	/*
+	 * txdma worker thread can call schedule_work on itself and cause
+	 * itself to be scheduled immediately and because the data might still
+	 * be in FIFO if it hasn't been pushed out after DMA, it is possible for
+	 * the worker to consume lot of CPU when the controller is slow, so we
+	 * reschedule if necessary.
+	 */
 	cppi41_check_fifo_empty(cppi);
-	spin_unlock_irqrestore(&musb->lock, flags);
 }
 
 /**
-- 
1.7.4.1

