Skip to contentSkip to conversation

wii: add usb 2.0 support

RFC PATCH Superseded Albert Herranz 3 participants 21 messages Feb 28, 2010 – Mar 2, 2010

Subsystems (6)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover wii: add usb 2.0 support

The following patch series adds USB 2.0 support for the Wii PowerPC platform via the EHCI controller present in the "Hollywood" chipset of the video game console.

v1 -> v2
- enable per-device DMA coherent support on the Wii
- add a generic dmabounce based on ARM dmabounce
- make PowerPC and ARM use the generic dmabounce support
(ARM just compile-tested)
- do not bounce buffers at the USB layer
- handle bounce buffers on the Wii at the platform support layer via the DMA API
- introduce a flag HCD_NO_COHERENT_MEM to avoid using coherent memory for USB buffers
- make the "Hollywood" EHCI controller use HCD_NO_COHERENT_MEM and honor MEM2 DMA constraints to solve the platform restrictions accessing coherent memory.

Albert Herranz (9):
powerpc: add per-device dma coherent support
wii: have generic dma coherent
dma-coherent: fix bitmap access races
add generic dmabounce support
arm: use generic dmabounce support
powerpc: add optional per-device dmabounce support
wii: add mem2 dma mapping ops
USB: add HCD_NO_COHERENT_MEM host controller driver flag
wii: hollywood ehci controller support

 arch/Kconfig                                 |    3 +
 arch/arm/Kconfig                             |    4 +-
 arch/arm/common/Kconfig                      |    6 +-
 arch/arm/common/dmabounce.c                  |  376 +++--------------
 arch/arm/include/asm/device.h                |    2 +-
 arch/powerpc/boot/wii.c                      |   34 ++
 arch/powerpc/include/asm/device.h            |    3 +
 arch/powerpc/include/asm/dma-mapping.h       |    1 +
 arch/powerpc/include/asm/wii.h               |   25 ++
 arch/powerpc/kernel/dma.c                    |    5 +
 arch/powerpc/platforms/embedded6xx/Kconfig   |    3 +
 arch/powerpc/platforms/embedded6xx/Makefile  |    2 +-
 arch/powerpc/platforms/embedded6xx/wii-dma.c |  557 ++++++++++++++++++++++++++
 drivers/base/dma-coherent.c                  |   11 +
 drivers/usb/core/buffer.c                    |   15 +-
 drivers/usb/core/hcd.c                       |   50 ++-
 drivers/usb/core/hcd.h                       |   13 +-
 drivers/usb/host/Kconfig                     |    8 +
 drivers/usb/host/ehci-hcd.c                  |    5 +
 drivers/usb/host/ehci-hlwd.c                 |  233 +++++++++++
 drivers/usb/host/ehci.h                      |   23 +
 include/linux/dmabounce.h                    |   71 ++++
 lib/Kconfig                                  |   10 +
 lib/Makefile                                 |    2 +
 lib/dmabounce.c                              |  389 ++++++++++++++++++
 25 files changed, 1512 insertions(+), 339 deletions(-)
 create mode 100644 arch/powerpc/include/asm/wii.h
 create mode 100755 arch/powerpc/platforms/embedded6xx/wii-dma.c
 create mode 100644 drivers/usb/host/ehci-hlwd.c
 create mode 100644 include/linux/dmabounce.h
 create mode 100644 lib/dmabounce.c
#1 lore raw

RFC PATCH v2 1/9 powerpc: add per-device dma coherent support

Use the generic per-device dma coherent allocator on powerpc. This allows a driver to declare coherent memory area from where a device can allocate coherent memory chunks.

---
 arch/powerpc/include/asm/dma-mapping.h |    1 +
 arch/powerpc/kernel/dma.c              |    5 +++++
 2 files changed, 6 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/dma-mapping.h +1 -0
index 80a973b..18ecec8 100644
@@ -17,6 +17,7 @@
 #include <linux/dma-debug.h>
 #include <asm/io.h>
 #include <asm/swiotlb.h>
+#include <asm-generic/dma-coherent.h>
 
 #define DMA_ERROR_CODE		(~(dma_addr_t)0x0)
 
