All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
@ 2019-02-05 11:06 Thierry Reding
  2019-02-05 16:10 ` Christoph Hellwig
  0 siblings, 1 reply; 9+ messages in thread
From: Thierry Reding @ 2019-02-05 11:06 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Marek Szyprowski, Robin Murphy, iommu, linux-kernel

From: Thierry Reding <treding@nvidia.com>

Drivers that are built as modules may want to use these functions, so
make them available like the rest of the functions.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 kernel/dma/direct.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 25bd19974223..f14b7ced4592 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -200,6 +200,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
 		return arch_dma_alloc(dev, size, dma_handle, gfp, attrs);
 	return dma_direct_alloc_pages(dev, size, dma_handle, gfp, attrs);
 }
+EXPORT_SYMBOL(dma_direct_alloc);
 
 void dma_direct_free(struct device *dev, size_t size,
 		void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs)
@@ -209,6 +210,7 @@ void dma_direct_free(struct device *dev, size_t size,
 	else
 		dma_direct_free_pages(dev, size, cpu_addr, dma_addr, attrs);
 }
+EXPORT_SYMBOL(dma_direct_free);
 
 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
     defined(CONFIG_SWIOTLB)
-- 
2.19.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 11:06 [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free() Thierry Reding
@ 2019-02-05 16:10 ` Christoph Hellwig
  2019-02-05 16:20   ` Thierry Reding
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2019-02-05 16:10 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Christoph Hellwig, Marek Szyprowski, Robin Murphy, iommu, linux-kernel

On Tue, Feb 05, 2019 at 12:06:02PM +0100, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> Drivers that are built as modules may want to use these functions, so
> make them available like the rest of the functions.
> 
> Signed-off-by: Thierry Reding <treding@nvidia.com>

How do they want to use these functions?  The proper way is to
call dma_alloc_coherent / dma_alloc_free and let the DMA API
handle the rest.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 16:10 ` Christoph Hellwig
@ 2019-02-05 16:20   ` Thierry Reding
  2019-02-05 16:38     ` Christoph Hellwig
  0 siblings, 1 reply; 9+ messages in thread
From: Thierry Reding @ 2019-02-05 16:20 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Marek Szyprowski, Robin Murphy, iommu, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1580 bytes --]

On Tue, Feb 05, 2019 at 05:10:36PM +0100, Christoph Hellwig wrote:
> On Tue, Feb 05, 2019 at 12:06:02PM +0100, Thierry Reding wrote:
> > From: Thierry Reding <treding@nvidia.com>
> > 
> > Drivers that are built as modules may want to use these functions, so
> > make them available like the rest of the functions.
> > 
> > Signed-off-by: Thierry Reding <treding@nvidia.com>
> 
> How do they want to use these functions?  The proper way is to
> call dma_alloc_coherent / dma_alloc_free and let the DMA API
> handle the rest.

This is again for the host1x driver and Tegra DRM where we had a similar
situation a little while ago where we needed to explicitly detach from a
DMA/IOMMU mapping.

I ran into a similar situation on Tegra186 (which has an ARM SMMU). The
host1x driver needs to allocate memory for a push buffer that commands
are written to. This push buffer is mapped translated through the SMMU
using direct IOMMU API usage. We use the same domain to also map command
buffers that userspace can pass in.

The problem is that if I use dma_alloc_coherent(), then the memory will
already be mapped via the SMMU at that point and then the driver, not
knowing that memory has already been mapped, will attempt to map an IOVA
which will cause an SMMU fault when the host1x tries to access the
memory.

I didn't find an equivalent to arm_iommu_detach_device() for non-ARM,
but then stumbled across this and thought it was rather convenient for
these cases. If there's a better way to deal with this situation, I'd be
happy to do so.

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 16:20   ` Thierry Reding
@ 2019-02-05 16:38     ` Christoph Hellwig
  2019-02-05 17:56       ` Thierry Reding
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2019-02-05 16:38 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Christoph Hellwig, Marek Szyprowski, Robin Murphy, iommu, linux-kernel

On Tue, Feb 05, 2019 at 05:20:57PM +0100, Thierry Reding wrote:
> The problem is that if I use dma_alloc_coherent(), then the memory will
> already be mapped via the SMMU at that point and then the driver, not
> knowing that memory has already been mapped, will attempt to map an IOVA
> which will cause an SMMU fault when the host1x tries to access the
> memory.
> 
> I didn't find an equivalent to arm_iommu_detach_device() for non-ARM,
> but then stumbled across this and thought it was rather convenient for
> these cases. If there's a better way to deal with this situation, I'd be
> happy to do so.

So you basically do a dma_direct_alloc + iommu_map?  Can you send me
a pointer to your code?  Maybe we need to add a proper IOMMU-layer
API for that.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 16:38     ` Christoph Hellwig
@ 2019-02-05 17:56       ` Thierry Reding
  2019-02-05 18:02         ` Christoph Hellwig
  0 siblings, 1 reply; 9+ messages in thread
From: Thierry Reding @ 2019-02-05 17:56 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Marek Szyprowski, Robin Murphy, iommu, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1010 bytes --]

On Tue, Feb 05, 2019 at 05:38:37PM +0100, Christoph Hellwig wrote:
> On Tue, Feb 05, 2019 at 05:20:57PM +0100, Thierry Reding wrote:
> > The problem is that if I use dma_alloc_coherent(), then the memory will
> > already be mapped via the SMMU at that point and then the driver, not
> > knowing that memory has already been mapped, will attempt to map an IOVA
> > which will cause an SMMU fault when the host1x tries to access the
> > memory.
> > 
> > I didn't find an equivalent to arm_iommu_detach_device() for non-ARM,
> > but then stumbled across this and thought it was rather convenient for
> > these cases. If there's a better way to deal with this situation, I'd be
> > happy to do so.
> 
> So you basically do a dma_direct_alloc + iommu_map?  Can you send me
> a pointer to your code?  Maybe we need to add a proper IOMMU-layer
> API for that.

