Skip to content

Commit 2475ff9

Browse files
committed
arm64: Add simple earlyprintk support
This patch adds support for "earlyprintk=" parameter on the kernel command line. The format is: earlyprintk=<name>[,<addr>][,<options>] where <name> is the name of the (UART) device, e.g. "pl011", <addr> is the I/O address. The <options> aren't currently used. The mapping of the earlyprintk device is done very early during kernel boot and there are restrictions on which functions it can call. A special early_io_map() function is added which creates the mapping from the pre-defined EARLY_IOBASE to the device I/O address passed via the kernel parameter. The pgd entry corresponding to EARLY_IOBASE is pre-populated in head.S during kernel boot. Only PL011 is currently supported and it is assumed that the interface is already initialised by the boot loader before the kernel is started. Signed-off-by: Catalin Marinas <catalin.marinas@arm.com> Acked-by: Arnd Bergmann <arnd@arndb.de>
1 parent d6bafb9 commit 2475ff9

9 files changed

Lines changed: 187 additions & 2 deletions

File tree

‎Documentation/arm64/memory.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ ffffffbc00000000 ffffffbdffffffff 8GB vmemmap
3535

3636
ffffffbe00000000 ffffffbffbbfffff ~8GB [guard, future vmmemap]
3737

38+
ffffffbffbc00000 ffffffbffbdfffff 2MB earlyprintk device
39+
3840
ffffffbffbe00000 ffffffbffbe0ffff 64KB PCI I/O space
3941

4042
ffffffbbffff0000 ffffffbcffffffff ~2MB [guard]

‎arch/arm64/Kconfig.debug‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ config DEBUG_STACK_USAGE
2424
Enables the display of the minimum amount of free stack which each
2525
task has ever had available in the sysrq-T output.
2626

27+
config EARLY_PRINTK
28+
bool "Early printk support"
29+
default y
30+
help
31+
Say Y here if you want to have an early console using the
32+
earlyprintk=<name>[,<addr>][,<options>] kernel parameter. It
33+
is assumed that the early console device has been initialised
34+
by the boot loader prior to starting the Linux kernel.
35+
2736
endmenu

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ extern void __iounmap(volatile void __iomem *addr);
230230
#define ioremap_wc(addr, size) __ioremap((addr), (size), __pgprot(PROT_NORMAL_NC))
231231
#define iounmap __iounmap
232232

233+
#define PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF)
234+
#define PROT_SECT_DEVICE_nGnRE (PROT_SECT_DEFAULT | PTE_PXN | PTE_UXN | PMD_ATTRINDX(MT_DEVICE_nGnRE))
235+
233236
#define ARCH_HAS_IOREMAP_WC
234237
#include <asm-generic/iomap.h>
235238

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define PAGE_OFFSET UL(0xffffffc000000000)
4444
#define MODULES_END (PAGE_OFFSET)
4545
#define MODULES_VADDR (MODULES_END - SZ_64M)
46+
#define EARLYCON_IOBASE (MODULES_VADDR - SZ_4M)
4647
#define VA_BITS (39)
4748
#define TASK_SIZE_64 (UL(1) << VA_BITS)
4849

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ typedef struct {
2626

2727
extern void paging_init(void);
2828
extern void setup_mm_for_reboot(void);
29+
extern void __iomem *early_io_map(phys_addr_t phys, unsigned long virt);
2930

3031
#endif

‎arch/arm64/kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ arm64-obj-$(CONFIG_MODULES) += arm64ksyms.o module.o
1717
arm64-obj-$(CONFIG_SMP) += smp.o
1818
arm64-obj-$(CONFIG_HW_PERF_EVENTS) += perf_event.o
1919
arm64-obj-$(CONFIG_HAVE_HW_BREAKPOINT)+= hw_breakpoint.o
20+
arm64-obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
2021

2122
obj-y += $(arm64-obj-y) vdso/
2223
obj-m += $(arm64-obj-m)

‎arch/arm64/kernel/early_printk.c‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Earlyprintk support.
3+
*
4+
* Copyright (C) 2012 ARM Ltd.
5+
* Author: Catalin Marinas <catalin.marinas@arm.com>
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License version 2 as
9+
* published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include <linux/kernel.h>
20+
#include <linux/console.h>
21+
#include <linux/init.h>
22+
#include <linux/string.h>
23+
#include <linux/mm.h>
24+
#include <linux/io.h>
25+
26+
#include <linux/amba/serial.h>
27+
28+
static void __iomem *early_base;
29+
static void (*printch)(char ch);
30+
31+
/*
32+
* PL011 single character TX.
33+
*/
34+
static void pl011_printch(char ch)
35+
{
36+
while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_TXFF)
37+
;
38+
writeb_relaxed(ch, early_base + UART01x_DR);
39+
while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_BUSY)
40+
;
41+
}
42+
43+
struct earlycon_match {
44+
const char *name;
45+
void (*printch)(char ch);
46+
};
47+
48+
static const struct earlycon_match earlycon_match[] __initconst = {
49+
{ .name = "pl011", .printch = pl011_printch, },
50+
{}
51+
};
52+
53+
static void early_write(struct console *con, const char *s, unsigned n)
54+
{
55+
while (n-- > 0) {
56+
if (*s == '\n')
57+
printch('\r');
58+
printch(*s);
59+
s++;
60+
}
61+
}
62+
63+
static struct console early_console = {
64+
.name = "earlycon",
65+
.write = early_write,
66+
.flags = CON_PRINTBUFFER | CON_BOOT,
67+
.index = -1,
68+
};
69+
70+
/*
71+
* Parse earlyprintk=... parameter in the format:
72+
*
73+
* <name>[,<addr>][,<options>]
74+
*
75+
* and register the early console. It is assumed that the UART has been
76+
* initialised by the bootloader already.
77+
*/
78+
static int __init setup_early_printk(char *buf)
79+
{
80+
const struct earlycon_match *match = earlycon_match;
81+
phys_addr_t paddr = 0;
82+
83+
if (!buf) {
84+
pr_warning("No earlyprintk arguments passed.\n");
85+
return 0;
86+
}
87+
88+
while (match->name) {
89+
size_t len = strlen(match->name);
90+
if (!strncmp(buf, match->name, len)) {
91+
buf += len;
92+
break;
93+
}
94+
match++;
95+
}
96+
if (!match->name) {
97+
pr_warning("Unknown earlyprintk arguments: %s\n", buf);
98+
return 0;
99+
}
100+
101+
/* I/O address */
102+
if (!strncmp(buf, ",0x", 3)) {
103+
char *e;
104+
paddr = simple_strtoul(buf + 1, &e, 16);
105+
buf = e;
106+
}
107+
/* no options parsing yet */
108+
109+
if (paddr)
110+
early_base = early_io_map(paddr, EARLYCON_IOBASE);
111+
112+
printch = match->printch;
113+
register_console(&early_console);
114+
115+
return 0;
116+
}
117+
118+
early_param("earlyprintk", setup_early_printk);

