162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2015 Synopsys, Inc. (www.synopsys.com)
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/memblock.h>
762306a36Sopenharmony_ci#include <linux/export.h>
862306a36Sopenharmony_ci#include <linux/highmem.h>
962306a36Sopenharmony_ci#include <linux/pgtable.h>
1062306a36Sopenharmony_ci#include <asm/processor.h>
1162306a36Sopenharmony_ci#include <asm/pgalloc.h>
1262306a36Sopenharmony_ci#include <asm/tlbflush.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/*
1562306a36Sopenharmony_ci * HIGHMEM API:
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * kmap() API provides sleep semantics hence referred to as "permanent maps"
1862306a36Sopenharmony_ci * It allows mapping LAST_PKMAP pages, using @last_pkmap_nr as the cursor
1962306a36Sopenharmony_ci * for book-keeping
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * kmap_atomic() can't sleep (calls pagefault_disable()), thus it provides
2262306a36Sopenharmony_ci * shortlived ala "temporary mappings" which historically were implemented as
2362306a36Sopenharmony_ci * fixmaps (compile time addr etc). Their book-keeping is done per cpu.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci *	Both these facts combined (preemption disabled and per-cpu allocation)
2662306a36Sopenharmony_ci *	means the total number of concurrent fixmaps will be limited to max
2762306a36Sopenharmony_ci *	such allocations in a single control path. Thus KM_TYPE_NR (another
2862306a36Sopenharmony_ci *	historic relic) is a small'ish number which caps max percpu fixmaps
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * ARC HIGHMEM Details
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * - the kernel vaddr space from 0x7z to 0x8z (currently used by vmalloc/module)
3362306a36Sopenharmony_ci *   is now shared between vmalloc and kmap (non overlapping though)
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * - Both fixmap/pkmap use a dedicated page table each, hooked up to swapper PGD
3662306a36Sopenharmony_ci *   This means each only has 1 PGDIR_SIZE worth of kvaddr mappings, which means
3762306a36Sopenharmony_ci *   2M of kvaddr space for typical config (8K page and 11:8:13 traversal split)
3862306a36Sopenharmony_ci *
3962306a36Sopenharmony_ci * - The fixed KMAP slots for kmap_local/atomic() require KM_MAX_IDX slots per
4062306a36Sopenharmony_ci *   CPU. So the number of CPUs sharing a single PTE page is limited.
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * - pkmap being preemptible, in theory could do with more than 256 concurrent
4362306a36Sopenharmony_ci *   mappings. However, generic pkmap code: map_new_virtual(), doesn't traverse
4462306a36Sopenharmony_ci *   the PGD and only works with a single page table @pkmap_page_table, hence
4562306a36Sopenharmony_ci *   sets the limit
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciextern pte_t * pkmap_page_table;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic noinline pte_t * __init alloc_kmap_pgtable(unsigned long kvaddr)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	pmd_t *pmd_k = pmd_off_k(kvaddr);
5362306a36Sopenharmony_ci	pte_t *pte_k;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	pte_k = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
5662306a36Sopenharmony_ci	if (!pte_k)
5762306a36Sopenharmony_ci		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
5862306a36Sopenharmony_ci		      __func__, PAGE_SIZE, PAGE_SIZE);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	pmd_populate_kernel(&init_mm, pmd_k, pte_k);
6162306a36Sopenharmony_ci	return pte_k;
6262306a36Sopenharmony_ci}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_civoid __init kmap_init(void)
6562306a36Sopenharmony_ci{
6662306a36Sopenharmony_ci	/* Due to recursive include hell, we can't do this in processor.h */
6762306a36Sopenharmony_ci	BUILD_BUG_ON(PAGE_OFFSET < (VMALLOC_END + FIXMAP_SIZE + PKMAP_SIZE));
6862306a36Sopenharmony_ci	BUILD_BUG_ON(LAST_PKMAP > PTRS_PER_PTE);
6962306a36Sopenharmony_ci	BUILD_BUG_ON(FIX_KMAP_SLOTS > PTRS_PER_PTE);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	pkmap_page_table = alloc_kmap_pgtable(PKMAP_BASE);
7262306a36Sopenharmony_ci	alloc_kmap_pgtable(FIXMAP_BASE);
7362306a36Sopenharmony_ci}
74