xref: /linux/arch/m68k/mm/motorola.c (revision 5553b15a4bbba8039e1f31b63642048286f540dc)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3f30c2269SUwe Zeisberger  * linux/arch/m68k/mm/motorola.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Routines specific to the Motorola MMU, originally from:
61da177e4SLinus Torvalds  * linux/arch/m68k/init.c
71da177e4SLinus Torvalds  * which are Copyright (C) 1995 Hamish Macdonald
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Moved 8/20/1999 Sam Creasey
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/signal.h>
141da177e4SLinus Torvalds #include <linux/sched.h>
151da177e4SLinus Torvalds #include <linux/mm.h>
161da177e4SLinus Torvalds #include <linux/swap.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/string.h>
191da177e4SLinus Torvalds #include <linux/types.h>
201da177e4SLinus Torvalds #include <linux/init.h>
211008a115SMike Rapoport #include <linux/memblock.h>
225a0e3ad6STejun Heo #include <linux/gfp.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/setup.h>
257c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
261da177e4SLinus Torvalds #include <asm/page.h>
271da177e4SLinus Torvalds #include <asm/pgalloc.h>
281da177e4SLinus Torvalds #include <asm/machdep.h>
291da177e4SLinus Torvalds #include <asm/io.h>
301da177e4SLinus Torvalds #ifdef CONFIG_ATARI
311da177e4SLinus Torvalds #include <asm/atari_stram.h>
321da177e4SLinus Torvalds #endif
33c85627fbSGeert Uytterhoeven #include <asm/sections.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #undef DEBUG
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #ifndef mm_cachebits
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * Bits to add to page descriptors for "normal" caching mode.
401da177e4SLinus Torvalds  * For 68020/030 this is 0.
411da177e4SLinus Torvalds  * For 68040, this is _PAGE_CACHE040 (cachable, copyback)
421da177e4SLinus Torvalds  */
431da177e4SLinus Torvalds unsigned long mm_cachebits;
441da177e4SLinus Torvalds EXPORT_SYMBOL(mm_cachebits);
451da177e4SLinus Torvalds #endif
461da177e4SLinus Torvalds 
471bcdc68dSMike Rapoport /* Prior to calling these routines, the page should have been flushed
481bcdc68dSMike Rapoport  * from both the cache and ATC, or the CPU might not notice that the
491bcdc68dSMike Rapoport  * cache setting for the page has been changed. -jskov
501bcdc68dSMike Rapoport  */
511bcdc68dSMike Rapoport static inline void nocache_page(void *vaddr)
521bcdc68dSMike Rapoport {
531bcdc68dSMike Rapoport 	unsigned long addr = (unsigned long)vaddr;
541bcdc68dSMike Rapoport 
551bcdc68dSMike Rapoport 	if (CPU_IS_040_OR_060) {
56e05c7b1fSMike Rapoport 		pte_t *ptep = virt_to_kpte(addr);
571bcdc68dSMike Rapoport 
581bcdc68dSMike Rapoport 		*ptep = pte_mknocache(*ptep);
591bcdc68dSMike Rapoport 	}
601bcdc68dSMike Rapoport }
611bcdc68dSMike Rapoport 
621bcdc68dSMike Rapoport static inline void cache_page(void *vaddr)
631bcdc68dSMike Rapoport {
641bcdc68dSMike Rapoport 	unsigned long addr = (unsigned long)vaddr;
651bcdc68dSMike Rapoport 
661bcdc68dSMike Rapoport 	if (CPU_IS_040_OR_060) {
67e05c7b1fSMike Rapoport 		pte_t *ptep = virt_to_kpte(addr);
681bcdc68dSMike Rapoport 
691bcdc68dSMike Rapoport 		*ptep = pte_mkcache(*ptep);
701bcdc68dSMike Rapoport 	}
711bcdc68dSMike Rapoport }
7213076a29SPeter Zijlstra 
7313076a29SPeter Zijlstra /*
7413076a29SPeter Zijlstra  * Motorola 680x0 user's manual recommends using uncached memory for address
7513076a29SPeter Zijlstra  * translation tables.
7613076a29SPeter Zijlstra  *
7713076a29SPeter Zijlstra  * Seeing how the MMU can be external on (some of) these chips, that seems like
7813076a29SPeter Zijlstra  * a very important recommendation to follow. Provide some helpers to combat
7913076a29SPeter Zijlstra  * 'variation' amongst the users of this.
8013076a29SPeter Zijlstra  */
8113076a29SPeter Zijlstra 
8213076a29SPeter Zijlstra void mmu_page_ctor(void *page)
8313076a29SPeter Zijlstra {
84*5553b15aSMatthew Wilcox (Oracle) 	__flush_pages_to_ram(page, 1);
8513076a29SPeter Zijlstra 	flush_tlb_kernel_page(page);
8613076a29SPeter Zijlstra 	nocache_page(page);
8713076a29SPeter Zijlstra }
8813076a29SPeter Zijlstra 
8913076a29SPeter Zijlstra void mmu_page_dtor(void *page)
9013076a29SPeter Zijlstra {
9113076a29SPeter Zijlstra 	cache_page(page);
9213076a29SPeter Zijlstra }
9313076a29SPeter Zijlstra 
945ad272abSPeter Zijlstra /* ++andreas: {get,free}_pointer_table rewritten to use unused fields from
955ad272abSPeter Zijlstra    struct page instead of separately kmalloced struct.  Stolen from
965ad272abSPeter Zijlstra    arch/sparc/mm/srmmu.c ... */
975ad272abSPeter Zijlstra 
985ad272abSPeter Zijlstra typedef struct list_head ptable_desc;
990e071ee6SPeter Zijlstra 
1000e071ee6SPeter Zijlstra static struct list_head ptable_list[2] = {
1010e071ee6SPeter Zijlstra 	LIST_HEAD_INIT(ptable_list[0]),
1020e071ee6SPeter Zijlstra 	LIST_HEAD_INIT(ptable_list[1]),
1030e071ee6SPeter Zijlstra };
1045ad272abSPeter Zijlstra 
1058f246087SLinus Walleij #define PD_PTABLE(page) ((ptable_desc *)&(virt_to_page((void *)(page))->lru))
1065ad272abSPeter Zijlstra #define PD_PAGE(ptable) (list_entry(ptable, struct page, lru))
1070e071ee6SPeter Zijlstra #define PD_MARKBITS(dp) (*(unsigned int *)&PD_PAGE(dp)->index)
1085ad272abSPeter Zijlstra 
1090e071ee6SPeter Zijlstra static const int ptable_shift[2] = {
1100e071ee6SPeter Zijlstra 	7+2, /* PGD, PMD */
1110e071ee6SPeter Zijlstra 	6+2, /* PTE */
1120e071ee6SPeter Zijlstra };
1135ad272abSPeter Zijlstra 
1140e071ee6SPeter Zijlstra #define ptable_size(type) (1U << ptable_shift[type])
1150e071ee6SPeter Zijlstra #define ptable_mask(type) ((1U << (PAGE_SIZE / ptable_size(type))) - 1)
1160e071ee6SPeter Zijlstra 
1170e071ee6SPeter Zijlstra void __init init_pointer_table(void *table, int type)
1185ad272abSPeter Zijlstra {
1195ad272abSPeter Zijlstra 	ptable_desc *dp;
1200e071ee6SPeter Zijlstra 	unsigned long ptable = (unsigned long)table;
1215ad272abSPeter Zijlstra 	unsigned long page = ptable & PAGE_MASK;
1220e071ee6SPeter Zijlstra 	unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
1235ad272abSPeter Zijlstra 
1245ad272abSPeter Zijlstra 	dp = PD_PTABLE(page);
1255ad272abSPeter Zijlstra 	if (!(PD_MARKBITS(dp) & mask)) {
1260e071ee6SPeter Zijlstra 		PD_MARKBITS(dp) = ptable_mask(type);
1270e071ee6SPeter Zijlstra 		list_add(dp, &ptable_list[type]);
1285ad272abSPeter Zijlstra 	}
1295ad272abSPeter Zijlstra 
1305ad272abSPeter Zijlstra 	PD_MARKBITS(dp) &= ~mask;
1315ad272abSPeter Zijlstra 	pr_debug("init_pointer_table: %lx, %x\n", ptable, PD_MARKBITS(dp));
1325ad272abSPeter Zijlstra 
1335ad272abSPeter Zijlstra 	/* unreserve the page so it's possible to free that page */
1345ad272abSPeter Zijlstra 	__ClearPageReserved(PD_PAGE(dp));
1355ad272abSPeter Zijlstra 	init_page_count(PD_PAGE(dp));
1365ad272abSPeter Zijlstra 
1375ad272abSPeter Zijlstra 	return;
1385ad272abSPeter Zijlstra }
1395ad272abSPeter Zijlstra 
1400e071ee6SPeter Zijlstra void *get_pointer_table(int type)
1415ad272abSPeter Zijlstra {
1420e071ee6SPeter Zijlstra 	ptable_desc *dp = ptable_list[type].next;
1430e071ee6SPeter Zijlstra 	unsigned int mask = list_empty(&ptable_list[type]) ? 0 : PD_MARKBITS(dp);
1440e071ee6SPeter Zijlstra 	unsigned int tmp, off;
1455ad272abSPeter Zijlstra 
1465ad272abSPeter Zijlstra 	/*
1475ad272abSPeter Zijlstra 	 * For a pointer table for a user process address space, a
1485ad272abSPeter Zijlstra 	 * table is taken from a page allocated for the purpose.  Each
1495ad272abSPeter Zijlstra 	 * page can hold 8 pointer tables.  The page is remapped in
1505ad272abSPeter Zijlstra 	 * virtual address space to be noncacheable.
1515ad272abSPeter Zijlstra 	 */
1525ad272abSPeter Zijlstra 	if (mask == 0) {
1535ad272abSPeter Zijlstra 		void *page;
1545ad272abSPeter Zijlstra 		ptable_desc *new;
1555ad272abSPeter Zijlstra 
1565ad272abSPeter Zijlstra 		if (!(page = (void *)get_zeroed_page(GFP_KERNEL)))
1575ad272abSPeter Zijlstra 			return NULL;
1585ad272abSPeter Zijlstra 
1590e071ee6SPeter Zijlstra 		if (type == TABLE_PTE) {
1600e071ee6SPeter Zijlstra 			/*
1610e071ee6SPeter Zijlstra 			 * m68k doesn't have SPLIT_PTE_PTLOCKS for not having
1620e071ee6SPeter Zijlstra 			 * SMP.
1630e071ee6SPeter Zijlstra 			 */
164bff28e6bSVishal Moola (Oracle) 			pagetable_pte_ctor(virt_to_ptdesc(page));
1650e071ee6SPeter Zijlstra 		}
1660e071ee6SPeter Zijlstra 
1675ad272abSPeter Zijlstra 		mmu_page_ctor(page);
1685ad272abSPeter Zijlstra 
1695ad272abSPeter Zijlstra 		new = PD_PTABLE(page);
1700e071ee6SPeter Zijlstra 		PD_MARKBITS(new) = ptable_mask(type) - 1;
1715ad272abSPeter Zijlstra 		list_add_tail(new, dp);
1725ad272abSPeter Zijlstra 
1735ad272abSPeter Zijlstra 		return (pmd_t *)page;
1745ad272abSPeter Zijlstra 	}
1755ad272abSPeter Zijlstra 
1760e071ee6SPeter Zijlstra 	for (tmp = 1, off = 0; (mask & tmp) == 0; tmp <<= 1, off += ptable_size(type))
1775ad272abSPeter Zijlstra 		;
1785ad272abSPeter Zijlstra 	PD_MARKBITS(dp) = mask & ~tmp;
1795ad272abSPeter Zijlstra 	if (!PD_MARKBITS(dp)) {
1805ad272abSPeter Zijlstra 		/* move to end of list */
1810e071ee6SPeter Zijlstra 		list_move_tail(dp, &ptable_list[type]);
1825ad272abSPeter Zijlstra 	}
1830e071ee6SPeter Zijlstra 	return page_address(PD_PAGE(dp)) + off;
1845ad272abSPeter Zijlstra }
1855ad272abSPeter Zijlstra 
1860e071ee6SPeter Zijlstra int free_pointer_table(void *table, int type)
1875ad272abSPeter Zijlstra {
1885ad272abSPeter Zijlstra 	ptable_desc *dp;
1890e071ee6SPeter Zijlstra 	unsigned long ptable = (unsigned long)table;
1900e071ee6SPeter Zijlstra 	unsigned long page = ptable & PAGE_MASK;
1910e071ee6SPeter Zijlstra 	unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
1925ad272abSPeter Zijlstra 
1935ad272abSPeter Zijlstra 	dp = PD_PTABLE(page);
1945ad272abSPeter Zijlstra 	if (PD_MARKBITS (dp) & mask)
1955ad272abSPeter Zijlstra 		panic ("table already free!");
1965ad272abSPeter Zijlstra 
1975ad272abSPeter Zijlstra 	PD_MARKBITS (dp) |= mask;
1985ad272abSPeter Zijlstra 
1990e071ee6SPeter Zijlstra 	if (PD_MARKBITS(dp) == ptable_mask(type)) {
2005ad272abSPeter Zijlstra 		/* all tables in page are free, free page */
2015ad272abSPeter Zijlstra 		list_del(dp);
2025ad272abSPeter Zijlstra 		mmu_page_dtor((void *)page);
2030e071ee6SPeter Zijlstra 		if (type == TABLE_PTE)
204bff28e6bSVishal Moola (Oracle) 			pagetable_pte_dtor(virt_to_ptdesc((void *)page));
2055ad272abSPeter Zijlstra 		free_page (page);
2065ad272abSPeter Zijlstra 		return 1;
2070e071ee6SPeter Zijlstra 	} else if (ptable_list[type].next != dp) {
2085ad272abSPeter Zijlstra 		/*
2095ad272abSPeter Zijlstra 		 * move this descriptor to the front of the list, since
2105ad272abSPeter Zijlstra 		 * it has one or more free tables.
2115ad272abSPeter Zijlstra 		 */
2120e071ee6SPeter Zijlstra 		list_move(dp, &ptable_list[type]);
2135ad272abSPeter Zijlstra 	}
2145ad272abSPeter Zijlstra 	return 0;
2155ad272abSPeter Zijlstra }
2165ad272abSPeter Zijlstra 
21712d810c1SRoman Zippel /* size of memory already mapped in head.S */
218486df8bcSAndreas Schwab extern __initdata unsigned long m68k_init_mapped_size;
21912d810c1SRoman Zippel 
22012d810c1SRoman Zippel extern unsigned long availmem;
22112d810c1SRoman Zippel 
222ef9285f6SPeter Zijlstra static pte_t *last_pte_table __initdata = NULL;
223ef9285f6SPeter Zijlstra 
2241da177e4SLinus Torvalds static pte_t * __init kernel_page_table(void)
2251da177e4SLinus Torvalds {
226ef9285f6SPeter Zijlstra 	pte_t *pte_table = last_pte_table;
2271da177e4SLinus Torvalds 
22841f1bf37SGeert Uytterhoeven 	if (PAGE_ALIGNED(last_pte_table)) {
2297e158826SGeert Uytterhoeven 		pte_table = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
230ef9285f6SPeter Zijlstra 		if (!pte_table) {
2318a7f97b9SMike Rapoport 			panic("%s: Failed to allocate %lu bytes align=%lx\n",
2328a7f97b9SMike Rapoport 					__func__, PAGE_SIZE, PAGE_SIZE);
2331da177e4SLinus Torvalds 		}
2341da177e4SLinus Torvalds 
235ef9285f6SPeter Zijlstra 		clear_page(pte_table);
236ef9285f6SPeter Zijlstra 		mmu_page_ctor(pte_table);
237ef9285f6SPeter Zijlstra 
238ef9285f6SPeter Zijlstra 		last_pte_table = pte_table;
239ef9285f6SPeter Zijlstra 	}
240ef9285f6SPeter Zijlstra 
241ef9285f6SPeter Zijlstra 	last_pte_table += PTRS_PER_PTE;
242ef9285f6SPeter Zijlstra 
243ef9285f6SPeter Zijlstra 	return pte_table;
244ef9285f6SPeter Zijlstra }
245ef9285f6SPeter Zijlstra 
246ef9285f6SPeter Zijlstra static pmd_t *last_pmd_table __initdata = NULL;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds static pmd_t * __init kernel_ptr_table(void)
2491da177e4SLinus Torvalds {
250ef9285f6SPeter Zijlstra 	if (!last_pmd_table) {
2511da177e4SLinus Torvalds 		unsigned long pmd, last;
2521da177e4SLinus Torvalds 		int i;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 		/* Find the last ptr table that was used in head.S and
2551da177e4SLinus Torvalds 		 * reuse the remaining space in that page for further
2561da177e4SLinus Torvalds 		 * ptr tables.
2571da177e4SLinus Torvalds 		 */
2581da177e4SLinus Torvalds 		last = (unsigned long)kernel_pg_dir;
2591da177e4SLinus Torvalds 		for (i = 0; i < PTRS_PER_PGD; i++) {
26060e50f34SMike Rapoport 			pud_t *pud = (pud_t *)(&kernel_pg_dir[i]);
26160e50f34SMike Rapoport 
26260e50f34SMike Rapoport 			if (!pud_present(*pud))
2631da177e4SLinus Torvalds 				continue;
26460e50f34SMike Rapoport 			pmd = pgd_page_vaddr(kernel_pg_dir[i]);
2651da177e4SLinus Torvalds 			if (pmd > last)
2661da177e4SLinus Torvalds 				last = pmd;
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 
269ef9285f6SPeter Zijlstra 		last_pmd_table = (pmd_t *)last;
2701da177e4SLinus Torvalds #ifdef DEBUG
271ef9285f6SPeter Zijlstra 		printk("kernel_ptr_init: %p\n", last_pmd_table);
2721da177e4SLinus Torvalds #endif
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
275ef9285f6SPeter Zijlstra 	last_pmd_table += PTRS_PER_PMD;
27641f1bf37SGeert Uytterhoeven 	if (PAGE_ALIGNED(last_pmd_table)) {
2777e158826SGeert Uytterhoeven 		last_pmd_table = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
278ef9285f6SPeter Zijlstra 		if (!last_pmd_table)
2798a7f97b9SMike Rapoport 			panic("%s: Failed to allocate %lu bytes align=%lx\n",
2808a7f97b9SMike Rapoport 			      __func__, PAGE_SIZE, PAGE_SIZE);
2811da177e4SLinus Torvalds 
282ef9285f6SPeter Zijlstra 		clear_page(last_pmd_table);
283ef9285f6SPeter Zijlstra 		mmu_page_ctor(last_pmd_table);
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 
286ef9285f6SPeter Zijlstra 	return last_pmd_table;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
28912d810c1SRoman Zippel static void __init map_node(int node)
2901da177e4SLinus Torvalds {
29112d810c1SRoman Zippel 	unsigned long physaddr, virtaddr, size;
2921da177e4SLinus Torvalds 	pgd_t *pgd_dir;
29360e50f34SMike Rapoport 	p4d_t *p4d_dir;
29460e50f34SMike Rapoport 	pud_t *pud_dir;
2951da177e4SLinus Torvalds 	pmd_t *pmd_dir;
2961da177e4SLinus Torvalds 	pte_t *pte_dir;
2971da177e4SLinus Torvalds 
29812d810c1SRoman Zippel 	size = m68k_memory[node].size;
29912d810c1SRoman Zippel 	physaddr = m68k_memory[node].addr;
30012d810c1SRoman Zippel 	virtaddr = (unsigned long)phys_to_virt(physaddr);
30112d810c1SRoman Zippel 	physaddr |= m68k_supervisor_cachemode |
30212d810c1SRoman Zippel 		    _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
3031da177e4SLinus Torvalds 	if (CPU_IS_040_OR_060)
3041da177e4SLinus Torvalds 		physaddr |= _PAGE_GLOBAL040;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	while (size > 0) {
3071da177e4SLinus Torvalds #ifdef DEBUG
308ef22d8abSPeter Zijlstra 		if (!(virtaddr & (PMD_SIZE-1)))
3091da177e4SLinus Torvalds 			printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
3101da177e4SLinus Torvalds 				virtaddr);
3111da177e4SLinus Torvalds #endif
3121da177e4SLinus Torvalds 		pgd_dir = pgd_offset_k(virtaddr);
3131da177e4SLinus Torvalds 		if (virtaddr && CPU_IS_020_OR_030) {
314ef22d8abSPeter Zijlstra 			if (!(virtaddr & (PGDIR_SIZE-1)) &&
315ef22d8abSPeter Zijlstra 			    size >= PGDIR_SIZE) {
3161da177e4SLinus Torvalds #ifdef DEBUG
3171da177e4SLinus Torvalds 				printk ("[very early term]");
3181da177e4SLinus Torvalds #endif
3191da177e4SLinus Torvalds 				pgd_val(*pgd_dir) = physaddr;
320ef22d8abSPeter Zijlstra 				size -= PGDIR_SIZE;
321ef22d8abSPeter Zijlstra 				virtaddr += PGDIR_SIZE;
322ef22d8abSPeter Zijlstra 				physaddr += PGDIR_SIZE;
3231da177e4SLinus Torvalds 				continue;
3241da177e4SLinus Torvalds 			}
3251da177e4SLinus Torvalds 		}
32660e50f34SMike Rapoport 		p4d_dir = p4d_offset(pgd_dir, virtaddr);
32760e50f34SMike Rapoport 		pud_dir = pud_offset(p4d_dir, virtaddr);
32860e50f34SMike Rapoport 		if (!pud_present(*pud_dir)) {
3291da177e4SLinus Torvalds 			pmd_dir = kernel_ptr_table();
3301da177e4SLinus Torvalds #ifdef DEBUG
3311da177e4SLinus Torvalds 			printk ("[new pointer %p]", pmd_dir);
3321da177e4SLinus Torvalds #endif
33360e50f34SMike Rapoport 			pud_set(pud_dir, pmd_dir);
3341da177e4SLinus Torvalds 		} else
33560e50f34SMike Rapoport 			pmd_dir = pmd_offset(pud_dir, virtaddr);
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 		if (CPU_IS_020_OR_030) {
3381da177e4SLinus Torvalds 			if (virtaddr) {
3391da177e4SLinus Torvalds #ifdef DEBUG
3401da177e4SLinus Torvalds 				printk ("[early term]");
3411da177e4SLinus Torvalds #endif
342ef22d8abSPeter Zijlstra 				pmd_val(*pmd_dir) = physaddr;
343ef22d8abSPeter Zijlstra 				physaddr += PMD_SIZE;
3441da177e4SLinus Torvalds 			} else {
3451da177e4SLinus Torvalds 				int i;
3461da177e4SLinus Torvalds #ifdef DEBUG
3471da177e4SLinus Torvalds 				printk ("[zero map]");
3481da177e4SLinus Torvalds #endif
349ef9285f6SPeter Zijlstra 				pte_dir = kernel_page_table();
350ef22d8abSPeter Zijlstra 				pmd_set(pmd_dir, pte_dir);
351ef22d8abSPeter Zijlstra 
3521da177e4SLinus Torvalds 				pte_val(*pte_dir++) = 0;
3531da177e4SLinus Torvalds 				physaddr += PAGE_SIZE;
354ef22d8abSPeter Zijlstra 				for (i = 1; i < PTRS_PER_PTE; physaddr += PAGE_SIZE, i++)
3551da177e4SLinus Torvalds 					pte_val(*pte_dir++) = physaddr;
3561da177e4SLinus Torvalds 			}
357ef22d8abSPeter Zijlstra 			size -= PMD_SIZE;
358ef22d8abSPeter Zijlstra 			virtaddr += PMD_SIZE;
3591da177e4SLinus Torvalds 		} else {
3601da177e4SLinus Torvalds 			if (!pmd_present(*pmd_dir)) {
3611da177e4SLinus Torvalds #ifdef DEBUG
3621da177e4SLinus Torvalds 				printk ("[new table]");
3631da177e4SLinus Torvalds #endif
3641da177e4SLinus Torvalds 				pte_dir = kernel_page_table();
3651da177e4SLinus Torvalds 				pmd_set(pmd_dir, pte_dir);
3661da177e4SLinus Torvalds 			}
3671da177e4SLinus Torvalds 			pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 			if (virtaddr) {
3701da177e4SLinus Torvalds 				if (!pte_present(*pte_dir))
3711da177e4SLinus Torvalds 					pte_val(*pte_dir) = physaddr;
3721da177e4SLinus Torvalds 			} else
3731da177e4SLinus Torvalds 				pte_val(*pte_dir) = 0;
3741da177e4SLinus Torvalds 			size -= PAGE_SIZE;
3751da177e4SLinus Torvalds 			virtaddr += PAGE_SIZE;
3761da177e4SLinus Torvalds 			physaddr += PAGE_SIZE;
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds #ifdef DEBUG
3811da177e4SLinus Torvalds 	printk("\n");
3821da177e4SLinus Torvalds #endif
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /*
3866d0b9225SAnshuman Khandual  * Alternate definitions that are compile time constants, for
3876d0b9225SAnshuman Khandual  * initializing protection_map.  The cachebits are fixed later.
3886d0b9225SAnshuman Khandual  */
3896d0b9225SAnshuman Khandual #define PAGE_NONE_C	__pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
3906d0b9225SAnshuman Khandual #define PAGE_SHARED_C	__pgprot(_PAGE_PRESENT | _PAGE_ACCESSED)
3916d0b9225SAnshuman Khandual #define PAGE_COPY_C	__pgprot(_PAGE_PRESENT | _PAGE_RONLY | _PAGE_ACCESSED)
3926d0b9225SAnshuman Khandual #define PAGE_READONLY_C	__pgprot(_PAGE_PRESENT | _PAGE_RONLY | _PAGE_ACCESSED)
3936d0b9225SAnshuman Khandual 
3946d0b9225SAnshuman Khandual static pgprot_t protection_map[16] __ro_after_init = {
3956d0b9225SAnshuman Khandual 	[VM_NONE]					= PAGE_NONE_C,
3966d0b9225SAnshuman Khandual 	[VM_READ]					= PAGE_READONLY_C,
3976d0b9225SAnshuman Khandual 	[VM_WRITE]					= PAGE_COPY_C,
3986d0b9225SAnshuman Khandual 	[VM_WRITE | VM_READ]				= PAGE_COPY_C,
3996d0b9225SAnshuman Khandual 	[VM_EXEC]					= PAGE_READONLY_C,
4006d0b9225SAnshuman Khandual 	[VM_EXEC | VM_READ]				= PAGE_READONLY_C,
4016d0b9225SAnshuman Khandual 	[VM_EXEC | VM_WRITE]				= PAGE_COPY_C,
4026d0b9225SAnshuman Khandual 	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_COPY_C,
4036d0b9225SAnshuman Khandual 	[VM_SHARED]					= PAGE_NONE_C,
4046d0b9225SAnshuman Khandual 	[VM_SHARED | VM_READ]				= PAGE_READONLY_C,
4056d0b9225SAnshuman Khandual 	[VM_SHARED | VM_WRITE]				= PAGE_SHARED_C,
4066d0b9225SAnshuman Khandual 	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED_C,
4076d0b9225SAnshuman Khandual 	[VM_SHARED | VM_EXEC]				= PAGE_READONLY_C,
4086d0b9225SAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READONLY_C,
4096d0b9225SAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED_C,
4106d0b9225SAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED_C
4116d0b9225SAnshuman Khandual };
4126d0b9225SAnshuman Khandual DECLARE_VM_GET_PAGE_PROT
4136d0b9225SAnshuman Khandual 
4146d0b9225SAnshuman Khandual /*
4151da177e4SLinus Torvalds  * paging_init() continues the virtual memory environment setup which
4161da177e4SLinus Torvalds  * was begun by the code in arch/head.S.
4171da177e4SLinus Torvalds  */
4181da177e4SLinus Torvalds void __init paging_init(void)
4191da177e4SLinus Torvalds {
4205d2ee1a1SMike Rapoport 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
42112d810c1SRoman Zippel 	unsigned long min_addr, max_addr;
4221008a115SMike Rapoport 	unsigned long addr;
42312d810c1SRoman Zippel 	int i;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds #ifdef DEBUG
426fb425d0bSGeert Uytterhoeven 	printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
4271da177e4SLinus Torvalds #endif
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	/* Fix the cache mode in the page descriptors for the 680[46]0.  */
4301da177e4SLinus Torvalds 	if (CPU_IS_040_OR_060) {
4311da177e4SLinus Torvalds 		int i;
4321da177e4SLinus Torvalds #ifndef mm_cachebits
4331da177e4SLinus Torvalds 		mm_cachebits = _PAGE_CACHE040;
4341da177e4SLinus Torvalds #endif
4351da177e4SLinus Torvalds 		for (i = 0; i < 16; i++)
4361da177e4SLinus Torvalds 			pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 
43912d810c1SRoman Zippel 	min_addr = m68k_memory[0].addr;
4400d9fad91SKars de Jong 	max_addr = min_addr + m68k_memory[0].size - 1;
441952eea9bSDavid Hildenbrand 	memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0,
442952eea9bSDavid Hildenbrand 			  MEMBLOCK_NONE);
44312d810c1SRoman Zippel 	for (i = 1; i < m68k_num_memory;) {
44412d810c1SRoman Zippel 		if (m68k_memory[i].addr < min_addr) {
44512d810c1SRoman Zippel 			printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
44612d810c1SRoman Zippel 				m68k_memory[i].addr, m68k_memory[i].size);
44712d810c1SRoman Zippel 			printk("Fix your bootloader or use a memfile to make use of this area!\n");
44812d810c1SRoman Zippel 			m68k_num_memory--;
44912d810c1SRoman Zippel 			memmove(m68k_memory + i, m68k_memory + i + 1,
45079930084SGeert Uytterhoeven 				(m68k_num_memory - i) * sizeof(struct m68k_mem_info));
45112d810c1SRoman Zippel 			continue;
45212d810c1SRoman Zippel 		}
453952eea9bSDavid Hildenbrand 		memblock_add_node(m68k_memory[i].addr, m68k_memory[i].size, i,
454952eea9bSDavid Hildenbrand 				  MEMBLOCK_NONE);
4550d9fad91SKars de Jong 		addr = m68k_memory[i].addr + m68k_memory[i].size - 1;
45612d810c1SRoman Zippel 		if (addr > max_addr)
45712d810c1SRoman Zippel 			max_addr = addr;
45812d810c1SRoman Zippel 		i++;
45912d810c1SRoman Zippel 	}
46012d810c1SRoman Zippel 	m68k_memoffset = min_addr - PAGE_OFFSET;
4610d9fad91SKars de Jong 	m68k_virt_to_node_shift = fls(max_addr - min_addr) - 6;
46212d810c1SRoman Zippel 
463fbe9c961SRoman Zippel 	module_fixup(NULL, __start_fixup, __stop_fixup);
464fbe9c961SRoman Zippel 	flush_icache();
465fbe9c961SRoman Zippel 
4660d9fad91SKars de Jong 	high_memory = phys_to_virt(max_addr) + 1;
4671da177e4SLinus Torvalds 
46812d810c1SRoman Zippel 	min_low_pfn = availmem >> PAGE_SHIFT;
4690d9fad91SKars de Jong 	max_pfn = max_low_pfn = (max_addr >> PAGE_SHIFT) + 1;
4701da177e4SLinus Torvalds 
4711008a115SMike Rapoport 	/* Reserve kernel text/data/bss and the memory allocated in head.S */
4721008a115SMike Rapoport 	memblock_reserve(m68k_memory[0].addr, availmem - m68k_memory[0].addr);
4731da177e4SLinus Torvalds 
47412d810c1SRoman Zippel 	/*
47512d810c1SRoman Zippel 	 * Map the physical memory available into the kernel virtual
4761008a115SMike Rapoport 	 * address space. Make sure memblock will not try to allocate
4771008a115SMike Rapoport 	 * pages beyond the memory we already mapped in head.S
47812d810c1SRoman Zippel 	 */
4791008a115SMike Rapoport 	memblock_set_bottom_up(true);
48012d810c1SRoman Zippel 
4811008a115SMike Rapoport 	for (i = 0; i < m68k_num_memory; i++) {
4821008a115SMike Rapoport 		m68k_setup_node(i);
48312d810c1SRoman Zippel 		map_node(i);
4841008a115SMike Rapoport 	}
48512d810c1SRoman Zippel 
4861da177e4SLinus Torvalds 	flush_tlb_all();
4871da177e4SLinus Torvalds 
488376e3fdeSFinn Thain 	early_memtest(min_addr, max_addr);
489376e3fdeSFinn Thain 
4901da177e4SLinus Torvalds 	/*
4911da177e4SLinus Torvalds 	 * initialize the bad page table and bad page to point
4921da177e4SLinus Torvalds 	 * to a couple of allocated pages
4931da177e4SLinus Torvalds 	 */
49415c3c114SMike Rapoport 	empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
4958a7f97b9SMike Rapoport 	if (!empty_zero_page)
4968a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
4978a7f97b9SMike Rapoport 		      __func__, PAGE_SIZE, PAGE_SIZE);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	/*
5001da177e4SLinus Torvalds 	 * Set up SFC/DFC registers
5011da177e4SLinus Torvalds 	 */
5029fde0348SChristoph Hellwig 	set_fc(USER_DATA);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds #ifdef DEBUG
5051da177e4SLinus Torvalds 	printk ("before free_area_init\n");
5061da177e4SLinus Torvalds #endif
5075d2ee1a1SMike Rapoport 	for (i = 0; i < m68k_num_memory; i++)
5084aac0b48SMichael Schmitz 		if (node_present_pages(i))
5094aac0b48SMichael Schmitz 			node_set_state(i, N_NORMAL_MEMORY);
5105d2ee1a1SMike Rapoport 
5115d2ee1a1SMike Rapoport 	max_zone_pfn[ZONE_DMA] = memblock_end_of_DRAM();
5125d2ee1a1SMike Rapoport 	free_area_init(max_zone_pfn);
5131da177e4SLinus Torvalds }
514