18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 1994 - 2000 Ralf Baechle
78c2ecf20Sopenharmony_ci * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
88c2ecf20Sopenharmony_ci * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
98c2ecf20Sopenharmony_ci * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <linux/bug.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/export.h>
148c2ecf20Sopenharmony_ci#include <linux/signal.h>
158c2ecf20Sopenharmony_ci#include <linux/sched.h>
168c2ecf20Sopenharmony_ci#include <linux/smp.h>
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/errno.h>
198c2ecf20Sopenharmony_ci#include <linux/string.h>
208c2ecf20Sopenharmony_ci#include <linux/types.h>
218c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
228c2ecf20Sopenharmony_ci#include <linux/ptrace.h>
238c2ecf20Sopenharmony_ci#include <linux/mman.h>
248c2ecf20Sopenharmony_ci#include <linux/mm.h>
258c2ecf20Sopenharmony_ci#include <linux/memblock.h>
268c2ecf20Sopenharmony_ci#include <linux/highmem.h>
278c2ecf20Sopenharmony_ci#include <linux/swap.h>
288c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
298c2ecf20Sopenharmony_ci#include <linux/pfn.h>
308c2ecf20Sopenharmony_ci#include <linux/hardirq.h>
318c2ecf20Sopenharmony_ci#include <linux/gfp.h>
328c2ecf20Sopenharmony_ci#include <linux/kcore.h>
338c2ecf20Sopenharmony_ci#include <linux/initrd.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
368c2ecf20Sopenharmony_ci#include <asm/cachectl.h>
378c2ecf20Sopenharmony_ci#include <asm/cpu.h>
388c2ecf20Sopenharmony_ci#include <asm/dma.h>
398c2ecf20Sopenharmony_ci#include <asm/kmap_types.h>
408c2ecf20Sopenharmony_ci#include <asm/maar.h>
418c2ecf20Sopenharmony_ci#include <asm/mmu_context.h>
428c2ecf20Sopenharmony_ci#include <asm/sections.h>
438c2ecf20Sopenharmony_ci#include <asm/pgalloc.h>
448c2ecf20Sopenharmony_ci#include <asm/tlb.h>
458c2ecf20Sopenharmony_ci#include <asm/fixmap.h>
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/*
488c2ecf20Sopenharmony_ci * We have up to 8 empty zeroed pages so we can map one of the right colour
498c2ecf20Sopenharmony_ci * when needed.	 This is necessary only on R4000 / R4400 SC and MC versions
508c2ecf20Sopenharmony_ci * where we have to avoid VCED / VECI exceptions for good performance at
518c2ecf20Sopenharmony_ci * any price.  Since page is never written to after the initialization we
528c2ecf20Sopenharmony_ci * don't have to care about aliases on other CPUs.
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_ciunsigned long empty_zero_page, zero_page_mask;
558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(empty_zero_page);
568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(zero_page_mask);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * Not static inline because used by IP27 special magic initialization code
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_civoid setup_zero_pages(void)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	unsigned int order, i;
648c2ecf20Sopenharmony_ci	struct page *page;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	if (cpu_has_vce)
678c2ecf20Sopenharmony_ci		order = 3;
688c2ecf20Sopenharmony_ci	else
698c2ecf20Sopenharmony_ci		order = 0;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
728c2ecf20Sopenharmony_ci	if (!empty_zero_page)
738c2ecf20Sopenharmony_ci		panic("Oh boy, that early out of memory?");
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	page = virt_to_page((void *)empty_zero_page);
768c2ecf20Sopenharmony_ci	split_page(page, order);
778c2ecf20Sopenharmony_ci	for (i = 0; i < (1 << order); i++, page++)
788c2ecf20Sopenharmony_ci		mark_page_reserved(page);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	zero_page_mask = ((PAGE_SIZE << order) - 1) & PAGE_MASK;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic void *__kmap_pgprot(struct page *page, unsigned long addr, pgprot_t prot)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	enum fixed_addresses idx;
868c2ecf20Sopenharmony_ci	unsigned int old_mmid;
878c2ecf20Sopenharmony_ci	unsigned long vaddr, flags, entrylo;
888c2ecf20Sopenharmony_ci	unsigned long old_ctx;
898c2ecf20Sopenharmony_ci	pte_t pte;
908c2ecf20Sopenharmony_ci	int tlbidx;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	BUG_ON(Page_dcache_dirty(page));
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	preempt_disable();
958c2ecf20Sopenharmony_ci	pagefault_disable();
968c2ecf20Sopenharmony_ci	idx = (addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1);
978c2ecf20Sopenharmony_ci	idx += in_interrupt() ? FIX_N_COLOURS : 0;
988c2ecf20Sopenharmony_ci	vaddr = __fix_to_virt(FIX_CMAP_END - idx);
998c2ecf20Sopenharmony_ci	pte = mk_pte(page, prot);
1008c2ecf20Sopenharmony_ci#if defined(CONFIG_XPA)
1018c2ecf20Sopenharmony_ci	entrylo = pte_to_entrylo(pte.pte_high);
1028c2ecf20Sopenharmony_ci#elif defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
1038c2ecf20Sopenharmony_ci	entrylo = pte.pte_high;
1048c2ecf20Sopenharmony_ci#else
1058c2ecf20Sopenharmony_ci	entrylo = pte_to_entrylo(pte_val(pte));
1068c2ecf20Sopenharmony_ci#endif
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	local_irq_save(flags);
1098c2ecf20Sopenharmony_ci	old_ctx = read_c0_entryhi();
1108c2ecf20Sopenharmony_ci	write_c0_entryhi(vaddr & (PAGE_MASK << 1));
1118c2ecf20Sopenharmony_ci	write_c0_entrylo0(entrylo);
1128c2ecf20Sopenharmony_ci	write_c0_entrylo1(entrylo);
1138c2ecf20Sopenharmony_ci	if (cpu_has_mmid) {
1148c2ecf20Sopenharmony_ci		old_mmid = read_c0_memorymapid();
1158c2ecf20Sopenharmony_ci		write_c0_memorymapid(MMID_KERNEL_WIRED);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci#ifdef CONFIG_XPA
1188c2ecf20Sopenharmony_ci	if (cpu_has_xpa) {
1198c2ecf20Sopenharmony_ci		entrylo = (pte.pte_low & _PFNX_MASK);
1208c2ecf20Sopenharmony_ci		writex_c0_entrylo0(entrylo);
1218c2ecf20Sopenharmony_ci		writex_c0_entrylo1(entrylo);
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci#endif
1248c2ecf20Sopenharmony_ci	tlbidx = num_wired_entries();
1258c2ecf20Sopenharmony_ci	write_c0_wired(tlbidx + 1);
1268c2ecf20Sopenharmony_ci	write_c0_index(tlbidx);
1278c2ecf20Sopenharmony_ci	mtc0_tlbw_hazard();
1288c2ecf20Sopenharmony_ci	tlb_write_indexed();
1298c2ecf20Sopenharmony_ci	tlbw_use_hazard();
1308c2ecf20Sopenharmony_ci	write_c0_entryhi(old_ctx);
1318c2ecf20Sopenharmony_ci	if (cpu_has_mmid)
1328c2ecf20Sopenharmony_ci		write_c0_memorymapid(old_mmid);
1338c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return (void*) vaddr;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_civoid *kmap_coherent(struct page *page, unsigned long addr)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	return __kmap_pgprot(page, addr, PAGE_KERNEL);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_civoid *kmap_noncoherent(struct page *page, unsigned long addr)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	return __kmap_pgprot(page, addr, PAGE_KERNEL_NC);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_civoid kunmap_coherent(void)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	unsigned int wired;
1518c2ecf20Sopenharmony_ci	unsigned long flags, old_ctx;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	local_irq_save(flags);
1548c2ecf20Sopenharmony_ci	old_ctx = read_c0_entryhi();
1558c2ecf20Sopenharmony_ci	wired = num_wired_entries() - 1;
1568c2ecf20Sopenharmony_ci	write_c0_wired(wired);
1578c2ecf20Sopenharmony_ci	write_c0_index(wired);
1588c2ecf20Sopenharmony_ci	write_c0_entryhi(UNIQUE_ENTRYHI(wired));
1598c2ecf20Sopenharmony_ci	write_c0_entrylo0(0);
1608c2ecf20Sopenharmony_ci	write_c0_entrylo1(0);
1618c2ecf20Sopenharmony_ci	mtc0_tlbw_hazard();
1628c2ecf20Sopenharmony_ci	tlb_write_indexed();
1638c2ecf20Sopenharmony_ci	tlbw_use_hazard();
1648c2ecf20Sopenharmony_ci	write_c0_entryhi(old_ctx);
1658c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1668c2ecf20Sopenharmony_ci	pagefault_enable();
1678c2ecf20Sopenharmony_ci	preempt_enable();
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_civoid copy_user_highpage(struct page *to, struct page *from,
1718c2ecf20Sopenharmony_ci	unsigned long vaddr, struct vm_area_struct *vma)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	void *vfrom, *vto;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	vto = kmap_atomic(to);
1768c2ecf20Sopenharmony_ci	if (cpu_has_dc_aliases &&
1778c2ecf20Sopenharmony_ci	    page_mapcount(from) && !Page_dcache_dirty(from)) {
1788c2ecf20Sopenharmony_ci		vfrom = kmap_coherent(from, vaddr);
1798c2ecf20Sopenharmony_ci		copy_page(vto, vfrom);
1808c2ecf20Sopenharmony_ci		kunmap_coherent();
1818c2ecf20Sopenharmony_ci	} else {
1828c2ecf20Sopenharmony_ci		vfrom = kmap_atomic(from);
1838c2ecf20Sopenharmony_ci		copy_page(vto, vfrom);
1848c2ecf20Sopenharmony_ci		kunmap_atomic(vfrom);
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci	if ((!cpu_has_ic_fills_f_dc) ||
1878c2ecf20Sopenharmony_ci	    pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK))
1888c2ecf20Sopenharmony_ci		flush_data_cache_page((unsigned long)vto);
1898c2ecf20Sopenharmony_ci	kunmap_atomic(vto);
1908c2ecf20Sopenharmony_ci	/* Make sure this page is cleared on other CPU's too before using it */
1918c2ecf20Sopenharmony_ci	smp_wmb();
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_civoid copy_to_user_page(struct vm_area_struct *vma,
1958c2ecf20Sopenharmony_ci	struct page *page, unsigned long vaddr, void *dst, const void *src,
1968c2ecf20Sopenharmony_ci	unsigned long len)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	if (cpu_has_dc_aliases &&
1998c2ecf20Sopenharmony_ci	    page_mapcount(page) && !Page_dcache_dirty(page)) {
2008c2ecf20Sopenharmony_ci		void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
2018c2ecf20Sopenharmony_ci		memcpy(vto, src, len);
2028c2ecf20Sopenharmony_ci		kunmap_coherent();
2038c2ecf20Sopenharmony_ci	} else {
2048c2ecf20Sopenharmony_ci		memcpy(dst, src, len);
2058c2ecf20Sopenharmony_ci		if (cpu_has_dc_aliases)
2068c2ecf20Sopenharmony_ci			SetPageDcacheDirty(page);
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci	if (vma->vm_flags & VM_EXEC)
2098c2ecf20Sopenharmony_ci		flush_cache_page(vma, vaddr, page_to_pfn(page));
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_civoid copy_from_user_page(struct vm_area_struct *vma,
2138c2ecf20Sopenharmony_ci	struct page *page, unsigned long vaddr, void *dst, const void *src,
2148c2ecf20Sopenharmony_ci	unsigned long len)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	if (cpu_has_dc_aliases &&
2178c2ecf20Sopenharmony_ci	    page_mapcount(page) && !Page_dcache_dirty(page)) {
2188c2ecf20Sopenharmony_ci		void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
2198c2ecf20Sopenharmony_ci		memcpy(dst, vfrom, len);
2208c2ecf20Sopenharmony_ci		kunmap_coherent();
2218c2ecf20Sopenharmony_ci	} else {
2228c2ecf20Sopenharmony_ci		memcpy(dst, src, len);
2238c2ecf20Sopenharmony_ci		if (cpu_has_dc_aliases)
2248c2ecf20Sopenharmony_ci			SetPageDcacheDirty(page);
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(copy_from_user_page);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_civoid __init fixrange_init(unsigned long start, unsigned long end,
2308c2ecf20Sopenharmony_ci	pgd_t *pgd_base)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
2338c2ecf20Sopenharmony_ci	pgd_t *pgd;
2348c2ecf20Sopenharmony_ci	pud_t *pud;
2358c2ecf20Sopenharmony_ci	pmd_t *pmd;
2368c2ecf20Sopenharmony_ci	pte_t *pte;
2378c2ecf20Sopenharmony_ci	int i, j, k;
2388c2ecf20Sopenharmony_ci	unsigned long vaddr;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	vaddr = start;
2418c2ecf20Sopenharmony_ci	i = pgd_index(vaddr);
2428c2ecf20Sopenharmony_ci	j = pud_index(vaddr);
2438c2ecf20Sopenharmony_ci	k = pmd_index(vaddr);
2448c2ecf20Sopenharmony_ci	pgd = pgd_base + i;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
2478c2ecf20Sopenharmony_ci		pud = (pud_t *)pgd;
2488c2ecf20Sopenharmony_ci		for ( ; (j < PTRS_PER_PUD) && (vaddr < end); pud++, j++) {
2498c2ecf20Sopenharmony_ci			pmd = (pmd_t *)pud;
2508c2ecf20Sopenharmony_ci			for (; (k < PTRS_PER_PMD) && (vaddr < end); pmd++, k++) {
2518c2ecf20Sopenharmony_ci				if (pmd_none(*pmd)) {
2528c2ecf20Sopenharmony_ci					pte = (pte_t *) memblock_alloc_low(PAGE_SIZE,
2538c2ecf20Sopenharmony_ci									   PAGE_SIZE);
2548c2ecf20Sopenharmony_ci					if (!pte)
2558c2ecf20Sopenharmony_ci						panic("%s: Failed to allocate %lu bytes align=%lx\n",
2568c2ecf20Sopenharmony_ci						      __func__, PAGE_SIZE,
2578c2ecf20Sopenharmony_ci						      PAGE_SIZE);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci					set_pmd(pmd, __pmd((unsigned long)pte));
2608c2ecf20Sopenharmony_ci					BUG_ON(pte != pte_offset_kernel(pmd, 0));
2618c2ecf20Sopenharmony_ci				}
2628c2ecf20Sopenharmony_ci				vaddr += PMD_SIZE;
2638c2ecf20Sopenharmony_ci			}
2648c2ecf20Sopenharmony_ci			k = 0;
2658c2ecf20Sopenharmony_ci		}
2668c2ecf20Sopenharmony_ci		j = 0;
2678c2ecf20Sopenharmony_ci	}
2688c2ecf20Sopenharmony_ci#endif
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistruct maar_walk_info {
2728c2ecf20Sopenharmony_ci	struct maar_config cfg[16];
2738c2ecf20Sopenharmony_ci	unsigned int num_cfg;
2748c2ecf20Sopenharmony_ci};
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic int maar_res_walk(unsigned long start_pfn, unsigned long nr_pages,
2778c2ecf20Sopenharmony_ci			 void *data)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct maar_walk_info *wi = data;
2808c2ecf20Sopenharmony_ci	struct maar_config *cfg = &wi->cfg[wi->num_cfg];
2818c2ecf20Sopenharmony_ci	unsigned int maar_align;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* MAAR registers hold physical addresses right shifted by 4 bits */
2848c2ecf20Sopenharmony_ci	maar_align = BIT(MIPS_MAAR_ADDR_SHIFT + 4);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* Fill in the MAAR config entry */
2878c2ecf20Sopenharmony_ci	cfg->lower = ALIGN(PFN_PHYS(start_pfn), maar_align);
2888c2ecf20Sopenharmony_ci	cfg->upper = ALIGN_DOWN(PFN_PHYS(start_pfn + nr_pages), maar_align) - 1;
2898c2ecf20Sopenharmony_ci	cfg->attrs = MIPS_MAAR_S;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Ensure we don't overflow the cfg array */
2928c2ecf20Sopenharmony_ci	if (!WARN_ON(wi->num_cfg >= ARRAY_SIZE(wi->cfg)))
2938c2ecf20Sopenharmony_ci		wi->num_cfg++;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	return 0;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ciunsigned __weak platform_maar_init(unsigned num_pairs)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	unsigned int num_configured;
3028c2ecf20Sopenharmony_ci	struct maar_walk_info wi;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	wi.num_cfg = 0;
3058c2ecf20Sopenharmony_ci	walk_system_ram_range(0, max_pfn, &wi, maar_res_walk);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	num_configured = maar_config(wi.cfg, wi.num_cfg, num_pairs);
3088c2ecf20Sopenharmony_ci	if (num_configured < wi.num_cfg)
3098c2ecf20Sopenharmony_ci		pr_warn("Not enough MAAR pairs (%u) for all memory regions (%u)\n",
3108c2ecf20Sopenharmony_ci			num_pairs, wi.num_cfg);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return num_configured;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_civoid maar_init(void)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	unsigned num_maars, used, i;
3188c2ecf20Sopenharmony_ci	phys_addr_t lower, upper, attr;
3198c2ecf20Sopenharmony_ci	static struct {
3208c2ecf20Sopenharmony_ci		struct maar_config cfgs[3];
3218c2ecf20Sopenharmony_ci		unsigned used;
3228c2ecf20Sopenharmony_ci	} recorded = { { { 0 } }, 0 };
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (!cpu_has_maar)
3258c2ecf20Sopenharmony_ci		return;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	/* Detect the number of MAARs */
3288c2ecf20Sopenharmony_ci	write_c0_maari(~0);
3298c2ecf20Sopenharmony_ci	back_to_back_c0_hazard();
3308c2ecf20Sopenharmony_ci	num_maars = read_c0_maari() + 1;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/* MAARs should be in pairs */
3338c2ecf20Sopenharmony_ci	WARN_ON(num_maars % 2);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* Set MAARs using values we recorded already */
3368c2ecf20Sopenharmony_ci	if (recorded.used) {
3378c2ecf20Sopenharmony_ci		used = maar_config(recorded.cfgs, recorded.used, num_maars / 2);
3388c2ecf20Sopenharmony_ci		BUG_ON(used != recorded.used);
3398c2ecf20Sopenharmony_ci	} else {
3408c2ecf20Sopenharmony_ci		/* Configure the required MAARs */
3418c2ecf20Sopenharmony_ci		used = platform_maar_init(num_maars / 2);
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* Disable any further MAARs */
3458c2ecf20Sopenharmony_ci	for (i = (used * 2); i < num_maars; i++) {
3468c2ecf20Sopenharmony_ci		write_c0_maari(i);
3478c2ecf20Sopenharmony_ci		back_to_back_c0_hazard();
3488c2ecf20Sopenharmony_ci		write_c0_maar(0);
3498c2ecf20Sopenharmony_ci		back_to_back_c0_hazard();
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	if (recorded.used)
3538c2ecf20Sopenharmony_ci		return;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	pr_info("MAAR configuration:\n");
3568c2ecf20Sopenharmony_ci	for (i = 0; i < num_maars; i += 2) {
3578c2ecf20Sopenharmony_ci		write_c0_maari(i);
3588c2ecf20Sopenharmony_ci		back_to_back_c0_hazard();
3598c2ecf20Sopenharmony_ci		upper = read_c0_maar();
3608c2ecf20Sopenharmony_ci#ifdef CONFIG_XPA
3618c2ecf20Sopenharmony_ci		upper |= (phys_addr_t)readx_c0_maar() << MIPS_MAARX_ADDR_SHIFT;
3628c2ecf20Sopenharmony_ci#endif
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci		write_c0_maari(i + 1);
3658c2ecf20Sopenharmony_ci		back_to_back_c0_hazard();
3668c2ecf20Sopenharmony_ci		lower = read_c0_maar();
3678c2ecf20Sopenharmony_ci#ifdef CONFIG_XPA
3688c2ecf20Sopenharmony_ci		lower |= (phys_addr_t)readx_c0_maar() << MIPS_MAARX_ADDR_SHIFT;
3698c2ecf20Sopenharmony_ci#endif
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		attr = lower & upper;
3728c2ecf20Sopenharmony_ci		lower = (lower & MIPS_MAAR_ADDR) << 4;
3738c2ecf20Sopenharmony_ci		upper = ((upper & MIPS_MAAR_ADDR) << 4) | 0xffff;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		pr_info("  [%d]: ", i / 2);
3768c2ecf20Sopenharmony_ci		if ((attr & MIPS_MAAR_V) != MIPS_MAAR_V) {
3778c2ecf20Sopenharmony_ci			pr_cont("disabled\n");
3788c2ecf20Sopenharmony_ci			continue;
3798c2ecf20Sopenharmony_ci		}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci		pr_cont("%pa-%pa", &lower, &upper);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci		if (attr & MIPS_MAAR_S)
3848c2ecf20Sopenharmony_ci			pr_cont(" speculate");
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci		pr_cont("\n");
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		/* Record the setup for use on secondary CPUs */
3898c2ecf20Sopenharmony_ci		if (used <= ARRAY_SIZE(recorded.cfgs)) {
3908c2ecf20Sopenharmony_ci			recorded.cfgs[recorded.used].lower = lower;
3918c2ecf20Sopenharmony_ci			recorded.cfgs[recorded.used].upper = upper;
3928c2ecf20Sopenharmony_ci			recorded.cfgs[recorded.used].attrs = attr;
3938c2ecf20Sopenharmony_ci			recorded.used++;
3948c2ecf20Sopenharmony_ci		}
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci#ifndef CONFIG_NEED_MULTIPLE_NODES
3998c2ecf20Sopenharmony_civoid __init paging_init(void)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	unsigned long max_zone_pfns[MAX_NR_ZONES];
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	pagetable_init();
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
4068c2ecf20Sopenharmony_ci	kmap_init();
4078c2ecf20Sopenharmony_ci#endif
4088c2ecf20Sopenharmony_ci#ifdef CONFIG_ZONE_DMA
4098c2ecf20Sopenharmony_ci	max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
4108c2ecf20Sopenharmony_ci#endif
4118c2ecf20Sopenharmony_ci#ifdef CONFIG_ZONE_DMA32
4128c2ecf20Sopenharmony_ci	max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
4138c2ecf20Sopenharmony_ci#endif
4148c2ecf20Sopenharmony_ci	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
4158c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
4168c2ecf20Sopenharmony_ci	max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (cpu_has_dc_aliases && max_low_pfn != highend_pfn) {
4198c2ecf20Sopenharmony_ci		printk(KERN_WARNING "This processor doesn't support highmem."
4208c2ecf20Sopenharmony_ci		       " %ldk highmem ignored\n",
4218c2ecf20Sopenharmony_ci		       (highend_pfn - max_low_pfn) << (PAGE_SHIFT - 10));
4228c2ecf20Sopenharmony_ci		max_zone_pfns[ZONE_HIGHMEM] = max_low_pfn;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	max_mapnr = highend_pfn ? highend_pfn : max_low_pfn;
4268c2ecf20Sopenharmony_ci#else
4278c2ecf20Sopenharmony_ci	max_mapnr = max_low_pfn;
4288c2ecf20Sopenharmony_ci#endif
4298c2ecf20Sopenharmony_ci	high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	free_area_init(max_zone_pfns);
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
4358c2ecf20Sopenharmony_cistatic struct kcore_list kcore_kseg0;
4368c2ecf20Sopenharmony_ci#endif
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic inline void __init mem_init_free_highmem(void)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
4418c2ecf20Sopenharmony_ci	unsigned long tmp;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	if (cpu_has_dc_aliases)
4448c2ecf20Sopenharmony_ci		return;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
4478c2ecf20Sopenharmony_ci		struct page *page = pfn_to_page(tmp);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		if (!memblock_is_memory(PFN_PHYS(tmp)))
4508c2ecf20Sopenharmony_ci			SetPageReserved(page);
4518c2ecf20Sopenharmony_ci		else
4528c2ecf20Sopenharmony_ci			free_highmem_page(page);
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci#endif
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_civoid __init mem_init(void)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	/*
4608c2ecf20Sopenharmony_ci	 * When _PFN_SHIFT is greater than PAGE_SHIFT we won't have enough PTE
4618c2ecf20Sopenharmony_ci	 * bits to hold a full 32b physical address on MIPS32 systems.
4628c2ecf20Sopenharmony_ci	 */
4638c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IS_ENABLED(CONFIG_32BIT) && (_PFN_SHIFT > PAGE_SHIFT));
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	maar_init();
4668c2ecf20Sopenharmony_ci	memblock_free_all();
4678c2ecf20Sopenharmony_ci	setup_zero_pages();	/* Setup zeroed pages.  */
4688c2ecf20Sopenharmony_ci	mem_init_free_highmem();
4698c2ecf20Sopenharmony_ci	mem_init_print_info(NULL);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
4728c2ecf20Sopenharmony_ci	if ((unsigned long) &_text > (unsigned long) CKSEG0)
4738c2ecf20Sopenharmony_ci		/* The -4 is a hack so that user tools don't have to handle
4748c2ecf20Sopenharmony_ci		   the overflow.  */
4758c2ecf20Sopenharmony_ci		kclist_add(&kcore_kseg0, (void *) CKSEG0,
4768c2ecf20Sopenharmony_ci				0x80000000 - 4, KCORE_TEXT);
4778c2ecf20Sopenharmony_ci#endif
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci#endif /* !CONFIG_NEED_MULTIPLE_NODES */
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_civoid free_init_pages(const char *what, unsigned long begin, unsigned long end)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	unsigned long pfn;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	for (pfn = PFN_UP(begin); pfn < PFN_DOWN(end); pfn++) {
4868c2ecf20Sopenharmony_ci		struct page *page = pfn_to_page(pfn);
4878c2ecf20Sopenharmony_ci		void *addr = phys_to_virt(PFN_PHYS(pfn));
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		memset(addr, POISON_FREE_INITMEM, PAGE_SIZE);
4908c2ecf20Sopenharmony_ci		free_reserved_page(page);
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci	printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_civoid (*free_init_pages_eva)(void *begin, void *end) = NULL;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_civoid __ref free_initmem(void)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	prom_free_prom_memory();
5008c2ecf20Sopenharmony_ci	/*
5018c2ecf20Sopenharmony_ci	 * Let the platform define a specific function to free the
5028c2ecf20Sopenharmony_ci	 * init section since EVA may have used any possible mapping
5038c2ecf20Sopenharmony_ci	 * between virtual and physical addresses.
5048c2ecf20Sopenharmony_ci	 */
5058c2ecf20Sopenharmony_ci	if (free_init_pages_eva)
5068c2ecf20Sopenharmony_ci		free_init_pages_eva((void *)&__init_begin, (void *)&__init_end);
5078c2ecf20Sopenharmony_ci	else
5088c2ecf20Sopenharmony_ci		free_initmem_default(POISON_FREE_INITMEM);
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
5128c2ecf20Sopenharmony_ciunsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
5138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__per_cpu_offset);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	return node_distance(cpu_to_node(from), cpu_to_node(to));
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistatic void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
5218c2ecf20Sopenharmony_ci				       size_t align)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	return memblock_alloc_try_nid(size, align, __pa(MAX_DMA_ADDRESS),
5248c2ecf20Sopenharmony_ci				      MEMBLOCK_ALLOC_ACCESSIBLE,
5258c2ecf20Sopenharmony_ci				      cpu_to_node(cpu));
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistatic void __init pcpu_fc_free(void *ptr, size_t size)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	memblock_free_early(__pa(ptr), size);
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_civoid __init setup_per_cpu_areas(void)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	unsigned long delta;
5368c2ecf20Sopenharmony_ci	unsigned int cpu;
5378c2ecf20Sopenharmony_ci	int rc;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/*
5408c2ecf20Sopenharmony_ci	 * Always reserve area for module percpu variables.  That's
5418c2ecf20Sopenharmony_ci	 * what the legacy allocator did.
5428c2ecf20Sopenharmony_ci	 */
5438c2ecf20Sopenharmony_ci	rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
5448c2ecf20Sopenharmony_ci				    PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
5458c2ecf20Sopenharmony_ci				    pcpu_cpu_distance,
5468c2ecf20Sopenharmony_ci				    pcpu_fc_alloc, pcpu_fc_free);
5478c2ecf20Sopenharmony_ci	if (rc < 0)
5488c2ecf20Sopenharmony_ci		panic("Failed to initialize percpu areas.");
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
5518c2ecf20Sopenharmony_ci	for_each_possible_cpu(cpu)
5528c2ecf20Sopenharmony_ci		__per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci#endif
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
5578c2ecf20Sopenharmony_ciunsigned long pgd_current[NR_CPUS];
5588c2ecf20Sopenharmony_ci#endif
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci/*
5618c2ecf20Sopenharmony_ci * Align swapper_pg_dir in to 64K, allows its address to be loaded
5628c2ecf20Sopenharmony_ci * with a single LUI instruction in the TLB handlers.  If we used
5638c2ecf20Sopenharmony_ci * __aligned(64K), its size would get rounded up to the alignment
5648c2ecf20Sopenharmony_ci * size, and waste space.  So we place it in its own section and align
5658c2ecf20Sopenharmony_ci * it in the linker script.
5668c2ecf20Sopenharmony_ci */
5678c2ecf20Sopenharmony_cipgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".bss..swapper_pg_dir");
5688c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PUD_FOLDED
5698c2ecf20Sopenharmony_cipud_t invalid_pud_table[PTRS_PER_PUD] __page_aligned_bss;
5708c2ecf20Sopenharmony_ci#endif
5718c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
5728c2ecf20Sopenharmony_cipmd_t invalid_pmd_table[PTRS_PER_PMD] __page_aligned_bss;
5738c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(invalid_pmd_table);
5748c2ecf20Sopenharmony_ci#endif
5758c2ecf20Sopenharmony_cipte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss;
5768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(invalid_pte_table);
577