[linux-dvb] PATCH for SAA7146 DMA buffer overflow in budget cards (updated)

Ingo Schneider mail at ingo-schneider.de
Sat Mar 4 10:03:30 CET 2006


Hello again !

Here comes a Patch which does the following changes for all ttpci budget 
cards:
- Issue a warning when more than 80% of the DMA buffer is being used 
(probably due to bad IRQ latency)
 Warnings are limited to the first 100 warnings and after that one 
warning for every 100 buffer overruns.
- Introduce a new parameter "bufsize" (in k) which increases the default 
DMA buffer of 188k up to 4 MB
 A buffer size of 470k does it for me even at high I/O load conditions.
- Now the patch doesn't break budget-patch anymore

Signed-off-by: Ingo Schneider <mail at ingo-schneider.de>

Oliver, can you please integrate this patch ?

Regards,
Ingo Schneider.

-------------- next part --------------
diff -urw drivers.orig/media/dvb/ttpci/budget-core.c drivers/media/dvb/ttpci/budget-core.c
--- drivers.orig/media/dvb/ttpci/budget-core.c	2006-03-01 23:37:27.000000000 +0100
+++ drivers/media/dvb/ttpci/budget-core.c	2006-03-04 09:29:12.000000000 +0100
@@ -39,9 +39,20 @@
 #include "budget.h"
 #include "ttpci-eeprom.h"
 
+#define TS_DEFAULT_WIDTH 376
+#define TS_DEFAULT_HEIGHT 512
+#define TS_MOD_WIDTH 376      // 2 * TS_SIZE (% 8 == 0)
+#define TS_MOD_HEIGHT 256     // 0x0100
+#define TS_MAX_WIDTH 3760     // 10 * (2*TS_SIZE)
+#define TS_MAX_HEIGHT 3840    // 0x0f00
+#define TS_MIN_BUFSIZE_K 188
+#define TS_MAX_BUFSIZE_K 4096 // SAA7146 can handle at most 4 MB DMA
 int budget_debug;
+static int budget_dma_buffer_size;
 module_param_named(debug, budget_debug, int, 0644);
+module_param_named(bufsize, budget_dma_buffer_size, int, 0444);
 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
+MODULE_PARM_DESC(bufsize, "DMA buffer size in kB (default: 188, max: 4096).");
 
 /****************************************************************************
  * TT budget / WinTV Nova
@@ -70,7 +81,7 @@
 
 	saa7146_write(dev, MC1, MASK_20);	// DMA3 off
 
-	memset(budget->grabbing, 0x00, TS_HEIGHT * TS_WIDTH);
+	memset(budget->grabbing, 0x00, budget->buffer_size);
 
 	saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
 
@@ -115,16 +126,12 @@
 
 	saa7146_write(dev, BASE_ODD3, 0);
 	saa7146_write(dev, BASE_EVEN3, 0);
-	saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
+	saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
 	saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
 
-	if (budget->card->type == BUDGET_FS_ACTIVY) {
-		saa7146_write(dev, PITCH3, TS_WIDTH / 2);
-		saa7146_write(dev, NUM_LINE_BYTE3, ((TS_HEIGHT * 2) << 16) | (TS_WIDTH / 2));
-	} else {
-		saa7146_write(dev, PITCH3, TS_WIDTH);
-		saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
-	}
+	saa7146_write(dev, PITCH3, budget->buffer_width);
+	saa7146_write(dev, NUM_LINE_BYTE3,
+			(budget->buffer_height << 16) | budget->buffer_width);
 
 	saa7146_write(dev, MC2, (MASK_04 | MASK_20));
 
@@ -141,11 +148,12 @@
 	u8 *mem = (u8 *) (budget->grabbing);
 	u32 olddma = budget->ttbp;
 	u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
+	int count = 0;
 
 	/* nearest lower position divisible by 188 */
 	newdma -= newdma % 188;
 
-	if (newdma >= TS_BUFLEN)
+	if (newdma >= budget->buffer_size)
 		return;
 
 	budget->ttbp = newdma;
@@ -154,11 +162,21 @@
 		return;
 
 	if (newdma > olddma) {	/* no wraparound, dump olddma..newdma */
-		dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, (newdma - olddma) / 188);
+		count = (newdma - olddma);
+		dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
 	} else {		/* wraparound, dump olddma..buflen and 0..newdma */
-		dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, (TS_BUFLEN - olddma) / 188);
+		count = budget->buffer_size - olddma;
+		dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
+		count += newdma;
 		dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
 	}
+	if (count > (budget->buffer_size * 80/100)) {
+		budget->buffer_warnings++;
+		if (budget->buffer_warnings < 100 || budget->buffer_warnings % 100 == 0) {
+			printk("ttpci vpeirq: used %d times >80%% of buffer (%d bytes now)\n",
+				budget->buffer_warnings, count);
+		}
+	}
 }
 
 
@@ -337,11 +355,17 @@
 	dvb_dmx_release(&budget->demux);
 }
 
