mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Lucas Stach <l.stach@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>,
	Jules Maselbas <jmaselbas@kalray.eu>,
	 barebox@lists.infradead.org
Cc: Yann Sionneau <ysionneau@kalray.eu>
Subject: Re: [PATCH 3/5] kvx: Implement dma handling primitives
Date: Tue, 02 Mar 2021 11:14:09 +0100	[thread overview]
Message-ID: <595cae1711b8bdc79876371194cadd772f8646e6.camel@pengutronix.de> (raw)
In-Reply-To: <04c18498-970c-9a69-ebf7-97bf85d2f961@pengutronix.de>

Am Dienstag, dem 02.03.2021 um 09:37 +0100 schrieb Ahmad Fatoum:
> Hello Jules, Yann,
> 
> On 01.03.21 16:58, Jules Maselbas wrote:
> > From: Yann Sionneau <ysionneau@kalray.eu>
> 
> Some comments inline. I am not a cache cohereny expert, so take
> it with a grain of salt.
> 
> > 
> > Signed-off-by: Yann Sionneau <ysionneau@kalray.eu>
> > Signed-off-by: Jules Maselbas <jmaselbas@kalray.eu>
> > ---
> 
> > --- /dev/null
> > +++ b/arch/kvx/include/asm/dma.h
> > @@ -0,0 +1,35 @@
> > +/* SPDX-License-Identifier: GPL-2.0-only */
> > +/* SPDX-FileCopyrightText: 2021 Yann Sionneau <ysionneau@kalray.eu>, Kalray Inc. */
> > +
> > +#ifndef __ASM_DMA_H
> > +#define __ASM_DMA_H
> > +
> > +#include <common.h>
> > +
> > +#define KVX_DDR_32BIT_RAM_WINDOW_BA	(0x80000000ULL)
> > +#define KVX_DDR_64BIT_RAM_WINDOW_BA	(0x100000000ULL)
> > +#define MAX_32BIT_ADDR			(0xffffffffULL)
> > +
> > +#define dma_alloc dma_alloc
> > +static inline void *dma_alloc(size_t size)
> > +{
> > +	return xmemalign(64, ALIGN(size, 64));
> > +}
> > +
> > +static inline void *dma_alloc_coherent(size_t size, dma_addr_t *dma_handle)
> > +{
> > +	void *ret = xmemalign(PAGE_SIZE, size);
> > +
> > +	if (dma_handle)
> > +		*dma_handle = (dma_addr_t)(uintptr_t)ret;
> > +
> > +	return ret;
> > +}
> 
> This would imply that the CPU barebox is booting is coherent with all
> 
> devices that barebox needs to access. Is that the case?
> 
> (See below)
> 
> > +
> > +static inline void dma_free_coherent(void *mem, dma_addr_t dma_handle,
> > +				     size_t size)
> > +{
> > +	free(mem);
> > +}
> > +
> > +#endif /* __ASM_DMA_H */
> > diff --git a/arch/kvx/include/asm/sys_arch.h b/arch/kvx/include/asm/sys_arch.h
> > index 9df32c4e7..ce07a5598 100644
> > --- a/arch/kvx/include/asm/sys_arch.h
> > +++ b/arch/kvx/include/asm/sys_arch.h
> > @@ -11,6 +11,9 @@
> >  #define EXCEPTION_STRIDE	0x40
> >  #define EXCEPTION_ALIGNMENT	0x100
> >  
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > +#define kvx_cluster_id() ((int) \
> > +	((kvx_sfr_get(PCR) & KVX_SFR_PCR_CID_MASK) \
> > +					>> KVX_SFR_PCR_CID_SHIFT))
> >  #define KVX_SFR_START(__sfr_reg) \
> >  	(KVX_SFR_## __sfr_reg ## _SHIFT)
> >  
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > diff --git a/arch/kvx/lib/Makefile b/arch/kvx/lib/Makefile
> > index d271ebccf..c730e1c23 100644
> > --- a/arch/kvx/lib/Makefile
> > +++ b/arch/kvx/lib/Makefile
> > @@ -3,4 +3,4 @@
> >  # Copyright (C) 2019 Kalray Inc.
> >  #
> >  
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > -obj-y	+= cpuinfo.o board.o dtb.o poweroff.o bootm.o setjmp.o cache.o
> > +obj-y	+= cpuinfo.o board.o dtb.o poweroff.o bootm.o setjmp.o cache.o dma-default.o
> > diff --git a/arch/kvx/lib/dma-default.c b/arch/kvx/lib/dma-default.c
> > new file mode 100644
> > index 000000000..755a8c66f
> > --- /dev/null
> > +++ b/arch/kvx/lib/dma-default.c
> > @@ -0,0 +1,91 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +// SPDX-FileCopyrightText: 2021 Yann Sionneau <ysionneau@kalray.eu>, Kalray Inc.
> > +
> > +#include <dma.h>
> > +#include <asm/barrier.h>
> > +#include <asm/io.h>
> > +#include <asm/cache.h>
> > +#include <asm/sfr.h>
> > +#include <asm/sys_arch.h>
> > +
> > +/*
> > + * The implementation of arch should follow the following rules:
> > + *		map		for_cpu		for_device	unmap
> > + * TO_DEV	writeback	none		writeback	none
> > + * FROM_DEV	invalidate	invalidate(*)	invalidate	invalidate(*)
> > + * BIDIR	writeback	invalidate	writeback	invalidate
> > + *
> > + * (*) - only necessary if the CPU speculatively prefetches.
> > + *
> > + * (see https://lkml.org/lkml/2018/5/18/979)
> > + */
> > +
> > +void dma_sync_single_for_device(dma_addr_t addr, size_t size,
> > +				enum dma_data_direction dir)
> > +{
> > +	switch (dir) {
> > +	case DMA_FROM_DEVICE:
> > +		kvx_dcache_invalidate_mem_area(addr, size);

Why do you need to explicitly invalidate, but not flush? Even if the
CPU speculatively prefetches, the coherency protocol should make sure
to invalidate the speculatively loaded lines, right?

> > +		break;
> > +	case DMA_TO_DEVICE:
> > +	case DMA_BIDIRECTIONAL:
> > +		/* allow device to read buffer written by CPU */
> > +		wmb();
> 
> If the interconnect was indeed coherent, like dma_alloc_coherent
> above hints, you wouldn't need any barriers here..?

Coherency does not imply strict ordering, so the barriers are in fact
correct, as the CPU write buffers and/or the interconnect can still
change the ordering of the writes as seen by a remote observer.

> > +		break;
> > +	default:
> > +		BUG();
> > +	}
> > +}
> > +
> > +void dma_sync_single_for_cpu(dma_addr_t addr, size_t size,
> > +				enum dma_data_direction dir)
> > +{
> > +	switch (dir) {
> > +	case DMA_FROM_DEVICE:
> > +	case DMA_TO_DEVICE:
> > +		break;
> > +	case DMA_BIDIRECTIONAL:
> > +		kvx_dcache_invalidate_mem_area(addr, size);
> > +		break;
> > +	default:
> > +		BUG();
> > +	}
> > +}
> > +
> > +#define KVX_DDR_ALIAS_OFFSET \
> > +	(KVX_DDR_64BIT_RAM_WINDOW_BA - KVX_DDR_32BIT_RAM_WINDOW_BA)
> > +#define KVX_DDR_ALIAS_WINDOW \
> > +	(KVX_DDR_64BIT_RAM_WINDOW_BA + KVX_DDR_ALIAS_OFFSET)
> > +
> > +/* Local smem is aliased between 0 and 16MB */
> > +#define KVX_SMEM_LOCAL_ALIAS 0x1000000ULL
> > +
> > +dma_addr_t dma_map_single(struct device_d *dev, void *ptr, size_t size,
> > +			  enum dma_data_direction dir)
> > +{
> > +	uintptr_t addr = (uintptr_t) ptr;
> > +
> > +	dma_sync_single_for_device(addr, size, dir);
> > +
> > +	/* Local smem alias should never be used for dma */
> > +	if (addr < KVX_SMEM_LOCAL_ALIAS)
> > +		return addr + (1 + kvx_cluster_id()) * KVX_SMEM_LOCAL_ALIAS;
> > +
> > +	if (dev->dma_mask && addr <= dev->dma_mask)
> > +		return addr;
> > +
> > +	if (addr >= KVX_DDR_ALIAS_WINDOW)
> > +		return DMA_ERROR_CODE;
> > +
> > +	addr -= KVX_DDR_ALIAS_OFFSET;
> > +	if (dev->dma_mask && addr > dev->dma_mask)
> > +		return DMA_ERROR_CODE;
> > +
> > +	return addr;
> > +}
> > +
> > +void dma_unmap_single(struct device_d *dev, dma_addr_t addr, size_t size,
> > +		      enum dma_data_direction dir)
> > +{
> > +	dma_sync_single_for_cpu(addr, size, dir);
> > +}
> > 
> 



_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2021-03-02 10:15 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-01 15:58 [PATCH 1/5] kvx: Implement setjmp/longjmp/initjmp Jules Maselbas
2021-03-01 15:58 ` [PATCH 2/5] kvx: Implement dcache invalidation primitive Jules Maselbas
2021-03-02  8:40   ` Ahmad Fatoum
2021-03-02 11:44     ` Jules Maselbas
2021-03-01 15:58 ` [PATCH 3/5] kvx: Implement dma handling primitives Jules Maselbas
2021-03-02  8:37   ` Ahmad Fatoum
2021-03-02  8:44     ` Ahmad Fatoum
2021-03-02 10:14     ` Lucas Stach [this message]
2021-03-02 10:58       ` Jules Maselbas
2021-03-03  9:14         ` Lucas Stach
2021-03-03  9:33           ` Yann Sionneau
2021-03-03  9:52             ` Ahmad Fatoum
2021-03-01 15:58 ` [PATCH 4/5] kvx: Request enough privilege to boot Linux Jules Maselbas
2021-03-01 15:58 ` [PATCH 5/5] kvx: lib: dtb: Remove unused variable Jules Maselbas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=595cae1711b8bdc79876371194cadd772f8646e6.camel@pengutronix.de \
    --to=l.stach@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=jmaselbas@kalray.eu \
    --cc=ysionneau@kalray.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox