diff mbox series

[09/10] sh: don't allow non-coherent DMA for NOMMU

Message ID 20200714121856.955680-10-hch@lst.de (mailing list archive)
State New, archived
Headers show
Series [01/10] sh: remove -Werror from Makefiles | expand

Commit Message

Christoph Hellwig July 14, 2020, 12:18 p.m. UTC
The code handling non-coherent DMA depends on being able to remap code
as non-cached.  But that can't be done without an MMU, so using this
option on NOMMU builds is broken.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 arch/sh/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Rich Felker Aug. 28, 2020, 2 a.m. UTC | #1
On Tue, Jul 14, 2020 at 02:18:55PM +0200, Christoph Hellwig wrote:
> The code handling non-coherent DMA depends on being able to remap code
> as non-cached.  But that can't be done without an MMU, so using this
> option on NOMMU builds is broken.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  arch/sh/Kconfig | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/sh/Kconfig b/arch/sh/Kconfig
> index f8027eee08edae..337eb496c45a0a 100644
> --- a/arch/sh/Kconfig
> +++ b/arch/sh/Kconfig
> @@ -61,6 +61,7 @@ config SUPERH
>  	select MAY_HAVE_SPARSE_IRQ
>  	select MODULES_USE_ELF_RELA
>  	select NEED_SG_DMA_LENGTH
> +	select NO_DMA if !MMU && !DMA_COHERENT
>  	select NO_GENERIC_PCI_IOPORT_MAP if PCI
>  	select OLD_SIGACTION
>  	select OLD_SIGSUSPEND
> @@ -135,7 +136,7 @@ config DMA_COHERENT
>  	bool

This change broke SD card support on J2 because MMC_SPI spuriously
depends on HAS_DMA. It looks like it can be fixed just by removing
that dependency from drivers/mmc/host/Kconfig.

Rich
Rich Felker Aug. 28, 2020, 2:11 a.m. UTC | #2
On Thu, Aug 27, 2020 at 10:00:48PM -0400, Rich Felker wrote:
> On Tue, Jul 14, 2020 at 02:18:55PM +0200, Christoph Hellwig wrote:
> > The code handling non-coherent DMA depends on being able to remap code
> > as non-cached.  But that can't be done without an MMU, so using this
> > option on NOMMU builds is broken.
> > 
> > Signed-off-by: Christoph Hellwig <hch@lst.de>
> > ---
> >  arch/sh/Kconfig | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/sh/Kconfig b/arch/sh/Kconfig
> > index f8027eee08edae..337eb496c45a0a 100644
> > --- a/arch/sh/Kconfig
> > +++ b/arch/sh/Kconfig
> > @@ -61,6 +61,7 @@ config SUPERH
> >  	select MAY_HAVE_SPARSE_IRQ
> >  	select MODULES_USE_ELF_RELA
> >  	select NEED_SG_DMA_LENGTH
> > +	select NO_DMA if !MMU && !DMA_COHERENT
> >  	select NO_GENERIC_PCI_IOPORT_MAP if PCI
> >  	select OLD_SIGACTION
> >  	select OLD_SIGSUSPEND
> > @@ -135,7 +136,7 @@ config DMA_COHERENT
> >  	bool
> 
> This change broke SD card support on J2 because MMC_SPI spuriously
> depends on HAS_DMA. It looks like it can be fixed just by removing
> that dependency from drivers/mmc/host/Kconfig.

It can't. mmp_spi_probe fails with ENOMEM, probably due to trying to
do some DMA setup thing that's not going to be needed if the
underlying SPI device doesn't support/use DMA.

Rich
Christoph Hellwig Aug. 28, 2020, 4:24 a.m. UTC | #3
On Thu, Aug 27, 2020 at 10:11:53PM -0400, Rich Felker wrote:
> > This change broke SD card support on J2 because MMC_SPI spuriously
> > depends on HAS_DMA. It looks like it can be fixed just by removing
> > that dependency from drivers/mmc/host/Kconfig.
> 
> It can't. mmp_spi_probe fails with ENOMEM, probably due to trying to
> do some DMA setup thing that's not going to be needed if the
> underlying SPI device doesn't support/use DMA.