+static int budget_mod_limit(int value, int mod, int limit)
+{
+	if (value > limit)
+		return limit - limit % mod;
+	else
+		return value - value % mod;
+}
 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
 		      struct saa7146_pci_extension_data *info,
 		      struct module *owner)
 {
-	int length = TS_WIDTH * TS_HEIGHT;
 	int ret = 0;
 	struct budget_info *bi = info->ext_priv;
 
@@ -352,6 +376,37 @@
 	budget->card = bi;
 	budget->dev = (struct saa7146_dev *) dev;
 
+	if (budget->card->type == BUDGET_FS_ACTIVY) {
+		budget->buffer_width = TS_SIZE;
+		budget->buffer_height = TS_DEFAULT_HEIGHT * 2;
+	} else {
+		budget->buffer_width = TS_DEFAULT_WIDTH;
+		budget->buffer_height = TS_DEFAULT_HEIGHT;
+	}
+	
+	if (budget_dma_buffer_size >= TS_MIN_BUFSIZE_K &&
+	    budget_dma_buffer_size <= TS_MAX_BUFSIZE_K) {
+		int bufsize = budget_dma_buffer_size * 1024;
+		
+		/*
+		   For activy, don't change the buffer width at all,
+		   since it must stay at TS_SIZE.
+		   For all other cards, only change the width of the buffer
+		   if the requested buffer size cannot be realized 
+		   by changing only the height of buffer.
+		 */
+		if (bufsize / budget->buffer_width > TS_MAX_HEIGHT &&
+			budget->card->type != BUDGET_FS_ACTIVY) {
+			budget->buffer_width = budget_mod_limit(
+				bufsize/TS_MAX_HEIGHT, TS_MOD_WIDTH, TS_MAX_WIDTH);
+		}
+		budget->buffer_height = budget_mod_limit(
+			bufsize/budget->buffer_width, TS_MOD_HEIGHT, TS_MAX_HEIGHT);
+	}
+	budget->buffer_size = budget->buffer_height * budget->buffer_width;
+	budget->buffer_warnings = 0;
+	dprintk(2, "budget: width = %d, height = %d\n",
+		budget->buffer_width, budget->buffer_height);
 	dvb_register_adapter(&budget->dvb_adapter, budget->card->name, owner);
 
 	/* set dd1 stream a & b */
@@ -392,7 +447,7 @@
 	ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
 
 	if (NULL ==
-	    (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, length, &budget->pt))) {
+	    (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt))) {
 		ret = -ENOMEM;
 		goto err;
 	}
diff -urw drivers.orig/media/dvb/ttpci/budget-patch.c drivers/media/dvb/ttpci/budget-patch.c
--- drivers.orig/media/dvb/ttpci/budget-patch.c	2006-03-01 23:37:27.000000000 +0100
+++ drivers/media/dvb/ttpci/budget-patch.c	2006-03-04 09:42:16.000000000 +0100
@@ -672,6 +672,17 @@
 	saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
 	// Set RPS1 Address register to point to RPS code               (r108 p42)
 	saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
+
+	if (!(budget = kmalloc (sizeof(struct budget_patch), GFP_KERNEL)))
+		return -ENOMEM;
+
+	dprintk(2, "budget: %p\n", budget);
+
+	if ((err = ttpci_budget_init (budget, dev, info, THIS_MODULE))) {
+		kfree (budget);
+		return err;
+	}
+
 	// Set Source Line Counter Threshold, using BRS                 (rCC p43)
 	// It generates HS event every TS_HEIGHT lines
 	// this is related to TS_WIDTH set in register
@@ -680,24 +691,13 @@
 	//,then RPS_THRESH1
 	// should be set to trigger every TS_HEIGHT (512) lines.
 	//
-	saa7146_write(dev, RPS_THRESH1, (TS_HEIGHT*1) | MASK_12 );
+        saa7146_write(dev, RPS_THRESH1, budget->buffer_height | MASK_12 );
 
 	// saa7146_write(dev, RPS_THRESH0, ((TS_HEIGHT/2)<<16) |MASK_28| (TS_HEIGHT/2) |MASK_12 );
 	// Enable RPS1                                                  (rFC p33)
 	saa7146_write(dev, MC1, (MASK_13 | MASK_29));
 
 
-	if (!(budget = kmalloc (sizeof(struct budget_patch), GFP_KERNEL)))
-		return -ENOMEM;
-
-	dprintk(2, "budget: %p\n", budget);
-
-	if ((err = ttpci_budget_init (budget, dev, info, THIS_MODULE))) {
-		kfree (budget);
-		return err;
-	}
-
-
 	dev->ext_priv = budget;
 
 	budget->dvb_adapter.priv = budget;
diff -urw drivers.orig/media/dvb/ttpci/budget.h drivers/media/dvb/ttpci/budget.h
--- drivers.orig/media/dvb/ttpci/budget.h	2006-03-01 23:37:27.000000000 +0100
+++ drivers/media/dvb/ttpci/budget.h	2006-03-04 09:58:57.000000000 +0100
@@ -56,6 +56,10 @@
 	int ci_present;
 	int video_port;
 
+	int buffer_width;
+	int buffer_height;
+	int buffer_size;
+	int buffer_warnings;
 	u8 tsf;
 	u32 ttbp;
 	int feeding;
@@ -77,11 +81,6 @@
 	.ext_priv = &x_var ## _info, \
 	.ext = &budget_extension };
 
-#define TS_WIDTH  (376)
-#define TS_HEIGHT (512)
-#define TS_BUFLEN (TS_WIDTH*TS_HEIGHT)
-#define TS_MAX_PACKETS (TS_BUFLEN/TS_SIZE)
-
 #define BUDGET_TT		   0
 #define BUDGET_TT_HW_DISEQC	   1
 #define BUDGET_PATCH		   3


More information about the linux-dvb mailing list