[zaptel-commits] sruffell: branch sruffell/voicebus r3813 - /team/sruffell/voicebus/voicebus.c

SVN commits to the Zaptel project zaptel-commits at lists.digium.com
Wed Feb 6 18:11:43 CST 2008


Author: sruffell
Date: Wed Feb  6 18:11:42 2008
New Revision: 3813

URL: http://svn.digium.com/view/zaptel?view=rev&rev=3813
Log:
Aligned the individual descriptors on cache line boundaries.

Modified:
    team/sruffell/voicebus/voicebus.c

Modified: team/sruffell/voicebus/voicebus.c
URL: http://svn.digium.com/view/zaptel/team/sruffell/voicebus/voicebus.c?view=diff&rev=3813&r1=3812&r2=3813
==============================================================================
--- team/sruffell/voicebus/voicebus.c (original)
+++ team/sruffell/voicebus/voicebus.c Wed Feb  6 18:11:42 2008
@@ -138,6 +138,8 @@
 	unsigned int 	direction;
 	/*! The number of buffers currently submitted to the hardware. */
 	atomic_t 	count;
+	/*! The number of bytes to pad each descriptor for cache alignment. */
+	unsigned int	padding;
 };
 
 #define __DECLARE_VB_ATTR(_name) struct device_attribute dev_attr_##_name;
@@ -154,6 +156,8 @@
 	spinlock_t lock;
 	/*! The size of the transmit and receive buffers for this card. */
 	u32 framesize;
+	/*! The number of u32s in the host system cache line. */	
+	u8 cache_line_size;
 	/*! Pool to allocate memory for the tx and rx descriptor rings. */
 	struct voicebus_descriptor_list rxd;
 	struct voicebus_descriptor_list txd;
@@ -286,6 +290,15 @@
 #define stop_vb_deferred(_x_) do {;} while(0)
 #endif
 
+static inline struct voicebus_descriptor *
+vb_descriptor(struct voicebus_descriptor_list *dl, int index)
+{
+	struct voicebus_descriptor *d;
+	d = (struct voicebus_descriptor *)((u8*)dl->desc + 
+		((sizeof(*d) + dl->padding) * index));
+	return d;
+}
+
 static int
 vb_initialize_descriptors(struct voicebus *vb, struct voicebus_descriptor_list *dl, 
 	u32 des1, unsigned int direction)
@@ -296,17 +309,26 @@
 
 	assert(dl);
 
+	/*
+	 * Add some padding to each descriptor to ensure that they are
+	 * aligned on host system cache-line boundaries. 
+	 *
+	 */
+	dl->padding = (vb->cache_line_size*sizeof(u32)) - sizeof(*d);
+	printk("HERE: %s:%d padding=%d\n", __FILE__, __LINE__, dl->padding);
+	
 	dl->desc = pci_alloc_consistent(vb->pdev, 
-		sizeof(*d)*DRING_SIZE, &dl->desc_dma);
+		(sizeof(*d) + dl->padding) * DRING_SIZE, &dl->desc_dma);
 	if (!dl->desc) {
 		return -ENOMEM;
 	}
 