Adding the linux-mmc and linux-spi lists, as that seems pretty odd.
Ulf Hansson Aug. 28, 2020, 9:26 a.m. UTC | #4
On Fri, 28 Aug 2020 at 06:24, Christoph Hellwig <hch@lst.de> wrote:
>
> On Thu, Aug 27, 2020 at 10:11:53PM -0400, Rich Felker wrote:
> > > This change broke SD card support on J2 because MMC_SPI spuriously
> > > depends on HAS_DMA. It looks like it can be fixed just by removing
> > > that dependency from drivers/mmc/host/Kconfig.
> >
> > It can't. mmp_spi_probe fails with ENOMEM, probably due to trying to
> > do some DMA setup thing that's not going to be needed if the
> > underlying SPI device doesn't support/use DMA.
>
> Adding the linux-mmc and linux-spi lists, as that seems pretty odd.

The mmc_spi driver needs modernizations, so I am not surprised to see
odd things.

My guess is that in ->probe() we check "if
(spi->master->dev.parent->dma_mask)" - > and runs dma_map*()
operations, which fails and leads to bailing out of ->probe() to
return an error code.

However, by looking at the code, one get the feeling that the DMA
support is somewhat prepared to be made optional. I guess it has never
been really tested, as the Kconfig option has "depends on HAS_DMA"  -
and it's been like that as long as I can remember.

Kind regards
Uffe
Rich Felker Aug. 28, 2020, 3:09 p.m. UTC | #5
On Fri, Aug 28, 2020 at 11:26:57AM +0200, Ulf Hansson wrote:
> On Fri, 28 Aug 2020 at 06:24, Christoph Hellwig <hch@lst.de> wrote:
> >
> > On Thu, Aug 27, 2020 at 10:11:53PM -0400, Rich Felker wrote:
> > > > This change broke SD card support on J2 because MMC_SPI spuriously
> > > > depends on HAS_DMA. It looks like it can be fixed just by removing
> > > > that dependency from drivers/mmc/host/Kconfig.
> > >
> > > It can't. mmp_spi_probe fails with ENOMEM, probably due to trying to
> > > do some DMA setup thing that's not going to be needed if the
> > > underlying SPI device doesn't support/use DMA.
> >
> > Adding the linux-mmc and linux-spi lists, as that seems pretty odd.
> 
> The mmc_spi driver needs modernizations, so I am not surprised to see
> odd things.
> 
> My guess is that in ->probe() we check "if
> (spi->master->dev.parent->dma_mask)" - > and runs dma_map*()
> operations, which fails and leads to bailing out of ->probe() to
> return an error code.
> 
> However, by looking at the code, one get the feeling that the DMA
> support is somewhat prepared to be made optional. I guess it has never
> been really tested, as the Kconfig option has "depends on HAS_DMA"  -
> and it's been like that as long as I can remember.

It always worked on our "byte-banged" SPI controller, with no DMA
controller present, before Christoph's changes in this patch series,
and seems to be working now (although I have some other, hopefully
unrelated regressions to debug) with #ifdef CONFIG_HAS_DMA around the
if (spi->master->dev.parent->dma_mask) block in mmc_spi_probe. That's
probably not the right fix though -- why isn't it checking
host->dma_dev instead and only attempting DMA setup if dma_dev is
non-null?

Rich
Christoph Hellwig Aug. 29, 2020, 8:31 a.m. UTC | #6
On Fri, Aug 28, 2020 at 11:09:43AM -0400, Rich Felker wrote:
> > However, by looking at the code, one get the feeling that the DMA
> > support is somewhat prepared to be made optional. I guess it has never
> > been really tested, as the Kconfig option has "depends on HAS_DMA"  -
> > and it's been like that as long as I can remember.
> 
> It always worked on our "byte-banged" SPI controller, with no DMA
> controller present, before Christoph's changes in this patch series,

Before that nommu sh builds provided a DMA mapping implementation
that even worked for the streaming side (dma_map_*), but would corrupt
data if you used dma_alloc_coherent memory to communicate with the
device. 

> and seems to be working now (although I have some other, hopefully
> unrelated regressions to debug) with #ifdef CONFIG_HAS_DMA around the
> if (spi->master->dev.parent->dma_mask) block in mmc_spi_probe. That's
> probably not the right fix though -- why isn't it checking
> host->dma_dev instead and only attempting DMA setup if dma_dev is
> non-null?