‎arch/arm64/kernel/head.S‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@
8282

8383
#ifdef CONFIG_ARM64_64K_PAGES
8484
#define MM_MMUFLAGS PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS
85-
#define IO_MMUFLAGS PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_XN | PTE_FLAGS
8685
#else
8786
#define MM_MMUFLAGS PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS
88-
#define IO_MMUFLAGS PMD_ATTRINDX(MT_DEVICE_nGnRE) | PMD_SECT_XN | PMD_FLAGS
8987
#endif
9088

9189
/*
@@ -368,6 +366,7 @@ ENDPROC(__calc_phys_offset)
368366
* - identity mapping to enable the MMU (low address, TTBR0)
369367
* - first few MB of the kernel linear mapping to jump to once the MMU has
370368
* been enabled, including the FDT blob (TTBR1)
369+
* - UART mapping if CONFIG_EARLY_PRINTK is enabled (TTBR1)
371370
*/
372371
__create_page_tables:
373372
pgtbl x25, x26, x24 // idmap_pg_dir and swapper_pg_dir addresses
@@ -420,6 +419,15 @@ __create_page_tables:
420419
sub x6, x6, #1 // inclusive range
421420
create_block_map x0, x7, x3, x5, x6
422421
1:
422+
#ifdef CONFIG_EARLY_PRINTK
423+
/*
424+
* Create the pgd entry for the UART mapping. The full mapping is done
425+
* later based earlyprintk kernel parameter.
426+
*/
427+
ldr x5, =EARLYCON_IOBASE // UART virtual address
428+
add x0, x26, #2 * PAGE_SIZE // section table address
429+
create_pgd_entry x26, x0, x5, x6, x7
430+
#endif
423431
ret
424432
ENDPROC(__create_page_tables)
425433
.ltorg

‎arch/arm64/mm/mmu.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/nodemask.h>
2626
#include <linux/memblock.h>
2727
#include <linux/fs.h>
28+
#include <linux/io.h>
2829

2930
#include <asm/cputype.h>
3031
#include <asm/sections.h>
@@ -251,6 +252,47 @@ static void __init create_mapping(phys_addr_t phys, unsigned long virt,
251252
} while (pgd++, addr = next, addr != end);
252253
}
253254

255+
#ifdef CONFIG_EARLY_PRINTK
256+
/*
257+
* Create an early I/O mapping using the pgd/pmd entries already populated
258+
* in head.S as this function is called too early to allocated any memory. The
259+
* mapping size is 2MB with 4KB pages or 64KB or 64KB pages.
260+
*/
261+
void __iomem * __init early_io_map(phys_addr_t phys, unsigned long virt)
262+
{
263+
unsigned long size, mask;
264+
bool page64k = IS_ENABLED(ARM64_64K_PAGES);
265+
pgd_t *pgd;
266+
pud_t *pud;
267+
pmd_t *pmd;
268+
pte_t *pte;
269+
270+
/*
271+
* No early pte entries with !ARM64_64K_PAGES configuration, so using
272+
* sections (pmd).
273+
*/
274+
size = page64k ? PAGE_SIZE : SECTION_SIZE;
275+
mask = ~(size - 1);
276+
277+
pgd = pgd_offset_k(virt);
278+
pud = pud_offset(pgd, virt);
279+
if (pud_none(*pud))
280+
return NULL;
281+
pmd = pmd_offset(pud, virt);
282+
283+
if (page64k) {
284+
if (pmd_none(*pmd))
285+
return NULL;
286+
pte = pte_offset_kernel(pmd, virt);
287+
set_pte(pte, __pte((phys & mask) | PROT_DEVICE_nGnRE));
288+
} else {
289+
set_pmd(pmd, __pmd((phys & mask) | PROT_SECT_DEVICE_nGnRE));
290+
}
291+
292+
return (void __iomem *)((virt & mask) + (phys & ~mask));
293+
}
294+
#endif
295+
254296
static void __init map_mem(void)
255297
{
256298
struct memblock_region *reg;

0 commit comments

Comments
 (0)