Skip to content

Commit bf4b558

Browse files
mosaltertorvalds
authored andcommitted
arm64: add early_ioremap support
Add support for early IO or memory mappings which are needed before the normal ioremap() is usable. This also adds fixmap support for permanent fixed mappings such as that used by the earlyprintk device register region. Signed-off-by: Mark Salter <msalter@redhat.com> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Cc: Borislav Petkov <borislav.petkov@amd.com> Cc: Dave Young <dyoung@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0bf757c commit bf4b558

11 files changed

Lines changed: 169 additions & 52 deletions

File tree

‎Documentation/arm64/memory.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ ffffffbffa000000 ffffffbffaffffff 16MB PCI I/O space
3939

4040
ffffffbffb000000 ffffffbffbbfffff 12MB [guard]
4141

42-
ffffffbffbc00000 ffffffbffbdfffff 2MB earlyprintk device
42+
ffffffbffbc00000 ffffffbffbdfffff 2MB fixed mappings
4343

4444
ffffffbffbe00000 ffffffbffbffffff 2MB [guard]
4545

@@ -66,7 +66,7 @@ fffffdfffa000000 fffffdfffaffffff 16MB PCI I/O space
6666

6767
fffffdfffb000000 fffffdfffbbfffff 12MB [guard]
6868

69-
fffffdfffbc00000 fffffdfffbdfffff 2MB earlyprintk device
69+
fffffdfffbc00000 fffffdfffbdfffff 2MB fixed mappings
7070

7171
fffffdfffbe00000 fffffdfffbffffff 2MB [guard]
7272

‎arch/arm64/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ config ARM64
1717
select GENERIC_CLOCKEVENTS
1818
select GENERIC_CLOCKEVENTS_BROADCAST if SMP
1919
select GENERIC_CPU_AUTOPROBE
20+
select GENERIC_EARLY_IOREMAP
2021
select GENERIC_IOMAP
2122
select GENERIC_IRQ_PROBE
2223
select GENERIC_IRQ_SHOW

‎arch/arm64/include/asm/Kbuild‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ generic-y += delay.h
1010
generic-y += div64.h
1111
generic-y += dma.h
1212
generic-y += emergency-restart.h
13+
generic-y += early_ioremap.h
1314
generic-y += errno.h
1415
generic-y += ftrace.h
1516
generic-y += hash.h

‎arch/arm64/include/asm/fixmap.h‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* fixmap.h: compile-time virtual memory allocation
3+
*
4+
* This file is subject to the terms and conditions of the GNU General Public
5+
* License. See the file "COPYING" in the main directory of this archive
6+
* for more details.
7+
*
8+
* Copyright (C) 1998 Ingo Molnar
9+
* Copyright (C) 2013 Mark Salter <msalter@redhat.com>
10+
*
11+
* Adapted from arch/x86_64 version.
12+
*
13+
*/
14+
15+
#ifndef _ASM_ARM64_FIXMAP_H
16+
#define _ASM_ARM64_FIXMAP_H
17+
18+
#ifndef __ASSEMBLY__
19+
#include <linux/kernel.h>
20+
#include <asm/page.h>
21+
22+
/*
23+
* Here we define all the compile-time 'special' virtual
24+
* addresses. The point is to have a constant address at
25+
* compile time, but to set the physical address only
26+
* in the boot process.
27+
*
28+
* These 'compile-time allocated' memory buffers are
29+
* page-sized. Use set_fixmap(idx,phys) to associate
30+
* physical memory with fixmap indices.
31+
*
32+
*/
33+
enum fixed_addresses {
34+
FIX_EARLYCON_MEM_BASE,
35+
__end_of_permanent_fixed_addresses,
36+
37+
/*
38+
* Temporary boot-time mappings, used by early_ioremap(),
39+
* before ioremap() is functional.
40+
*/
41+
#ifdef CONFIG_ARM64_64K_PAGES
42+
#define NR_FIX_BTMAPS 4
43+
#else
44+
#define NR_FIX_BTMAPS 64
45+
#endif
46+
#define FIX_BTMAPS_SLOTS 7
47+
#define TOTAL_FIX_BTMAPS (NR_FIX_BTMAPS * FIX_BTMAPS_SLOTS)
48+
49+
FIX_BTMAP_END = __end_of_permanent_fixed_addresses,
50+
FIX_BTMAP_BEGIN = FIX_BTMAP_END + TOTAL_FIX_BTMAPS - 1,
51+
__end_of_fixed_addresses
52+
};
53+
54+
#define FIXADDR_SIZE (__end_of_permanent_fixed_addresses << PAGE_SHIFT)
55+
#define FIXADDR_START (FIXADDR_TOP - FIXADDR_SIZE)
56+
57+
#define FIXMAP_PAGE_IO __pgprot(PROT_DEVICE_nGnRE)
58+
59+
extern void __early_set_fixmap(enum fixed_addresses idx,
60+
phys_addr_t phys, pgprot_t flags);
61+
62+
#define __set_fixmap __early_set_fixmap
63+
64+
#include <asm-generic/fixmap.h>
65+
66+
#endif /* !__ASSEMBLY__ */
67+
#endif /* _ASM_ARM64_FIXMAP_H */