I don't think dma_dev can be NULL right now.  dma_dev is assigned here:

	if (spi->master->dev.parent->dma_mask) {
		struct device   *dev = spi->master->dev.parent;

		host->dma_dev = dev;

but for any OF or real bus device dma_mask never is zero (it actually is
a pointer), and the value of it also is initialized to 32-bit by default,
making this effectively an "if (1) {".  The driver needs some way to
communicate if a given device actually is DMA capable or not. Or is that
purely a factor of the platform which would be a little strange.

In which case we should do something like:


diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
index 39bb1e30c2d722..3b0cc9a70e6432 100644
--- a/drivers/mmc/host/mmc_spi.c
+++ b/drivers/mmc/host/mmc_spi.c
@@ -1374,7 +1374,7 @@ static int mmc_spi_probe(struct spi_device *spi)
 	if (!host->data)
 		goto fail_nobuf1;
 
-	if (spi->master->dev.parent->dma_mask) {
+	if (IS_ENABLED(CONFIG_HAS_DMA)) {
 		struct device	*dev = spi->master->dev.parent;
 
 		host->dma_dev = dev;
Ulf Hansson Aug. 31, 2020, 11:28 a.m. UTC | #7
On Sat, 29 Aug 2020 at 10:31, Christoph Hellwig <hch@lst.de> wrote:
>
> On Fri, Aug 28, 2020 at 11:09:43AM -0400, Rich Felker wrote:
> > > However, by looking at the code, one get the feeling that the DMA
> > > support is somewhat prepared to be made optional. I guess it has never
> > > been really tested, as the Kconfig option has "depends on HAS_DMA"  -
> > > and it's been like that as long as I can remember.
> >
> > It always worked on our "byte-banged" SPI controller, with no DMA
> > controller present, before Christoph's changes in this patch series,
>
> Before that nommu sh builds provided a DMA mapping implementation
> that even worked for the streaming side (dma_map_*), but would corrupt
> data if you used dma_alloc_coherent memory to communicate with the
> device.

I see.

>
> > and seems to be working now (although I have some other, hopefully
> > unrelated regressions to debug) with #ifdef CONFIG_HAS_DMA around the
> > if (spi->master->dev.parent->dma_mask) block in mmc_spi_probe. That's
> > probably not the right fix though -- why isn't it checking
> > host->dma_dev instead and only attempting DMA setup if dma_dev is
> > non-null?
>
> I don't think dma_dev can be NULL right now.  dma_dev is assigned here:
>
>         if (spi->master->dev.parent->dma_mask) {
>                 struct device   *dev = spi->master->dev.parent;
>
>                 host->dma_dev = dev;
>
> but for any OF or real bus device dma_mask never is zero (it actually is
> a pointer), and the value of it also is initialized to 32-bit by default,
> making this effectively an "if (1) {".  The driver needs some way to
> communicate if a given device actually is DMA capable or not. Or is that
> purely a factor of the platform which would be a little strange.

Okay, thanks for clarifying things.

>
> In which case we should do something like:
>
>
> diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
> index 39bb1e30c2d722..3b0cc9a70e6432 100644
> --- a/drivers/mmc/host/mmc_spi.c
> +++ b/drivers/mmc/host/mmc_spi.c
> @@ -1374,7 +1374,7 @@ static int mmc_spi_probe(struct spi_device *spi)
>         if (!host->data)
>                 goto fail_nobuf1;
>
> -       if (spi->master->dev.parent->dma_mask) {
> +       if (IS_ENABLED(CONFIG_HAS_DMA)) {

I guess "#ifdef CONFIG_HAS_DMA", would work as well as CONFIG_HAS_DMA
can't be "m".

I would also suggest moving the DMA setup things into a separate
function. Then when CONFIG_HAS_DMA is unset, we can use a stub.

>                 struct device   *dev = spi->master->dev.parent;
>
>                 host->dma_dev = dev;

Let me cook a patch that we can try.

Kind regards
Uffe
diff mbox series

Patch

diff --git a/arch/sh/Kconfig b/arch/sh/Kconfig
index f8027eee08edae..337eb496c45a0a 100644
--- a/arch/sh/Kconfig
+++ b/arch/sh/Kconfig
@@ -61,6 +61,7 @@  config SUPERH
 	select MAY_HAVE_SPARSE_IRQ
 	select MODULES_USE_ELF_RELA
 	select NEED_SG_DMA_LENGTH
+	select NO_DMA if !MMU && !DMA_COHERENT
 	select NO_GENERIC_PCI_IOPORT_MAP if PCI
 	select OLD_SIGACTION
 	select OLD_SIGSUSPEND
@@ -135,7 +136,7 @@  config DMA_COHERENT
 	bool
 
 config DMA_NONCOHERENT
-	def_bool !DMA_COHERENT
+	def_bool !NO_DMA && !DMA_COHERENT
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 
 config PGTABLE_LEVELS