Skip to contentSkip to conversation

arm64: add early_ioremap support

PATCH In review Mark Salter 2 participants 3 messages Oct 4, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: add early_ioremap support

EFI runtime support needs to make temporary short term mappings at early boot time before ioremap is available. This patch adds support for such early mappings based on "fixmap" support found in a number of other architectures. This also allows for a number of permanent mappings which may need to persist beyond paging_init(). This implementation uses the 2MiB of address space currently dedicated to the earlyprintk console registers. This allows for mapping up to 32 64K pages or 512 4K pages.

---
 Documentation/arm64/memory.txt   |   2 +-
 arch/arm64/include/asm/fixmap.h  | 117 ++++++++++++++++
 arch/arm64/include/asm/io.h      |   6 +
 arch/arm64/include/asm/memory.h  |   2 +-
 arch/arm64/kernel/early_printk.c |   8 +-
 arch/arm64/kernel/head.S         |   9 +-
 arch/arm64/kernel/setup.c        |   3 +
 arch/arm64/mm/ioremap.c          | 285 +++++++++++++++++++++++++++++++++++++++
 arch/arm64/mm/mmu.c              |  41 ------
 9 files changed, 422 insertions(+), 51 deletions(-)
 create mode 100644 arch/arm64/include/asm/fixmap.h
Documentation/arm64/memory.txt +1 -1
index 78a37712..f2bf9e1 100644
@@ -35,7 +35,7 @@ ffffffbc00000000	ffffffbdffffffff	   8GB		vmemmap
 
 ffffffbe00000000	ffffffbffbbfffff	  ~8GB		[guard, future vmmemap]
 
-ffffffbffbc00000	ffffffbffbdfffff	   2MB		earlyprintk device
+ffffffbffbc00000	ffffffbffbdfffff	   2MB		fixed mappings
 
 ffffffbffbe00000	ffffffbffbe0ffff	  64KB		PCI I/O space
 
arch/arm64/include/asm/fixmap.h new file +117 -0

117 lines not on this page · raw message

arch/arm64/include/asm/io.h +6 -0
index 1d12f89..c9bac21 100644
@@ -236,6 +236,12 @@ extern void __iounmap(volatile void __iomem *addr);
 #define ioremap_cached(addr, size)	__ioremap((addr), (size), __pgprot(PROT_NORMAL))
 #define iounmap				__iounmap
 
+extern void __iomem *early_ioremap(resource_size_t phys_addr,
+				   unsigned long size);
+extern void __iomem *early_memremap(resource_size_t phys_addr,
+				    unsigned long size);
+extern void early_iounmap(void __iomem *addr, unsigned long size);
+
 #define PROT_SECT_DEFAULT	(PMD_TYPE_SECT | PMD_SECT_AF)
 #define PROT_SECT_DEVICE_nGnRE	(PROT_SECT_DEFAULT | PTE_PXN | PTE_UXN | PMD_ATTRINDX(MT_DEVICE_nGnRE))
 
arch/arm64/include/asm/memory.h +1 -1
index 20925bc..58e68c7 100644
@@ -43,7 +43,7 @@
 #define PAGE_OFFSET		UL(0xffffffc000000000)
 #define MODULES_END		(PAGE_OFFSET)
 #define MODULES_VADDR		(MODULES_END - SZ_64M)
-#define EARLYCON_IOBASE		(MODULES_VADDR - SZ_4M)
+#define FIXADDR_TOP		(MODULES_VADDR - SZ_2M - SZ_64K)
 #define VA_BITS			(39)
 #define TASK_SIZE_64		(UL(1) << VA_BITS)
 
arch/arm64/kernel/early_printk.c +6 -2
index fbb6e18..850d9a4 100644
@@ -26,6 +26,8 @@
 #include <linux/amba/serial.h>
 #include <linux/serial_reg.h>
 
+#include <asm/fixmap.h>
+
 static void __iomem *early_base;
 static void (*printch)(char ch);
 
