18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Synopsys, Inc. (www.synopsys.com)
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/memblock.h>
78c2ecf20Sopenharmony_ci#include <linux/export.h>
88c2ecf20Sopenharmony_ci#include <linux/highmem.h>
98c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
108c2ecf20Sopenharmony_ci#include <asm/processor.h>
118c2ecf20Sopenharmony_ci#include <asm/pgalloc.h>
128c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * HIGHMEM API:
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * kmap() API provides sleep semantics hence referred to as "permanent maps"
188c2ecf20Sopenharmony_ci * It allows mapping LAST_PKMAP pages, using @last_pkmap_nr as the cursor
198c2ecf20Sopenharmony_ci * for book-keeping
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * kmap_atomic() can't sleep (calls pagefault_disable()), thus it provides
228c2ecf20Sopenharmony_ci * shortlived ala "temporary mappings" which historically were implemented as
238c2ecf20Sopenharmony_ci * fixmaps (compile time addr etc). Their book-keeping is done per cpu.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci *	Both these facts combined (preemption disabled and per-cpu allocation)
268c2ecf20Sopenharmony_ci *	means the total number of concurrent fixmaps will be limited to max
278c2ecf20Sopenharmony_ci *	such allocations in a single control path. Thus KM_TYPE_NR (another
288c2ecf20Sopenharmony_ci *	historic relic) is a small'ish number which caps max percpu fixmaps
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * ARC HIGHMEM Details
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * - the kernel vaddr space from 0x7z to 0x8z (currently used by vmalloc/module)
338c2ecf20Sopenharmony_ci *   is now shared between vmalloc and kmap (non overlapping though)
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * - Both fixmap/pkmap use a dedicated page table each, hooked up to swapper PGD
368c2ecf20Sopenharmony_ci *   This means each only has 1 PGDIR_SIZE worth of kvaddr mappings, which means
378c2ecf20Sopenharmony_ci *   2M of kvaddr space for typical config (8K page and 11:8:13 traversal split)
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * - fixmap anyhow needs a limited number of mappings. So 2M kvaddr == 256 PTE
408c2ecf20Sopenharmony_ci *   slots across NR_CPUS would be more than sufficient (generic code defines
418c2ecf20Sopenharmony_ci *   KM_TYPE_NR as 20).
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * - pkmap being preemptible, in theory could do with more than 256 concurrent
448c2ecf20Sopenharmony_ci *   mappings. However, generic pkmap code: map_new_virtual(), doesn't traverse
458c2ecf20Sopenharmony_ci *   the PGD and only works with a single page table @pkmap_page_table, hence
468c2ecf20Sopenharmony_ci *   sets the limit
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciextern pte_t * pkmap_page_table;
508c2ecf20Sopenharmony_cistatic pte_t * fixmap_page_table;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_civoid *kmap_atomic_high_prot(struct page *page, pgprot_t prot)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	int idx, cpu_idx;
558c2ecf20Sopenharmony_ci	unsigned long vaddr;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	cpu_idx = kmap_atomic_idx_push();
588c2ecf20Sopenharmony_ci	idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
598c2ecf20Sopenharmony_ci	vaddr = FIXMAP_ADDR(idx);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	set_pte_at(&init_mm, vaddr, fixmap_page_table + idx,
628c2ecf20Sopenharmony_ci		   mk_pte(page, prot));
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return (void *)vaddr;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmap_atomic_high_prot);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_civoid kunmap_atomic_high(void *kv)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	unsigned long kvaddr = (unsigned long)kv;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (kvaddr >= FIXMAP_BASE && kvaddr < (FIXMAP_BASE + FIXMAP_SIZE)) {
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		/*
758c2ecf20Sopenharmony_ci		 * Because preemption is disabled, this vaddr can be associated
768c2ecf20Sopenharmony_ci		 * with the current allocated index.
778c2ecf20Sopenharmony_ci		 * But in case of multiple live kmap_atomic(), it still relies on
788c2ecf20Sopenharmony_ci		 * callers to unmap in right order.
798c2ecf20Sopenharmony_ci		 */
808c2ecf20Sopenharmony_ci		int cpu_idx = kmap_atomic_idx();
818c2ecf20Sopenharmony_ci		int idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		WARN_ON(kvaddr != FIXMAP_ADDR(idx));
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci		pte_clear(&init_mm, kvaddr, fixmap_page_table + idx);
868c2ecf20Sopenharmony_ci		local_flush_tlb_kernel_range(kvaddr, kvaddr + PAGE_SIZE);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		kmap_atomic_idx_pop();
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kunmap_atomic_high);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic noinline pte_t * __init alloc_kmap_pgtable(unsigned long kvaddr)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	pmd_t *pmd_k = pmd_off_k(kvaddr);
968c2ecf20Sopenharmony_ci	pte_t *pte_k;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	pte_k = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
998c2ecf20Sopenharmony_ci	if (!pte_k)
1008c2ecf20Sopenharmony_ci		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
1018c2ecf20Sopenharmony_ci		      __func__, PAGE_SIZE, PAGE_SIZE);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	pmd_populate_kernel(&init_mm, pmd_k, pte_k);
1048c2ecf20Sopenharmony_ci	return pte_k;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_civoid __init kmap_init(void)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	/* Due to recursive include hell, we can't do this in processor.h */
1108c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PAGE_OFFSET < (VMALLOC_END + FIXMAP_SIZE + PKMAP_SIZE));
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	BUILD_BUG_ON(KM_TYPE_NR > PTRS_PER_PTE);
1138c2ecf20Sopenharmony_ci	pkmap_page_table = alloc_kmap_pgtable(PKMAP_BASE);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	BUILD_BUG_ON(LAST_PKMAP > PTRS_PER_PTE);
1168c2ecf20Sopenharmony_ci	fixmap_page_table = alloc_kmap_pgtable(FIXMAP_BASE);
1178c2ecf20Sopenharmony_ci}
118