ignatenkobrain / rpms / kernel

Forked from rpms/kernel 4 years ago
Clone
f2c60e
From patchwork Thu Aug  3 15:52:08 2017
f2c60e
Content-Type: text/plain; charset="utf-8"
f2c60e
MIME-Version: 1.0
f2c60e
Content-Transfer-Encoding: 7bit
f2c60e
Subject: [v3] dma-mapping: skip USB devices when configuring DMA during probe
f2c60e
From: Johan Hovold <johan@kernel.org>
f2c60e
X-Patchwork-Id: 9879371
f2c60e
Message-Id: <20170803155208.22165-1-johan@kernel.org>
f2c60e
To: Christoph Hellwig <hch@lst.de>,
f2c60e
 Marek Szyprowski <m.szyprowski@samsung.com>,
f2c60e
 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
f2c60e
Cc: =?UTF-8?q?Andreas=20F=C3=A4rber?= <afaerber@suse.de>,
f2c60e
 linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
f2c60e
 Alan Stern <stern@rowland.harvard.edu>, Johan Hovold <johan@kernel.org>,
f2c60e
 stable <stable@vger.kernel.org>, Robin Murphy <robin.murphy@arm.com>,
f2c60e
 Sricharan R <sricharan@codeaurora.org>,
f2c60e
 Stefan Wahren <stefan.wahren@i2se.com>
f2c60e
Date: Thu,  3 Aug 2017 17:52:08 +0200
f2c60e
f2c60e
USB devices use the DMA mask and offset of the controller, which have
f2c60e
already been setup when a device is probed. Note that modifying the
f2c60e
DMA mask of a USB device would change the mask for the controller (and
f2c60e
all devices on the bus) as the mask is literally shared.
f2c60e
f2c60e
Since commit 2bf698671205 ("USB: of: fix root-hub device-tree node
f2c60e
handling"), of_dma_configure() would be called also for root hubs, which
f2c60e
use the device node of the controller. A separate, long-standing bug
f2c60e
that makes of_dma_configure() generate a 30-bit DMA mask from the RPI3's
f2c60e
"dma-ranges" would thus set a broken mask also for the controller. This
f2c60e
in turn prevents USB devices from enumerating when control transfers
f2c60e
fail:
f2c60e
f2c60e
	dwc2 3f980000.usb: Cannot do DMA to address 0x000000003a166a00
f2c60e
f2c60e
Note that the aforementioned DMA-mask bug was benign for the HCD itself
f2c60e
as the dwc2 driver overwrites the mask previously set by
f2c60e
of_dma_configure() for the platform device in its probe callback. The
f2c60e
mask would only later get corrupted when the root-hub child device was
f2c60e
probed.
f2c60e
f2c60e
Fix this, and similar future problems, by adding a flag to struct device
f2c60e
which prevents driver core from calling dma_configure() during probe and
f2c60e
making sure it is set for USB devices.
f2c60e
f2c60e
Fixes: 09515ef5ddad ("of/acpi: Configure dma operations at probe time for platform/amba/pci bus devices")
f2c60e
Cc: stable <stable@vger.kernel.org>	# 4.12
f2c60e
Cc: Robin Murphy <robin.murphy@arm.com>
f2c60e
Cc: Sricharan R <sricharan@codeaurora.org>
f2c60e
Cc: Stefan Wahren <stefan.wahren@i2se.com>
f2c60e
Reported-by: Hans Verkuil <hverkuil@xs4all.nl>
f2c60e
Signed-off-by: Johan Hovold <johan@kernel.org>
f2c60e
---
f2c60e
f2c60e
v3
f2c60e
 - add flag to struct device to prevent DMA configuration during probe instead
f2c60e
   of checking for the USB bus type, which is not available when USB is built
f2c60e
   as a module as noted by Alan
f2c60e
 - drop moderated rpi list from CC
f2c60e
f2c60e
v2
f2c60e
 - amend commit message and point out that the long-standing 30-bit DMA-mask
f2c60e
   bug was benign to the dwc2 HCD itself (Robin)
f2c60e
 - add and use a new dev_is_usb() helper (Robin)
f2c60e
f2c60e
f2c60e
 drivers/base/dma-mapping.c | 6 ++++++
f2c60e
 drivers/usb/core/usb.c     | 1 +
f2c60e
 include/linux/device.h     | 3 +++
f2c60e
 3 files changed, 10 insertions(+)
f2c60e
f2c60e
diff --git a/drivers/base/dma-mapping.c b/drivers/base/dma-mapping.c
f2c60e
index b555ff9dd8fc..f9f703be0ad1 100644
f2c60e
--- a/drivers/base/dma-mapping.c
f2c60e
+++ b/drivers/base/dma-mapping.c
f2c60e
@@ -345,6 +345,9 @@ int dma_configure(struct device *dev)
f2c60e
 	enum dev_dma_attr attr;
f2c60e
 	int ret = 0;
f2c60e
 
f2c60e
+	if (dev->skip_dma_configure)
f2c60e
+		return 0;
f2c60e
+
f2c60e
 	if (dev_is_pci(dev)) {
f2c60e
 		bridge = pci_get_host_bridge_device(to_pci_dev(dev));
f2c60e
 		dma_dev = bridge;
f2c60e
@@ -369,6 +372,9 @@ int dma_configure(struct device *dev)
f2c60e
 
f2c60e
 void dma_deconfigure(struct device *dev)
f2c60e
 {
f2c60e
+	if (dev->skip_dma_configure)
f2c60e
+		return;
f2c60e
+
f2c60e
 	of_dma_deconfigure(dev);
f2c60e
 	acpi_dma_deconfigure(dev);
f2c60e
 }
f2c60e
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
f2c60e
index 17681d5638ac..2a85d905b539 100644
f2c60e
--- a/drivers/usb/core/usb.c
f2c60e
+++ b/drivers/usb/core/usb.c
f2c60e
@@ -588,6 +588,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
f2c60e
 	 * Note: calling dma_set_mask() on a USB device would set the
f2c60e
 	 * mask for the entire HCD, so don't do that.
f2c60e
 	 */
f2c60e
+	dev->dev.skip_dma_configure = true;
f2c60e
 	dev->dev.dma_mask = bus->sysdev->dma_mask;
f2c60e
 	dev->dev.dma_pfn_offset = bus->sysdev->dma_pfn_offset;
f2c60e
 	set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
f2c60e
diff --git a/include/linux/device.h b/include/linux/device.h
f2c60e
index 723cd54b94da..022cf258068b 100644
f2c60e
--- a/include/linux/device.h
f2c60e
+++ b/include/linux/device.h
f2c60e
@@ -877,6 +877,8 @@ struct dev_links_info {
f2c60e
  * @offline:	Set after successful invocation of bus type's .offline().
f2c60e
  * @of_node_reused: Set if the device-tree node is shared with an ancestor
f2c60e
  *              device.
f2c60e
+ * @skip_dma_configure: Set if driver core should not configure DMA for this
f2c60e
+ *              device during probe.
f2c60e
  *
f2c60e
  * At the lowest level, every device in a Linux system is represented by an
f2c60e
  * instance of struct device. The device structure contains the information
f2c60e
@@ -965,6 +967,7 @@ struct device {
f2c60e
 	bool			offline_disabled:1;
f2c60e
 	bool			offline:1;
f2c60e
 	bool			of_node_reused:1;
f2c60e
+	bool			skip_dma_configure:1;
f2c60e
 };
f2c60e
 
f2c60e
 static inline struct device *kobj_to_dev(struct kobject *kobj)