All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] dm: core: Ensure DMA regions start up with the cache clean
@ 2017-04-04 19:00 Simon Glass
  2017-04-13 21:16 ` Simon Glass
  0 siblings, 1 reply; 2+ messages in thread
From: Simon Glass @ 2017-04-04 19:00 UTC (permalink / raw)
  To: u-boot

There is a strange interaction with drivers which use DMA if the cache
starts off in a dirty state. Buffer space which the driver reads (but has
not previously written) can contain zero bytes from alloc_priv(). This can
cause corruption of the memory used by DMA for incoming data.

Fix this and add a comment to explain the problem.

This allows the dwc2 driver to work correctly with driver model, for
example.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/core/device.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index 70fcfc23e0..8c9f5293d0 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -255,8 +255,36 @@ static void *alloc_priv(int size, uint flags)
 
 	if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
 		priv = memalign(ARCH_DMA_MINALIGN, size);
-		if (priv)
+		if (priv) {
 			memset(priv, '\0', size);
+
+			/*
+			 * Ensure that the zero bytes are flushed to memory.
+			 * This prevents problems if the driver uses this as
+			 * both an input and an output buffer:
+			 *
+			 * 1. Zeroes written to buffer (here) and sit in the
+			 *	cache
+			 * 2. Driver issues a read command to DMA
+			 * 3. CPU runs out of cache space and evicts some cache
+			 *	data in the buffer, writing zeroes to RAM from
+			 *	the memset() above
+			 * 4. DMA completes
+			 * 5. Buffer now has some DMA data and some zeroes
+			 * 6. Data being read is now incorrect
+			 *
+			 * To prevent this, ensure that the cache is clean
+			 * within this range at the start. The driver can then
+			 * use normal flush-after-write, invalidate-before-read
+			 * procedures.
+			 *
+			 * TODO(sjg at chromium.org): Drop this microblaze
+			 * exception.
+			 */
+#ifndef CONFIG_MICROBLAZE
+			flush_dcache_range((ulong)priv, (ulong)priv + size);
+#endif
+		}
 	} else {
 		priv = calloc(1, size);
 	}
-- 
2.12.2.715.g7642488e1d-goog

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

* [U-Boot] [PATCH] dm: core: Ensure DMA regions start up with the cache clean
  2017-04-04 19:00 [U-Boot] [PATCH] dm: core: Ensure DMA regions start up with the cache clean Simon Glass
@ 2017-04-13 21:16 ` Simon Glass
  0 siblings, 0 replies; 2+ messages in thread
From: Simon Glass @ 2017-04-13 21:16 UTC (permalink / raw)
  To: u-boot

On 4 April 2017 at 13:00, Simon Glass <sjg@chromium.org> wrote:
> There is a strange interaction with drivers which use DMA if the cache
> starts off in a dirty state. Buffer space which the driver reads (but has
> not previously written) can contain zero bytes from alloc_priv(). This can
> cause corruption of the memory used by DMA for incoming data.
>
> Fix this and add a comment to explain the problem.
>
> This allows the dwc2 driver to work correctly with driver model, for
> example.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  drivers/core/device.c | 30 +++++++++++++++++++++++++++++-
>  1 file changed, 29 insertions(+), 1 deletion(-)

Applied to u-boot-dm

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

end of thread, other threads:[~2017-04-13 21:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-04 19:00 [U-Boot] [PATCH] dm: core: Ensure DMA regions start up with the cache clean Simon Glass
2017-04-13 21:16 ` Simon Glass

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.