@@ -141,8 +143,10 @@ static int __init setup_early_printk(char *buf)
 	}
 	/* no options parsing yet */
 
-	if (paddr)
-		early_base = early_io_map(paddr, EARLYCON_IOBASE);
+	if (paddr) {
+		set_fixmap_io(FIX_EARLYCON, paddr);
+		early_base = (void __iomem *)fix_to_virt(FIX_EARLYCON);
+	}
 
 	printch = match->printch;
 	early_console = &early_console_dev;
arch/arm64/kernel/head.S +3 -6
index f436cfb..2b5fc13 100644
@@ -484,7 +484,7 @@ ENDPROC(__calc_phys_offset)
  *   - identity mapping to enable the MMU (low address, TTBR0)
  *   - first few MB of the kernel linear mapping to jump to once the MMU has
  *     been enabled, including the FDT blob (TTBR1)
- *   - UART mapping if CONFIG_EARLY_PRINTK is enabled (TTBR1)
+ *   - pgd entry for fixed mappings
  */
 __create_page_tables:
 	pgtbl	x25, x26, x24			// idmap_pg_dir and swapper_pg_dir addresses
@@ -537,15 +537,12 @@ __create_page_tables:
 	sub	x6, x6, #1			// inclusive range
 	create_block_map x0, x7, x3, x5, x6
 1:
-#ifdef CONFIG_EARLY_PRINTK
 	/*
-	 * Create the pgd entry for the UART mapping. The full mapping is done
-	 * later based earlyprintk kernel parameter.
+	 * Create the pgd entry for the fixed mappings.
 	 */
-	ldr	x5, =EARLYCON_IOBASE		// UART virtual address
+	ldr	x5, =FIXADDR_TOP		// Fixed mapping virtual address
 	add	x0, x26, #2 * PAGE_SIZE		// section table address
 	create_pgd_entry x26, x0, x5, x6, x7
-#endif
 	ret
 ENDPROC(__create_page_tables)
 	.ltorg
arch/arm64/kernel/setup.c +3 -0
index 055cfb8..fb8b27e 100644
@@ -42,6 +42,7 @@
 #include <linux/of_fdt.h>
 #include <linux/of_platform.h>
 
+#include <asm/fixmap.h>
 #include <asm/cputype.h>
 #include <asm/elf.h>
 #include <asm/cputable.h>
@@ -252,6 +253,8 @@ void __init setup_arch(char **cmdline_p)
 
 	*cmdline_p = boot_command_line;
 
+	early_ioremap_init();
+
 	parse_early_param();
 
 	arm64_memblock_init();
arch/arm64/mm/ioremap.c +285 -0

294 lines not on this page · raw message

arch/arm64/mm/mmu.c +0 -41

47 lines not on this page · raw message

Signature · 1 line
1.8.3.1
maintainer
#1 lore raw

Hi Mark,

arch/arm64/include/asm/fixmap.h line 10

Quoted 13 lines
arch/arm64/include/asm/fixmap.h new file +10 -0
@@ -0,0 +1,117 @@
+/*
+ * fixmap.h: compile-time virtual memory allocation
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ * Copyright (C) 1998 Ingo Molnar
+ *
+ */

I can see several architectures having very similar macros/functions in fixmap.h. It would make sense to create a generic fixmap.h holding at least the fix_to_virt and related macros, FIXADDR_START etc. with enum fixed_addresses in arch code.

Quoted 11 lines
arch/arm64/kernel/setup.c +1 -0
@@ -42,6 +42,7 @@
 #include <linux/of_fdt.h>
 #include <linux/of_platform.h>
 
+#include <asm/fixmap.h>
 #include <asm/cputype.h>
 #include <asm/elf.h>
 #include <asm/cputable.h>

arch/arm64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 258

Quoted 6 lines
arch/arm64/kernel/setup.c +2 -0
@@ -252,6 +253,8 @@ void __init setup_arch(char **cmdline_p)
 
        *cmdline_p = boot_command_line;
 
