mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-18 22:19:30 +02:00
Patch series "mm/early_ioremap: clarify and clean up
early_ioremap_reset()".
__late_set_fixmap() and __late_clear_fixmap() are only used after
early_ioremap_reset() has been called, but the comment above them does not
say anything about that. So arm64, riscv and powerpc, whose
__set_fixmap() works before and after paging_init(), describe the same
situation in three different ways:
calls reset defines the macros
arm64 yes yes
riscv no yes
powerpc no no
Patch 1 documents when early_ioremap_reset() needs to be called and that
only architectures calling it need to define the macros.
Patches 2 and 3 remove the unneeded riscv macros, which are unreachable,
and the arm64 reset call and macros, which change nothing.
No functional change.
This patch (of 3):
__late_set_fixmap() and __late_clear_fixmap() are only used after
early_ioremap_reset() has been called.
arm64, riscv and powerpc all have a __set_fixmap() that works before and
after paging_init(), so they do not need to call early_ioremap_reset() or
define the macros, but they describe the same situation in three different
ways:
calls reset defines the macros
arm64 yes yes
riscv no yes
powerpc no no
The existing comment is vague and allows all three. Replace it with
comments that make it clear when the reset and the macros are needed.
No functional change.
Link: https://lore.kernel.org/20260708170647.362562-1-ekffu200098@gmail.com
Link: https://lore.kernel.org/20260708170647.362562-2-ekffu200098@gmail.com
Signed-off-by: Sang-Heon Jeon <ekffu200098@gmail.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David Hildenbrand <david@kernel.org>
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
316 lines
7.0 KiB
C
316 lines
7.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Provide common bits of early_ioremap() support for architectures needing
|
|
* temporary mappings during boot before ioremap() is available.
|
|
*
|
|
* This is mostly a direct copy of the x86 early_ioremap implementation.
|
|
*
|
|
* (C) Copyright 1995 1996, 2014 Linus Torvalds
|
|
*
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/init.h>
|
|
#include <linux/io.h>
|
|
#include <linux/module.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/vmalloc.h>
|
|
#include <asm/fixmap.h>
|
|
#include <asm/early_ioremap.h>
|
|
#include "internal.h"
|
|
|
|
#ifdef CONFIG_MMU
|
|
static int early_ioremap_debug __initdata;
|
|
|
|
static int __init early_ioremap_debug_setup(char *str)
|
|
{
|
|
early_ioremap_debug = 1;
|
|
|
|
return 0;
|
|
}
|
|
early_param("early_ioremap_debug", early_ioremap_debug_setup);
|
|
|
|
#define early_ioremap_dbg(fmt, args...) \
|
|
do { \
|
|
if (unlikely(early_ioremap_debug)) { \
|
|
pr_warn(fmt, ##args); \
|
|
dump_stack(); \
|
|
} \
|
|
} while (0)
|
|
|
|
static int after_paging_init __initdata;
|
|
|
|
pgprot_t __init __weak early_memremap_pgprot_adjust(resource_size_t phys_addr,
|
|
unsigned long size,
|
|
pgprot_t prot)
|
|
{
|
|
return prot;
|
|
}
|
|
|
|
/*
|
|
* Only architectures whose early_ioremap() must stop using __early_set_fixmap()
|
|
* after paging_init() need to call this.
|
|
*/
|
|
void __init early_ioremap_reset(void)
|
|
{
|
|
after_paging_init = 1;
|
|
}
|
|
|
|
/*
|
|
* Only architectures that call early_ioremap_reset() need to define
|
|
* __late_set_fixmap() and __late_clear_fixmap(), which early_ioremap() uses
|
|
* instead of __early_set_fixmap() after the reset.
|
|
*/
|
|
#ifndef __late_set_fixmap
|
|
static inline void __init __late_set_fixmap(enum fixed_addresses idx,
|
|
phys_addr_t phys, pgprot_t prot)
|
|
{
|
|
BUG();
|
|
}
|
|
#endif
|
|
|
|
#ifndef __late_clear_fixmap
|
|
static inline void __init __late_clear_fixmap(enum fixed_addresses idx)
|
|
{
|
|
BUG();
|
|
}
|
|
#endif
|
|
|
|
static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
|
|
static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
|
|
static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
|
|
|
|
void __init early_ioremap_setup(void)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
|
|
WARN_ON_ONCE(prev_map[i]);
|
|
slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
|
|
}
|
|
}
|
|
|
|
static int __init check_early_ioremap_leak(void)
|
|
{
|
|
int count = 0;
|
|
int i;
|
|
|
|
for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
|
|
if (prev_map[i])
|
|
count++;
|
|
|
|
if (WARN(count, KERN_WARNING
|
|
"Debug warning: early ioremap leak of %d areas detected.\n"
|
|
"please boot with early_ioremap_debug and report the dmesg.\n",
|
|
count))
|
|
return 1;
|
|
return 0;
|
|
}
|
|
late_initcall(check_early_ioremap_leak);
|
|
|
|
static void __init __iomem *
|
|
__early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
|
|
{
|
|
unsigned long offset;
|
|
resource_size_t last_addr;
|
|
unsigned int nrpages;
|
|
enum fixed_addresses idx;
|
|
int i, slot;
|
|
|
|
WARN_ON(system_state >= SYSTEM_RUNNING);
|
|
|
|
slot = -1;
|
|
for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
|
|
if (!prev_map[i]) {
|
|
slot = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (WARN(slot < 0, "%s(%pa, %08lx) not found slot\n",
|
|
__func__, &phys_addr, size))
|
|
return NULL;
|
|
|
|
/* Don't allow wraparound or zero size */
|
|
last_addr = phys_addr + size - 1;
|
|
if (WARN_ON(!size || last_addr < phys_addr))
|
|
return NULL;
|
|
|
|
prev_size[slot] = size;
|
|
/*
|
|
* Mappings have to be page-aligned
|
|
*/
|
|
offset = offset_in_page(phys_addr);
|
|
phys_addr &= PAGE_MASK;
|
|
size = PAGE_ALIGN(last_addr + 1) - phys_addr;
|
|
|
|
/*
|
|
* Mappings have to fit in the FIX_BTMAP area.
|
|
*/
|
|
nrpages = size >> PAGE_SHIFT;
|
|
if (WARN_ON(nrpages > NR_FIX_BTMAPS))
|
|
return NULL;
|
|
|
|
early_ioremap_dbg("%s(%pa, %08lx) [%d] => %08lx + %08lx\n",
|
|
__func__, &phys_addr, size, slot, slot_virt[slot], offset);
|
|
|
|
/*
|
|
* Ok, go for it..
|
|
*/
|
|
idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
|
|
while (nrpages > 0) {
|
|
if (after_paging_init)
|
|
__late_set_fixmap(idx, phys_addr, prot);
|
|
else
|
|
__early_set_fixmap(idx, phys_addr, prot);
|
|
phys_addr += PAGE_SIZE;
|
|
--idx;
|
|
--nrpages;
|
|
}
|
|
|
|
prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
|
|
return prev_map[slot];
|
|
}
|
|
|
|
void __init early_iounmap(void __iomem *addr, unsigned long size)
|
|
{
|
|
unsigned long virt_addr;
|
|
unsigned long offset;
|
|
unsigned int nrpages;
|
|
enum fixed_addresses idx;
|
|
int i, slot;
|
|
|
|
slot = -1;
|
|
for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
|
|
if (prev_map[i] == addr) {
|
|
slot = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (WARN(slot < 0, "%s(%p, %08lx) not found slot\n",
|
|
__func__, addr, size))
|
|
return;
|
|
|
|
if (WARN(prev_size[slot] != size,
|
|
"%s(%p, %08lx) [%d] size not consistent %08lx\n",
|
|
__func__, addr, size, slot, prev_size[slot]))
|
|
return;
|
|
|
|
early_ioremap_dbg("%s(%p, %08lx) [%d]\n", __func__, addr, size, slot);
|
|
|
|
virt_addr = (unsigned long)addr;
|
|
if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)))
|
|
return;
|
|
|
|
offset = offset_in_page(virt_addr);
|
|
nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
|
|
|
|
idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
|
|
while (nrpages > 0) {
|
|
if (after_paging_init)
|
|
__late_clear_fixmap(idx);
|
|
else
|
|
__early_set_fixmap(idx, 0, FIXMAP_PAGE_CLEAR);
|
|
--idx;
|
|
--nrpages;
|
|
}
|
|
prev_map[slot] = NULL;
|
|
}
|
|
|
|
/* Remap an IO device */
|
|
void __init __iomem *
|
|
early_ioremap(resource_size_t phys_addr, unsigned long size)
|
|
{
|
|
return __early_ioremap(phys_addr, size, FIXMAP_PAGE_IO);
|
|
}
|
|
|
|
/* Remap memory */
|
|
void __init *
|
|
early_memremap(resource_size_t phys_addr, unsigned long size)
|
|
{
|
|
pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
|
|
FIXMAP_PAGE_NORMAL);
|
|
|
|
return (__force void *)__early_ioremap(phys_addr, size, prot);
|
|
}
|
|
#ifdef FIXMAP_PAGE_RO
|
|
void __init *
|
|
early_memremap_ro(resource_size_t phys_addr, unsigned long size)
|
|
{
|
|
pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
|
|
FIXMAP_PAGE_RO);
|
|
|
|
return (__force void *)__early_ioremap(phys_addr, size, prot);
|
|
}
|
|
#endif
|
|
|
|
#ifdef CONFIG_ARCH_USE_MEMREMAP_PROT
|
|
void __init *
|
|
early_memremap_prot(resource_size_t phys_addr, unsigned long size,
|
|
unsigned long prot_val)
|
|
{
|
|
return (__force void *)__early_ioremap(phys_addr, size,
|
|
__pgprot(prot_val));
|
|
}
|
|
#endif
|
|
|
|
#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
|
|
|
|
/*
|
|
* If no empty slot, handle that and return -ENOMEM.
|
|
*/
|
|
int __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
|
|
{
|
|
unsigned long slop, clen;
|
|
char *p;
|
|
|
|
while (size) {
|
|
slop = offset_in_page(src);
|
|
clen = size;
|
|
if (clen > MAX_MAP_CHUNK - slop)
|
|
clen = MAX_MAP_CHUNK - slop;
|
|
p = early_memremap(src & PAGE_MASK, clen + slop);
|
|
if (!p)
|
|
return -ENOMEM;
|
|
memcpy(dest, p + slop, clen);
|
|
early_memunmap(p, clen + slop);
|
|
dest += clen;
|
|
src += clen;
|
|
size -= clen;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#else /* CONFIG_MMU */
|
|
|
|
void __init __iomem *
|
|
early_ioremap(resource_size_t phys_addr, unsigned long size)
|
|
{
|
|
return (__force void __iomem *)phys_addr;
|
|
}
|
|
|
|
/* Remap memory */
|
|
void __init *
|
|
early_memremap(resource_size_t phys_addr, unsigned long size)
|
|
{
|
|
return (void *)phys_addr;
|
|
}
|
|
void __init *
|
|
early_memremap_ro(resource_size_t phys_addr, unsigned long size)
|
|
{
|
|
return (void *)phys_addr;
|
|
}
|
|
|
|
void __init early_iounmap(void __iomem *addr, unsigned long size)
|
|
{
|
|
}
|
|
|
|
#endif /* CONFIG_MMU */
|
|
|
|
|
|
void __init early_memunmap(void *addr, unsigned long size)
|
|
{
|
|
early_iounmap((__force void __iomem *)addr, size);
|
|
}
|