arch/powerpc/kernel/dma.c +5 -0
index 6215062..83d5232 100644
@@ -27,6 +27,9 @@ void *dma_direct_alloc_coherent(struct device *dev, size_t size,
 {
 	void *ret;
 #ifdef CONFIG_NOT_COHERENT_CACHE
+	if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
+		return ret;
+
 	ret = __dma_alloc_coherent(dev, size, dma_handle, flag);
 	if (ret == NULL)
 		return NULL;
@@ -54,6 +57,8 @@ void dma_direct_free_coherent(struct device *dev, size_t size,
 			      void *vaddr, dma_addr_t dma_handle)
 {
 #ifdef CONFIG_NOT_COHERENT_CACHE
+	if (dma_release_from_coherent(dev, get_order(size), vaddr))
+		return;
 	__dma_free_coherent(size, vaddr);
 #else
 	free_pages((unsigned long)vaddr, get_order(size));
Signature · 1 line
1.6.3.3
#2 lore raw

RFC PATCH v2 2/9 wii: have generic dma coherent

In reply to Albert Herranz

Let the Nintendo Wii gaming console use per-device dma coherent allocations. This will be used later by some of its drivers.

---
 arch/powerpc/platforms/embedded6xx/Kconfig |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/powerpc/platforms/embedded6xx/Kconfig +1 -0
index 524d971..fe77ab2 100644
@@ -119,6 +119,7 @@ config WII
 	bool "Nintendo-Wii"
 	depends on EMBEDDED6xx
 	select GAMECUBE_COMMON
+	select HAVE_GENERIC_DMA_COHERENT
 	help
 	  Select WII if configuring for the Nintendo Wii.
 	  More information at: <http://gc-linux.sourceforge.net/>
Signature · 1 line
1.6.3.3
#3 lore raw

RFC PATCH v2 3/9 dma-coherent: fix bitmap access races

In reply to Albert Herranz

The coherent per-device memory handling functions use the in-kernel bitmap library to account for the allocated regions.

The bitmap functions, though, do not protect the bitmap structure from being modified concurrently. This can lead, for example, to double allocations if dma_alloc_from_coherent() is called while another dma_alloc_from_coherent() is already in progress.

Fix those races by protecting concurrent modifications of the allocation bitmap. spin_lock_irqsave()/spin_unlock_irqrestore() is used as the allocation/release functions are planned to be used in interrupt context for streaming DMA mappings/unmappings via bounce buffers.

---
 drivers/base/dma-coherent.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
drivers/base/dma-coherent.c +11 -0
index 962a3b5..9d27d63 100644
@@ -11,6 +11,7 @@ struct dma_coherent_mem {
 	int		size;
 	int		flags;
 	unsigned long	*bitmap;
+	spinlock_t	lock;	/* protect bitmap operations */
 };
 
 int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
@@ -44,6 +45,7 @@ int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
 	dev->dma_mem->device_base = device_addr;
 	dev->dma_mem->size = pages;
 	dev->dma_mem->flags = flags;
+	spin_lock_init(&dev->dma_mem->lock);
 
 	if (flags & DMA_MEMORY_MAP)
 		return DMA_MEMORY_MAP;
@@ -77,6 +79,7 @@ void *dma_mark_declared_memory_occupied(struct device *dev,
 {
 	struct dma_coherent_mem *mem = dev->dma_mem;
 	int pos, err;
+	unsigned long flags;
 
 	size += device_addr & ~PAGE_MASK;
 
@@ -84,7 +87,9 @@ void *dma_mark_declared_memory_occupied(struct device *dev,
 		return ERR_PTR(-EINVAL);
 
 	pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
+	spin_lock_irqsave(&mem->lock, flags);
 	err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
+	spin_unlock_irqrestore(&mem->lock, flags);
 	if (err != 0)
 		return ERR_PTR(err);
 	return mem->virt_base + (pos << PAGE_SHIFT);
@@ -112,6 +117,7 @@ int dma_alloc_from_coherent(struct device *dev, ssize_t size,
 	struct dma_coherent_mem *mem;
 	int order = get_order(size);
 	int pageno;
+	unsigned long flags;
 
 	if (!dev)
 		return 0;
@@ -124,7 +130,9 @@ int dma_alloc_from_coherent(struct device *dev, ssize_t size,
 	if (unlikely(size > (mem->size << PAGE_SHIFT)))
 		goto err;
 
+	spin_lock_irqsave(&mem->lock, flags);
 	pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
+	spin_unlock_irqrestore(&mem->lock, flags);
 	if (unlikely(pageno < 0))
 		goto err;
 
@@ -163,12 +171,15 @@ EXPORT_SYMBOL(dma_alloc_from_coherent);
 int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
 {
 	struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
+	unsigned long flags;
 
 	if (mem && vaddr >= mem->virt_base && vaddr <
 		   (mem->virt_base + (mem->size << PAGE_SHIFT))) {
 		int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
 
+		spin_lock_irqsave(&mem->lock, flags);
 		bitmap_release_region(mem->bitmap, page, order);
+		spin_unlock_irqrestore(&mem->lock, flags);
 		return 1;
 	}
 	return 0;
Signature · 1 line
1.6.3.3
#4 lore raw

RFC PATCH v2 4/9 add generic dmabounce support

In reply to Albert Herranz

This patch makes part of the ARM dmabounce code available to other architectures as a generic API.
See included kernel-doc annotations for the actual API implemented.

An architecture can opt-in for generic dmabounce support by defining HAVE_DMABOUNCE.

This support will be used later to address DMA memory access restrictions on the Nintendo Wii video game console.

---
 arch/Kconfig              |    3 +
 include/linux/dmabounce.h |   77 +++++++++
 lib/Kconfig               |   10 ++
 lib/Makefile              |    2 +
 lib/dmabounce.c           |  395 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 487 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/dmabounce.h
 create mode 100644 lib/dmabounce.c
arch/Kconfig +3 -0
index 9d055b4..98ff26d 100644
@@ -130,6 +130,9 @@ config HAVE_CLK
 config HAVE_DMA_API_DEBUG
 	bool
 
+config HAVE_DMABOUNCE
+	bool
+
 config HAVE_DEFAULT_NO_SPIN_MUTEXES
 	bool
 
include/linux/dmabounce.h new file +77 -0

77 lines not on this page · raw message

lib/Kconfig +10 -0
index 97b136f..b53b7dc 100644
@@ -207,4 +207,14 @@ config GENERIC_ATOMIC64
 config LRU_CACHE
 	tristate
 
+config DMABOUNCE
+	bool
+	depends on HAVE_DMABOUNCE
+	select ZONE_DMA if ARM
+	default y
+
+config DMABOUNCE_STATS
+	bool "Track dmabounce statistics"
+	depends on DMABOUNCE
+
 endmenu
lib/Makefile +2 -0
index 3b0b4a6..097c2ed 100644
@@ -100,6 +100,8 @@ obj-$(CONFIG_GENERIC_CSUM) += checksum.o
 
 obj-$(CONFIG_GENERIC_ATOMIC64) += atomic64.o
 
+obj-$(CONFIG_DMABOUNCE) += dmabounce.o
+
 hostprogs-y	:= gen_crc32table
 clean-files	:= crc32table.h
 
lib/dmabounce.c new file +395 -0

395 lines not on this page · raw message

Signature · 1 line
1.6.3.3
#5 lore raw

RFC PATCH v2 4/9 add generic dmabounce support

Quoted 2 lines · Albert Herranz

This patch makes part of the ARM dmabounce code available to other architectures as a generic API.

There is already a generic dma bounce implementation - it's called swiotlb - lib/swiotlb.c. We should eventually switch the ARM dmabounce stuff over to that instead of keeping dmabounce around.

The only problem I forsee is that on ARM, we have devices which can only address the least significant N bits of RAM, but there may be an offset on the base address of RAM on the bus which isn't included in these N bits.

Even more fun is where we have a DMA controller which can address N bits of RAM, except for bit M which must be zero... (where N > M).

#6 lore raw

RFC PATCH v2 4/9 add generic dmabounce support

Quoted 8 lines · Russell King - ARM Linux
Quoted 2 lines · Albert Herranz

This patch makes part of the ARM dmabounce code available to other architectures as a generic API.

There is already a generic dma bounce implementation - it's called swiotlb - lib/swiotlb.c. We should eventually switch the ARM dmabounce stuff over to that instead of keeping dmabounce around.

Quoted 8 lines

The only problem I forsee is that on ARM, we have devices which can only address the least significant N bits of RAM, but there may be an offset on the base address of RAM on the bus which isn't included in these N bits.

Even more fun is where we have a DMA controller which can address N bits of RAM, except for bit M which must be zero... (where N > M).

In the Wii we have several limitations:
- it is a NOT_COHERENT_CACHE platform
- write accesses to coherent memory from the main processor must be done always in 32-bit chunks
- some devices can only reliably perform DMA to/from a specific region of memory (the second block of RAM, 64MB at 0x10000000, called MEM2)

So if swiotlb is the way to go I can try to adapt it to take into account these cases:
- it should allocate the io_tlb_start and io_tlb_overflow_buffer areas from MEM2 (add allocation/freeing hooks for these areas?)
- it should copy data from coherent memory in 32-bit chunks (add copy to/from coherent hooks?)
- it should decide to bounce buffers sitting in MEM1 (add a dma_needs_bounce() hook?)

Thanks,
Albert

#7 lore raw

RFC PATCH v2 5/9 arm: use generic dmabounce support

In reply to Albert Herranz

Update ARM dmabounce to use the generic dmabounce support.

---
 arch/arm/Kconfig              |    4 +-
 arch/arm/common/Kconfig       |    6 +-
 arch/arm/common/dmabounce.c   |  376 +++++++---------------------------------
 arch/arm/include/asm/device.h |    2 +-
 include/linux/dmabounce.h     |    6 -
 lib/dmabounce.c               |    6 -
 6 files changed, 70 insertions(+), 330 deletions(-)
arch/arm/Kconfig +2 -2
index 184a6bd..25fe1d0 100644
@@ -417,7 +417,7 @@ config ARCH_IXP4XX
 	select GENERIC_GPIO
 	select GENERIC_TIME
 	select GENERIC_CLOCKEVENTS
-	select DMABOUNCE if PCI
+	select HAVE_DMABOUNCE if PCI
 	help
 	  Support for Intel's IXP4XX (XScale) family of processors.
 
@@ -974,7 +974,7 @@ config PCI_HOST_ITE8152
 	bool
 	depends on PCI && MACH_ARMCORE
 	default y
-	select DMABOUNCE
+	select HAVE_DMABOUNCE
 
 source "drivers/pci/Kconfig"
 
arch/arm/common/Kconfig +1 -5
index 4efbb9d..01869a5 100644
@@ -20,11 +20,7 @@ config ICST307
 
 config SA1111
 	bool
-	select DMABOUNCE if !ARCH_PXA
-
-config DMABOUNCE
-	bool
-	select ZONE_DMA
+	select HAVE_DMABOUNCE if !ARCH_PXA
 
 config TIMER_ACORN
 	bool
arch/arm/common/dmabounce.c +66 -310

493 lines not on this page · raw message

arch/arm/include/asm/device.h +1 -1
index 9f390ce..709d501 100644
@@ -8,7 +8,7 @@
 
 struct dev_archdata {
 #ifdef CONFIG_DMABOUNCE
-	struct dmabounce_device_info *dmabounce;
+	struct dmabounce_info *dmabounce;
 #endif
 };
 
include/linux/dmabounce.h +0 -6
index d60dc04..dc3ad5b 100644
@@ -1,9 +1,6 @@
 #ifndef _LINUX_DMABOUNCE_H
 #define _LINUX_DMABOUNCE_H
 
-/* FIXME remove later when arch/arm/common/dmabounce.c is updated */
-#ifndef CONFIG_ARM
-
 #ifdef CONFIG_DMABOUNCE
 
 #include <linux/dmapool.h>
@@ -71,7 +68,4 @@ extern void dmabounce_info_unregister(struct device *dev);
 
 #endif /* CONFIG_DMABOUNCE */
 
-/* FIXME remove later when arch/arm/common/dmabounce.c is updated */
-#endif /* !CONFIG_ARM */
-
 #endif /* _LINUX_DMABOUNCE_H */
lib/dmabounce.c +0 -6
index 620d314..7cb4740 100644
@@ -18,9 +18,6 @@
  * version 2 as published by the Free Software Foundation.
  */
 
-/* FIXME remove later when arch/arm/common/dmabounce.c is updated */
-#ifndef CONFIG_ARM
-
 #define DRV_MODULE_NAME "dmabounce"
 #define DRV_DESCRIPTION "Generic DMA bounce buffer functions"
 #define DRV_AUTHOR      "Christopher Hoover <[email protected]>, " \
@@ -390,6 +387,3 @@ EXPORT_SYMBOL(dmabounce_info_unregister);
 MODULE_AUTHOR(DRV_AUTHOR);
 MODULE_DESCRIPTION(DRV_DESCRIPTION);
 MODULE_LICENSE("GPL");
-
-/* FIXME remove later when arch/arm/common/dmabounce.c is updated */
-#endif /* !CONFIG_ARM */
Signature · 1 line
1.6.3.3
#8 lore raw

RFC PATCH v2 6/9 powerpc: add optional per-device dmabounce support

In reply to Albert Herranz

Add support for the PowerPC architecture for using the generic dmabounce support on a per-device basis.

---
 arch/powerpc/include/asm/device.h |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/device.h +3 -0
index 6d94d27..b9a47e8 100644
@@ -28,6 +28,9 @@ struct dev_archdata {
 #ifdef CONFIG_SWIOTLB
 	dma_addr_t		max_direct_dma_addr;
 #endif
+#ifdef CONFIG_DMABOUNCE
+	struct dmabounce_info	*dmabounce;
+#endif
 };
 
 static inline void dev_archdata_set_node(struct dev_archdata *ad,
Signature · 1 line
1.6.3.3
#9 lore raw

RFC PATCH v2 7/9 wii: add mem2 dma mapping ops

In reply to Albert Herranz

Some of the devices in the "Hollywood" chipset of the Nintendo Wii video game console have restrictions performing DMA transfers to the first contiguous RAM region (known as MEM1).
For example, up to 3 bytes of the last word of a DMA transfer of a non-32 bit aligned length to MEM1 may be lost.
Such restrictions do not apply when using the second contiguous RAM region (known as MEM2).

Add a set of DMA mapping operations which said devices can use to make sure that DMA transfers are always performed to/from memory buffers within MEM2.

QUICKFIX: wii-dma

---
 arch/powerpc/boot/wii.c                      |   34 ++
 arch/powerpc/include/asm/wii.h               |   25 ++
 arch/powerpc/platforms/embedded6xx/Kconfig   |    1 +
 arch/powerpc/platforms/embedded6xx/Makefile  |    2 +-
 arch/powerpc/platforms/embedded6xx/wii-dma.c |  557 ++++++++++++++++++++++++++
 5 files changed, 618 insertions(+), 1 deletions(-)
 create mode 100644 arch/powerpc/include/asm/wii.h
 create mode 100755 arch/powerpc/platforms/embedded6xx/wii-dma.c
arch/powerpc/boot/wii.c +34 -0
index 2ebaec0..f884006 100644
@@ -30,6 +30,9 @@ BSS_STACK(8192);
 #define MEM2_TOP		(0x10000000 + 64*1024*1024)
 #define FIRMWARE_DEFAULT_SIZE	(12*1024*1024)
 
+#define MEM2_DMA_BASE_PROP	"linux,wii-mem2-dma-base"
+#define MEM2_DMA_SIZE_PROP	"linux,wii-mem2-dma-size"
+#define MEM2_DMA_DEFAULT_SIZE	(1*1024*1024)
 
 struct mipc_infohdr {
 	char magic[3];
@@ -101,6 +104,30 @@ out:
 
 }
 
+static void mem2_fixups(u32 *top, u32 *reg)
+{
+	void *chosen;
+	u32 dma_base, dma_size;
+	int len;
+
+	chosen = finddevice("/chosen");
+	if (!chosen)
+		fatal("Can't find chosen node\n");
+
+	len = getprop(chosen, MEM2_DMA_SIZE_PROP, &dma_size, sizeof(dma_size));
+	if (len != sizeof(dma_size))
+		dma_size = MEM2_DMA_DEFAULT_SIZE;
+	if (dma_size > reg[3])
+		dma_size = reg[3];
+	setprop_val(chosen, MEM2_DMA_SIZE_PROP, dma_size);
+
+	*top -= dma_size;
+	dma_base = *top;
+	setprop_val(chosen, MEM2_DMA_BASE_PROP, dma_base);
+
+	printf("mem2_dma: %08X@%08X\n", dma_size, dma_base);
+}
+
 static void platform_fixups(void)
 {
 	void *mem;
@@ -127,9 +154,16 @@ static void platform_fixups(void)
 		mem2_boundary = MEM2_TOP - FIRMWARE_DEFAULT_SIZE;
 	}
 
+	mem2_fixups(&mem2_boundary, reg);
+
 	if (mem2_boundary > reg[2] && mem2_boundary < reg[2] + reg[3]) {
 		reg[3] = mem2_boundary - reg[2];
 		printf("top of MEM2 @ %08X\n", reg[2] + reg[3]);
+		/*
+		 * Find again the memory node as it may have changed its
+		 * position after adding some non-existing properties.
+		 */
+		mem = finddevice("/memory");
 		setprop(mem, "reg", reg, sizeof(reg));
 	}
 
arch/powerpc/include/asm/wii.h new file +25 -0
new file mode 100644
index 0000000..bb83c32
@@ -0,0 +1,25 @@
+/*
+ * arch/powerpc/include/asm/wii.h
+ *
+ * Nintendo Wii board-specific definitions
+ * Copyright (C) 2010 The GameCube Linux Team
+ * Copyright (C) 2010 Albert Herranz
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ */
+
+#ifndef __ASM_POWERPC_WII_H
+#define __ASM_POWERPC_WII_H
+
+/*
+ * DMA operations for the Nintendo Wii.
+ */
+extern struct dma_map_ops wii_mem2_dma_ops;
+
+extern int wii_set_mem2_dma_constraints(struct device *dev);
+extern void wii_clear_mem2_dma_constraints(struct device *dev);
+
+#endif /* __ASM_POWERPC_WII_H */
arch/powerpc/platforms/embedded6xx/Kconfig +1 -0
index fe77ab2..4d33755 100644
@@ -120,6 +120,7 @@ config WII
 	depends on EMBEDDED6xx
 	select GAMECUBE_COMMON
 	select HAVE_GENERIC_DMA_COHERENT
+	select HAVE_DMABOUNCE
 	help
 	  Select WII if configuring for the Nintendo Wii.
 	  More information at: <http://gc-linux.sourceforge.net/>
arch/powerpc/platforms/embedded6xx/Makefile +1 -1
index 66c23e4..4d4c776 100644
@@ -10,4 +10,4 @@ obj-$(CONFIG_PPC_C2K)		+= c2k.o
 obj-$(CONFIG_USBGECKO_UDBG)	+= usbgecko_udbg.o
 obj-$(CONFIG_GAMECUBE_COMMON)	+= flipper-pic.o
 obj-$(CONFIG_GAMECUBE)		+= gamecube.o
-obj-$(CONFIG_WII)		+= wii.o hlwd-pic.o
+obj-$(CONFIG_WII)		+= wii.o hlwd-pic.o wii-dma.o
arch/powerpc/platforms/embedded6xx/wii-dma.c new file +557 -0

557 lines not on this page · raw message

Signature · 1 line
1.6.3.3
#10 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

In reply to Albert Herranz

The HCD_NO_COHERENT_MEM USB host controller driver flag can be enabled to instruct the USB stack to avoid allocating coherent memory for USB buffers.

This flag is useful to overcome some esoteric memory access restrictions found in some platforms.
For example, the Nintendo Wii video game console is a NOT_COHERENT_CACHE platform that is unable to safely perform non-32 bit uncached writes to RAM because the byte enables are not connected to the bus. Thus, in that platform, "coherent" DMA buffers cannot be directly used by the kernel code unless it guarantees that all write accesses to said buffers are done in 32 bit chunks (which is not the case in the USB subsystem).

To avoid this unwanted behaviour HCD_NO_COHERENT_MEM can be enabled at the HCD controller, causing USB buffer allocations to be satisfied from normal kernel memory. In this case, the USB stack will make sure that the buffers get properly mapped/unmapped for DMA transfers using the DMA streaming mapping API.

Note that other parts of the USB stack may also use coherent memory, like for example the hardware descriptors used in the EHCI controllers. This needs to be checked and addressed separately. In the EHCI example, hardware descriptors are accessed in 32-bit (or 64-bit) chunks, causing no problems in the described scenario.

---
 drivers/usb/core/buffer.c |   15 ++++++++-----
 drivers/usb/core/hcd.c    |   50 +++++++++++++++++++++++++++++++++++++-------
 drivers/usb/core/hcd.h    |   13 ++++++-----
 3 files changed, 58 insertions(+), 20 deletions(-)
drivers/usb/core/buffer.c +9 -6
index 3ba2fff..fede7a0 100644
@@ -53,8 +53,9 @@ int hcd_buffer_create(struct usb_hcd *hcd)
 	char		name[16];
 	int 		i, size;
 
-	if (!hcd->self.controller->dma_mask &&
-	    !(hcd->driver->flags & HCD_LOCAL_MEM))
+	if ((!hcd->self.controller->dma_mask &&
+	    !(hcd->driver->flags & HCD_LOCAL_MEM)) ||
+	    (hcd->driver->flags & HCD_NO_COHERENT_MEM))
 		return 0;
 
 	for (i = 0; i < HCD_BUFFER_POOLS; i++) {
@@ -109,8 +110,9 @@ void *hcd_buffer_alloc(
 	int 			i;
 
 	/* some USB hosts just use PIO */
-	if (!bus->controller->dma_mask &&
-	    !(hcd->driver->flags & HCD_LOCAL_MEM)) {
+	if ((!bus->controller->dma_mask &&
+	    !(hcd->driver->flags & HCD_LOCAL_MEM)) ||
+	    (hcd->driver->flags & HCD_NO_COHERENT_MEM)) {
 		*dma = ~(dma_addr_t) 0;
 		return kmalloc(size, mem_flags);
 	}
@@ -135,8 +137,9 @@ void hcd_buffer_free(
 	if (!addr)
 		return;
 
-	if (!bus->controller->dma_mask &&
-	    !(hcd->driver->flags & HCD_LOCAL_MEM)) {
+	if ((!bus->controller->dma_mask &&
+	    !(hcd->driver->flags & HCD_LOCAL_MEM)) ||
+	    (hcd->driver->flags & HCD_NO_COHERENT_MEM)) {
 		kfree(addr);
 		return;
 	}
drivers/usb/core/hcd.c +42 -8

87 lines not on this page · raw message

drivers/usb/core/hcd.h +7 -6
index bbe2b92..cde42f3 100644
@@ -183,12 +183,13 @@ struct hc_driver {
 	irqreturn_t	(*irq) (struct usb_hcd *hcd);
 
 	int	flags;
-#define	HCD_MEMORY	0x0001		/* HC regs use memory (else I/O) */
-#define	HCD_LOCAL_MEM	0x0002		/* HC needs local memory */
-#define	HCD_USB11	0x0010		/* USB 1.1 */
-#define	HCD_USB2	0x0020		/* USB 2.0 */
-#define	HCD_USB3	0x0040		/* USB 3.0 */
-#define	HCD_MASK	0x0070
+#define	HCD_MEMORY		0x0001	/* HC regs use memory (else I/O) */
+#define	HCD_LOCAL_MEM		0x0002	/* HC needs local memory */
+#define	HCD_NO_COHERENT_MEM	0x0004	/* HC avoids use of "coherent" mem */
+#define	HCD_USB11		0x0010	/* USB 1.1 */
+#define	HCD_USB2		0x0020	/* USB 2.0 */
+#define	HCD_USB3		0x0040	/* USB 3.0 */
+#define	HCD_MASK		0x0070
 
 	/* called to init HCD and root hub */
 	int	(*reset) (struct usb_hcd *hcd);
Signature · 1 line
1.6.3.3
maintainer
#11 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 50 lines · Albert Herranz

The HCD_NO_COHERENT_MEM USB host controller driver flag can be enabled to instruct the USB stack to avoid allocating coherent memory for USB buffers.

This flag is useful to overcome some esoteric memory access restrictions found in some platforms.
For example, the Nintendo Wii video game console is a NOT_COHERENT_CACHE platform that is unable to safely perform non-32 bit uncached writes to RAM because the byte enables are not connected to the bus. Thus, in that platform, "coherent" DMA buffers cannot be directly used by the kernel code unless it guarantees that all write accesses to said buffers are done in 32 bit chunks (which is not the case in the USB subsystem).

To avoid this unwanted behaviour HCD_NO_COHERENT_MEM can be enabled at the HCD controller, causing USB buffer allocations to be satisfied from normal kernel memory. In this case, the USB stack will make sure that the buffers get properly mapped/unmapped for DMA transfers using the DMA streaming mapping API.

Note that other parts of the USB stack may also use coherent memory, like for example the hardware descriptors used in the EHCI controllers. This needs to be checked and addressed separately. In the EHCI example, hardware descriptors are accessed in 32-bit (or 64-bit) chunks, causing no problems in the described scenario.

drivers/usb/core/hcd.c +18 -0
@@ -1260,6 +1260,34 @@ static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
 	*dma_handle = 0;
 }
 
+static int urb_needs_setup_dma_map(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&
+		urb->setup_dma == ~(dma_addr_t)0);
+}
+
+static int urb_needs_setup_dma_unmap(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&
+		urb->setup_dma && urb->setup_dma != ~(dma_addr_t)0);
+}
+
+static int urb_needs_transfer_dma_map(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&

drivers/usb/core/hcd.c static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle, · line 1290

Quoted 10 lines
drivers/usb/core/hcd.c +10 -0
+		urb->transfer_dma == ~(dma_addr_t)0);
+}
+
+static int urb_needs_transfer_dma_unmap(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&
+		urb->transfer_dma && urb->transfer_dma != ~(dma_addr_t)0);
+}
+

These functions would be a lot easier to understand if they were expanded into multiple test and return statements, rather than squeezing all the Boolean manipulations into single expressions. (Not to mention the fact that other developement is going to make them even more complicated than they are now...)

Also, I can't help thinking that the corresponding *_map() and *_unmap() routines are so similar, it ought to be possible to combine them. The only difference is a check for a NULL DMA address, and it's not clear to me why it is present. It's also not clear why the test for a DMA address of all ones is present. Maybe they both can be removed.

Alan Stern

#12 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 32 lines · Alan Stern
Quoted 32 lines
drivers/usb/core/hcd.c +26 -0
@@ -1260,6 +1260,34 @@ static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
 	*dma_handle = 0;
 }
 
+static int urb_needs_setup_dma_map(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&
+		urb->setup_dma == ~(dma_addr_t)0);
+}
+
+static int urb_needs_setup_dma_unmap(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&
+		urb->setup_dma && urb->setup_dma != ~(dma_addr_t)0);
+}
+
+static int urb_needs_transfer_dma_map(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&
+		urb->transfer_dma == ~(dma_addr_t)0);
+}
+
+static int urb_needs_transfer_dma_unmap(struct usb_hcd *hcd, struct urb *urb)
+{
+	return !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
+	       ((hcd->driver->flags & HCD_NO_COHERENT_MEM) &&
+		urb->transfer_dma && urb->transfer_dma != ~(dma_addr_t)0);
Quoted 9 lines

drivers/usb/core/hcd.c static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle, · line 1290

Quoted 2 lines
drivers/usb/core/hcd.c +2 -0
+}
+

These functions would be a lot easier to understand if they were expanded into multiple test and return statements, rather than squeezing all the Boolean manipulations into single expressions. (Not to mention the fact that other developement is going to make them even more complicated than they are now...)

Yes, agreed. I'll enhance that, thanks.

Quoted 7 lines

Also, I can't help thinking that the corresponding *_map() and *_unmap() routines are so similar, it ought to be possible to combine them. The only difference is a check for a NULL DMA address, and it's not clear to me why it is present. It's also not clear why the test for a DMA address of all ones is present. Maybe they both can be removed.

I think too that I can simplify that logic.
I added those checks in a defensive way seeking robustness while I familiarize with the USB stack innards. So far, those cases are just avoiding mappings when urb_needs_transfer_dma_map()/urb_needs_transfer_dma_unmap() are called with urb->transfer_buffer == 0 and urb->transfer_dma == 0.

I guess that those cases are related to scatterlist-based urb requests. What should be the correct way to check if a urb has already been scatter/gather-mapped?

The final logic would be something like:
- map if URB_NO_TRANSFER_DMA_MAP is cleared
- otherwise (URB_TRANSFER_NO_DMA_MAP is set so) map if HCD_NO_COHERENT_MEM is set _and_ it's not a scatter/gather request (as that should have been mapped already by usb_buffer_map_sg())

Am I on the right path?

Quoted 2 lines

Alan Stern

Thanks,
Albert

maintainer
#13 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 15 lines · Albert Herranz
Quoted 7 lines

Also, I can't help thinking that the corresponding *_map() and *_unmap() routines are so similar, it ought to be possible to combine them. The only difference is a check for a NULL DMA address, and it's not clear to me why it is present. It's also not clear why the test for a DMA address of all ones is present. Maybe they both can be removed.

I think too that I can simplify that logic.
I added those checks in a defensive way seeking robustness while I familiarize with the USB stack innards. So far, those cases are just avoiding mappings when urb_needs_transfer_dma_map()/urb_needs_transfer_dma_unmap() are called with urb->transfer_buffer == 0 and urb->transfer_dma == 0.

Quoted 3 lines

I guess that those cases are related to scatterlist-based urb requests. What should be the correct way to check if a urb has already been scatter/gather-mapped?

If urb->num_sgs > 0 then urb has been s-g mapped. Although we don't currently check for it, quite a few URBs have transfer_buffer_length == 0 (a number of control requests are like this, for example) so they don't need a mapping either.

Quoted 7 lines

The final logic would be something like:
- map if URB_NO_TRANSFER_DMA_MAP is cleared
- otherwise (URB_TRANSFER_NO_DMA_MAP is set so) map if HCD_NO_COHERENT_MEM is set _and_ it's not a scatter/gather request (as that should have been mapped already by usb_buffer_map_sg())

Am I on the right path?

More or less. I would do it somewhat differently:

If URB_NO_TRANSFER_DMA_MAP is set then no map is needed. Otherwise if num_sgs > 0 then no map is needed. Otherwise if HCD_NO_COHERENT_MEM is set then use
hcd_alloc_coherent().
Otherwise if transfer_buffer_length > 0 then use
dma_map_single().

Similar logic is needed for the setup buffer mapping, but you can assume that control URBs never use scatter-gather so there's no need to check num_sgs (and there's no need to check the transfer length, since setup packets are always 8 bytes long).

In fact, I think URB_NO_SETUP_DMA_MAP doesn't really offer any worthwhile advantages. (About the only place where multiple control requests are used in rapid succession is during firmware transfers, and those aren't time-constrained.) It is currently used in a few drivers, but we ought to be able to remove it without too much effort. That might make a good project.

Alan Stern

#14 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 5 lines · Alan Stern

If urb->num_sgs > 0 then urb has been s-g mapped. Although we don't currently check for it, quite a few URBs have transfer_buffer_length == 0 (a number of control requests are like this, for example) so they don't need a mapping either.

Ok, I'll use urb->num_sgs > 0 to check for s-g mapped requests.

Quoted 8 lines
Quoted 7 lines

The final logic would be something like:
- map if URB_NO_TRANSFER_DMA_MAP is cleared
- otherwise (URB_TRANSFER_NO_DMA_MAP is set so) map if HCD_NO_COHERENT_MEM is set _and_ it's not a scatter/gather request (as that should have been mapped already by usb_buffer_map_sg())

Am I on the right path?

Quoted 9 lines

More or less. I would do it somewhat differently:

If URB_NO_TRANSFER_DMA_MAP is set then no map is needed. Otherwise if num_sgs > 0 then no map is needed. Otherwise if HCD_NO_COHERENT_MEM is set then use
hcd_alloc_coherent().
Otherwise if transfer_buffer_length > 0 then use
dma_map_single().

I think that logic is not quite right.
Remember that the final goal is to avoid allocating USB buffers from coherent memory (because the USB drivers access USB buffers without access restrictions but the platform I'm working on can't write to coherent memory unless it is done in 32-bit chunks). And we want to avoid bouncing at the USB layer too (that's what v1 did).

The strategy so far is:
- we have modified the USB buffer allocation routines hcd_buffer_alloc()/hcd_buffer_free() to always return normal kernel memory when HCD_NO_COHERENT_MEM is set on the host controller.
- during map_urb_for_dma()/unmap_urb_for_dma() we need to make sure that those USB buffers are sync'ed, even if we are told USB_NO_{SETUP,TRANSFER}_DMA_MAP

So the logic would be:

If URB_NO_TRANSFER_DMA_MAP is _cleared_ then do the mapping
- this case covers normal kernel memory used as a buffer and not already DMA mapped by a USB driver

Otherwise if HCD_NO_COHERENT_MEM is set _and_ num_sgs == 0 _and_ transfer_buffer_length > 0 then do the mapping too
- this case covers USB buffers allocated via usb_buffer_alloc() and marked URB_NO_TRANSFER_DMA_MAP by a USB driver, which are allocated from normal kernel memory when HCD_NO_COHERENT_MEM is set (we avoid bouncing buffers here too, at least if they sit already within MEM2 in the Wii, but anyway that's part of the platform DMA mapping code)

s-g urbs do not need a mapping as they have already been mapped, marked URB_NO_TRANSFER_DMA_MAP and have num_sgs > 0

Quoted 5 lines

Similar logic is needed for the setup buffer mapping, but you can assume that control URBs never use scatter-gather so there's no need to check num_sgs (and there's no need to check the transfer length, since setup packets are always 8 bytes long).

Quoted 7 lines

In fact, I think URB_NO_SETUP_DMA_MAP doesn't really offer any worthwhile advantages. (About the only place where multiple control requests are used in rapid succession is during firmware transfers, and those aren't time-constrained.) It is currently used in a few drivers, but we ought to be able to remove it without too much effort. That might make a good project.

I'll leave that projects to others or, in any case, address it later :)

Quoted 2 lines

Alan Stern

Thanks,
Albert

maintainer
#15 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 12 lines · Albert Herranz
Quoted 11 lines
Quoted 1 line

Am I on the right path?

More or less. I would do it somewhat differently:

If URB_NO_TRANSFER_DMA_MAP is set then no map is needed. Otherwise if num_sgs > 0 then no map is needed. Otherwise if HCD_NO_COHERENT_MEM is set then use
hcd_alloc_coherent().
Otherwise if transfer_buffer_length > 0 then use
dma_map_single().

Quoted 2 lines

I think that logic is not quite right.
Remember that the final goal is to avoid allocating USB buffers from coherent memory (because the USB drivers access USB buffers without access restrictions but the platform I'm working on can't write to coherent memory unless it is done in 32-bit chunks).

Actually the final goal is to make the mapping/unmapping algorithms clear and correct. One of the subgoals involves avoiding coherent USB buffers, but there are others as well (if you look back the through the linux-usb mailing list for the last few weeks you'll see a discussion about a controller which has to use PIO for control transfers but can use DMA for other types).

Quoted 9 lines

And we want to avoid bouncing at the USB layer too (that's what v1 did).

The strategy so far is:
- we have modified the USB buffer allocation routines hcd_buffer_alloc()/hcd_buffer_free() to always return normal kernel memory when HCD_NO_COHERENT_MEM is set on the host controller.
- during map_urb_for_dma()/unmap_urb_for_dma() we need to make sure that those USB buffers are sync'ed, even if we are told USB_NO_{SETUP,TRANSFER}_DMA_MAP

So the logic would be:

If URB_NO_TRANSFER_DMA_MAP is _cleared_ then do the mapping

No, that's wrong because it ignores the HCD_LOCAL_MEM flag.

Quoted 6 lines

- this case covers normal kernel memory used as a buffer and not already DMA mapped by a USB driver

Otherwise if HCD_NO_COHERENT_MEM is set _and_ num_sgs == 0 _and_ transfer_buffer_length > 0 then do the mapping too
- this case covers USB buffers allocated via usb_buffer_alloc() and marked URB_NO_TRANSFER_DMA_MAP by a USB driver, which are allocated from normal kernel memory when HCD_NO_COHERENT_MEM is set (we avoid bouncing buffers here too, at least if they sit already within MEM2 in the Wii, but anyway that's part of the platform DMA mapping code)

s-g urbs do not need a mapping as they have already been mapped, marked URB_NO_TRANSFER_DMA_MAP and have num_sgs > 0

Actually the test for transfer_buffer_length == 0 should be done first, since obviously no mapping is needed if there's no data. (And in fact the current code does do this; I was wrong earlier when I said it doesn't.)

So let's make things a little easier by first testing the conditions under which no mapping is needed:

If transfer_buffer_length is 0 then do nothing. Otherwise if num_sgs > 0 then do nothing. Otherwise if URB_NO_TRANSFER_DMA_MAP and transfer_dma
are both set (this avoids your HCD_NO_COHERENT_MEM case) then do nothing.

The remaining cases all need mapping and/or bounce buffers:

Otherwise if HCD_LOCAL_MEM is set then call hcd_alloc_coherent. Otherwise call dma_map_single.

Finally, the unmapping tests can be simplified greatly if the kind of mapping is recorded in the URB flags.

Alan Stern

#16 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 15 lines · Alan Stern
Quoted 10 lines · Albert Herranz
Quoted 10 lines
Quoted 1 line

Am I on the right path?

More or less. I would do it somewhat differently:

If URB_NO_TRANSFER_DMA_MAP is set then no map is needed. Otherwise if num_sgs > 0 then no map is needed. Otherwise if HCD_NO_COHERENT_MEM is set then use
hcd_alloc_coherent().
Otherwise if transfer_buffer_length > 0 then use
dma_map_single().

Quoted 2 lines

I think that logic is not quite right.
Remember that the final goal is to avoid allocating USB buffers from coherent memory (because the USB drivers access USB buffers without access restrictions but the platform I'm working on can't write to coherent memory unless it is done in 32-bit chunks).

Quoted 7 lines

Actually the final goal is to make the mapping/unmapping algorithms clear and correct. One of the subgoals involves avoiding coherent USB buffers, but there are others as well (if you look back the through the linux-usb mailing list for the last few weeks you'll see a discussion about a controller which has to use PIO for control transfers but can use DMA for other types).

Well, I was talking about our particular case here. I did not imply that we should forget about the other cases.

Quoted 6 lines
Quoted 6 lines

And we want to avoid bouncing at the USB layer too (that's what v1 did).

The strategy so far is:
- we have modified the USB buffer allocation routines hcd_buffer_alloc()/hcd_buffer_free() to always return normal kernel memory when HCD_NO_COHERENT_MEM is set on the host controller.
- during map_urb_for_dma()/unmap_urb_for_dma() we need to make sure that those USB buffers are sync'ed, even if we are told USB_NO_{SETUP,TRANSFER}_DMA_MAP

Quoted 6 lines
Quoted 3 lines

So the logic would be:

If URB_NO_TRANSFER_DMA_MAP is _cleared_ then do the mapping

No, that's wrong because it ignores the HCD_LOCAL_MEM flag.

When I said "do the mapping" there I meant to do a dma_map_single() if self.uses_dma, else if HCD_LOCAL_MEM is set then do a hcd_alloc_coherent(). I should have been more clear on that.

Quoted 12 lines
Quoted 6 lines

- this case covers normal kernel memory used as a buffer and not already DMA mapped by a USB driver

Otherwise if HCD_NO_COHERENT_MEM is set _and_ num_sgs == 0 _and_ transfer_buffer_length > 0 then do the mapping too
- this case covers USB buffers allocated via usb_buffer_alloc() and marked URB_NO_TRANSFER_DMA_MAP by a USB driver, which are allocated from normal kernel memory when HCD_NO_COHERENT_MEM is set (we avoid bouncing buffers here too, at least if they sit already within MEM2 in the Wii, but anyway that's part of the platform DMA mapping code)

s-g urbs do not need a mapping as they have already been mapped, marked URB_NO_TRANSFER_DMA_MAP and have num_sgs > 0

Actually the test for transfer_buffer_length == 0 should be done first, since obviously no mapping is needed if there's no data. (And in fact the current code does do this; I was wrong earlier when I said it doesn't.)

Quoted 9 lines

So let's make things a little easier by first testing the conditions under which no mapping is needed:

If transfer_buffer_length is 0 then do nothing. Otherwise if num_sgs > 0 then do nothing. Otherwise if URB_NO_TRANSFER_DMA_MAP and transfer_dma
are both set (this avoids your HCD_NO_COHERENT_MEM case) then do nothing.

I see. This case would include the PIO case too (for which dma_handle is set to all 1s).

So this assumes that transfer_dma should be set initially to 0 when allocating USB buffers for HCD_NO_COHERENT_MEM.

Quoted 8 lines

The remaining cases all need mapping and/or bounce buffers:

Otherwise if HCD_LOCAL_MEM is set then call hcd_alloc_coherent. Otherwise call dma_map_single.

Finally, the unmapping tests can be simplified greatly if the kind of mapping is recorded in the URB flags.

Good point.

Quoted 2 lines

Alan Stern

Thanks for your comments,
Albert

maintainer
#17 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 9 lines · Albert Herranz
Quoted 6 lines

If transfer_buffer_length is 0 then do nothing. Otherwise if num_sgs > 0 then do nothing. Otherwise if URB_NO_TRANSFER_DMA_MAP and transfer_dma
are both set (this avoids your HCD_NO_COHERENT_MEM case) then do nothing.

I see. This case would include the PIO case too (for which dma_handle is set to all 1s).

The test above should be transfer_dma != ~0, not transfer_dma != 0, since ~0 means the DMA address isn't set. In fact I forgot to include the PIO case; it should be handled by changing the remaining tests as follows:

Otherwise if hcd->self.uses_dma is set then
If this URB doesn't require PIO then call dma_map_single
Otherwise if HCD_LOCAL_MEM is set then call hcd_alloc_coherent Otherwise do nothing (PIO case).

Currently "this URB doesn't require PIO" is always true, but in the future it won't be.

Quoted 2 lines

So this assumes that transfer_dma should be set initially to 0 when allocating USB buffers for HCD_NO_COHERENT_MEM.

No, it should be set to ~0, the same as when buffers are allocated for a PIO-based controller.

Alan Stern

#18 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 16 lines · Alan Stern
Quoted 8 lines · Albert Herranz
Quoted 6 lines

If transfer_buffer_length is 0 then do nothing. Otherwise if num_sgs > 0 then do nothing. Otherwise if URB_NO_TRANSFER_DMA_MAP and transfer_dma
are both set (this avoids your HCD_NO_COHERENT_MEM case) then do nothing.

I see. This case would include the PIO case too (for which dma_handle is set to all 1s).

The test above should be transfer_dma != ~0, not transfer_dma != 0, since ~0 means the DMA address isn't set. In fact I forgot to include the PIO case; it should be handled by changing the remaining tests as follows:

Quoted 8 lines

Otherwise if hcd->self.uses_dma is set then
If this URB doesn't require PIO then call dma_map_single
Otherwise if HCD_LOCAL_MEM is set then call hcd_alloc_coherent Otherwise do nothing (PIO case).

Currently "this URB doesn't require PIO" is always true, but in the future it won't be.

Can this be currently tested?
Should I make provisions for this check now too?

Quoted 6 lines
Quoted 2 lines

So this assumes that transfer_dma should be set initially to 0 when allocating USB buffers for HCD_NO_COHERENT_MEM.

No, it should be set to ~0, the same as when buffers are allocated for a PIO-based controller.

This logic now resembles more the one in my v2 proposal although with different formal checks.
I think I'll code and post another iteration of the 8/9 patch with your proposed checks and then we can continue further discussion on it. I'll try to add explanatory comments for each check.

Quoted 2 lines

Alan Stern

Thanks a lot for your input,
Albert

maintainer
#19 lore raw

RFC PATCH v2 8/9 USB: add HCD_NO_COHERENT_MEM host controller driver flag

Quoted 5 lines · Albert Herranz
Quoted 3 lines

Currently "this URB doesn't require PIO" is always true, but in the future it won't be.

Can this be currently tested?

Not yet, but soon. You can follow the gory details in this email thread:

	http://marc.info/?l=linux-usb&m=126477569707174&w=2

See especially this email:

	http://marc.info/?l=linux-usb&m=126630714002200&w=2
Quoted 1 line

Should I make provisions for this check now too?

Don't worry about it. If you structure the code as I described, it will be easy to insert the check later on.

Alan Stern

#20 lore raw

RFC PATCH v2 9/9 wii: hollywood ehci controller support

In reply to Albert Herranz

Add support for the USB Enhanced Host Controller Interface included in the "Hollywood" chipset of the Nintendo Wii video game console.

---
 arch/powerpc/platforms/embedded6xx/Kconfig |    1 +
 drivers/usb/host/Kconfig                   |    8 +
 drivers/usb/host/ehci-hcd.c                |    5 +
 drivers/usb/host/ehci-hlwd.c               |  233 ++++++++++++++++++++++++++++
 drivers/usb/host/ehci.h                    |   23 +++
 5 files changed, 270 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/host/ehci-hlwd.c
arch/powerpc/platforms/embedded6xx/Kconfig +1 -0
index 4d33755..0eb56a7 100644
@@ -121,6 +121,7 @@ config WII
 	select GAMECUBE_COMMON
 	select HAVE_GENERIC_DMA_COHERENT
 	select HAVE_DMABOUNCE
+	select USB_ARCH_HAS_EHCI
 	help
 	  Select WII if configuring for the Nintendo Wii.
 	  More information at: <http://gc-linux.sourceforge.net/>
drivers/usb/host/Kconfig +8 -0
index 2678a16..342954f 100644
@@ -131,6 +131,14 @@ config USB_EHCI_HCD_PPC_OF
 	  Enables support for the USB controller present on the PowerPC
 	  OpenFirmware platform bus.
 
+config USB_EHCI_HCD_HLWD
+	bool "Nintendo Wii (Hollywood) EHCI USB controller support"
+	depends on USB_EHCI_HCD && WII
+	default y
+	---help---
+	  Say Y here to support the EHCI USB controller found in the
+	  Hollywood chipset of the Nintendo Wii video game console.
+
 config USB_W90X900_EHCI
 	bool "W90X900(W90P910) EHCI support"
 	depends on USB_EHCI_HCD && ARCH_W90X900
drivers/usb/host/ehci-hcd.c +5 -0
index 1ec3857..395c6a1 100644
@@ -1133,6 +1133,11 @@ MODULE_LICENSE ("GPL");
 #define OF_PLATFORM_DRIVER	ehci_hcd_ppc_of_driver
 #endif
 
+#ifdef CONFIG_USB_EHCI_HCD_HLWD
+#include "ehci-hlwd.c"
+#define OF_PLATFORM_DRIVER	ehci_hcd_hlwd_driver
+#endif
+
 #ifdef CONFIG_XPS_USB_HCD_XILINX
 #include "ehci-xilinx-of.c"
 #define OF_PLATFORM_DRIVER	ehci_hcd_xilinx_of_driver
drivers/usb/host/ehci-hlwd.c new file +233 -0

233 lines not on this page · raw message

drivers/usb/host/ehci.h +23 -0
index 2d85e21..8fb519b 100644
@@ -599,6 +599,27 @@ ehci_port_speed(struct ehci_hcd *ehci, unsigned int portsc)
 #define ehci_big_endian_mmio(e)		0
 #endif
 
+#ifdef CONFIG_USB_EHCI_HCD_HLWD
+
+/*
+ * The Nintendo Wii video game console has no PCI hardware.
+ * The USB controllers are part of the "Hollywood" chipset and are
+ * accessed via the platform internal I/O accessors.
+ */
+static inline unsigned int ehci_readl(const struct ehci_hcd *ehci,
+				      __u32 __iomem *regs)
+{
+	return in_be32(regs);
+}
+
+static inline void ehci_writel(const struct ehci_hcd *ehci,
+			       const unsigned int val, __u32 __iomem *regs)
+{
+	out_be32(regs, val);
+}
+
+#else
+
 /*
  * Big-endian read/write functions are arch-specific.
  * Other arches can be added if/when they're needed.
@@ -632,6 +653,8 @@ static inline void ehci_writel(const struct ehci_hcd *ehci,
 #endif
 }
 
+#endif /* CONFIG_USB_EHCI_HCD_HLWD */
+
 /*
  * On certain ppc-44x SoC there is a HW issue, that could only worked around with
  * explicit suspend/operate of OHCI. This function hereby makes sense only on that arch.
Signature · 1 line
1.6.3.3