Sure, here you go:

	https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/drivers/gpu/host1x/cdma.c#n106

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 17:56       ` Thierry Reding
@ 2019-02-05 18:02         ` Christoph Hellwig
  2019-02-05 22:29           ` Thierry Reding
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2019-02-05 18:02 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Christoph Hellwig, Marek Szyprowski, Robin Murphy, iommu, linux-kernel

On Tue, Feb 05, 2019 at 06:56:11PM +0100, Thierry Reding wrote:
> Sure, here you go:
> 
> 	https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/drivers/gpu/host1x/cdma.c#n106

Yes, I think we I can come up with a nicer helper for that.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 18:02         ` Christoph Hellwig
@ 2019-02-05 22:29           ` Thierry Reding
  2019-02-06  6:57             ` Christoph Hellwig
  2019-02-06  7:37             ` Marek Szyprowski
  0 siblings, 2 replies; 9+ messages in thread
From: Thierry Reding @ 2019-02-05 22:29 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Marek Szyprowski, Robin Murphy, iommu, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 820 bytes --]

On Tue, Feb 05, 2019 at 07:02:18PM +0100, Christoph Hellwig wrote:
> On Tue, Feb 05, 2019 at 06:56:11PM +0100, Thierry Reding wrote:
> > Sure, here you go:
> > 
> > 	https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/drivers/gpu/host1x/cdma.c#n106
> 
> Yes, I think we I can come up with a nicer helper for that.

One thing I could also try is to remove direct IOMMU API usage at least
from the host1x driver. I think this might work nowadays.

For Tegra DRM we still need direct IOMMU API usage because we need to be
able to map into an IOMMU domain without knowing the struct device *
that we're mapping for (it could be needed by any of up to four display
controllers). For host1x we always only have one struct device *, so the
DMA mapping API should be good enough.

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 22:29           ` Thierry Reding
@ 2019-02-06  6:57             ` Christoph Hellwig
  2019-02-06  7:37             ` Marek Szyprowski
  1 sibling, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2019-02-06  6:57 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Christoph Hellwig, Marek Szyprowski, Robin Murphy, iommu, linux-kernel

On Tue, Feb 05, 2019 at 11:29:12PM +0100, Thierry Reding wrote:
> > > 	https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/drivers/gpu/host1x/cdma.c#n106
> > 
> > Yes, I think we I can come up with a nicer helper for that.
> 
> One thing I could also try is to remove direct IOMMU API usage at least
> from the host1x driver. I think this might work nowadays.
> 
> For Tegra DRM we still need direct IOMMU API usage because we need to be
> able to map into an IOMMU domain without knowing the struct device *
> that we're mapping for (it could be needed by any of up to four display
> controllers). For host1x we always only have one struct device *, so the
> DMA mapping API should be good enough.

If you can convert it to plain DMA API usage, please do.

I did look into an IOMMU API memory allocator, and while we can do it
easily for coherent devices, we need some arch hooks for non-coherent
device support.  With a pending series from me we have those for arm64,
but 32-bit arm support will require a lot more work first.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free()
  2019-02-05 22:29           ` Thierry Reding
  2019-02-06  6:57             ` Christoph Hellwig
@ 2019-02-06  7:37             ` Marek Szyprowski
  1 sibling, 0 replies; 9+ messages in thread
From: Marek Szyprowski @ 2019-02-06  7:37 UTC (permalink / raw)
  To: Thierry Reding, Christoph Hellwig; +Cc: Robin Murphy, iommu, linux-kernel

Hi Thierry,

On 2019-02-05 23:29, Thierry Reding wrote:
> On Tue, Feb 05, 2019 at 07:02:18PM +0100, Christoph Hellwig wrote:
>> On Tue, Feb 05, 2019 at 06:56:11PM +0100, Thierry Reding wrote:
>>> Sure, here you go:
>>>
>>> 	https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/drivers/gpu/host1x/cdma.c#n106
>> Yes, I think we I can come up with a nicer helper for that.
> One thing I could also try is to remove direct IOMMU API usage at least
> from the host1x driver. I think this might work nowadays.
>
> For Tegra DRM we still need direct IOMMU API usage because we need to be
> able to map into an IOMMU domain without knowing the struct device *
> that we're mapping for (it could be needed by any of up to four display
> controllers). For host1x we always only have one struct device *, so the
> DMA mapping API should be good enough.

In case of Tegra DRM you may try to use Exynos DRM approach. Exynos DRM
also consists of more than one DMA capable device and uses one common
IOMMU address space for them. It is achieved by attaching all those
devices to the IOMMU domain of the first CRTC device that has been
registered. Then that device is used for DMA-mapping calls. It is not
very elegant, but works fine and allows to use standard DMA-mapping calls.

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2019-02-06  7:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-05 11:06 [PATCH] dma-direct: Export dma_direct_alloc() and dma_direct_free() Thierry Reding
2019-02-05 16:10 ` Christoph Hellwig
2019-02-05 16:20   ` Thierry Reding
2019-02-05 16:38     ` Christoph Hellwig
2019-02-05 17:56       ` Thierry Reding
2019-02-05 18:02         ` Christoph Hellwig
2019-02-05 22:29           ` Thierry Reding
2019-02-06  6:57             ` Christoph Hellwig
2019-02-06  7:37             ` Marek Szyprowski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.