bc092b9
From 265292f2b0da0eb414c409871ba0f94a99ec33c1 Mon Sep 17 00:00:00 2001
bc092b9
From: Vladimir Serbinenko <phcoder@gmail.com>
bc092b9
Date: Mon, 8 May 2017 22:06:04 +0200
bc092b9
Subject: [PATCH 027/176] arm_coreboot: Support DMA.
bc092b9
bc092b9
This is needed to support USB and some other busses.
bc092b9
---
bc092b9
 grub-core/Makefile.am             |  1 +
bc092b9
 grub-core/Makefile.core.def       |  1 +
bc092b9
 grub-core/kern/arm/cache.c        | 34 ++++++++++++++++++++++
bc092b9
 grub-core/kern/arm/cache_armv7.S  | 12 ++++++++
bc092b9
 grub-core/kern/arm/coreboot/dma.c | 59 +++++++++++++++++++++++++++++++++++++++
bc092b9
 include/grub/cache.h              |  7 ++---
bc092b9
 include/grub/dma.h                | 44 +++++++++++++++++++++++++++++
bc092b9
 include/grub/pci.h                | 22 +--------------
bc092b9
 8 files changed, 155 insertions(+), 25 deletions(-)
bc092b9
 create mode 100644 grub-core/kern/arm/coreboot/dma.c
bc092b9
 create mode 100644 include/grub/dma.h
bc092b9
bc092b9
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
bc092b9
index a2aaf9f54..104513847 100644
bc092b9
--- a/grub-core/Makefile.am
bc092b9
+++ b/grub-core/Makefile.am
bc092b9
@@ -248,6 +248,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/gfxterm.h
bc092b9
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/font.h
bc092b9
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/bufio.h
bc092b9
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/fdt.h
bc092b9
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/dma.h
bc092b9
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/arm/coreboot/kernel.h
bc092b9
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/fdtbus.h
bc092b9
 endif