-	for ( i = 0, d=dl->desc; i < DRING_SIZE; ++i, ++d) {
-		memset(d, 0, sizeof(*d));
+	memset(dl->desc, 0, (sizeof(*d) + dl->padding) * DRING_SIZE);
+	for ( i = 0; i < DRING_SIZE; ++i) {
+		d = vb_descriptor(dl, i);
 		d->des1 = des1;
 	}
-	dl->desc[DRING_SIZE-1].des1 |= cpu_to_le32(END_OF_RING);
+	d->des1 |= cpu_to_le32(END_OF_RING);
 	dl->direction = direction;
 	atomic_set(&dl->count, 0);
 	return 0;
@@ -340,7 +362,7 @@
 	 *
 	 */
 #define MESSAGE "%d ms is an invalid value for minumum latency.  Setting to %d ms.\n"
-	if ( DRING_SIZE <= ms ) {
+	if ( DRING_SIZE < ms ) {
 		VB_PRINTK(vb, WARNING, MESSAGE, ms, DRING_SIZE);
 		return -EINVAL;
 	} else if (VOICEBUS_DEFAULT_LATENCY > ms ) {
@@ -373,7 +395,8 @@
 
 	assert(vb_is_stopped(vb));
 
-	for (i=0, d = dl->desc; i < DRING_SIZE; ++i, ++d) {
+	for (i=0; i < DRING_SIZE; ++i) {
+		d = vb_descriptor(dl, i);
 		if (d->buffer1) {
 			d->buffer1 = 0;
 			assert(dl->pending[i]);
@@ -396,7 +419,8 @@
 	}
 	vb_cleanup_descriptors(vb, dl);
 	pci_free_consistent(
-		vb->pdev, sizeof(struct voicebus_descriptor)*DRING_SIZE, 
+		vb->pdev, 
+		(sizeof(struct voicebus_descriptor)+dl->padding)*DRING_SIZE,
 		dl->desc, dl->desc_dma);
 }
 
@@ -547,36 +571,34 @@
 {
 	unsigned long timeout;
 	u32 reg;
-	u8 cache_line_size;
+	u32 pci_access;
 	const u32 DEFAULT_PCI_ACCESS = 0xfff80002;
 	BUG_ON(in_interrupt());
 	
-	if (pci_read_config_byte(vb->pdev, 0x0c, &cache_line_size)) {
-		VB_PRINTK(vb, ERR, "Failed read of cache line " \
-		 "size from PCI configuration space.\n");
-		return -EIO;
-	}
-
-	switch (cache_line_size) {
+	switch (vb->cache_line_size) {
 	case 0x08:
-		reg = DEFAULT_PCI_ACCESS | (0x1 << 14);
+		pci_access = DEFAULT_PCI_ACCESS | (0x1 << 14);
 		break;
 	case 0x10:
-		reg = DEFAULT_PCI_ACCESS | (0x2 << 14);
+		pci_access = DEFAULT_PCI_ACCESS | (0x2 << 14);
 		break;
 	case 0x20: 
-		reg = DEFAULT_PCI_ACCESS | (0x3 << 14);
+		pci_access = DEFAULT_PCI_ACCESS | (0x3 << 14);
 		break;
 	default:
 		VB_PRINTK(vb, WARNING, "Host system set a cache size "\
 		 "of %d which is not supported. " \
 		 "Disabling memory write line and memory read line.",
-		 cache_line_size);
-		reg = 0xfe584202;
+		 vb->cache_line_size);
+		pci_access = 0xfe584202;
 		break;
 	}
-	
-	vb_setctl(vb, 0x0000, reg | 1);
+
+	/* The transmit and receive descriptors will have the same padding. */
+	pci_access |= ((vb->txd.padding / sizeof(u32)) << 2) & 0x7c;
+
+	vb_setctl(vb, 0x0000, pci_access | 1);
+
 	timeout = jiffies + HZ/10; /* 100ms interval */
 	do {
 		reg = vb_getctl(vb, 0x0000);
@@ -587,6 +609,8 @@
 		 "within 100ms!");
 		return -EIO;
 	}
+
+	vb_setctl(vb, 0x0000, pci_access);
 
 	vb_cleanup_descriptors(vb, &vb->txd);
 	vb_cleanup_descriptors(vb, &vb->rxd);
@@ -660,7 +684,8 @@
 	volatile struct voicebus_descriptor *d;
 	unsigned int tail = dl->tail;
 	assert_in_vb_deferred(vb);
-	d = &(dl->desc[tail]);
+
+	d = vb_descriptor(dl, tail); 
 
 	if (unlikely(d->buffer1)) {
 		/* Do not overwrite a buffer that is still in progress. */
@@ -685,7 +710,7 @@
 	void *vbb;
 	unsigned int head = dl->head;
 	assert_in_vb_deferred(vb);
-	d = &((dl->desc)[head]);
+	d = vb_descriptor(dl, head);
 	if (!OWNED(d)) {
 		dma_unmap_single(&vb->pdev->dev, d->buffer1, 
 			vb->framesize, dl->direction);
@@ -1429,6 +1454,12 @@
 	/* ---------------------------------------------------------------- 
 	   Configure the hardware / kernel module interfaces. 
 	   ---------------------------------------------------------------- */
+	if (pci_read_config_byte(vb->pdev, 0x0c, &vb->cache_line_size)) {
+		VB_PRINTK(vb, ERR, "Failed read of cache line " \
+		 "size from PCI configuration space.\n");
+		goto cleanup;
+	}
+
 	if (pci_enable_device(pdev)) {
 		VB_PRINTK(vb, ERR, "Failed call to pci_enable_device.\n");
 		retval = -EIO;




More information about the zaptel-commits mailing list