Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753179AbcDSLkO (ORCPT ); Tue, 19 Apr 2016 07:40:14 -0400 Received: from mga14.intel.com ([192.55.52.115]:31027 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752278AbcDSLkM (ORCPT ); Tue, 19 Apr 2016 07:40:12 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,506,1455004800"; d="asc'?scan'208";a="787859330" From: Felipe Balbi To: David Woodhouse , Vinod Koul Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Subject: [RFC] DMA initialization for manually created devices User-Agent: Notmuch/0.21+96~g9bbc54b (http://notmuchmail.org) Emacs/25.0.90.3 (x86_64-pc-linux-gnu) Date: Tue, 19 Apr 2016 14:38:09 +0300 Message-ID: <87bn55esm6.fsf@intel.com> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4558 Lines: 131 --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Hi folks, Today I noticed two issues with dwc3 on PCI-based systems which, while debugging, I uncovered some details which we might want to change, however I need a little guidance here. The first problem is that when running with intel-iommu disable and falling back to swiotlb, I can easily run out of space for DMA mapping/unmapping: [ 574.862949] DMA: Out of SW-IOMMU space for 217 bytes at device dwc3.0.au= to [ 574.870820] dwc3 dwc3.0.auto: failed to map buffer I checked that I'm not leaking any of the mapped buffers and they're all balanced with a matching unmap call. The second problem is that when enabling intel-iommu then I can't allocate from coherent: [ 81.797657] DMAR: Allocating domain for dwc3.0.auto failed [ 81.803980] dwc3 dwc3.0.auto: can't allocate event buffer [ 81.810221] dwc3 dwc3.0.auto: failed to allocate event buffers The reason for that I'm using a manually created platform_device and that misses dev->archdata which the underlying/parent PCI device has. Here I have two options: 1) continue to use my manually allocated platform_device pointer for DMA operations and just copy necessary bits from the parent PCI device: diff --git a/drivers/usb/dwc3/dwc3-pci.c b/drivers/usb/dwc3/dwc3-pci.c index adc1e8a624cb..011d0055abd0 100644 =2D-- a/drivers/usb/dwc3/dwc3-pci.c +++ b/drivers/usb/dwc3/dwc3-pci.c @@ -174,6 +174,14 @@ static int dwc3_pci_probe(struct pci_dev *pci, if (ret) goto err; =20 + dwc3->dev.dma_mask =3D &pci->dma_mask; + dwc3->dev.dma_parms =3D &pci->dma_parms; + + /* is there a better way ?? */ + memcpy(&dwc3->dev.archdata, &dev->archdata, sizeof(dev->archdata)); + + dma_set_coherent_mask(&dwc3->dev, pci->dma_mask); + dwc3->dev.parent =3D dev; ACPI_COMPANION_SET(&dwc3->dev, ACPI_COMPANION(dev)); =20 This works fine with intel-iommu, I just tested. 2) map/unmap using the parent PCI device. IOW: diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c index 143deb420481..a4e4b0417bf3 100644 =2D-- a/drivers/usb/dwc3/ep0.c +++ b/drivers/usb/dwc3/ep0.c @@ -967,7 +967,7 @@ static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, u32 transfer_size =3D 0; u32 maxpacket; =20 =2D ret =3D usb_gadget_map_request(&dwc->gadget, &req->request, + ret =3D usb_gadget_map_request_by_dev(dwc->dev, parent, &req->request, dep->number); if (ret) { dwc3_trace(trace_dwc3_ep0, "failed to map request\n"); @@ -995,7 +995,7 @@ static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, dwc->ep0_bounce_addr, transfer_size, DWC3_TRBCTL_CONTROL_DATA, false); } else { =2D ret =3D usb_gadget_map_request(&dwc->gadget, &req->request, + ret =3D usb_gadget_map_request_by_dev(dwc->dev, parent, &req->request, dep->number); if (ret) { dwc3_trace(trace_dwc3_ep0, "failed to map request\n"); diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index e39f29bd2fff..0732d14d2687 100644 =2D-- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -191,7 +191,7 @@ void dwc3_gadget_giveback(struct dwc3_ep *dep, struct d= wc3_request *req, if (dwc->ep0_bounced && dep->number =3D=3D 0) dwc->ep0_bounced =3D false; else =2D usb_gadget_unmap_request(&dwc->gadget, &req->request, + usb_gadget_unmap_request_by_dev(dwc->dev->parent, &req->request, req->direction); =20 trace_dwc3_gadget_giveback(req); This I haven't tested, but it should work. Anyway, the question is: which of the two approaches is preferred ? cheers ps: I haven't debugged why I'm swiotlb error, that's in my list for after $subject gets solved. =2D-=20 balbi --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJXFhiiAAoJEIaOsuA1yqREFb0QAJVdNvEBt3XfDQNkDHQKzZXW vbsnfkSpH/Nsua7yp+8fEefLcCnYgB+duvBMV7R9Fw6TUCUlUON9ZT9c8vQ7q53g w7D3Sxs3yRHQBc4ZZ785S322EI/uMLSC3Q9CIVRqwfGzCi2uGbcYjJpomraU7d/0 iBm6UvydoBZke+PUPtg0FM++EwnwKDQXNyrGAmfkoZahmUuiLVcLXg6E4iTAgofv EB0nePRGRpGz2v8CRaJNKegF0fkdFmMa2eoNYMIfhdH8wqsiHkjXF3Bu2jokITqx A1LfulRajILAmdDeJbJfa/d4jkVds/MESkKFSLkfNNJB3x7ayXB5w0D9eyDyle2A vH5PFhP6wfnyq0yViPmErpi5Z28VBlGJal1sywQfAnEHLNVce7KWoS2k/4205OcP rZV8hM0NG1o0IGSI3UyayiogPSEglkC66KynlPw42aYLA16clBYMR9KZdtotZ3rH X/QvKAqpwJDQ4cM4AzQs5yC0PF0uG8DJcO7eIGfJQx9vQXulHqmZ0CUbFQAmyFGZ BUao5Xw/pGAhPpcJdZefxuCqRW9GFuhFf/U/22+usuMa7XjlJeKmOy6ngP+7SnCW yBurnQYtR11usKq/GS10UM+QlmJIeSrsGhhjk+q4HPjf1vKlD15GQKACT6ep/Tid PdM9/ZNdp3squXKZ5oSc =hnTs -----END PGP SIGNATURE----- --=-=-=--