+       early_ioremap_init();
+
        parse_early_param();

Should the early_ioremap_init() call happen after parse_early_param()? Is early_ioremap_debug initialised already?

Quoted 22 lines
arch/arm64/mm/ioremap.c +9 -0
@@ -25,6 +25,10 @@
 #include <linux/vmalloc.h>
 #include <linux/io.h>
 
+#include <asm/fixmap.h>
+#include <asm/tlbflush.h>
+#include <asm/pgalloc.h>
+
 static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
                                      pgprot_t prot, void *caller)
 {
@@ -82,3 +86,284 @@ void __iounmap(volatile void __iomem *io_addr)
        vunmap(addr);
 }
 EXPORT_SYMBOL(__iounmap);
+
+static int early_ioremap_debug __initdata;
+
+static int __init early_ioremap_debug_setup(char *str)
+{

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 103

Quoted 10 lines
arch/arm64/mm/ioremap.c +10 -0
+       early_ioremap_debug = 1;
+
+       return 0;
+}
+early_param("early_ioremap_debug", early_ioremap_debug_setup);
+
+static int after_paging_init __initdata;
+#ifndef CONFIG_ARM64_64K_PAGES
+static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
+#endif

bm_pte[PTRS_PER_PTE];

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 107

Quoted 4 lines
arch/arm64/mm/ioremap.c +4 -0
+
+static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
+{
+       pgd_t *pgd = &swapper_pg_dir[pgd_index(addr)];

pgd_offset_k(addr);

Quoted 4 lines
arch/arm64/mm/ioremap.c +4 -0
+       pud_t *pud = pud_offset(pgd, addr);
+       pmd_t *pmd = pmd_offset(pud, addr);
+
+       return pmd;

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 121

Quoted 10 lines
arch/arm64/mm/ioremap.c +10 -0
+}
+
+static inline pte_t * __init early_ioremap_pte(unsigned long addr)
+{
+#ifdef CONFIG_ARM64_64K_PAGES
+       pmd_t *pmd = early_ioremap_pmd(addr);
+       return pte_offset_kernel(pmd, addr);
+#else
+       return &bm_pte[pte_index(addr)];
+#endif

If we populate the pmd correctly with 4K pages (and I think we do in early_ioremap_init()), can we not just use this function without the #ifdef-else part (always pte_offset_kernel())?

Quoted 7 lines
arch/arm64/mm/ioremap.c +7 -0
+static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
+
+void __init early_ioremap_init(void)
+{
+       pmd_t *pmd;
+       int i;
+

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 140

Quoted 10 lines
arch/arm64/mm/ioremap.c +10 -0
+       if (early_ioremap_debug)
+               pr_info("early_ioremap_init()\n");
+
+       for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
+               slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
+
+       pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
+#ifndef CONFIG_ARM64_64K_PAGES
+       /* need to populate pmd for 4k pagesize only */
+       memset(bm_pte, 0, sizeof(bm_pte));

Do we need memset() here? bm_pte[] is placed in the .bss section.

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 151

Quoted 11 lines
arch/arm64/mm/ioremap.c +11 -0
+       pmd_populate_kernel(&init_mm, pmd, bm_pte);
+#endif
+
+       /*
+        * The boot-ioremap range spans multiple pmds, for which
+        * we are not prepared:
+        */
+#define __FIXADDR_TOP (-PAGE_SIZE)
+       BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
+                    != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
+#undef __FIXADDR_TOP

Why this #define/#undef? FIXADDR_TOP is statically defined.

Quoted 12 lines
arch/arm64/mm/ioremap.c +12 -0
+void __init __set_fixmap(enum fixed_addresses idx,
+                        phys_addr_t phys, pgprot_t flags)
+{
+       unsigned long addr = __fix_to_virt(idx);
+       pte_t *pte;
+
+       if (idx >= __end_of_fixed_addresses) {
+               BUG();
+               return;
+       }
+       if (after_paging_init) {
+               WARN_ON(1);

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 193

Quoted 10 lines
arch/arm64/mm/ioremap.c +10 -0
+               return;
+       }
+
+       pte = early_ioremap_pte(addr);
+
+       if (pgprot_val(flags))
+               set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
+       else
+               pte_clear(&init_mm, addr, pte);
+       flush_tlb_kernel_range(addr, addr+PAGE_SIZE);

Would __set_fixmap be used to change valid ptes? If not, we could keep the flush_tlb_kernel_range() call only under the 'else' block.

As I was going through the patch, I realised that the early_ioremap() looks really to the x86 implementation. Can we move it into a library to be shared between the two?

Signature · 1 line
Catalin
#2 lore raw
Quoted 12 lines · Catalin Marinas

Hi Mark,

Quoted 10 lines
arch/arm64/include/asm/fixmap.h new file +7 -0
@@ -0,0 +1,117 @@
+/*
+ * fixmap.h: compile-time virtual memory allocation
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
Quoted 8 lines

arch/arm64/include/asm/fixmap.h line 10

Quoted 3 lines
arch/arm64/include/asm/fixmap.h new file +3 -0
+ * Copyright (C) 1998 Ingo Molnar
+ *
+ */

I can see several architectures having very similar macros/functions in fixmap.h. It would make sense to create a generic fixmap.h holding at least the fix_to_virt and related macros, FIXADDR_START etc. with enum fixed_addresses in arch code.

Right, there are a quite a few using similar fixmap code. I actually looked at consolidating them, but I gave up so that I could get something working for EFI work in progress. But yeah, I think I will step back and have another go at it. I think I can get at a few of the architectures using common code and make it so others can be converted as desired.

Quoted 13 lines
Quoted 13 lines
arch/arm64/kernel/setup.c +1 -0
@@ -42,6 +42,7 @@
 #include <linux/of_fdt.h>
 #include <linux/of_platform.h>
 
+#include <asm/fixmap.h>
 #include <asm/cputype.h>
 #include <asm/elf.h>
 #include <asm/cputable.h>
@@ -252,6 +253,8 @@ void __init setup_arch(char **cmdline_p)
 
Quoted 8 lines

arch/arm64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 258

Quoted 5 lines
arch/arm64/kernel/setup.c +2 -0
        *cmdline_p = boot_command_line;
 
+       early_ioremap_init();
+
        parse_early_param();

Should the early_ioremap_init() call happen after parse_early_param()? Is early_ioremap_debug initialised already?

No, early_ioremap_debug is not initialized which makes the debug prints in early_ioremap_init() unreachable. I'll fix that. On x86, the init comes first, but there is other code between that and parse_early_param() which uses early_ioremap(). Not the case for arm64.

Quoted 27 lines
Quoted 27 lines
arch/arm64/mm/ioremap.c +13 -0
@@ -25,6 +25,10 @@
 #include <linux/vmalloc.h>
 #include <linux/io.h>
 
+#include <asm/fixmap.h>
+#include <asm/tlbflush.h>
+#include <asm/pgalloc.h>
+
 static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
                                      pgprot_t prot, void *caller)
 {
@@ -82,3 +86,284 @@ void __iounmap(volatile void __iomem *io_addr)
        vunmap(addr);
 }
 EXPORT_SYMBOL(__iounmap);
+
+static int early_ioremap_debug __initdata;
+
+static int __init early_ioremap_debug_setup(char *str)
+{
+       early_ioremap_debug = 1;
+
+       return 0;
+}
Quoted 8 lines

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 103

Quoted 6 lines
arch/arm64/mm/ioremap.c +6 -0
+early_param("early_ioremap_debug", early_ioremap_debug_setup);
+
+static int after_paging_init __initdata;
+#ifndef CONFIG_ARM64_64K_PAGES
+static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
+#endif

bm_pte[PTRS_PER_PTE];

ok

Quoted 7 lines

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 107

Quoted 5 lines
arch/arm64/mm/ioremap.c +4 -0
+
+static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
+{
+       pgd_t *pgd = &swapper_pg_dir[pgd_index(addr)];

pgd_offset_k(addr);

ok

Quoted 11 lines
Quoted 11 lines
arch/arm64/mm/ioremap.c +10 -0
+       pud_t *pud = pud_offset(pgd, addr);
+       pmd_t *pmd = pmd_offset(pud, addr);
+
+       return pmd;
+}
+
+static inline pte_t * __init early_ioremap_pte(unsigned long addr)
+{
+#ifdef CONFIG_ARM64_64K_PAGES
+       pmd_t *pmd = early_ioremap_pmd(addr);
Quoted 8 lines

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 121

Quoted 4 lines
arch/arm64/mm/ioremap.c +4 -0
+       return pte_offset_kernel(pmd, addr);
+#else
+       return &bm_pte[pte_index(addr)];
+#endif

If we populate the pmd correctly with 4K pages (and I think we do in early_ioremap_init()), can we not just use this function without the #ifdef-else part (always pte_offset_kernel())?

Ah, yes. Nice!

Quoted 12 lines
Quoted 12 lines
arch/arm64/mm/ioremap.c +11 -0
+static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
+
+void __init early_ioremap_init(void)
+{
+       pmd_t *pmd;
+       int i;
+
+       if (early_ioremap_debug)
+               pr_info("early_ioremap_init()\n");
+
+       for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
Quoted 8 lines

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 140

Quoted 6 lines
arch/arm64/mm/ioremap.c +6 -0
+               slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
+
+       pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
+#ifndef CONFIG_ARM64_64K_PAGES
+       /* need to populate pmd for 4k pagesize only */
+       memset(bm_pte, 0, sizeof(bm_pte));

Do we need memset() here? bm_pte[] is placed in the .bss section.

No we don't. If .bss isn't cleared here, we have bigger problems.

Quoted 14 lines

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 151

Quoted 12 lines
arch/arm64/mm/ioremap.c +11 -0
+       pmd_populate_kernel(&init_mm, pmd, bm_pte);
+#endif
+
+       /*
+        * The boot-ioremap range spans multiple pmds, for which
+        * we are not prepared:
+        */
+#define __FIXADDR_TOP (-PAGE_SIZE)
+       BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
+                    != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
+#undef __FIXADDR_TOP

Why this #define/#undef? FIXADDR_TOP is statically defined.

Right. Copy and paste from x86 where is isn't static, so the trick to trigger a BUILD_BUG_ON isn't needed here.

Quoted 18 lines
Quoted 18 lines
arch/arm64/mm/ioremap.c +17 -0
+void __init __set_fixmap(enum fixed_addresses idx,
+                        phys_addr_t phys, pgprot_t flags)
+{
+       unsigned long addr = __fix_to_virt(idx);
+       pte_t *pte;
+
+       if (idx >= __end_of_fixed_addresses) {
+               BUG();
+               return;
+       }
+       if (after_paging_init) {
+               WARN_ON(1);
+               return;
+       }
+
+       pte = early_ioremap_pte(addr);
+
Quoted 8 lines

arch/arm64/mm/ioremap.c void __iounmap(volatile void __iomem *io_addr) · line 193

Quoted 5 lines
arch/arm64/mm/ioremap.c +5 -0
+       if (pgprot_val(flags))
+               set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
+       else
+               pte_clear(&init_mm, addr, pte);
+       flush_tlb_kernel_range(addr, addr+PAGE_SIZE);

Would __set_fixmap be used to change valid ptes? If not, we could keep the flush_tlb_kernel_range() call only under the 'else' block.

A valid pte shouldn't be updated except for the unmap case. Nothing prevents it though. We could do the tlb flush only in the else block and add a BUG_ON to catch attempts to modify an already used pte.

Quoted 5 lines

As I was going through the patch, I realised that the early_ioremap() looks really to the x86 implementation. Can we move it into a library to be shared between the two?

Yes, that's where I started. I will look at consolidating the common bits.

Thanks.

--Mark