bc092b9
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
bc092b9
index 6be6e7f61..e4f253a20 100644
bc092b9
--- a/grub-core/Makefile.core.def
bc092b9
+++ b/grub-core/Makefile.core.def
bc092b9
@@ -163,6 +163,7 @@ kernel = {
bc092b9
   arm_coreboot = term/ps2.c;
bc092b9
   arm_coreboot = term/arm/pl050.c;
bc092b9
   arm_coreboot = commands/keylayouts.c;
bc092b9
+  arm_coreboot = kern/arm/coreboot/dma.c;
bc092b9
 
bc092b9
   terminfoinkernel = term/terminfo.c;
bc092b9
   terminfoinkernel = term/tparm.c;
bc092b9
diff --git a/grub-core/kern/arm/cache.c b/grub-core/kern/arm/cache.c
bc092b9
index 34154ccdb..af1c4bbf5 100644
bc092b9
--- a/grub-core/kern/arm/cache.c
bc092b9
+++ b/grub-core/kern/arm/cache.c
bc092b9
@@ -29,6 +29,8 @@ void grub_arm_clean_dcache_range_armv6 (grub_addr_t start, grub_addr_t end,
bc092b9
 					grub_addr_t dlinesz);
bc092b9
 void grub_arm_clean_dcache_range_armv7 (grub_addr_t start, grub_addr_t end,
bc092b9
 					grub_addr_t dlinesz);
bc092b9
+void grub_arm_clean_dcache_range_poc_armv7 (grub_addr_t start, grub_addr_t end,
bc092b9
+					    grub_addr_t dlinesz);
bc092b9
 void grub_arm_invalidate_icache_range_armv6 (grub_addr_t start, grub_addr_t end,
bc092b9
 					     grub_addr_t dlinesz);
bc092b9
 void grub_arm_invalidate_icache_range_armv7 (grub_addr_t start, grub_addr_t end,
bc092b9
@@ -253,6 +255,38 @@ grub_arch_sync_caches (void *address, grub_size_t len)
bc092b9
 }
bc092b9
 
bc092b9
 void
bc092b9
+grub_arch_sync_dma_caches (volatile void *address, grub_size_t len)
bc092b9
+{
bc092b9
+  grub_addr_t start = (grub_addr_t) address;
bc092b9
+  grub_addr_t end = start + len;
bc092b9
+
bc092b9
+  if (type == ARCH_UNKNOWN)
bc092b9
+    probe_caches ();
bc092b9
+  start = ALIGN_DOWN (start, grub_arch_cache_max_linesz);
bc092b9
+  end = ALIGN_UP (end, grub_arch_cache_max_linesz);
bc092b9
+  switch (type)
bc092b9
+    {
bc092b9
+    case ARCH_ARMV6:
bc092b9
+      grub_arm_clean_dcache_range_armv6 (start, end, grub_arch_cache_dlinesz);
bc092b9
+      grub_arm_invalidate_icache_range_armv6 (start, end,
bc092b9
+					      grub_arch_cache_ilinesz);
bc092b9
+      break;
bc092b9
+    case ARCH_ARMV5_WRITE_THROUGH:
bc092b9
+    case ARCH_ARMV6_UNIFIED:
bc092b9
+      grub_arm_clean_dcache_range_armv6 (start, end, grub_arch_cache_dlinesz);
bc092b9
+      break;
bc092b9
+    case ARCH_ARMV7:
bc092b9
+      grub_arm_clean_dcache_range_poc_armv7 (start, end, grub_arch_cache_dlinesz);
bc092b9
+      grub_arm_invalidate_icache_range_armv7 (start, end,
bc092b9
+					      grub_arch_cache_ilinesz);
bc092b9
+      break;
bc092b9
+      /* Pacify GCC.  */
bc092b9
+    case ARCH_UNKNOWN:
bc092b9
+      break;
bc092b9
+    }
bc092b9
+}
bc092b9
+
bc092b9
+void
bc092b9
 grub_arm_disable_caches_mmu (void)
bc092b9
 {
bc092b9
   if (type == ARCH_UNKNOWN)
bc092b9
diff --git a/grub-core/kern/arm/cache_armv7.S b/grub-core/kern/arm/cache_armv7.S
bc092b9
index 1ef2754af..5ae76a3d8 100644
bc092b9
--- a/grub-core/kern/arm/cache_armv7.S
bc092b9
+++ b/grub-core/kern/arm/cache_armv7.S
bc092b9
@@ -33,6 +33,18 @@
bc092b9
 # define ISB	isb
bc092b9
 #define ARMV7 1
bc092b9
 
bc092b9
+FUNCTION(grub_arm_clean_dcache_range_poc_armv7)
bc092b9
+	DSB
bc092b9
+	@ Clean data cache for range to point-of-coherence
bc092b9
+1:	cmp	r0, r1
bc092b9
+	bge	2f
bc092b9
+	mcr	p15, 0, r0, c7, c14, 1	@ DCCMVAC
bc092b9
+	add	r0, r0, r2		@ Next line
bc092b9
+	b	1b
bc092b9
+2:	DSB
bc092b9
+	bx	lr
bc092b9
+
bc092b9
+
bc092b9
 	@ r0  - CLIDR
bc092b9
 	@ r1  - LoC
bc092b9
 	@ r2  - current level
bc092b9
diff --git a/grub-core/kern/arm/coreboot/dma.c b/grub-core/kern/arm/coreboot/dma.c
bc092b9
new file mode 100644
bc092b9
index 000000000..2c2a62789
bc092b9
--- /dev/null
bc092b9
+++ b/grub-core/kern/arm/coreboot/dma.c
bc092b9
@@ -0,0 +1,59 @@
bc092b9
+/*
bc092b9
+ *  GRUB  --  GRand Unified Bootloader
bc092b9
+ *  Copyright (C) 2007,2009  Free Software Foundation, Inc.
bc092b9
+ *
bc092b9
+ *  GRUB is free software: you can redistribute it and/or modify
bc092b9
+ *  it under the terms of the GNU General Public License as published by
bc092b9
+ *  the Free Software Foundation, either version 3 of the License, or
bc092b9
+ *  (at your option) any later version.
bc092b9
+ *
bc092b9
+ *  GRUB is distributed in the hope that it will be useful,
bc092b9
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
bc092b9
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
bc092b9
+ *  GNU General Public License for more details.
bc092b9
+ *
bc092b9
+ *  You should have received a copy of the GNU General Public License
bc092b9
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
bc092b9
+ */
bc092b9
+
bc092b9
+#include <grub/dl.h>
bc092b9
+#include <grub/dma.h>
bc092b9
+#include <grub/mm.h>
bc092b9
+#include <grub/misc.h>
bc092b9
+#include <grub/mm_private.h>
bc092b9
+#include <grub/cache.h>
bc092b9
+
bc092b9
+struct grub_pci_dma_chunk *
bc092b9
+grub_memalign_dma32 (grub_size_t align, grub_size_t size)
bc092b9
+{
bc092b9
+  void *ret;
bc092b9
+  if (align < 64)
bc092b9
+    align = 64;
bc092b9
+  size = ALIGN_UP (size, align);
bc092b9
+  ret = grub_memalign (align, size);
bc092b9
+  if (!ret)
bc092b9
+    return 0;
bc092b9
+  grub_arch_sync_dma_caches (ret, size);
bc092b9
+  return ret;
bc092b9
+}
bc092b9
+
bc092b9
+void
bc092b9
+grub_dma_free (struct grub_pci_dma_chunk *ch)
bc092b9
+{
bc092b9
+  grub_size_t size = (((struct grub_mm_header *) ch) - 1)->size * GRUB_MM_ALIGN;
bc092b9
+  grub_arch_sync_dma_caches (ch, size);
bc092b9
+  grub_free (ch);
bc092b9
+}
bc092b9
+
bc092b9
+volatile void *
bc092b9
+grub_dma_get_virt (struct grub_pci_dma_chunk *ch)
bc092b9
+{
bc092b9
+  return (void *) ch;
bc092b9
+}
bc092b9
+
bc092b9
+grub_uint32_t
bc092b9
+grub_dma_get_phys (struct grub_pci_dma_chunk *ch)
bc092b9
+{
bc092b9
+  return (grub_uint32_t) (grub_addr_t) ch;
bc092b9
+}
bc092b9
+
bc092b9
diff --git a/include/grub/cache.h b/include/grub/cache.h
bc092b9
index fc669dfd1..1c98ce270 100644
bc092b9
--- a/include/grub/cache.h
bc092b9
+++ b/include/grub/cache.h
bc092b9
@@ -34,15 +34,14 @@ void EXPORT_FUNC(grub_arch_sync_caches) (void *address, grub_size_t len);
bc092b9
 #endif
bc092b9
 
bc092b9
 #ifndef GRUB_MACHINE_EMU
bc092b9
-#ifdef _mips
bc092b9
-void EXPORT_FUNC(grub_arch_sync_dma_caches) (volatile void *address,
bc092b9
-					     grub_size_t len);
bc092b9
-#else
bc092b9
+#if defined (__i386__) || defined (__x86_64__)
bc092b9
 static inline void
bc092b9
 grub_arch_sync_dma_caches (volatile void *address __attribute__ ((unused)),
bc092b9
 			   grub_size_t len __attribute__ ((unused)))
bc092b9
 {
bc092b9
 }
bc092b9
+#else
bc092b9
+void EXPORT_FUNC(grub_arch_sync_dma_caches) (volatile void *address, grub_size_t len);
bc092b9
 #endif
bc092b9
 #endif
bc092b9
 
bc092b9
diff --git a/include/grub/dma.h b/include/grub/dma.h
bc092b9
new file mode 100644
bc092b9
index 000000000..19992ebc1
bc092b9
--- /dev/null
bc092b9
+++ b/include/grub/dma.h
bc092b9
@@ -0,0 +1,44 @@
bc092b9
+/*
bc092b9
+ *  GRUB  --  GRand Unified Bootloader
bc092b9
+ *  Copyright (C) 2008,2009  Free Software Foundation, Inc.
bc092b9
+ *
bc092b9
+ *  GRUB is free software: you can redistribute it and/or modify
bc092b9
+ *  it under the terms of the GNU General Public License as published by
bc092b9
+ *  the Free Software Foundation, either version 3 of the License, or
bc092b9
+ *  (at your option) any later version.
bc092b9
+ *
bc092b9
+ *  GRUB is distributed in the hope that it will be useful,
bc092b9
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
bc092b9
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
bc092b9
+ *  GNU General Public License for more details.
bc092b9
+ *
bc092b9
+ *  You should have received a copy of the GNU General Public License
bc092b9
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
bc092b9
+ */
bc092b9
+
bc092b9
+#ifndef	GRUB_DMA_H
bc092b9
+#define	GRUB_DMA_H	1
bc092b9
+
bc092b9
+struct grub_pci_dma_chunk;
bc092b9
+
bc092b9
+struct grub_pci_dma_chunk *EXPORT_FUNC(grub_memalign_dma32) (grub_size_t align,
bc092b9
+							     grub_size_t size);
bc092b9
+void EXPORT_FUNC(grub_dma_free) (struct grub_pci_dma_chunk *ch);
bc092b9
+volatile void *EXPORT_FUNC(grub_dma_get_virt) (struct grub_pci_dma_chunk *ch);
bc092b9
+grub_uint32_t EXPORT_FUNC(grub_dma_get_phys) (struct grub_pci_dma_chunk *ch);
bc092b9
+
bc092b9
+static inline void *
bc092b9
+grub_dma_phys2virt (grub_uint32_t phys, struct grub_pci_dma_chunk *chunk)
bc092b9
+{
bc092b9
+  return ((grub_uint8_t *) grub_dma_get_virt (chunk)
bc092b9
+	  + (phys - grub_dma_get_phys (chunk)));
bc092b9
+}
bc092b9
+
bc092b9
+static inline grub_uint32_t
bc092b9
+grub_dma_virt2phys (volatile void *virt, struct grub_pci_dma_chunk *chunk)
bc092b9
+{
bc092b9
+  return (((grub_uint8_t *) virt - (grub_uint8_t *) grub_dma_get_virt (chunk))
bc092b9
+	  + grub_dma_get_phys (chunk));
bc092b9
+}
bc092b9
+
bc092b9
+#endif
bc092b9
diff --git a/include/grub/pci.h b/include/grub/pci.h
bc092b9
index 70d9a0513..262c89b74 100644
bc092b9
--- a/include/grub/pci.h
bc092b9
+++ b/include/grub/pci.h
bc092b9
@@ -142,27 +142,7 @@ grub_pci_address_t EXPORT_FUNC(grub_pci_make_address) (grub_pci_device_t dev,
bc092b9
 void EXPORT_FUNC(grub_pci_iterate) (grub_pci_iteratefunc_t hook,
bc092b9
 				    void *hook_data);
bc092b9
 
bc092b9
-struct grub_pci_dma_chunk;
bc092b9
-
bc092b9
-struct grub_pci_dma_chunk *EXPORT_FUNC(grub_memalign_dma32) (grub_size_t align,
bc092b9
-							     grub_size_t size);
bc092b9
-void EXPORT_FUNC(grub_dma_free) (struct grub_pci_dma_chunk *ch);
bc092b9
-volatile void *EXPORT_FUNC(grub_dma_get_virt) (struct grub_pci_dma_chunk *ch);
bc092b9
-grub_uint32_t EXPORT_FUNC(grub_dma_get_phys) (struct grub_pci_dma_chunk *ch);
bc092b9
-
bc092b9
-static inline void *
bc092b9
-grub_dma_phys2virt (grub_uint32_t phys, struct grub_pci_dma_chunk *chunk)
bc092b9
-{
bc092b9
-  return ((grub_uint8_t *) grub_dma_get_virt (chunk)
bc092b9
-	  + (phys - grub_dma_get_phys (chunk)));
bc092b9
-}
bc092b9
-
bc092b9
-static inline grub_uint32_t
bc092b9
-grub_dma_virt2phys (volatile void *virt, struct grub_pci_dma_chunk *chunk)
bc092b9
-{
bc092b9
-  return (((grub_uint8_t *) virt - (grub_uint8_t *) grub_dma_get_virt (chunk))
bc092b9
-	  + grub_dma_get_phys (chunk));
bc092b9
-}
bc092b9
+#include <grub/dma.h>
bc092b9
 
bc092b9
 grub_uint8_t
bc092b9
 EXPORT_FUNC (grub_pci_find_capability) (grub_pci_device_t dev, grub_uint8_t cap);
bc092b9
-- 
bc092b9
2.13.0
bc092b9