‎arch/arm64/include/asm/io.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <asm/byteorder.h>
2828
#include <asm/barrier.h>
2929
#include <asm/pgtable.h>
30+
#include <asm/early_ioremap.h>
3031

3132
#include <xen/xen.h>
3233

‎arch/arm64/include/asm/memory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#define PAGE_OFFSET (UL(0xffffffffffffffff) << (VA_BITS - 1))
5050
#define MODULES_END (PAGE_OFFSET)
5151
#define MODULES_VADDR (MODULES_END - SZ_64M)
52-
#define EARLYCON_IOBASE (MODULES_VADDR - SZ_4M)
52+
#define FIXADDR_TOP (MODULES_VADDR - SZ_2M - PAGE_SIZE)
5353
#define TASK_SIZE_64 (UL(1) << VA_BITS)
5454

5555
#ifdef CONFIG_COMPAT

‎arch/arm64/kernel/early_printk.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <linux/amba/serial.h>
2727
#include <linux/serial_reg.h>
2828

29+
#include <asm/fixmap.h>
30+
2931
static void __iomem *early_base;
3032
static void (*printch)(char ch);
3133

@@ -141,8 +143,10 @@ static int __init setup_early_printk(char *buf)
141143
}
142144
/* no options parsing yet */
143145

144-
if (paddr)
145-
early_base = early_io_map(paddr, EARLYCON_IOBASE);
146+
if (paddr) {
147+
set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr);
148+
early_base = (void __iomem *)fix_to_virt(FIX_EARLYCON_MEM_BASE);
149+
}
146150

147151
printch = match->printch;
148152
early_console = &early_console_dev;

‎arch/arm64/kernel/head.S‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ ENDPROC(__calc_phys_offset)
404404
* - identity mapping to enable the MMU (low address, TTBR0)
405405
* - first few MB of the kernel linear mapping to jump to once the MMU has
406406
* been enabled, including the FDT blob (TTBR1)
407-
* - UART mapping if CONFIG_EARLY_PRINTK is enabled (TTBR1)
407+
* - pgd entry for fixed mappings (TTBR1)
408408
*/
409409
__create_page_tables:
410410
pgtbl x25, x26, x24 // idmap_pg_dir and swapper_pg_dir addresses
@@ -461,15 +461,12 @@ __create_page_tables:
461461
sub x6, x6, #1 // inclusive range
462462
create_block_map x0, x7, x3, x5, x6
463463
1:
464-
#ifdef CONFIG_EARLY_PRINTK
465464
/*
466-
* Create the pgd entry for the UART mapping. The full mapping is done
467-
* later based earlyprintk kernel parameter.
465+
* Create the pgd entry for the fixed mappings.
468466
*/
469-
ldr x5, =EARLYCON_IOBASE // UART virtual address
467+
ldr x5, =FIXADDR_TOP // Fixed mapping virtual address
470468
add x0, x26, #2 * PAGE_SIZE // section table address
471469
create_pgd_entry x26, x0, x5, x6, x7
472-
#endif
473470
ret
474471
ENDPROC(__create_page_tables)
475472
.ltorg

‎arch/arm64/kernel/setup.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/of_fdt.h>
4343
#include <linux/of_platform.h>
4444

45+
#include <asm/fixmap.h>
4546
#include <asm/cputype.h>
4647
#include <asm/elf.h>
4748
#include <asm/cputable.h>
@@ -361,6 +362,7 @@ void __init setup_arch(char **cmdline_p)
361362
*cmdline_p = boot_command_line;
362363

363364
init_mem_pgprot();
365+
early_ioremap_init();
364366

365367
parse_early_param();
366368

‎arch/arm64/mm/ioremap.c‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <linux/vmalloc.h>
2626
#include <linux/io.h>
2727

28+
#include <asm/fixmap.h>
29+
#include <asm/tlbflush.h>
30+
#include <asm/pgalloc.h>
31+
2832
static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
2933
pgprot_t prot, void *caller)
3034
{
@@ -98,3 +102,84 @@ void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
98102
__builtin_return_address(0));
99103
}
100104
EXPORT_SYMBOL(ioremap_cache);
105+
106+
#ifndef CONFIG_ARM64_64K_PAGES
107+
static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
108+
#endif
109+
110+
static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
111+
{
112+
pgd_t *pgd;
113+
pud_t *pud;
114+
115+
pgd = pgd_offset_k(addr);
116+
BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
117+
118+
pud = pud_offset(pgd, addr);
119+
BUG_ON(pud_none(*pud) || pud_bad(*pud));
120+
121+
return pmd_offset(pud, addr);
122+
}
123+
124+
static inline pte_t * __init early_ioremap_pte(unsigned long addr)
125+
{
126+
pmd_t *pmd = early_ioremap_pmd(addr);
127+
128+
BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd));
129+
130+
return pte_offset_kernel(pmd, addr);
131+
}
132+
133+
void __init early_ioremap_init(void)
134+
{
135+
pmd_t *pmd;
136+
137+
pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
138+
#ifndef CONFIG_ARM64_64K_PAGES
139+
/* need to populate pmd for 4k pagesize only */
140+
pmd_populate_kernel(&init_mm, pmd, bm_pte);
141+
#endif
142+
/*
143+
* The boot-ioremap range spans multiple pmds, for which
144+
* we are not prepared:
145+
*/
146+
BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
147+
!= (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
148+
149+
if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
150+
WARN_ON(1);
151+
pr_warn("pmd %p != %p\n",
152+
pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
153+
pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
154+
fix_to_virt(FIX_BTMAP_BEGIN));
155+
pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
156+
fix_to_virt(FIX_BTMAP_END));
157+
158+
pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
159+
pr_warn("FIX_BTMAP_BEGIN: %d\n",
160+
FIX_BTMAP_BEGIN);
161+
}
162+
163+
early_ioremap_setup();
164+
}
165+
166+
void __init __early_set_fixmap(enum fixed_addresses idx,
167+
phys_addr_t phys, pgprot_t flags)
168+
{
169+
unsigned long addr = __fix_to_virt(idx);
170+
pte_t *pte;
171+
172+
if (idx >= __end_of_fixed_addresses) {
173+
BUG();
174+
return;
175+
}
176+
177+
pte = early_ioremap_pte(addr);
178+
179+
if (pgprot_val(flags))
180+
set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
181+
else {
182+
pte_clear(&init_mm, addr, pte);
183+
flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
184+
}
185+
}

0 commit comments

Comments
 (0)